Skip to content

Instantly share code, notes, and snippets.

@bgerstle
bgerstle / lctl_clean
Created May 30, 2014 11:44
Script I wrote to unset all the environment variables in launchctl. Note that you might need to `rm /etc/launchd.conf` to really "clean" it.
unsetenv PATH
unsetenv GREP_COLOR
unsetenv MANPATH
unsetenv LDFLAGS
unsetenv TERM_PROGRAM
unsetenv GEM_HOME
unsetenv M2
unsetenv TERM
unsetenv SHELL
unsetenv TMPDIR
@bgerstle
bgerstle / gen_no_login.py
Last active August 29, 2015 14:04
Generates a link to a Sauce Labs job that anyone can access, using a permanent access token
#! /usr/bin/env python
import hmac
import sys
import os
from hashlib import md5
def exit_with_usage():
print """
Usage: gen_no_login job_id [username] [access_key]
@bgerstle
bgerstle / javscript_dislikes.js
Last active August 29, 2015 14:08
Reasons I don't like Javascript
[] === [] //< false, see http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript
@bgerstle
bgerstle / DictionaryCopyingTests.m
Last active August 29, 2015 14:15
Some tests demonstrating copying behavior of immutable and mutable dictionaries.
- (void)testCopyReturnsImmutable
{
NSDictionary* immutable = @{@"foo": @"bar"};
NSMutableDictionary* mutable = [immutable mutableCopy];
NSDictionary* copyOfMutable = [mutable copy];
XCTAssertFalse([copyOfMutable isMemberOfClass:[NSMutableDictionary class]]);
// interestingly, the class is actually __NSDictionaryI, so you can't assert isMemberOfClass:NSDictionary
XCTAssertTrue([copyOfMutable isKindOfClass:[NSDictionary class]]);
}
@bgerstle
bgerstle / exception_fail.m
Created March 24, 2015 02:41
Objective-C @try/@catch/@finally prevents "control reaches end of non-void function" warnings/errors from being detected.
// This demonstrates a critical, yet absent compiler warning for the missing return in this non-void function.
- (id)nonVoidFunctionWithoutReturn {
@try {
[NSException raise:@"foo" format:nil];
} @catch (NSException* e) {
NSLog([e description]);
}
/*
@finally {
// adding @finally doesn't make a difference
@bgerstle
bgerstle / measureBlockFailure.txt
Created April 12, 2015 22:02
Stack trace with failed assertion due to calling `-[XCTestCase measureBlock]` inside a spec.
2015-04-12 18:01:37.224 xctest[36363:1321742] *** Assertion failure in -[HomeFriesTests.AppleHashSpec measureMetrics:automaticallyStartMeasuring:forBlock:], /SourceCache/XCTest_Sim/XCTest-7503/XCTestFramework/Classes/XCTestCase.m:1022
<unknown>:0: error: -[HomeFriesTests.AppleHashSpec a_hashable_person__should_have_a_performant_hash_function] : failed: caught "NSInternalInconsistencyException", "-numberOfTestIterationsForTestWithSelector: returned 0 for -[HomeFriesTests.AppleHashSpec (null)]. -numberOfTestIterationsForTestWithSelector: must return 1 for tests that call -measure...Block: APIs."
(
0 CoreFoundation 0x000000010c124c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010bdbdbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010c124aca +[NSException raise:format:arguments:] + 106
3 Foundation 0x000000010b9d298f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] +
@bgerstle
bgerstle / csv_histogram.hs
Last active August 29, 2015 14:19
Haskell CSV histogram printer
import qualified Data.Text as Text
import qualified Data.Text.IO
import qualified Data.Map as Map
import Data.List
import Data.Ord
-- Restrict histogram values to Int (shouldn't be too big and removes default constraint warnings)
type TextHistogramValue = Int
type TextHistogramKey = Text.Text
type TextHistogramEntry = (TextHistogramKey, TextHistogramValue)
@bgerstle
bgerstle / extract_xcappdata.sh
Created May 27, 2015 14:23
Extract the "Documents" folder from one or more "xcappdata" archives into a given directory.
#! /usr/bin/env bash
set -e
declare -r OUTPUT_DIR="$1"
shift
if [[ "$OUTPUT_DIR" == "" ]]; then
echo "Need to specify an output directory as the first argument"
@bgerstle
bgerstle / block_arg_cycles.m
Last active August 29, 2015 14:23
Retain cycle unit tests
@interface WMFRetainCycleTests : XCTestCase
@property (strong, nonatomic) void(^block)(id);
@end
@implementation WMFRetainCycleTests
- (void)testUsingArgument {
WMFRetainCycleTests* noCycle = [WMFRetainCycleTests new];
__weak WMFRetainCycleTests* weakRef = noCycle;
noCycle.block = ^ (id y) {
@bgerstle
bgerstle / GenericPrintableResponses.swift
Last active August 29, 2015 14:27
Experimented with Swift generics after reading David Owens' post about protocols (http://owensd.io/2015/08/06/protocols.html)
import Foundation
public protocol UniversalResourceType {
var locator: NSURL { get }
}
public protocol HTTPResponseType : UniversalResourceType, Printable { }
public class HTTPResponse<T: Printable> : UniversalResourceType, Printable {
public var response: T