~endlessm/jasmine-gjs/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* global jasmineImporter */

const {Gio, GLib} = imports.gi;

const Config = jasmineImporter.config;
const Options = jasmineImporter.options;

// This is in case we are running the tests from a build tree that is different
// from the source tree, for example during 'meson test'.
const envSrcdir = GLib.getenv('SRCDIR');
const SRCDIR = envSrcdir ? `${envSrcdir}/` : '';

describe('Ensure array', function () {
    it('does not change an array', function () {
        expect(Config.ensureArray(['a', 'b'])).toEqual(['a', 'b']);
    });

    it('puts a single value into an array', function () {
        expect(Config.ensureArray('a')).toEqual(['a']);
    });
});

describe('Loading configuration', function () {
    beforeEach(function () {
        // suppress messages
        spyOn(window, 'print');
        spyOn(window, 'printerr');
    });

    it('loads from a file', function () {
        const config = Config.loadConfig({config: `${SRCDIR}test/fixtures/jasmine.json`});
        expect(config.a).toEqual('b');
        expect(config.c).toEqual('d');
    });

    it("doesn't load the file if no-config specified", function () {
        const config = Config.loadConfig({
            'no-config': true,
            config: `${SRCDIR}test/fixtures/jasmine.json`,
        });
        const keys = Object.keys(config);
        expect(keys).not.toContain('a');
        expect(keys).not.toContain('c');
    });

    it('loads the default file if none given', function () {
        const config = Config.loadConfig({}, `${SRCDIR}test/fixtures/jasmine.json`);
        expect(config.a).toEqual('b');
        expect(config.c).toEqual('d');
    });

    it("errors out if the file doesn't exist", function () {
        expect(() => Config.loadConfig({config: 'nonexist.json'})).toThrow();
    });

    it("doesn't error out if the default file doesn't exist", function () {
        expect(() => Config.loadConfig({}, 'nonexist.json')).not.toThrow();
    });

    it('errors out if the file is invalid', function () {
        expect(() => Config.loadConfig({
            config: `${SRCDIR}test/fixtures/invalid.json`,
        })).toThrow();
    });

    it("resolves paths relative to the config file's location", function () {
        const config = Config.loadConfig({config: `${SRCDIR}test/fixtures/path.json`});
        const location = Gio.File.new_for_path(`${SRCDIR}test/fixtures`);

        expect(config.include_paths).toContain(location.get_path());
        expect(config.spec_files).toContain(location.get_child('someSpec.js').get_path());
    });

    it('warns about unrecognized config options', function () {
        Config.loadConfig({config: `${SRCDIR}test/fixtures/jasmine.json`});
        expect(window.printerr).toHaveBeenCalledWith(jasmine.stringMatching(/^warning: /));
    });
});

describe('Configuration options to arguments', function () {
    it('lets command line arguments override config options', function () {
        const args = Config.configToArgs({options: '--color'},
            ...Options.parseOptions(['--no-color']));
        expect(args.indexOf('--no-color')).toBeGreaterThan(args.indexOf('--color'));
    });

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

    it('adds more than one exclusion path', function () {
        const args = Config.configToArgs({exclude: ['a', 'b']});
        expect(args.join(' ')).toMatch('--exclude a');
        expect(args.join(' ')).toMatch('--exclude b');
    });

    it('adds exclusions from the command line', function () {
        const args = Config.configToArgs({}, ...Options.parseOptions(['--exclude', 'a.js']));
        expect(args.join(' ')).toMatch('--exclude a.js');
    });

    it('combines exclusions from the command line and the config file', function () {
        const args = Config.configToArgs({exclude: 'b.js'},
            ...Options.parseOptions(['--exclude', 'a.js']));
        expect(args.join(' ')).toMatch('--exclude a.js');
        expect(args.join(' ')).toMatch('--exclude b.js');
    });

    it('adds one extra option', function () {
        const args = Config.configToArgs({options: '--foo'});
        expect(args).toContain('--foo');
    });

    it('adds more than one extra option', function () {
        const args = Config.configToArgs({options: ['--foo', '--bar']});
        expect(args.join(' ')).toMatch('--foo --bar');
        // order should be preserved here
    });

    it('adds one spec file', function () {
        const args = Config.configToArgs({spec_files: 'a'});
        expect(args).toContain('a');
    });

    it('adds more than one spec file', function () {
        const args = Config.configToArgs({spec_files: ['a', 'b']});
        expect(args).toContain('a');
        expect(args).toContain('b');
    });

    it('does not add spec files from config if there were some on the command line', function () {
        const args = Config.configToArgs({spec_files: ['a', 'b']}, ['c']);
        expect(args).not.toContain('a');
        expect(args).not.toContain('b');
    });

    it('passes the arguments on to the subprocess', function () {
        const args = Config.configToArgs({}, ...Options.parseOptions(['--color', 'spec.js']));
        expect(args).toContain('--color', 'spec.js');
    });

    it('passes the config file on to the subprocess as arguments', function () {
        const args = Config.configToArgs({
            environment: {},
            options: ['--color'],
            exclude: ['nonspec*.js'],
            spec_files: ['a.js', 'b.js'],
        }, [], {});
        expect(args.join(' ')).toMatch(/--exclude nonspec\*\.js/);
        expect(args).toContain('--color', 'a.js', 'b.js');
    });

    it('does not pass the config file specs if specs were on the command line', function () {
        const args = Config.configToArgs({
            environment: {},
            spec_files: ['spec2.js'],
        }, ['spec1.js']);
        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 () {
    it('sets environment variables in the launcher', function () {
        const launcher = Config.prepareLauncher({
            environment: {
                'MY_VARIABLE': 'my_value',
            },
        });
        expect(launcher.getenv('MY_VARIABLE')).toEqual('my_value');
    });

    it('unsets environment variables with null values', function () {
        const launcher = Config.prepareLauncher({
            environment: {
                'MY_VARIABLE': null,
            },
        });
        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 () {
    let args;

    beforeEach(function () {
        args = ['jasmine-runner', '--verbose', 'foo.js'];
    });

    it('executes jasmine-runner with a different interpreter binary', function () {
        args = Config.wrapArgs(args, {
            interpreter: '/path/to/custom/gjs',
        });
        expect(args).toEqual(['/path/to/custom/gjs', 'jasmine-runner', '--verbose', 'foo.js']);
    });

    it('allows adding arguments to the interpreter', function () {
        args = Config.wrapArgs(args, {
            interpreter: 'gjs -d',
        });
        expect(args).toEqual(['gjs', '-d', 'jasmine-runner', '--verbose', 'foo.js']);
    });

    it('executes jasmine-runner with a different interpreter binary from the command line', function () {
        args = Config.wrapArgs(args, {}, {
            interpreter: '/path/to/custom/gjs',
        });
        expect(args).toEqual(['/path/to/custom/gjs', 'jasmine-runner', '--verbose', 'foo.js']);
    });

    it('gives the interpreter specified on the command line priority', function () {
        args = Config.wrapArgs(args, {
            interpreter: '/path/to/other/gjs',
        }, {
            interpreter: '/path/to/custom/gjs',
        });
        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']);
    });
});