Skip to content

Instantly share code, notes, and snippets.

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.platform.commons.util.ReflectionUtils;
@cthornton
cthornton / BazelJunit5FilterParser.java
Last active July 21, 2021 20:40
Better Parsing of Bazel Test Filter for JUnit5
static List<String> parseOptions(String testBridgeTestOnly) {
// transform env.TESTBRIDGE_TEST_ONLY
List<String> methodNames = new ArrayList<>();
List<String> classNames = new ArrayList<>();
List<String> packageNames = new ArrayList<>();
ArrayList<String> tests = new ArrayList<>();
// Tests are separated by | But so are methods so we can't blindly split on the | character.
@cthornton
cthornton / hashing.cpp
Created October 25, 2018 22:37
Hash function in c++
// Example:
//
// cout << hashString("hello world") << endl; // prints: 1128946856
#include <boost/functional/hash.hpp>
int hashString(const string& input) {
boost::hash<string> string_hash;
return string_hash(input);
}
@cthornton
cthornton / hashing.java
Created October 25, 2018 22:27
Hashing
// Example:
//
// System.out.println(Hasher.sha1("hello world")); // prints: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
// System.out.println(Hasher.sha1AsInt("hello world")); // prints: 896314922
import java.nio.charset.*;
import com.google.common.hash.*;
class Hasher {
public static String sha1(String input) {
@cthornton
cthornton / pkcs7_degenerate.rb
Last active September 12, 2017 21:53
Ruby PKCS#7 Certificate Only Degenerate (application/x-x509-ca-ra-cert)
# Creates a degenerate PKCS#7 certificate only in ruby (SCEP application/x-x509-ca-ra-cert)
# Inspiration: https://github.com/AppBlade/TestHub/blob/master/app/controllers/scep_controller.rb#L92-L112
#
# Tested Ruby 2.2.0 OSX 10.10, OpenSSL 1.0.1l
require 'openssl'
cert = OpenSSL::X509::Certificate.new File.read('some_cert.crt')
# Fails ruby 2.2, OpenSSL 1.0.1l!!
p7certs = OpenSSL::PKCS7.new
@cthornton
cthornton / update_ghost.rb
Created September 10, 2014 06:04
Automatically Update Ghost Blog
#!/usr/bin/env ruby
#
# Automatically updates ghost per instructions at:
#
# http://support.ghost.org/how-to-upgrade/
#
# Make sure to back up your files before proceeding (i.e. git repo)!
#
require 'tempfile'
class PostsController < ApiController
def create
@post = current_user.posts.create!(params.require(:post))
end
end
class ApiController < ActionController::Base
rescue_from ActionController::ParameterMissing do |e|
# You can even render a jbuilder template too!
render json: {error: e.message }, status: :unprocessable_entity
end
end
class PostsController < ApiController
def update
@post = current_user.posts.find(params[:id])
@post.update_attributes!(params[:post])
end
def create
@post = current_user.posts.create!(params[:post])
end
end
class ApiController < ApplicationController::Base
rescue_from ActiveRecord::RecordNotFound do |e|
render json: { error: e.message }, status: :not_found
end
rescue_from ActiveRecord::RecordInvalid do |invalid|
# You can even use jbuilder templates to make this cleaner!
render 'shared/record_invalid', locals: { exception: invalid },
status: :unprocessable_entity