Skip to content

Commit 0e3f1a0

Browse files
authored
Merge pull request #15 from JPeer264/feature/fail-non-git
Fail on non git directories
2 parents 2c4daf9 + dcf46aa commit 0e3f1a0

File tree

7 files changed

+98
-8
lines changed

7 files changed

+98
-8
lines changed

appveyor.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Test against this version of Node.js
2+
environment:
3+
matrix:
4+
# node.js
5+
- nodejs_version: "4"
6+
- nodejs_version: "6"
7+
8+
# Install scripts. (runs after repo cloning)
9+
install:
10+
# Get the latest stable version of Node.js or io.js
11+
- ps: Install-Product node $env:nodejs_version
12+
# install modules
13+
- npm config set loglevel warn
14+
- npm i -g npm
15+
- npm i
16+
17+
# Post-install test scripts.
18+
test_script:
19+
# Output useful info for debugging.
20+
- node --version
21+
- npm --version
22+
# run tests
23+
- npm test
24+
25+
# Don't actually build.
26+
build: off

lib/cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import chalk from 'chalk';
33
import execa from 'execa';
44
import yargs from 'yargs';
55
import inquirer from 'inquirer';
6+
import isGit from 'is-git-repository';
67
import updateNotifier from 'update-notifier';
78

89
import pkg from '../package.json';
@@ -24,6 +25,8 @@ updateNotifier({ pkg }).notify();
2425

2526
if (argv.v) {
2627
console.log(`v${pkg.version}`);
28+
} else if (!isGit()) {
29+
console.error('fatal: Not a git repository (or any of the parent directories): .git');
2730
} else {
2831
inquirer.prompt(questionsList).then((answers) => {
2932
const message = answers.moreInfo ? `${answers.editor}` : `${answers.type} ${answers.description}`;

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"sgc": "dest/cli.js"
99
},
1010
"scripts": {
11-
"pretest": "npm run lint",
11+
"pretest": "npm run lint & npm run babel",
1212
"test": "nyc ava",
1313
"lint": "eslint lib test",
1414
"babel": "babel lib -d dest",
@@ -25,7 +25,8 @@
2525
},
2626
"nyc": {
2727
"exclude": [
28-
"test"
28+
"test",
29+
"dest"
2930
]
3031
},
3132
"repository": {
@@ -64,6 +65,7 @@
6465
"chalk": "^1.1.3",
6566
"execa": "^0.6.1",
6667
"inquirer": "^3.0.6",
68+
"is-git-repository": "^1.0.1",
6769
"json-extra": "^0.5.0",
6870
"object.entries": "^1.0.4",
6971
"update-notifier": "^2.1.0",

test/cli.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { homedir } from 'os';
2+
import test from 'ava';
3+
import path from 'path';
4+
import execa from 'execa';
5+
6+
import pkg from '../package.json';
7+
8+
const cli = 'dest/cli.js';
9+
10+
test('should print the right version', async (t) => {
11+
const { stdout } = await execa(cli, ['--version']);
12+
13+
t.is(stdout, `v${pkg.version}`);
14+
});
15+
16+
test('should fail on non git repository', async (t) => {
17+
// assume that the homedir is not a git repo
18+
process.chdir(homedir());
19+
20+
const { stderr } = await execa(path.join(__dirname, '..', cli));
21+
22+
process.chdir(path.join(__dirname, '..'));
23+
24+
t.is(stderr, 'fatal: Not a git repository (or any of the parent directories): .git');
25+
});
26+

test/getConfig.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import fs from 'fs-extra';
21
import os from 'os';
32
import test from 'ava';
43
import path from 'path';
4+
import fs from 'fs-extra';
55
import json from 'json-extra';
6+
67
import getConfig from '../lib/getConfig';
78

89
const cwd = process.cwd();
910
const homedir = os.homedir();
1011
const fixtures = path.join(cwd, 'test', 'fixtures');
11-
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
12+
const date = new Date();
13+
const datetime = date.toISOString().slice(0, 10);
14+
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 4);
1215

1316
let globalExist = false;
1417

@@ -17,13 +20,13 @@ let globalExist = false;
1720
test.before(() => {
1821
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
1922
globalExist = true;
20-
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}.back`));
23+
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`));
2124
}
2225
});
2326

2427
test.after(() => {
2528
if (globalExist) {
26-
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}.back`), path.join(homedir, '.sgcrc'));
29+
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`), path.join(homedir, '.sgcrc'));
2730
}
2831
});
2932

@@ -42,14 +45,14 @@ test('read config from package.json', (t) => {
4245
packageJson.sgc = sgcrc;
4346

4447
// manipulate local package
45-
fs.renameSync(path.join(cwd, 'package.json'), path.join(cwd, `package.json.${randomString}.back`));
48+
fs.renameSync(path.join(cwd, 'package.json'), path.join(cwd, `package.json.${randomString}-${datetime}.back`));
4649
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(packageJson));
4750

4851
t.deepEqual(getConfig(), sgcrc);
4952

5053
// revert local package
5154
fs.removeSync(path.join(cwd, 'package.json'));
52-
fs.renameSync(path.join(cwd, `package.json.${randomString}.back`), path.join(cwd, 'package.json'));
55+
fs.renameSync(path.join(cwd, `package.json.${randomString}-${datetime}.back`), path.join(cwd, 'package.json'));
5356
});
5457

5558
test('read global config', (t) => {

test/prompConfig.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
import os from 'os';
12
import test from 'ava';
23
import path from 'path';
34
import chalk from 'chalk';
5+
import fs from 'fs-extra';
46
import json from 'json-extra';
7+
58
import getConfig from '../lib/getConfig';
69
import { choices, questions } from '../lib/promptConfig';
710

811
const cwd = process.cwd();
12+
const homedir = os.homedir();
13+
const date = new Date();
14+
const datetime = date.toISOString().slice(0, 10);
15+
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 4);
16+
17+
let globalExist = false;
18+
19+
20+
// rename global .sgcrc
21+
test.before(() => {
22+
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
23+
globalExist = true;
24+
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`));
25+
}
26+
});
27+
28+
test.after(() => {
29+
if (globalExist) {
30+
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`), path.join(homedir, '.sgcrc'));
31+
}
32+
});
933

1034
test('get configuration file equals .sgcrc_default', (t) => {
1135
t.deepEqual(getConfig(), json.readToObjSync(path.join(cwd, '.sgcrc_default')));

yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,12 @@ is-generator-fn@^1.0.0:
22172217
version "1.0.0"
22182218
resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-1.0.0.tgz#969d49e1bb3329f6bb7f09089be26578b2ddd46a"
22192219

2220+
is-git-repository@^1.0.1:
2221+
version "1.0.1"
2222+
resolved "https://registry.yarnpkg.com/is-git-repository/-/is-git-repository-1.0.1.tgz#db80512b6ce8db3fa90c84292a7b525e427f5746"
2223+
dependencies:
2224+
execa "^0.6.1"
2225+
22202226
is-glob@^2.0.0, is-glob@^2.0.1:
22212227
version "2.0.1"
22222228
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863"

0 commit comments

Comments
 (0)