Skip to content

Commit

Permalink
Windows support, at least for the tests re: issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
mixu committed Oct 30, 2012
1 parent 010b331 commit 42b1016
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
8 changes: 5 additions & 3 deletions lib/glue.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ Renderer.prototype._render = function(onDone) {

var result = [];
this.build.render(result, function(pkgId) {
var relpath = self.build.main.replace(/^\.\//, '').replace(new RegExp('^'+self.build.basepath), '');
var relpath = self.build.main.replace(/^\.\//, '').replace(new RegExp('^' +
(path.sep == '\\' ? self.build.basepath.replace(/\\/g, '\\\\') : self.build.basepath ) // windows
), '');
onDone({
replaced: self.replaced,
code: self.code,
Expand Down Expand Up @@ -172,13 +174,13 @@ Renderer.prototype.watch = function(onDone) {
Renderer.prototype._updateBasePath = function() {
var basepath = this.build.basepath;
basepath = this._fullPath(basepath);
basepath += (basepath[basepath.length-1] !== '/' ? '/' : '');
basepath += (basepath[basepath.length-1] !== path.sep ? path.sep : '');
this.build.basepath = basepath;
}

Renderer.prototype._fullPath = function(p) {
if(p.substr(0, 1) == '.') {
p = path.normalize(this.options.reqpath + '/' + p);
p = path.normalize(this.options.reqpath + path.sep + p);
}
return p;
};
Expand Down
6 changes: 4 additions & 2 deletions lib/group.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs');
var fs = require('fs'),
path = require('path');

// A set of file/folder paths defined as:
// - include(path/file)
Expand All @@ -16,10 +17,11 @@ Group.prototype.include = function(filepath){
paths = (Array.isArray(filepath) ? filepath : [ filepath ]);

paths.forEach(function(p) {
p = path.normalize(p); // for windows
var isDirectory = fs.statSync(p).isDirectory();

if (isDirectory) {
p += (p[p.length-1] !== '/' ? '/' : '');
p += (p[p.length-1] !== path.sep ? path.sep : '');
return fs.readdirSync(p).forEach(function (f) {
self.include(p + f);
});
Expand Down
8 changes: 6 additions & 2 deletions lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Package.prototype.dependency = function(name, contextPath) {
resolver.expand(resolvePath, function(basePath, main, files, dependencies) {
child.name = name;
child.files = files;
child.main = main.replace(new RegExp('^'+basePath), '');
child.main = (main.substr(0, basePath.length) == basePath ? main.substr(basePath.length) : main);
child.basepath = basePath;
dependencies.forEach(function(dep) {
child.dependency(dep, basePath);
Expand All @@ -75,7 +75,11 @@ Package.prototype.dependency = function(name, contextPath) {
Package.prototype._runHandlers = function(selfId, onDone) {
var self = this;
function relative(filename){
return filename.replace(new RegExp('^'+self.basepath), '');
var temp = (filename.substr(0, self.basepath.length) == self.basepath ? filename.substr(self.basepath.length) : filename);
if(path.sep != '/') {
temp = temp.replace(/\\/g, '/'); // windows
}
return temp;
}
var opts = { relative: relative },
result = {},
Expand Down
2 changes: 1 addition & 1 deletion test/glue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports['glue'] = {

'concat calls render() on arguments and returns the full result': function(done) {
var assertions = 0;
this.g.concat([
Glue.concat([
{ render: function(done) { assertions++; done(undefined, 'a'); } },
{ render: function(done) { assertions++; done(undefined, 'b'); } },
], function(err, txt) {
Expand Down
9 changes: 5 additions & 4 deletions test/group.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs'),
path = require('path'),
assert = require('assert'),
Group = require('../lib/group');

Expand All @@ -13,16 +14,16 @@ exports['given a group'] = {
var g = this.group;
var result = g.include(__dirname+'/fixtures/rendertest/simple.js').resolve();
assert.equal(result.length, 1);
assert.equal(result[0], __dirname+'/fixtures/rendertest/simple.js');
assert.equal(result[0], path.normalize(__dirname+'/fixtures/rendertest/simple.js'));
done();

},
'can include a directory': function(done) {
var g = this.group;
var result = g.include(__dirname+'/fixtures/rendertest/').resolve();
assert.equal(result.length, 2);
assert.equal(result[0], __dirname+'/fixtures/rendertest/has_dependency.js');
assert.equal(result[1], __dirname+'/fixtures/rendertest/simple.js');
assert.equal(result[0], path.normalize(__dirname+'/fixtures/rendertest/has_dependency.js'));
assert.equal(result[1], path.normalize(__dirname+'/fixtures/rendertest/simple.js'));
done();
},

Expand All @@ -32,7 +33,7 @@ exports['given a group'] = {
.exclude(new RegExp('.*simple\\.js$'))
.resolve();
assert.equal(result.length, 1);
assert.equal(result[0], __dirname+'/fixtures/rendertest/has_dependency.js');
assert.equal(result[0], path.normalize(__dirname+'/fixtures/rendertest/has_dependency.js'));
done();
}
};
Expand Down
31 changes: 16 additions & 15 deletions test/package.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs'),
path = require('path'),
util = require('util'),
assert = require('assert'),
Package = require('../lib/package');
Expand Down Expand Up @@ -33,9 +34,9 @@ exports['package'] = {
var child = this.p.children[0];
assert.equal(child.name, 'foo');
assert.equal(child.main, '/foo.js');
assert.equal(child.basepath, __dirname+'/fixtures/expandsingle/node_modules');
assert.equal(child.basepath, path.normalize(__dirname+'/fixtures/expandsingle/node_modules'));
assert.equal(child.files.length, 1);
assert.equal(child.files[0], __dirname+'/fixtures/expandsingle/node_modules/foo.js');
assert.equal(child.files[0], path.normalize(__dirname+'/fixtures/expandsingle/node_modules/foo.js'));

var result = [];
this.p.render(result, function(selfId) {
Expand All @@ -54,11 +55,11 @@ exports['package'] = {
var child = this.p.children[0];
assert.equal(child.name, 'foo');
assert.equal(child.main, '/index.js');
assert.equal(child.basepath, __dirname+'/fixtures/expandindex/node_modules/foo');
assert.equal(child.basepath, path.normalize(__dirname+'/fixtures/expandindex/node_modules/foo'));
assert.equal(child.files.length, 3);
assert.equal(child.files[0], child.basepath+'/index.js');
assert.equal(child.files[1], child.basepath+'/lib/sub.js');
assert.equal(child.files[2], child.basepath+'/other.js');
assert.equal(child.files[0], path.normalize(child.basepath+'/index.js'));
assert.equal(child.files[1], path.normalize(child.basepath+'/lib/sub.js'));
assert.equal(child.files[2], path.normalize(child.basepath+'/other.js'));

var result = [];
this.p.render(result, function(selfId) {
Expand All @@ -81,11 +82,11 @@ exports['package'] = {
assert.equal(p.children.length, 1);
assert.equal(p.children[0].name, 'foo');
assert.equal(p.children[0].main, '/lib/sub.js');
assert.equal(p.children[0].basepath, __dirname+'/fixtures/expandpackage/node_modules/foo');
assert.equal(p.children[0].basepath, path.normalize(__dirname+'/fixtures/expandpackage/node_modules/foo'));
assert.equal(p.children[0].files.length, 3);
assert.equal(p.children[0].files[0], p.children[0].basepath+'/lib/sub.js');
assert.equal(p.children[0].files[1], p.children[0].basepath+'/other.js');
assert.equal(p.children[0].files[2], p.children[0].basepath+'/package.json');
assert.equal(p.children[0].files[0], path.normalize(p.children[0].basepath+'/lib/sub.js'));
assert.equal(p.children[0].files[1], path.normalize(p.children[0].basepath+'/other.js'));
assert.equal(p.children[0].files[2], path.normalize(p.children[0].basepath+'/package.json'));

var result = [];
p.render(result, function(selfId) {
Expand All @@ -106,18 +107,18 @@ exports['package'] = {
assert.equal(p.children.length, 1);
assert.equal(p.children[0].name, 'foo');
assert.equal(p.children[0].main, '/index.js');
assert.equal(p.children[0].basepath, __dirname+'/fixtures/hassubdependency/node_modules/foo');
assert.equal(p.children[0].basepath, path.normalize(__dirname+'/fixtures/hassubdependency/node_modules/foo'));
assert.equal(p.children[0].files.length, 2);
assert.equal(p.children[0].files[0], p.children[0].basepath+'/index.js');
assert.equal(p.children[0].files[1], p.children[0].basepath+'/package.json');
assert.equal(p.children[0].files[0], path.normalize(p.children[0].basepath+'/index.js'));
assert.equal(p.children[0].files[1], path.normalize(p.children[0].basepath+'/package.json'));
// subdependency
assert.equal(p.children[0].children.length, 1);
var child = p.children[0].children[0];
assert.equal(child.name, 'bar');
assert.equal(child.main, '/index.js');
assert.equal(child.basepath, __dirname+'/fixtures/hassubdependency/node_modules/foo/node_modules/bar');
assert.equal(child.basepath, path.normalize(__dirname+'/fixtures/hassubdependency/node_modules/foo/node_modules/bar'));
assert.equal(child.files.length, 1);
assert.equal(child.files[0], child.basepath+'/index.js');
assert.equal(child.files[0], path.normalize(child.basepath+'/index.js'));

var result = [];
p.render(result, function(selfId) {
Expand Down

0 comments on commit 42b1016

Please sign in to comment.