Permalink
Browse files

added tests for build target parsing

  • Loading branch information...
1 parent f8b6e53 commit 2f2c46ca871eb6e773b18ccba860f6f90ff593c4 @popeindustries committed Nov 15, 2011
Showing with 59 additions and 38 deletions.
  1. +5 −4 lib/builder.js
  2. +4 −4 src/builder.coffee
  3. +50 −28 test/builder-test.coffee
  4. +0 −2 test/file-test.coffee
View
@@ -179,13 +179,14 @@ module.exports = Builder = (function() {
};
Builder.prototype._targetFactory = function(input, output, type) {
- var inSources, inputpath, location, outputpath, _i, _len, _ref;
+ var dir, inSources, inputpath, location, outputpath, _i, _len, _ref;
inputpath = path.resolve(this.base, input);
- outputpath = path.resolve(this.base, ouput);
- _ref = this[type + 'Sources'].location;
+ outputpath = path.resolve(this.base, output);
+ _ref = this[type + 'Sources'].locations;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
location = _ref[_i];
- inSources = path.dirname(inputpath).test(location);
+ dir = fs.statSync(inputpath).isDirectory() ? inputpath : path.dirname(inputpath);
+ inSources = dir.indexOf(location) >= 0;
if (inSources) break;
}
if (!inSources) {
View
@@ -139,10 +139,11 @@ module.exports = class Builder
_targetFactory: (input, output, type) ->
inputpath = path.resolve @base, input
- outputpath = path.resolve @base, ouput
+ outputpath = path.resolve @base, output
# Check that input is included in sources
- for location in @[type + 'Sources'].location
- inSources = path.dirname(inputpath).test location
+ for location in @[type + 'Sources'].locations
+ dir = if fs.statSync(inputpath).isDirectory() then inputpath else path.dirname(inputpath)
+ inSources = dir.indexOf(location) >= 0
break if inSources
# Abort if input isn't in sources
unless inSources
@@ -153,5 +154,4 @@ module.exports = class Builder
term.out "#{term.colour('ERROR [invalid]', term.RED)} a file (#{term.colour(output, term.GREY)}) is not a valid output target for a directory (#{term.colour(input, term.GREY)}) input target", 2
return null
return new target[type.toUpperCase() + 'Target'] inputpath, outputpath
-
View
@@ -4,8 +4,9 @@ vows = require 'vows'
Builder = require '../lib/builder'
term = require '../lib/terminal'
file = require '../lib/file'
+target = require '../lib/target'
-# term.silent = true
+term.silent = true
loadConfig = (workingDir, configPath) ->
process.chdir(workingDir)
@@ -40,69 +41,90 @@ vows.describe('builder/matching')
vows.describe('builder/configuration')
.addBatch
'loading config JSON file':
- 'from valid working directory':
+ 'from a valid working directory':
topic: ->
loadConfig(path.resolve(__dirname, 'fixtures/project'))
- 'returns true': (result) ->
+ 'should return true': (result) ->
assert.isTrue result
- 'from valid nested working directory':
+ 'from a valid nested working directory':
topic: ->
loadConfig(path.resolve(__dirname, 'fixtures/project/src/coffee'))
- 'returns true': (result) ->
+ 'should return true': (result) ->
assert.isTrue result
- 'from invalid working directory':
+ 'from an invalid working directory':
topic: ->
loadConfig(path.resolve(__dirname, '../../'))
- 'returns false': (result) ->
+ 'should return false': (result) ->
assert.isFalse result
- 'with invalid format':
+ 'with a invalid format':
topic: ->
loadConfig(path.resolve(__dirname, 'fixtures/bad-json'))
- 'returns false': (result) ->
+ 'should return false': (result) ->
assert.isFalse result
- 'with valid file path':
+ 'with a valid file path':
topic: ->
loadConfig(path.resolve(__dirname), 'fixtures/project/build.json')
- 'returns true': (result) ->
+ 'should return true': (result) ->
assert.isTrue result
- 'with valid directory path':
+ 'with a valid directory path':
topic: ->
loadConfig(path.resolve(__dirname), 'fixtures/project')
- 'returns true': (result) ->
+ 'should return true': (result) ->
assert.isTrue result
- 'with invalid path':
+ 'with an invalid path':
topic: ->
loadConfig(path.resolve(__dirname), 'fixtures/js/build.json')
- 'returns false': (result) ->
+ 'should return false': (result) ->
assert.isFalse result
.export(module)
-vows.describe('builder/initialization')
+vows.describe('builder/initialization/source')
.addBatch
'parsing a source file':
- 'with valid path':
+ 'with a valid path':
topic: ->
- @coffeFile = path.resolve(__dirname, 'fixtures/project/src/coffee/main.coffee')
- Builder::_fileFactory @coffeFile
- 'returns a File instance': (result) ->
+ Builder::_fileFactory path.resolve(__dirname, 'fixtures/project/src/coffee/main.coffee')
+ 'should return a File instance': (result) ->
assert.instanceOf result, file.JSFile
- 'with the correct filename': (result) ->
- assert.equal @coffeFile, result.filepath
- 'that has been built':
+ 'that has already been built':
topic: ->
Builder::_fileFactory path.resolve(__dirname, 'fixtures/project/js/main.js')
- 'is ignored and returns nothing': (result) ->
+ 'should return "null"': (result) ->
assert.isNull result
.addBatch
'parsing a source directory':
'containing nested source files':
topic: ->
builder = new Builder
- @length = builder.jsSources.count
builder._parseSourceFolder path.resolve(__dirname, 'fixtures/project/src/coffee'), null, builder.jsSources
builder
- 'increases the source cache size by the number of valid files': (builder) ->
- assert.equal builder.jsSources.count - @length, 5
- 'skips ignored files': (builder) ->
+ 'should increase the source cache size by the number of valid files': (builder) ->
+ assert.equal builder.jsSources.count, 5
+ 'should skip ignored files': (builder) ->
assert.isUndefined builder.jsSources[path.resolve(__dirname, 'fixtures/project/src/coffee/ignored/_ignored.coffee')]
+ .export(module)
+
+vows.describe('builder/initialization/target')
+ .addBatch
+ 'parsing a build target':
+ topic: ->
+ builder = new Builder
+ builder.base = path.resolve(__dirname, 'fixtures/project')
+ builder._parseSourceFolder path.resolve(builder.base, 'src/coffee/other'), null, builder.jsSources
+ builder
+ 'with an input file that is not found on the source path':
+ topic: (builder) ->
+ builder._targetFactory 'src/coffee/main.coffee', 'js/main.js', builder.JS
+ 'should return "null"': (result) ->
+ assert.isNull result
+ 'with a directory input and a file output':
+ topic: (builder) ->
+ builder._targetFactory 'src/coffee/other', 'js/main.js', builder.JS
+ 'should return "null"': (result) ->
+ assert.isNull result
+ 'with a valid input and output':
+ topic: (builder) ->
+ builder._targetFactory 'src/coffee/other/Class.coffee', 'js/Class.js', builder.JS
+ 'should return a Target instance': (result) ->
+ assert.instanceOf result, target.JSTarget
.export(module)
View
@@ -6,8 +6,6 @@ term = require '../lib/terminal'
file = require '../lib/file'
coffee = require 'coffee-script'
-# term.silent = true
-
vows.describe('file/matching')
.addBatch
'JS file content':

0 comments on commit 2f2c46c

Please sign in to comment.