Skip to content

Commit

Permalink
Updated docs
Browse files Browse the repository at this point in the history
Added coffeescript loader for node
Added coffeescript test for node
Moved node tests into /test
  • Loading branch information
alex-seville committed Dec 31, 2012
1 parent 98c6579 commit 036bf6c
Show file tree
Hide file tree
Showing 30 changed files with 125 additions and 190 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Thanks to the following people:
* [msaglietto](https://github.com/msaglietto)
* [adambiggs](https://github.com/adambiggs)
* [ashwinr](https://github.com/ashwinr)
* [provegard](https://github.com/provegard)
* [flrent](https://github.com/flrent)

And thanks also to: [RequireJS](http://requirejs.org/), [Esprima](http://esprima.org/), [node-falafel](https://github.com/substack/node-falafel), [Mocha](http://visionmedia.github.com/mocha/), [Qunit](http://qunitjs.com/).
Expand Down
9 changes: 8 additions & 1 deletion docs/advanced_browser.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Advanced Guide (browser version)

This guide details creating a [custom reporter](#reporters) or [custom adapter](#adapters) for Blanket.js
This guide details creating a [custom reporter](#reporters), [custom adapter](#adapters), or [cusotm loader](#loaders) for Blanket.js

It is assumed that you have already read the Intermediate guide.

Expand Down Expand Up @@ -100,3 +100,10 @@ An example coverage result object could be:
}
}
```

## Loaders

Loaders are used to provide custom requirejs loaders to blanket.
CoffeeScript files can be covered in this manner by providing an overriding version of the requirejs coffeescript plugin.

See the [coffeescript loader](https://raw.github.com/Migrii/blanket/master/src/loaders/blanket_cs.js) as an example of a loader.
51 changes: 0 additions & 51 deletions docs/getting_started_node.md.orig

This file was deleted.

2 changes: 1 addition & 1 deletion docs/intermediate_browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To begin you will need:

3. Add a `data-cover-only` attribute to avoid having to add `data-cover` to each script you want covered. You can pass the filter value as a string to match, an array of filename, or a regular expression:
```<script src="blanket.min.js" data-cover-adapter="jasmine-blanket.js"
data-cover-only="['source1.js','src/source2.js']"></script>```
data-cover-only="[source1.js,src/source2.js]"></script>```

4. Open the test runner in the browser. The coverage details will be appended below the test results.

Expand Down
67 changes: 0 additions & 67 deletions docs/intermediate_node.md.orig

This file was deleted.

1 change: 1 addition & 0 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function(grunt) {
blanketTest: {
normal:{
node: "<%= cmds.mocha %> <%= runners.node %>",
nodeCS: "<%= cmds.mochaCS %> <%= runners.nodeCS %>",
browser: "<%= cmds.phantom %> <%= phantom.qunit %> <%= runners.browser %>",
browserRequire: "<%= cmds.phantom %> <%= phantom.qunit %> <%= runners.browserRequire %>",
browserBackbone: "<%= cmds.phantom %> <%= phantom.qunit %> <%= runners.browserBackbone %>",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"threshold": 70
},
"blanket": {
"pattern": "test"
"pattern": "test",
"loader": "./node-loaders/coffee-script"
}
},
"keywords": [
Expand Down
70 changes: 59 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,77 @@
/* */
/*---------------------------------*/
/* Blanket.js */
/* version 0.9.4 alpha */
/* version 1.0.0 */
/* See README.md for revision news */
/*---------------------------------*/
/* */
/*-------------------------------*/


var fs = require("fs");
var path = process.cwd() + '/package.json';

var file = JSON.parse(fs.readFileSync(path, 'utf8'));

var packageConfig = file.scripts &&
var fs = require("fs"),
path = require("path"),
configPath = process.cwd() + '/package.json',
file = JSON.parse(fs.readFileSync(configPath, 'utf8')),
packageConfig = file.scripts &&
file.scripts.blanket &&
file.scripts.blanket.pattern ?
file.scripts.blanket.pattern :
"src";
pattern = packageConfig;
"src",
pattern = packageConfig,
blanket = require("./blanket").blanket,
oldLoader = require.extensions['.js'];


var blanket = require("./blanket").blanket;
blanket.options("filter",pattern);
require("./node");

//helper functions
blanket.normalizeBackslashes = function (str) {
return str.replace(/\\/g, '/');
};

//you can pass in a string, a regex, or an array of files
blanket.matchPattern = function (filename,pattern){
if (typeof pattern === 'string'){
return filename.indexOf(blanket.normalizeBackslashes(pattern)) > -1;
}else if ( pattern instanceof Array ){
return pattern.some(function(elem){
return filename.indexOf(blanket.normalizeBackslashes(elem)) > -1;
});
}else if (pattern instanceof RegExp){
return pattern.test(filename);
}else{
throw new Error("Bad file instrument indicator. Must be a string, regex, or array.");
}
};

//instrument js files
require.extensions['.js'] = function(localModule, filename) {
var pattern = blanket.options("filter");
filename = blanket.normalizeBackslashes(filename);
if (blanket.matchPattern(filename,pattern)){
var content = fs.readFileSync(filename, 'utf8');
blanket.instrument({
inputFile: content,
inputFileName: filename
},function(instrumented){
var baseDirPath = blanket.normalizeBackslashes(path.dirname(filename))+'/.';
try{
instrumented = instrumented.replace(/require\s*\(\s*("|')\./g,'require($1'+baseDirPath);
localModule._compile(instrumented, filename);
}
catch(err){
console.log("Error parsing instrumented code: "+err);
}
});
}else{
oldLoader(localModule,filename);
}
};

//if a loader is specified, use it
if (file.scripts.blanket.loader){
require(file.scripts.blanket.loader)(blanket);
}

module.exports = blanket;

Expand Down
33 changes: 33 additions & 0 deletions src/node-loaders/coffee-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var fs = require("fs"),
path = require("path");

module.exports = function(blanket){
var coffeeScript = require("coffee-script");
var oldLoaderCS = require.extensions['.coffee'];

require.extensions['.coffee'] = function(localModule, filename) {

var pattern = blanket.options("filter");
filename = blanket.normalizeBackslashes(filename);
if (blanket.matchPattern(filename,pattern)){

var content = fs.readFileSync(filename, 'utf8');
content = coffeeScript.compile(content);
blanket.instrument({
inputFile: content,
inputFileName: filename
},function(instrumented){
var baseDirPath = blanket.normalizeBackslashes(path.dirname(filename))+'/.';
try{
instrumented = instrumented.replace(/require\s*\(\s*("|')\./g,'require($1'+baseDirPath);
localModule._compile(instrumented, filename);
}
catch(err){
console.log("Error parsing instrumented code: "+err);
}
});
}else{
oldLoaderCS(localModule,filename);
}
};
};
51 changes: 0 additions & 51 deletions src/node.js

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions test/test-node/fixture/src/sample.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = ->
10
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions test-node/testrunner.js → test/test-node/testrunner.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var path = require("path");
var blanket = require("../src/index");
var blanket = require("../../src/index");
blanket.options("filter","/src/blanket");

/*
since we're using blanket to test blanket,
we need to remove the module entry from the require cache
so that it can be instrumented.
*/
delete require.cache[path.normalize(__dirname+"/../src/blanket.js")];
delete require.cache[path.normalize(__dirname+"/../../src/blanket.js")];
/*
now start the tests
*/
Expand Down
4 changes: 4 additions & 0 deletions test/test-node/testrunner_cs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var blanket = require("../../src/index");
blanket.options("filter","src/sample");

require("./tests/coffee-script/test.coffee");
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* core test to lib/blanket.js */

var assert = require("assert"),
blanketCore = require("../../src/blanket").blanket,
falafel = require("../../src/lib/falafel").falafel,
blanketCore = require("../../../src/blanket").blanket,
falafel = require("../../../src/lib/falafel").falafel,
core_fixtures = require("../fixture/core_fixtures");


Expand Down
5 changes: 5 additions & 0 deletions test/test-node/tests/coffee-script/test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sampleTest = require("../../fixture/src/sample.coffee")
assert = require("assert")
describe "require test", ->
it "should return 10", ->
assert.equal 10, sampleTest()
Loading

0 comments on commit 036bf6c

Please sign in to comment.