Skip to content

Commit 2914d69

Browse files
committed
feat: throw when path is not a repo
1 parent a2d0dfa commit 2914d69

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

lib/loader.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Fs = require('fs');
44
const GitUrlParse = require('git-url-parse');
55
const Path = require('path');
6+
const SimpleGit = require('simple-git/promise');
67
const Wreck = require('@hapi/wreck');
78

89

@@ -13,32 +14,42 @@ internals.createRepositoryLoader = (repository) => {
1314

1415
const parsedRepository = GitUrlParse(repository);
1516

16-
return async (filename) => {
17+
return {
18+
loadFile: async (filename) => {
1719

18-
if (parsedRepository.source !== 'github.com') {
19-
throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/node-support');
20-
}
20+
if (parsedRepository.source !== 'github.com') {
21+
throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/node-support');
22+
}
2123

22-
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
24+
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
2325

24-
const { payload } = await Wreck.get(url);
26+
const { payload } = await Wreck.get(url);
2527

26-
return payload;
28+
return payload;
29+
}
2730
};
2831
};
2932

3033

31-
internals.createPathLoader = (path) => {
34+
internals.createPathLoader = async (path) => {
3235

33-
return (filename) => {
36+
const isRepo = await SimpleGit(path).checkIsRepo();
3437

35-
const fullPath = Path.join(path, filename);
38+
if (!isRepo) {
39+
throw new Error(`${path} is not a git repository`);
40+
}
3641

37-
if (!Fs.existsSync(fullPath)) {
38-
return;
39-
}
42+
return {
43+
loadFile: (filename) => {
4044

41-
return Fs.readFileSync(fullPath);
45+
const fullPath = Path.join(path, filename);
46+
47+
if (!Fs.existsSync(fullPath)) {
48+
return;
49+
}
50+
51+
return Fs.readFileSync(fullPath);
52+
}
4253
};
4354
};
4455

lib/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Loader = require('./loader');
55

66
exports.detect = async ({ path, repository, packageName }) => {
77

8-
const loadFile = Loader.create({ path, repository, packageName });
8+
const { loadFile } = await Loader.create({ path, repository, packageName });
99

1010
const packageJson = JSON.parse((await loadFile('package.json')).toString());
1111

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dependencies": {
3232
"@hapi/wreck": "^16.0.1",
3333
"git-url-parse": "^11.1.2",
34-
"js-yaml": "^3.13.1"
34+
"js-yaml": "^3.13.1",
35+
"simple-git": "^1.131.0"
3536
}
3637
}

test/index.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Fs = require('fs');
44
const Nock = require('nock');
55
const Path = require('path');
66
const Sinon = require('sinon');
7+
const SimpleGit = require('simple-git/promise');
78
const Tmp = require('tmp');
89

910
const NodeSupport = require('..');
@@ -17,12 +18,16 @@ const internals = {
1718
tmpObjects: []
1819
};
1920

20-
internals.prepareFixture = ({ travisYml, packageJson } = {}) => {
21+
internals.prepareFixture = async ({ travisYml, packageJson, git = true } = {}) => {
2122

2223
const tmpObj = Tmp.dirSync({ unsafeCleanup: true });
2324

2425
internals.tmpObjects.push(tmpObj);
2526

27+
if (git) {
28+
await SimpleGit(tmpObj.name).init();
29+
}
30+
2631
if (travisYml) {
2732
Fs.copyFileSync(Path.join(__dirname, 'fixtures', travisYml), Path.join(tmpObj.name, '.travis.yml'));
2833
}
@@ -78,7 +83,7 @@ describe('node-support', () => {
7883

7984
it('leaves out `travis` when no `.travis.yml` present', async () => {
8085

81-
const path = internals.prepareFixture();
86+
const path = await internals.prepareFixture();
8287

8388
const result = await NodeSupport.detect({ path });
8489

@@ -91,7 +96,7 @@ describe('node-support', () => {
9196

9297
it('returns the single node version', async () => {
9398

94-
const path = internals.prepareFixture({
99+
const path = await internals.prepareFixture({
95100
travisYml: '_single-version.yml'
96101
});
97102

@@ -109,7 +114,7 @@ describe('node-support', () => {
109114

110115
it('returns default node version', async () => {
111116

112-
const path = internals.prepareFixture({
117+
const path = await internals.prepareFixture({
113118
travisYml: '_minimal.yml'
114119
});
115120

@@ -127,7 +132,7 @@ describe('node-support', () => {
127132

128133
it('returns empty array when no node detected', async () => {
129134

130-
const path = internals.prepareFixture({
135+
const path = await internals.prepareFixture({
131136
travisYml: '_no-node.yml'
132137
});
133138

@@ -145,7 +150,7 @@ describe('node-support', () => {
145150

146151
it('returns node versions from matrix env vars (NODEJS_VER)', async () => {
147152

148-
const path = internals.prepareFixture({
153+
const path = await internals.prepareFixture({
149154
travisYml: 'kangax-html-minifier.yml'
150155
});
151156

@@ -163,7 +168,7 @@ describe('node-support', () => {
163168

164169
it('returns node versions from matrix env vars (TRAVIS_NODE_VERSION)', async () => {
165170

166-
const path = internals.prepareFixture({
171+
const path = await internals.prepareFixture({
167172
travisYml: 'nodejs-nan.yml'
168173
});
169174

@@ -181,7 +186,7 @@ describe('node-support', () => {
181186

182187
it('returns node versions from matrix env vars (NODE_VER)', async () => {
183188

184-
const path = internals.prepareFixture({
189+
const path = await internals.prepareFixture({
185190
travisYml: 'reactivex-rxjs.yml'
186191
});
187192

@@ -199,7 +204,7 @@ describe('node-support', () => {
199204

200205
it('handles non-matching matrix env vars', async () => {
201206

202-
const path = internals.prepareFixture({
207+
const path = await internals.prepareFixture({
203208
travisYml: 'caolan-async.yml'
204209
});
205210

@@ -217,7 +222,7 @@ describe('node-support', () => {
217222

218223
it('returns node versions from matrix include', async () => {
219224

220-
const path = internals.prepareFixture({
225+
const path = await internals.prepareFixture({
221226
travisYml: 'nodejs-readable-stream.yml'
222227
});
223228

@@ -235,7 +240,7 @@ describe('node-support', () => {
235240

236241
it('handles single matrix include', async () => {
237242

238-
const path = internals.prepareFixture({
243+
const path = await internals.prepareFixture({
239244
travisYml: 'postcss-autoprefixer.yml'
240245
});
241246

@@ -253,7 +258,7 @@ describe('node-support', () => {
253258

254259
it('handles matrix includes without node versions', async () => {
255260

256-
const path = internals.prepareFixture({
261+
const path = await internals.prepareFixture({
257262
travisYml: 'shinn-is-resolvable.yml'
258263
});
259264

@@ -271,7 +276,7 @@ describe('node-support', () => {
271276

272277
it('handles missing env.matrix', async () => {
273278

274-
const path = internals.prepareFixture({
279+
const path = await internals.prepareFixture({
275280
travisYml: '_no-env-matrix.yml'
276281
});
277282

@@ -286,6 +291,14 @@ describe('node-support', () => {
286291
}
287292
});
288293
});
294+
295+
it('throws when path is not a git repo', async () => {
296+
297+
const path = await internals.prepareFixture({ git: false });
298+
299+
await expect(NodeSupport.detect({ path }))
300+
.to.reject(`${path} is not a git repository`);
301+
});
289302
});
290303

291304

0 commit comments

Comments
 (0)