Skip to content

Commit

Permalink
Allow executing jasmine-runner under a debugger
Browse files Browse the repository at this point in the history
The default debugger is 'gdb --args', but others can be specified.

Closes: #16
  • Loading branch information
ptomato committed Aug 25, 2020
1 parent fb6d0d7 commit 01da266
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
5 changes: 2 additions & 3 deletions bin/jasmine.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env gjs

const {Gio, GLib} = imports.gi;
const {GLib} = imports.gi;
const System = imports.system;

let runnerPath = '@pkglibexecdir@/jasmine-runner';
Expand All @@ -27,8 +27,7 @@ if (options.version) {
const config = Config.loadConfig(options);

// Launch Jasmine in a subprocess so we can control the environment
const launcher = new Gio.SubprocessLauncher();
Config.prepareLauncher(launcher, config);
const launcher = Config.prepareLauncher(config, options);
let args = Config.configToArgs(config, files, options);
args.unshift(runnerPath); // argv[0]
args = Config.wrapArgs(args, config, options);
Expand Down
12 changes: 11 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ function configToArgs(config, specFiles = [], options = {}) {
return retval;
}

function prepareLauncher(launcher, config) {
function prepareLauncher(config, options = {}) {
let flags = Gio.SubprocessFlags.NONE;
if (options.debug)
flags |= Gio.SubprocessFlags.STDIN_INHERIT;
const launcher = new Gio.SubprocessLauncher({flags});
if (config.environment) {
Object.keys(config.environment).forEach(key => {
if (config.environment[key] === null)
Expand All @@ -117,12 +121,18 @@ function prepareLauncher(launcher, config) {
launcher.setenv(key, config.environment[key], true);
});
}
return launcher;
}

function wrapArgs(args, config, options = {}) {
if (options.interpreter)
args.unshift(...options.interpreter.split(' '));
else if (config.interpreter)
args.unshift(...config.interpreter.split(' '));
if (options.debug) {
if (!options.interpreter && !config.interpreter)
args.unshift('gjs');
args.unshift(...options.debug.split(' '));
}
return args;
}
6 changes: 6 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const ARGS = {
help: 'run with the given program instead of /usr/bin/env gjs',
action: 'store',
},
'debug': {
help: 'run with a debugger [gdb --args]',
action: 'store',
nargs: '?',
const: 'gdb --args',
},
};

function parseOptions(argv) {
Expand Down
38 changes: 28 additions & 10 deletions test/configSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,22 @@ describe('Configuration options to arguments', function () {
});

describe('Manipulating the environment', function () {
let launcher;

beforeEach(function () {
launcher = jasmine.createSpyObj('launcher', ['setenv', 'unsetenv']);
});

it('sets environment variables in the launcher', function () {
Config.prepareLauncher(launcher, {
const launcher = Config.prepareLauncher({
environment: {
'MY_VARIABLE': 'my_value',
},
});
expect(launcher.setenv).toHaveBeenCalledWith('MY_VARIABLE', 'my_value', true);
expect(launcher.getenv('MY_VARIABLE')).toEqual('my_value');
});

it('unsets environment variables with null values', function () {
Config.prepareLauncher(launcher, {
const launcher = Config.prepareLauncher({
environment: {
'MY_VARIABLE': null,
},
});
expect(launcher.unsetenv).toHaveBeenCalledWith('MY_VARIABLE');
expect(launcher.getenv('MY_VARIABLE')).toBeNull();
});
});

Expand Down Expand Up @@ -243,4 +237,28 @@ describe('Manipulating the launcher command line', function () {
});
expect(args).toEqual(['/path/to/custom/gjs', 'jasmine-runner', '--verbose', 'foo.js']);
});

it('executes jasmine-runner with a debugger', function () {
args = Config.wrapArgs(args, {}, {
debug: 'gdb --args',
});
expect(args).toEqual(['gdb', '--args', 'gjs', 'jasmine-runner', '--verbose', 'foo.js']);
});

it('does not pass the gjs interpreter to the debugger if a custom one is configured', function () {
args = Config.wrapArgs(args, {
interpreter: '/path/to/custom/gjs',
}, {
debug: 'lldb --',
});
expect(args).toEqual(['lldb', '--', '/path/to/custom/gjs', 'jasmine-runner', '--verbose', 'foo.js']);
});

it('does not pass the gjs interpreter to the debugger if a custom one is given on the command line', function () {
args = Config.wrapArgs(args, {}, {
debug: 'lldb --',
interpreter: '/path/to/custom/gjs',
});
expect(args).toEqual(['lldb', '--', '/path/to/custom/gjs', 'jasmine-runner', '--verbose', 'foo.js']);
});
});

0 comments on commit 01da266

Please sign in to comment.