Skip to content

Commit

Permalink
test(bundler): cleanup test code on windows
Browse files Browse the repository at this point in the history
Also fix a missed forceCjsWrap on windows.
  • Loading branch information
3cp committed Oct 16, 2018
1 parent 54aa49b commit b26be7d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/build/bundled-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ exports.BundledSource = class {
let contents;
// forceCjsWrap bypasses a r.js parse bug.
// See lib/amodro-trace/read/cjs.js for more info.
let forceCjsWrap = !!modulePath.match(/\/(cjs|commonjs)\//i);
let forceCjsWrap = !!modulePath.match(/(\/|\\)(cjs|commonjs)(\/|\\)/i);

try {
contents = cjsTransform(modulePath, this.contents, forceCjsWrap);
Expand Down
14 changes: 7 additions & 7 deletions lib/build/package-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ exports.PackageAnalyzer = class {
// when path is node_modules/package/foo/bar
// set path to node_modules/package
// set main to foo/bar
loaderConfig.path = path.relative(this.project.paths.root, description.location);
loaderConfig.path = path.relative(this.project.paths.root, description.location).replace(/\\/g, '/');

if (pathParts.dir.length > description.location.length + 1) {
loaderConfig.main =
pathParts.dir.substr(description.location.length + 1) + '/' + removeJsExtension(pathParts.base);
const main = path.join(pathParts.dir.substr(description.location.length + 1), removeJsExtension(pathParts.base));
loaderConfig.main = main.replace(/\\/g, '/');
} else if (pathParts.dir.length === description.location.length) {
loaderConfig.main = removeJsExtension(pathParts.base);
loaderConfig.main = removeJsExtension(pathParts.base).replace(/\\/g, '/');
} else {
throw new Error(`Path: "${loaderConfig.path}" is not in: ${description.location}`);
}
}
} else {
loaderConfig.main = removeJsExtension(loaderConfig.main);
loaderConfig.main = removeJsExtension(loaderConfig.main).replace(/\\/g, '/');
}
}
})
Expand All @@ -94,7 +94,7 @@ function fillUpPackageRoot(project, description) {
try {
let stat = fs.statSync(path.resolve(project.paths.root, _path));
if (stat.isFile()) {
description.loaderConfig.packageRoot = path.dirname(description.loaderConfig.path);
description.loaderConfig.packageRoot = path.dirname(description.loaderConfig.path).replace(/\\/g, '/');
}
} catch (e) {
// _path is not a file
Expand Down Expand Up @@ -188,7 +188,7 @@ function determineLoaderConfig(project, description) {
description.loaderConfig = {name: description.name};
}

description.loaderConfig.path = path.relative(project.paths.root, description.location);
description.loaderConfig.path = path.relative(project.paths.root, description.location).replace(/\\/g, '/');
description.loaderConfig.main = removeJsExtension(mainFile);
} else {
throw new Error(`The "${description.name}" package references a main file that does not exist: ${sourcePath}`);
Expand Down
2 changes: 1 addition & 1 deletion lib/build/stub-core-nodejs-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const UNAVAIABLE_CORE_MODULES = [
const EMPTY_MODULE = 'define(function(){});';

function resolvePath(packageName, root) {
return path.relative(root, Utils.resolvePackagePath(packageName));
return path.relative(root, Utils.resolvePackagePath(packageName)).replace(/\\/g, '/');
}

// note all paths here assumes local node_modules folder
Expand Down
29 changes: 15 additions & 14 deletions spec/lib/build/dependency-inclusion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DependencyInclusion = require('../../../lib/build/dependency-inclusion').D
const DependencyDescription = require('../../../lib/build/dependency-description').DependencyDescription;
const mockfs = require('mock-fs');
const Minimatch = require('minimatch').Minimatch;
const path = require('path');

describe('the DependencyInclusion module', () => {
let bundler;
Expand All @@ -22,7 +23,7 @@ describe('the DependencyInclusion module', () => {
SourceInclusion.prototype.getAllModuleIds = function() {
if (this.includedBy && !this.pattern.match(/\?|\{|\*/)) {
// simple pattern
let id = this.includedBy.description.name + '/' + this.pattern.substr(this.includedBy.description.loaderConfig.path.length + 1);
let id = this.includedBy.description.name + '/' + this.pattern.substr(this.includedBy.description.loaderConfig.path.length + 1).replace(/\\/g, '/');
if (id.endsWith('.js')) id = id.substr(0, id.length - 3);
return [id];
}
Expand Down Expand Up @@ -66,7 +67,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResources()
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/index.js');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'index.js'));
expect(bundle.addAlias).toHaveBeenCalledWith('my-package', 'my-package/index');
done();
})
Expand Down Expand Up @@ -96,7 +97,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResources()
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/index.css');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'index.css'));
expect(bundle.addAlias).not.toHaveBeenCalled();
done();
})
Expand Down Expand Up @@ -126,7 +127,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResources()
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package.css/index.css');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package.css', 'index.css'));
expect(bundle.addAlias).toHaveBeenCalledWith('my-package.css', 'my-package.css/index.css');
done();
})
Expand Down Expand Up @@ -192,9 +193,9 @@ describe('the DependencyInclusion module', () => {
sut.traceResources()
.then(() => {
expect(bundle.includes.length).toBe(3);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/index.js');
expect(bundle.includes[1].pattern).toBe('../node_modules/my-package/lib/foo.js');
expect(bundle.includes[2].pattern).toBe('../node_modules/my-package/lib/foo.css');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'index.js'));
expect(bundle.includes[1].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'lib', 'foo.js'));
expect(bundle.includes[2].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'lib', 'foo.css'));
expect(bundle.addAlias).toHaveBeenCalledWith('my-package', 'my-package/index');
done();
})
Expand Down Expand Up @@ -230,7 +231,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResource('lib/foo')
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/lib/foo.js');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'lib', 'foo.js'));
expect(bundle.addAlias).not.toHaveBeenCalled();
done();
})
Expand Down Expand Up @@ -268,8 +269,8 @@ describe('the DependencyInclusion module', () => {
.then(() => sut.traceResource('css/bar.css'))
.then(() => {
expect(bundle.includes.length).toBe(2);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/dist/js/foo.js');
expect(bundle.includes[1].pattern).toBe('../node_modules/my-package/dist/css/bar.css');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'dist', 'js', 'foo.js'));
expect(bundle.includes[1].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'dist', 'css', 'bar.css'));
expect(bundle.addAlias).toHaveBeenCalledTimes(2);
expect(bundle.addAlias.calls.argsFor(0)).toEqual(['my-package/foo', 'my-package/dist/js/foo']);
expect(bundle.addAlias.calls.argsFor(1)).toEqual(['my-package/css/bar.css', 'my-package/dist/css/bar.css']);
Expand Down Expand Up @@ -343,7 +344,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResource('lib/foo')
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/lib/foo.json');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'lib', 'foo.json'));
expect(bundle.addAlias).toHaveBeenCalledWith('my-package/lib/foo', 'my-package/lib/foo.json');
done();
})
Expand Down Expand Up @@ -379,7 +380,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResource('lib/foo')
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/lib/foo/index.js');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'lib', 'foo', 'index.js'));
expect(bundle.addAlias).toHaveBeenCalledWith('my-package/lib/foo', 'my-package/lib/foo/index');
done();
})
Expand Down Expand Up @@ -415,7 +416,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResource('lib/foo')
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/lib/foo/index.json');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'lib', 'foo', 'index.json'));
expect(bundle.addAlias).toHaveBeenCalledWith('my-package/lib/foo', 'my-package/lib/foo/index.json');
done();
})
Expand Down Expand Up @@ -452,7 +453,7 @@ describe('the DependencyInclusion module', () => {
sut.traceResource('lib/foo')
.then(() => {
expect(bundle.includes.length).toBe(1);
expect(bundle.includes[0].pattern).toBe('../node_modules/my-package/lib/foo/fmodule.js');
expect(bundle.includes[0].pattern).toBe(path.join('..', 'node_modules', 'my-package', 'lib', 'foo', 'fmodule.js'));
expect(bundle.addAlias).toHaveBeenCalledWith('my-package/lib/foo', 'my-package/lib/foo/fmodule');
done();
})
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/build/package-analyzer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ describe('The PackageAnalyzer', () => {
sut.analyze('my-package')
.then(description => {
expect(description.loaderConfig.name).toBe('my-package');
expect(description.loaderConfig.path).toBe(path.join('..', 'node_modules', 'my-package'));
expect(description.loaderConfig.path).toBe('../node_modules/my-package');
expect(description.loaderConfig.main).toBe('foo');
done();
})
Expand Down Expand Up @@ -473,7 +473,7 @@ describe('The PackageAnalyzer', () => {
sut.analyze('my-package')
.then(description => {
expect(description.loaderConfig.name).toBe('my-package');
expect(description.loaderConfig.path).toBe(path.join('..', 'node_modules', 'my-package'));
expect(description.loaderConfig.path).toBe('../node_modules/my-package');
expect(description.loaderConfig.main).toBe('index');
done();
})
Expand All @@ -492,7 +492,7 @@ describe('The PackageAnalyzer', () => {
sut.analyze('my-package')
.then(description => {
expect(description.loaderConfig.name).toBe('my-package');
expect(description.loaderConfig.path).toBe(path.join('..', 'node_modules', 'my-package'));
expect(description.loaderConfig.path).toBe('../node_modules/my-package');
expect(description.loaderConfig.main).toBe('index');
done();
})
Expand Down Expand Up @@ -524,7 +524,7 @@ describe('The PackageAnalyzer', () => {
fsConfig[project.paths.root] = {};
mockfs(fsConfig);

let p = path.resolve(path.join('node_modules', 'my-package', 'foo.js'));
let p = path.resolve('node_modules', 'my-package', 'foo.js');

sut.analyze('my-package')
.then(() => done.fail('should have thrown an exception'))
Expand Down
11 changes: 6 additions & 5 deletions spec/lib/build/source-inclusion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const BundlerMock = require('../../mocks/bundler');
const SourceInclusion = require('../../../lib/build/source-inclusion').SourceInclusion;
const mockfs = require('mock-fs');
const Minimatch = require('minimatch').Minimatch;
const path = require('path');

describe('the SourceInclusion module', () => {
let bundler;
Expand Down Expand Up @@ -108,10 +109,10 @@ describe('the SourceInclusion module', () => {
};

let sut = new SourceInclusion(bundle, '../node_modules/foo/**/*.js');
sut._getProjectRoot = () => '/src';
sut._getProjectRoot = () => 'src';
mockfs({
'/node_modules/foo/foo-bar.js': 'some-content',
'/node_modules/foo/fop/bar.js': 'some-content'
'node_modules/foo/foo-bar.js': 'some-content',
'node_modules/foo/fop/bar.js': 'some-content'
});

sut.addAllMatchingResources()
Expand All @@ -123,8 +124,8 @@ describe('the SourceInclusion module', () => {
expect(arg0[1]).toEqual(sut);
expect(arg1[1]).toEqual(sut);

expect(arg0[0].path).toEqual('/node_modules/foo/foo-bar.js');
expect(arg1[0].path).toEqual('/node_modules/foo/fop/bar.js');
expect(arg0[0].path).toEqual(path.resolve('node_modules/foo/foo-bar.js'));
expect(arg1[0].path).toEqual(path.resolve('node_modules/foo/fop/bar.js'));
done();
})
.catch(e => done.fail(e));
Expand Down

0 comments on commit b26be7d

Please sign in to comment.