Skip to content

Instantly share code, notes, and snippets.

View mhenrixon's full-sized avatar
🐢
I may be slow to respond.

Mikael Henriksson mhenrixon

🐢
I may be slow to respond.
View GitHub Profile
# Public: Stubs partial search
# Examples =>
# stub_partial_search(:node, 'name:web*').and_return([{ 'fqdn' => 'web01.example.com' }])
#
def stub_partial_search(type, query)
allow(Chef).to receive(:partial_search).and_call_original
expect(Chef).to receive(:partial_search).with(type, query, any_args)
end
@mhenrixon
mhenrixon / 0_reuse_code.js
Last active August 29, 2015 14:15
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@mhenrixon
mhenrixon / add_missing_newline.sh
Created April 22, 2014 09:44
Recursively adds newline to the end of every file with the .rb extension
for file in **/*.rb; do
[ -e "$file" ] || continue
if [[ -n "$(tail -c 1 <"$file")" ]]; then
echo >> "$file"
fi;
done
@mhenrixon
mhenrixon / cs_merge
Last active February 1, 2020 03:43
How we merged multiple github repos into a monolithic project repo
#!/bin/bash
#
# This script merges all the repos we have into subfolders in a new repo
#
# Clones or copies a repository from either disc or remote repository.
cloppy() {
local dir=$1
if [[ -z $2 ]]; then
local repo=$dir
@mhenrixon
mhenrixon / dotify_spec.rb
Last active August 29, 2015 13:57
How to dotify a ruby hash (to be able to more easily convert it to a java like dot notated config file)
def dotify(hash, k = [])
return {k.join('.') => hash} unless hash.is_a?(Hash)
hash.inject({}){ |h, v| h.merge! dotify(v[-1], k + [v[0]]) }
end
describe "dotify" do
let(:hash) { { foo: { bar: { baz: true } } } }
it "converts hash to dot notated keys" do
expect(dotify(hash)).to eq("foo.bar.baz" => true)
@mhenrixon
mhenrixon / call_for_receive_message_chain.rb
Last active December 27, 2015 22:29
This is my case for why I want to stub a chain of messages. Even if you take a look at the refactored version that test could still contain a bug. What I want to test is that some external object. In the last alternative not only does the test code become ugly but the implementation does too. I only use this for rails associations to keep stuff …
# I usually end up with something like below in my code
def fetch_something
current_something.coll_association.find_by some: 123, other: 'sasd'
end
# I want to test that the finder gets called with the right arguments
it "finds cool_association using the right arguments" do
expect(current_something).to receive_message_chain('cool_association.find_by').
with(some: 123, other: 'sasd') { cool_association }
end
Update server
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo reboot
Enable Swedish locale and utf-8
sudo locale-gen sv_SE.UTF-8
sudo dpkg-reconfigure locales
Open sudo vim /etc/environment and add the folowing lines
@mhenrixon
mhenrixon / dns_cache.sh
Created April 17, 2013 11:48
How to flush dns cache on Mac OS X
dscacheutil -flushcache
@mhenrixon
mhenrixon / getJSON.js
Created March 15, 2013 10:23
fetching widgets
$.getJSON("/admin/widgets", function(data) {
$.each(data, function(index, object) {
var widget = object.table;
console.log('widget id:' + widget.id, + ', name' + widget.name);
});
});
@mhenrixon
mhenrixon / pkill.sh
Created March 10, 2013 13:32
How to kill all processes with a specific name (no questions asked)
#!/bin/sh
for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
kill $X;
done