Skip to content

Instantly share code, notes, and snippets.

@fatso83
Created August 14, 2017 08:55
Show Gist options
  • Select an option

  • Save fatso83/1ce5b4fadd71fab6358aa8a7f1e534ea to your computer and use it in GitHub Desktop.

Select an option

Save fatso83/1ce5b4fadd71fab6358aa8a7f1e534ea to your computer and use it in GitHub Desktop.
A test suitable for git bisect to show sinonjs/sinon#1528
#!/bin/bash
# test script built for use with `git bisect` to find a regression
# usage: git bisect start v1.16.1 v1.15.4 && git bisect run ../test-script.sh
#
# Warning: You need a Node version <= 0.12.5
# Since this script checks out former versions of Sinon, that had build scripts
# that relied on the directory layout of NPM versions < 3, it is wise to
# use `nvm` or `n` to set the current Node version to 0.12.5 or equivalent
# To support running this script when it is placed outside of the project
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#echo SCRIPT_DIR=$SCRIPT_DIR
#echo PWD=$PWD
# delete node_modules to avoid errors on `npm install` - does not delete .bin
/bin/rm -r node_modules/* 2>/dev/null
# install the karma browser runner
if [[ -e $SCRIPT_DIR/backup-karma-dir ]]; then
echo "Reusing previous Karma download"
mkdir node_modules 2>/dev/null
cp -r $SCRIPT_DIR/backup-karma-dir/* node_modules/
else
#reinstall
npm install karma mocha chai karma-chrome-launcher karma-mocha karma-chai
# save for later for speed up
mkdir "$SCRIPT_DIR/backup-karma-dir"
cp -r node_modules/* "$SCRIPT_DIR/backup-karma-dir/"
fi
npm install && ./build || exit 1
# make a karma config
cat > $SCRIPT_DIR/karma.conf << EOF
module.exports = function (config) {
config.set({
basePath : '.',
// frameworks to use
frameworks : ['mocha', 'chai'],
reporters : ['progress'],
// list of files / patterns to load in the browser
files : [
'pkg/sinon.js',
'regression.test.js'
],
browsers : ['Chrome' ],
singleRun : true
});
};
EOF
# add a regression test
cat > $SCRIPT_DIR/regression.test.js << EOF
console.log('sinon', window.sinon, sinon)
describe('#1528', function() {
it('should pass', function(done) {
var server = sinon.fakeServer.create();
server.respondImmediately = true;
// each response should be triggered exactly once
var fooResponded = false;
var defaultResponded = false;
// default responder
server.respondWith(function(xhr){
console.log('default requested')
if (defaultResponded)
console.log("default requested more than once!");
defaultResponded = true;
try {
xhr.respond(404, {}, '');
}catch(err) {
console.log(err);
// due to phantomjs peculiarities we need to wrap it before rethrowing
// to make the process exit with 1
done(err)
throw new Error(err);
}
});
// foo responder
server.respondWith('/foo', function(xhr){
console.log('foo requested')
if (fooResponded)
console.log("foo requested more than once!");
fooResponded = true;
return xhr.respond(200, {}, '');
});
// request foo
var r = new XMLHttpRequest();
console.log('requesting foo');
r.open('GET', '/foo', false);
r.send();
//console.log('status == 200?', r.status === 200);
r.status === 200 && done() || done("wrong http status ");
});
});
EOF
# run the tests
./node_modules/.bin/karma start $SCRIPT_DIR/karma.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment