Skip to content

Commit 8aeb258

Browse files
committed
feat: basic importing
1 parent 69b3f2d commit 8aeb258

File tree

8 files changed

+128
-4
lines changed

8 files changed

+128
-4
lines changed

lib/travis/index.js

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

2525

26-
internals.scan = async (travisYaml) => {
26+
internals.normalizeImports = (travisYaml) => {
27+
28+
return internals.toArray(travisYaml.import).map((entry) => {
29+
30+
if (typeof entry === 'string') {
31+
entry = { source: entry };
32+
}
33+
34+
return entry;
35+
});
36+
};
37+
38+
39+
internals.applyImports = async (yaml, { loadFile }) => {
40+
41+
if (!yaml.import) {
42+
return;
43+
}
44+
45+
const imports = internals.normalizeImports(yaml);
46+
47+
for (const entry of imports) {
48+
49+
const buffer = await loadFile(entry.source);
50+
51+
const imported = Yaml.safeLoad(buffer, {
52+
schema: Yaml.FAILSAFE_SCHEMA,
53+
json: true
54+
});
55+
56+
for (const key in imported) {
57+
yaml[key] = imported[key];
58+
}
59+
}
60+
};
61+
62+
63+
internals.scan = async (travisYaml, options) => {
64+
65+
await internals.applyImports(travisYaml, options);
2766

2867
const rawSet = new Set();
2968

@@ -35,7 +74,7 @@ internals.scan = async (travisYaml) => {
3574

3675
for (const env of internals.toArray(travisYaml.env.matrix)) {
3776

38-
const matches = env.match(/(?:NODEJS_VER|TRAVIS_NODE_VERSION|NODE_VER)="?(node\/)?(?<version>[\w./*]+)"?/); /* hack syntax highlighter 🤦‍♂️ */
77+
const matches = env.match(/(?:NODEJS_VER|TRAVIS_NODE_VERSION|NODE_VER)="?(node\/)?(?<version>[\w./*]+)"?/);
3978

4079
if (matches) {
4180
rawSet.add(matches.groups.version);
@@ -96,6 +135,6 @@ exports.detect = async ({ loadFile }) => {
96135
});
97136

98137
return {
99-
travis: await internals.scan(travisYaml)
138+
travis: await internals.scan(travisYaml, { loadFile })
100139
};
101140
};

test/fixtures/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = class TestContext {
7575
});
7676
}
7777

78-
async setupRepoFolder({ travisYml, packageJson, npmShrinkwrapJson, packageLockJson, git = true } = {}) {
78+
async setupRepoFolder({ travisYml, partials, packageJson, npmShrinkwrapJson, packageLockJson, git = true } = {}) {
7979

8080
const tmpObj = Tmp.dirSync({ unsafeCleanup: true });
8181

@@ -87,6 +87,13 @@ module.exports = class TestContext {
8787
Fs.copyFileSync(Path.join(__dirname, 'travis-ymls', travisYml), Path.join(this.path, '.travis.yml'));
8888
}
8989

90+
if (partials) {
91+
Fs.mkdirSync(Path.join(this.path, 'partials'));
92+
for (const fn of ['node-14.yml']) {
93+
Fs.copyFileSync(Path.join(__dirname, 'travis-ymls', 'testing-imports', 'partials', fn), Path.join(this.path, 'partials', fn));
94+
}
95+
}
96+
9097
if (packageJson !== false) {
9198
Fs.writeFileSync(Path.join(this.path, 'package.json'), JSON.stringify(packageJson || {
9299
name: 'test-module',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_js:
2+
- "14"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
import:
3+
- source: partials/node-14.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
import:
3+
- partials/node-14.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
import:
3+
source: partials/node-14.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: node_js
2+
import: partials/node-14.yml

test/travis.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
const NodeSupport = require('..');
4+
5+
const TestContext = require('./fixtures');
6+
7+
8+
const { describe, it, beforeEach, afterEach } = exports.lab = require('@hapi/lab').script();
9+
const { expect } = require('@hapi/code');
10+
11+
12+
const internals = {};
13+
14+
15+
internals.assertCommit = (result) => {
16+
17+
expect(result.commit).to.match(/^[0-9a-f]{40}$/);
18+
delete result.commit;
19+
};
20+
21+
22+
describe('.travis.yml parsing', () => {
23+
24+
let fixture;
25+
26+
beforeEach(() => {
27+
28+
fixture = new TestContext();
29+
});
30+
31+
afterEach(() => {
32+
33+
fixture.cleanup();
34+
});
35+
36+
describe('simple', () => {
37+
38+
for (const yml of ['simple-single-object', 'simple-single-string', 'simple-array-object', 'simple-array-string']) {
39+
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+
});
47+
48+
const result = await NodeSupport.detect({ path: fixture.path });
49+
50+
internals.assertCommit(result);
51+
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+
});
61+
});
62+
}
63+
});
64+
65+
});

0 commit comments

Comments
 (0)