Skip to content

Commit 7cc34de

Browse files
committed
feat: handle edge cases
1 parent e9bb27f commit 7cc34de

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

lib/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internals.detectFromTravisYaml = (travisYaml) => {
1717
versions.forEach((v) => raw.add(v));
1818
}
1919

20-
if (travisYaml.env) {
20+
if (travisYaml.env && travisYaml.env.matrix) {
2121

2222
for (const env of travisYaml.env.matrix) {
2323

@@ -27,6 +27,10 @@ internals.detectFromTravisYaml = (travisYaml) => {
2727
}
2828
}
2929

30+
if (!raw.size) {
31+
raw.add('latest');
32+
}
33+
3034
return { raw: [...raw] };
3135
};
3236

@@ -37,11 +41,14 @@ module.exports.detect = ({ path }) => {
3741

3842
const { name, version } = packageJson;
3943

40-
const travisYaml = Yaml.safeLoad(Fs.readFileSync(Path.join(path, '.travis.yml')), { schema: Yaml.FAILSAFE_SCHEMA });
44+
const result = { name, version };
45+
46+
const travisYamlPath = Path.join(path, '.travis.yml');
47+
48+
if (Fs.existsSync(travisYamlPath)) {
49+
const travisYaml = Yaml.safeLoad(Fs.readFileSync(travisYamlPath), { schema: Yaml.FAILSAFE_SCHEMA });
50+
result.travis = internals.detectFromTravisYaml(travisYaml);
51+
}
4152

42-
return {
43-
name,
44-
version,
45-
travis: internals.detectFromTravisYaml(travisYaml)
46-
};
53+
return result;
4754
};

test/fixtures/_minimal.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: node_js

test/fixtures/_no-env-matrix.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
env:
3+
- NO_MATRIX=true
File renamed without changes.

test/index.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ internals.prepareFixture = (travisYml) => {
2121

2222
internals.tmpObjects.push(tmpObj);
2323

24-
Fs.copyFileSync(Path.join(__dirname, 'fixtures', travisYml), Path.join(tmpObj.name, '.travis.yml'));
24+
if (travisYml) {
25+
Fs.copyFileSync(Path.join(__dirname, 'fixtures', travisYml), Path.join(tmpObj.name, '.travis.yml'));
26+
}
2527

2628
Fs.writeFileSync(Path.join(tmpObj.name, 'package.json'), JSON.stringify({
2729
name: 'test-module',
@@ -48,7 +50,7 @@ describe('node-support', () => {
4850

4951
describe('path', () => {
5052

51-
it('returns node versions from .travis.yml at the path', async () => {
53+
it('returns node versions from `.travis.yml` at the path', async () => {
5254

5355
const path = Path.join(__dirname, '..');
5456

@@ -63,9 +65,21 @@ describe('node-support', () => {
6365
});
6466
});
6567

68+
it('leaves out `travis` when no `.travis.yml` present', async () => {
69+
70+
const path = internals.prepareFixture();
71+
72+
const result = await NodeSupport.detect({ path });
73+
74+
expect(result).to.equal({
75+
name: 'test-module',
76+
version: '0.0.0-development'
77+
});
78+
});
79+
6680
it('returns the single node version', async () => {
6781

68-
const path = internals.prepareFixture('single-version.yml');
82+
const path = internals.prepareFixture('_single-version.yml');
6983

7084
const result = await NodeSupport.detect({ path });
7185

@@ -78,6 +92,22 @@ describe('node-support', () => {
7892
});
7993
});
8094

95+
96+
it('returns default node version', async () => {
97+
98+
const path = internals.prepareFixture('_minimal.yml');
99+
100+
const result = await NodeSupport.detect({ path });
101+
102+
expect(result).to.equal({
103+
name: 'test-module',
104+
version: '0.0.0-development',
105+
travis: {
106+
raw: ['latest']
107+
}
108+
});
109+
});
110+
81111
it('returns node versions from matrix env vars (NODEJS_VER)', async () => {
82112

83113
const path = internals.prepareFixture('kangax-html-minifier.yml');
@@ -122,6 +152,21 @@ describe('node-support', () => {
122152
}
123153
});
124154
});
155+
156+
it('handles missing env.matrix', async () => {
157+
158+
const path = internals.prepareFixture('_no-env-matrix.yml');
159+
160+
const result = await NodeSupport.detect({ path });
161+
162+
expect(result).to.equal({
163+
name: 'test-module',
164+
version: '0.0.0-development',
165+
travis: {
166+
raw: ['latest']
167+
}
168+
});
169+
});
125170
});
126171
});
127172
});

0 commit comments

Comments
 (0)