Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix include paths being passed to subprocess #39

Merged
merged 2 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ function optionsToArgs(options) {

function configToArgs(config, specFiles = [], options = {}) {
let retval = [];
if (config.include_paths) {
ensureArray(config.include_paths).forEach(path => {
retval.push('-I');
retval.push(path);
});
}
if (config.exclude) {
ensureArray(config.exclude).forEach(exclude => {
retval.push('--exclude');
Expand Down Expand Up @@ -121,6 +115,13 @@ function prepareLauncher(config, options = {}) {
launcher.setenv(key, config.environment[key], true);
});
}
if (config.include_paths) {
const existingPaths = launcher.getenv('GJS_PATH');
const paths = ensureArray(config.include_paths).slice();
if (existingPaths)
paths.unshift(existingPaths);
launcher.setenv('GJS_PATH', paths.join(':'), /* overwrite = */ true);
}
return launcher;
}

Expand Down
8 changes: 6 additions & 2 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,18 @@ function parseOptions(argv) {

let argvElement;
while ((argvElement = argv.shift())) {
if (!argvElement.startsWith('--')) {
if (!argvElement.startsWith('-')) {
files.push(argvElement);
continue;
}

if (!argvElement.startsWith('--')) {
printerr(`warning: Unknown argument "${argvElement}"`);
continue;
}
const argName = argvElement.slice(2);
if (!(argName in ARGS)) {
printerr(`warning: Unknown argument "${argName}"`);
printerr(`warning: Unknown argument "${argvElement}"`);
continue;
}

Expand Down
49 changes: 29 additions & 20 deletions test/configSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,6 @@ describe('Configuration options to arguments', function () {
expect(args.indexOf('--no-color')).toBeGreaterThan(args.indexOf('--color'));
});

it('adds one search path', function () {
const args = Config.configToArgs({include_paths: '/a'});
expect(args.join(' ')).toMatch('-I /a');
});

it('adds multiple search paths', function () {
const args = Config.configToArgs({include_paths: ['/a', '/b']});
expect(args.join(' ')).toMatch('-I /a');
expect(args.join(' ')).toMatch('-I /b');
});

it('adds search paths in the right order', function () {
const args = Config.configToArgs({include_paths: ['/a', '/b']});
const argstring = args.join(' ');
expect(argstring.indexOf('-I /a')).toBeLessThan(argstring.indexOf('-I /b'));
});

it('adds one exclusion path', function () {
const args = Config.configToArgs({exclude: 'a'});
expect(args.join(' ')).toMatch('--exclude a');
Expand Down Expand Up @@ -160,13 +143,10 @@ describe('Configuration options to arguments', function () {
it('passes the config file on to the subprocess as arguments', function () {
const args = Config.configToArgs({
environment: {},
include_paths: ['/path1', '/path2'],
options: ['--color'],
exclude: ['nonspec*.js'],
spec_files: ['a.js', 'b.js'],
}, [], {});
expect(args.join(' ')).toMatch('-I /path1');
expect(args.join(' ')).toMatch('-I /path2');
expect(args.join(' ')).toMatch(/--exclude nonspec\*\.js/);
expect(args).toContain('--color', 'a.js', 'b.js');
});
Expand All @@ -179,6 +159,14 @@ describe('Configuration options to arguments', function () {
expect(args).toContain('spec1.js');
expect(args).not.toContain('spec2.js');
});

it('does not pass include paths as -I arguments', function () {
const args = Config.configToArgs({
environment: {},
include_paths: ['/path1', '/path2'],
}, [], {});
expect(args.join(' ')).not.toMatch(/-I/);
});
});

describe('Manipulating the environment', function () {
Expand All @@ -199,6 +187,27 @@ describe('Manipulating the environment', function () {
});
expect(launcher.getenv('MY_VARIABLE')).toBeNull();
});

it('adds one search path', function () {
const launcher = Config.prepareLauncher({include_paths: '/a'});
expect(launcher.getenv('GJS_PATH')).toEqual('/a');
});

it('adds multiple search paths in the right order', function () {
const launcher = Config.prepareLauncher({include_paths: ['/a', '/b']});
expect(launcher.getenv('GJS_PATH')).toEqual('/a:/b');
});

it('adds search paths with a lower priority than existing search paths', function () {
const oldPath = GLib.getenv('GJS_PATH');
GLib.setenv('GJS_PATH', '/a:/b', /* overwrite = */ true);
const launcher = Config.prepareLauncher({include_paths: ['/c', '/d']});
expect(launcher.getenv('GJS_PATH')).toEqual('/a:/b:/c:/d');
if (oldPath)
GLib.setenv('GJS_PATH', oldPath, /* overwrite = */ true);
else
GLib.unsetenv('GJS_PATH');
});
});

describe('Manipulating the launcher command line', function () {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/include.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"include_paths": "include",
"spec_files": "someSpec.js"
}
5 changes: 5 additions & 0 deletions test/fixtures/include/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* exported importedFunction */

function importedFunction() {
return true;
}
5 changes: 5 additions & 0 deletions test/fixtures/include/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('A spec that should not be loaded', function () {
it('is not loaded', function () {
fail('Spec was loaded when it should not be');
});
});
6 changes: 6 additions & 0 deletions test/jasmineBootSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ describe('Jasmine boot', function () {
expect(testJasmine.specFiles).toEqual([]);
testJasmine.addSpecFiles([`${SRCDIR}test/fixtures`]);
expect(testJasmine.specFiles).toMatchAllFiles([
`${SRCDIR}test/fixtures/include/module.js`,
`${SRCDIR}test/fixtures/include/spec.js`,
`${SRCDIR}test/fixtures/otherSpec.js`,
`${SRCDIR}test/fixtures/path1/test.js`,
`${SRCDIR}test/fixtures/path2/test.js`,
Expand All @@ -139,6 +141,8 @@ describe('Jasmine boot', function () {
testJasmine.exclusions = ['otherSpec.js', 'syntaxErrorSpec.js'];
testJasmine.addSpecFiles([`${SRCDIR}test/fixtures`]);
expect(testJasmine.specFiles).toMatchAllFiles([
`${SRCDIR}test/fixtures/include/module.js`,
`${SRCDIR}test/fixtures/include/spec.js`,
`${SRCDIR}test/fixtures/someSpec.js`,
`${SRCDIR}test/fixtures/path1/test.js`,
`${SRCDIR}test/fixtures/path2/test.js`,
Expand All @@ -149,6 +153,8 @@ describe('Jasmine boot', function () {
testJasmine.exclusions = ['test/fixtures'];
testJasmine.addSpecFiles([`${SRCDIR}test/fixtures`]);
expect(testJasmine.specFiles).toMatchAllFiles([
`${SRCDIR}test/fixtures/include/module.js`,
`${SRCDIR}test/fixtures/include/spec.js`,
`${SRCDIR}test/fixtures/path1/test.js`,
`${SRCDIR}test/fixtures/path2/test.js`,
]);
Expand Down
5 changes: 5 additions & 0 deletions test/optionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ describe('Argument parser', function () {
Options.parseOptions(args);
expect(args.length).toBe(3);
});

it('does not treat a short option as a file', function () {
const [files] = Options.parseOptions(['-c', 'file.js']);
expect(files).toEqual(['file.js']);
});
});