summaryrefslogtreecommitdiff
path: root/installed-tests/js/testImporter.js
blob: f03bf634b63b34c002119627374c33abad884b0c (plain)
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
274
275
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2008 litl, LLC

describe('GI importer', function () {
    it('can import GI modules', function () {
        var GLib = imports.gi.GLib;
        expect(GLib.MAJOR_VERSION).toEqual(2);
    });

    describe('on failure', function () {
        // For these tests, we provide special overrides files to sabotage the
        // import, at the path resource:///org/gjs/jsunit/modules/badOverrides.
        let oldSearchPath;
        beforeAll(function () {
            oldSearchPath = imports.overrides.searchPath.slice();
            imports.overrides.searchPath = ['resource:///org/gjs/jsunit/modules/badOverrides'];
        });

        afterAll(function () {
            imports.overrides.searchPath = oldSearchPath;
        });

        it("throws an exception when the overrides file can't be imported", function () {
            expect(() => imports.gi.WarnLib).toThrowError(SyntaxError);
        });

        it('throws an exception when the overrides import throws one', function () {
            expect(() => imports.gi.GIMarshallingTests).toThrow('💩');
        });

        it('throws an exception when the overrides _init throws one', function () {
            expect(() => imports.gi.Regress).toThrow('💩');
        });

        it('throws an exception when the overrides _init is a primitive', function () {
            expect(() => imports.gi.Gio).toThrowError(/_init/);
        });
    });
});

// Jasmine v3 often uses duck-typing (checking for a property to determine a type) to pretty print objects.
// Unfortunately, checking for jasmineToString and other properties causes our importer objects to throw when resolving.
// Luckily, we can override the default behavior with a custom formatter.
function formatImporter(obj) {
    if (typeof obj === 'object' && obj.toString && (obj.toString()?.startsWith('[object GjsModule') || obj.toString()?.startsWith('[GjsFileImporter ')))
        return obj.toString();

    return undefined;
}

describe('Importer', function () {
    let oldSearchPath;
    let foobar, subA, subB, subFoobar;

    beforeAll(function () {
        oldSearchPath = imports.searchPath.slice();
        imports.searchPath = ['resource:///org/gjs/jsunit/modules'];

        foobar = imports.foobar;
        subA = imports.subA;
        subB = imports.subA.subB;
        subFoobar = subB.foobar;
    });

    afterAll(function () {
        imports.searchPath = oldSearchPath;
    });

    beforeEach(function () {
        jasmine.addCustomObjectFormatter(formatImporter);
    });

    it('is on the global object (backwards compatibility)', function () {
        expect(imports instanceof globalThis.GjsFileImporter).toBeTruthy();
    });

    it('is abstract', function () {
        expect(() => new globalThis.GjsFileImporter()).toThrow();
    });

    it('exists', function () {
        expect(imports).toBeDefined();
    });

    it('has a toString representation', function () {
        expect(imports.toString()).toEqual('[GjsFileImporter root]');
        expect(subA.toString()).toEqual('[GjsFileImporter subA]');
    });

    it('throws an import error when trying to import a nonexistent module', function () {
        expect(() => imports.nonexistentModuleName)
            .toThrow(jasmine.objectContaining({name: 'ImportError'}));
    });

    it('throws an error when evaluating the module file throws an error', function () {
        expect(() => imports.alwaysThrows).toThrow();
        // Try again to make sure that we properly discarded the module object
        expect(() => imports.alwaysThrows).toThrow();
    });

    it('can import a module', function () {
        expect(foobar).toBeDefined();
        expect(foobar.foo).toEqual('This is foo');
        expect(foobar.bar).toEqual('This is bar');
    });

    it('can import a module with a toString property', function () {
        expect(foobar.testToString('foo')).toEqual('foo');
    });

    it('makes deleting the import a no-op', function () {
        expect(delete imports.foobar).toBeFalsy();
        expect(imports.foobar).toBe(foobar);
    });

    it('gives the same object when importing a second time', function () {
        foobar.somethingElse = 'Should remain';
        const foobar2 = imports.foobar;
        expect(foobar2.somethingElse).toEqual('Should remain');
    });

    it('can import a submodule', function () {
        expect(subB).toBeDefined();
        expect(subFoobar).toBeDefined();
        expect(subFoobar.foo).toEqual('This is foo');
        expect(subFoobar.bar).toEqual('This is bar');
    });

    it('imports modules with a toString representation', function () {
        expect(Object.prototype.toString.call(foobar))
            .toEqual('[object GjsModule foobar]');
        expect(subFoobar.toString())
            .toEqual('[object GjsModule subA.subB.foobar]');
    });

    it('does not share the same object for a module on a different path', function () {
        foobar.somethingElse = 'Should remain';
        expect(subFoobar.somethingElse).not.toBeDefined();
    });

    it('gives the same object when importing a submodule a second time', function () {
        subFoobar.someProp = 'Should be here';
        const subFoobar2 = imports.subA.subB.foobar;
        expect(subFoobar2.someProp).toEqual('Should be here');
    });

    it('has no meta properties on the toplevel importer', function () {
        expect(imports.__moduleName__).toBeNull();
        expect(imports.__parentModule__).toBeNull();
    });

    it('sets the names of imported modules', function () {
        expect(subA.__moduleName__).toEqual('subA');
        expect(subB.__moduleName__).toEqual('subB');
    });

    it('gives a module the importer object as parent module', function () {
        expect(subA.__parentModule__).toBe(imports);
    });

    it('gives a submodule the module as parent module', function () {
        expect(subB.__parentModule__).toBe(subA);
    });

    // We want to check that the copy of the 'a' module imported directly
    // is the same as the copy that 'b' imports, and that we don't have two
    // copies because of the A imports B imports A loop.
    it('does not make a separate copy of a module imported in two places', function () {
        let A = imports.mutualImport.a;
        A.incrementCount();
        expect(A.getCount()).toEqual(1);
        expect(A.getCountViaB()).toEqual(1);
    });

    it('evaluates an __init__.js file in an imported directory', function () {
        expect(subB.testImporterFunction()).toEqual('__init__ function tested');
    });

    it('throws on an __init__.js file with a syntax error', function () {
        expect(() => imports.subBadInit.SOMETHING).toThrowError(SyntaxError);
    });

    it('throws when an __init__.js throws an error', function () {
        expect(() => imports.subErrorInit.SOMETHING).toThrowError('a bad init!');
    });

    it('accesses a class defined in an __init__.js file', function () {
        let o = new subB.ImporterClass();
        expect(o).not.toBeNull();
        expect(o.testMethod()).toEqual('__init__ class tested');
    });

    it('can import a file encoded in UTF-8', function () {
        const ModUnicode = imports.modunicode;
        expect(ModUnicode.uval).toEqual('const \u2665 utf8');
    });

    describe("properties defined in the module's lexical scope", function () {
        let LexicalScope;

        beforeAll(function () {
            globalThis.expectMe = true;
            LexicalScope = imports.lexicalScope;
        });

        it('will log a compatibility warning when accessed', function () {
            const GLib = imports.gi.GLib;
            GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
                "Some code accessed the property 'b' on the module " +
                "'lexicalScope'.*");
            GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
                "Some code accessed the property 'c' on the module " +
                "'lexicalScope'.*");

            void LexicalScope.b;
            void LexicalScope.c;

            // g_test_assert_expected_messages() is a macro, not introspectable
            GLib.test_assert_expected_messages_internal('Gjs',
                'testImporter.js', 179, '');
        });

        it('can be accessed', function () {
            expect(LexicalScope.a).toEqual(1);
            expect(LexicalScope.b).toEqual(2);
            expect(LexicalScope.c).toEqual(3);
            expect(LexicalScope.d).toEqual(4);
        });

        it('does not leak module properties into the global scope', function () {
            expect(globalThis.d).not.toBeDefined();
        });
    });

    describe('enumerating modules', function () {
        let keys;
        beforeEach(function () {
            keys = [];
            for (let key in imports)
                keys.push(key);
        });

        it('gets all of them', function () {
            expect(keys).toContain('foobar', 'subA', 'mutualImport', 'modunicode');
        });

        it('includes modules that throw on import', function () {
            expect(keys).toContain('alwaysThrows');
        });

        it('does not include meta properties', function () {
            expect(keys).not.toContain('__parentModule__');
            expect(keys).not.toContain('__moduleName__');
            expect(keys).not.toContain('searchPath');
        });
    });

    it("doesn't crash when resolving a non-string property", function () {
        expect(imports[0]).not.toBeDefined();
        expect(imports.foobar[0]).not.toBeDefined();
    });

    it('scripts support relative dynamic imports', async function () {
        const {say} = await import('./modules/say.js');

        expect(typeof say).toBe('function');
        expect(say('hello')).toBe('<( hello )');
    });

    it('imported scripts support relative dynamic imports', async function () {
        const response = await imports.dynamic.test();

        expect(response).toBe('<( I did it! )');
    });
});