Skip to content

Commit dd032e8

Browse files
committed
feat: support indirect import
1 parent bd432dc commit dd032e8

File tree

5 files changed

+67
-25
lines changed

5 files changed

+67
-25
lines changed

lib/travis/index.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,33 @@ internals.toArray = (v) => {
2323
};
2424

2525

26-
internals.normalizeImports = (travisYaml) => {
26+
internals.normalizeImports = (travisYaml, { relativeTo }) => {
2727

2828
return internals.toArray(travisYaml.import).map((entry) => {
2929

3030
if (typeof entry === 'string') {
3131
entry = { source: entry };
3232
}
3333

34+
if (entry.source.startsWith('./')) {
35+
const relativeParts = relativeTo.source.split('/');
36+
relativeParts.pop();
37+
relativeParts.push(entry.source.substring(2));
38+
entry.source = relativeParts.join('/');
39+
}
40+
3441
return entry;
3542
});
3643
};
3744

3845

39-
internals.applyImports = async (yaml, { loadFile }) => {
46+
internals.applyImports = async (yaml, { loadFile, relativeTo }) => {
4047

4148
if (!yaml.import) {
4249
return;
4350
}
4451

45-
const imports = internals.normalizeImports(yaml);
52+
const imports = internals.normalizeImports(yaml, { relativeTo });
4653

4754
for (const entry of imports) {
4855

@@ -53,7 +60,14 @@ internals.applyImports = async (yaml, { loadFile }) => {
5360
json: true
5461
});
5562

63+
await internals.applyImports(imported, { loadFile, relativeTo: entry });
64+
5665
for (const key in imported) {
66+
67+
if (key === 'import') {
68+
continue;
69+
}
70+
5771
yaml[key] = imported[key];
5872
}
5973
}

test/fixtures/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ module.exports = class TestContext {
8989

9090
if (partials) {
9191
Fs.mkdirSync(Path.join(this.path, 'partials'));
92-
for (const fn of ['node-14.yml']) {
92+
const partialYmls = [
93+
'indirect-node-14.yml',
94+
'node-14.yml'
95+
];
96+
for (const fn of partialYmls) {
9397
Fs.copyFileSync(Path.join(__dirname, 'travis-ymls', 'testing-imports', 'partials', fn), Path.join(this.path, 'partials', fn));
9498
}
9599
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
import:
3+
- source: partials/indirect-node-14.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import:
2+
- source: ./node-14.yml

test/travis.js

+40-21
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,52 @@ describe('.travis.yml parsing', () => {
3333
fixture.cleanup();
3434
});
3535

36-
describe('simple', () => {
36+
for (const yml of ['simple-single-object', 'simple-single-string', 'simple-array-object', 'simple-array-string']) {
3737

38-
for (const yml of ['simple-single-object', 'simple-single-string', 'simple-array-object', 'simple-array-string']) {
38+
// eslint-disable-next-line no-loop-func
39+
it(`resolves simple local import (${yml})`, async () => {
3940

40-
// eslint-disable-next-line no-loop-func
41-
it(`resolves simple local import (${yml})`, async () => {
42-
43-
await fixture.setupRepoFolder({
44-
partials: true,
45-
travisYml: `testing-imports/${yml}.yml`
46-
});
41+
await fixture.setupRepoFolder({
42+
partials: true,
43+
travisYml: `testing-imports/${yml}.yml`
44+
});
4745

48-
const result = await NodeSupport.detect({ path: fixture.path });
46+
const result = await NodeSupport.detect({ path: fixture.path });
4947

50-
internals.assertCommit(result);
48+
internals.assertCommit(result);
5149

52-
expect(result).to.equal({
53-
name: 'test-module',
54-
version: '0.0.0-development',
55-
timestamp: 1580673602000,
56-
travis: {
57-
raw: ['14'],
58-
resolved: { '14': '14.3.0' }
59-
}
60-
});
50+
expect(result).to.equal({
51+
name: 'test-module',
52+
version: '0.0.0-development',
53+
timestamp: 1580673602000,
54+
travis: {
55+
raw: ['14'],
56+
resolved: { '14': '14.3.0' }
57+
}
6158
});
62-
}
59+
});
60+
}
61+
62+
it('resolves indirect imports', async () => {
63+
64+
await fixture.setupRepoFolder({
65+
partials: true,
66+
travisYml: `testing-imports/indirect.yml`
67+
});
68+
69+
const result = await NodeSupport.detect({ path: fixture.path });
70+
71+
internals.assertCommit(result);
72+
73+
expect(result).to.equal({
74+
name: 'test-module',
75+
version: '0.0.0-development',
76+
timestamp: 1580673602000,
77+
travis: {
78+
raw: ['14'],
79+
resolved: { '14': '14.3.0' }
80+
}
81+
});
6382
});
6483

6584
});

0 commit comments

Comments
 (0)