Skip to content

Commit 3924e87

Browse files
committed
tests pass, but more should be added
1 parent 360b1f4 commit 3924e87

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

ember-classic-import-meta-glob/addon/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,20 @@ export function importMetaGlob(glob, options, modulePath) {
5959
let [, ...reversedParts] = modulePath.split('/').reverse();
6060
let currentDir = reversedParts.reverse().join('/');
6161

62-
console.log({ glob, currentDir, modulePath });
6362
assert(
6463
`not a valid path. Received: '${currentDir}' from '${modulePath}'`,
6564
currentDir.length > 0,
6665
);
6766

6867
// TODO: drop the extensions, since at runtime, we don't have them.
6968
let globsArray = Array.isArray(glob) ? glob : [glob];
69+
70+
[...globsArray].forEach((g) => {
71+
let extensionless = g.replace(/\.\w{2,3}$/, '');
72+
73+
globsArray.push(extensionless);
74+
});
75+
7076
let fullGlobs = globsArray.map((g) => {
7177
return `${currentDir}/${g.replace(/^.\//, '')}`;
7278
});

test-app/tests/glob-test/the-test.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { globs as appTreeGlob } from 'test-app/glob-imports';
44

55
const testTreeGlob = import.meta.glob('./from-tests/**', { eager: true });
66

7+
const HERE = 'test-app/tests/glob-test/the-test';
8+
79
module('Glob tests', function () {
810
test('works from app', function (assert) {
911
let keys = Object.keys(appTreeGlob);
@@ -49,57 +51,56 @@ module('Glob tests', function () {
4951

5052
module('underlying runtime', function () {
5153
test('extensions are stripped and the same as without using extensions', (assert) => {
52-
let a = importMetaGlob('./from-tests/**/*.js', { eager: true });
53-
let b = importMetaGlob('./from-tests/**/*', { eager: true });
54+
let a = importMetaGlob('./from-tests/**/*.js', { eager: true }, HERE);
55+
let b = importMetaGlob('./from-tests/**/*', { eager: true }, HERE);
5456

55-
assert.deepEqual(a, b);
57+
assert.deepEqual(Object.keys(a), Object.keys(b));
58+
assert.deepEqual(Object.values(a)[0].a, Object.values(b)[0].a);
59+
assert.deepEqual(Object.values(a)[1].b, Object.values(b)[1].b);
60+
assert.deepEqual(Object.values(a)[2].c, Object.values(b)[2].c);
5661
});
5762

5863
module('errors', function () {
5964
module('glob', function () {
6065
test('errors when trying to escape the app', function (assert) {
6166
assert.throws(() => {
62-
importMetaGlob(
63-
'../../../something',
64-
{ eager: true },
65-
'test-app/tests/glob-test/the-test',
66-
);
67-
}, /not a valid path/);
67+
importMetaGlob('../../../something', { eager: true }, HERE);
68+
}, /Cannot have a path that escapes the app/);
6869
});
6970

7071
test('errors when a sibling path tries to escape the app', function (assert) {
7172
assert.throws(() => {
7273
importMetaGlob(
7374
'./from-tests/../../../../something',
7475
{ eager: true },
75-
'test-app/tests/glob-test/the-test',
76+
HERE,
7677
);
7778
}, /Cannot have a path that escapes the app/);
7879
});
7980

8081
test('invalid glob', function (assert) {
8182
assert.throws(() => {
82-
importMetaGlob('**/*');
83+
importMetaGlob('**/*', HERE);
8384
}, /The glob pattern must be a relative path starting with either/);
8485
});
8586
});
8687

8788
module('options', function () {
8889
test('errors with incorrect options', function (assert) {
8990
assert.throws(() => {
90-
importMetaGlob('./**/*', true);
91+
importMetaGlob('./**/*', true, HERE);
9192
}, /the second argument to import.meta.glob must be an object/);
9293
});
9394

9495
test('when passing options, cannot be empty', function (assert) {
9596
assert.throws(() => {
96-
importMetaGlob('./**/*', {});
97+
importMetaGlob('./**/*', {}, HERE);
9798
}, /the only supported option is 'eager'/);
9899
});
99100

100101
test('when passing options, only eager is allowed', function (assert) {
101102
assert.throws(() => {
102-
importMetaGlob('./**/*', { boop: 1 });
103+
importMetaGlob('./**/*', { boop: 1 }, HERE);
103104
}, /the only supported option is 'eager'/);
104105
});
105106
});
@@ -110,6 +111,12 @@ module('Glob tests', function () {
110111
importMetaGlob('./**/*', { eager: true });
111112
}, /the third argument to import.meta.glob must be passed and be the module path/);
112113
});
114+
115+
test(`errors with wrong path (can't be the entrypoint)`, function (assert) {
116+
assert.throws(() => {
117+
importMetaGlob('../../../something', { eager: true }, 'test-app');
118+
}, /not a valid path/);
119+
});
113120
});
114121
});
115122
});

0 commit comments

Comments
 (0)