Skip to content

Commit 30f4659

Browse files
author
ksa
committed
Testing the cli functionality
1 parent 9e56c7d commit 30f4659

File tree

6 files changed

+174
-64
lines changed

6 files changed

+174
-64
lines changed

bin/jest-codemods.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
require('../dist/cli');
2+
require('../dist/cli/index');

src/cli/git-status.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import isGitClean from 'is-git-clean';
2+
3+
export default function checkGitStatus(force) {
4+
let clean = false;
5+
let errorMessage = 'Unable to determine if git directory is clean';
6+
try {
7+
clean = isGitClean.sync(process.cwd());
8+
errorMessage = 'Git directory is not clean';
9+
} catch (err) {
10+
if (err && err.stderr && err.stderr.indexOf('Not a git repository') >= 0) {
11+
clean = true;
12+
}
13+
}
14+
15+
if (!clean) {
16+
if (force) {
17+
console.log(`WARNING: ${errorMessage}. Forcibly continuing.`);
18+
} else {
19+
console.log(
20+
`ERROR: ${errorMessage}. Refusing to continue.`,
21+
'Ensure you have a backup of your tests or commit the latest changes before continuing.',
22+
'You may use the --force flag to override this safety check.'
23+
);
24+
process.exit(1);
25+
}
26+
}
27+
}

src/cli/git-status.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-env jest */
2+
3+
let gitStatusReturnValue;
4+
jest.setMock('is-git-clean', {
5+
sync: () => {
6+
if (typeof gitStatusReturnValue === 'boolean') {
7+
return gitStatusReturnValue;
8+
}
9+
throw gitStatusReturnValue;
10+
},
11+
});
12+
13+
const checkGitStatus = require('./git-status').default;
14+
15+
it('does not exit and output any logs when git repo is clean', () => {
16+
gitStatusReturnValue = true;
17+
console.log = jest.fn();
18+
process.exit = jest.fn();
19+
checkGitStatus();
20+
expect(console.log).not.toBeCalled();
21+
expect(process.exit).not.toBeCalled();
22+
});
23+
24+
it('does not exit and output any logs when not a git repo', () => {
25+
const err = new Error();
26+
err.stderr = 'Not a git repository';
27+
gitStatusReturnValue = err;
28+
console.log = jest.fn();
29+
process.exit = jest.fn();
30+
checkGitStatus();
31+
expect(console.log).not.toBeCalled();
32+
expect(process.exit).not.toBeCalled();
33+
});
34+
35+
it('exits and output logs when git repo is dirty', () => {
36+
gitStatusReturnValue = false;
37+
console.log = jest.fn();
38+
process.exit = jest.fn();
39+
checkGitStatus();
40+
expect(console.log).toBeCalled();
41+
expect(process.exit).toBeCalled();
42+
});
43+
44+
it('exits and output logs when git detection fail', () => {
45+
gitStatusReturnValue = new Error('bum');
46+
console.log = jest.fn();
47+
process.exit = jest.fn();
48+
checkGitStatus();
49+
expect(console.log).toBeCalled();
50+
expect(process.exit).toBeCalled();
51+
});
52+
53+
it('does not exit when git repo is dirty and force flag is given', () => {
54+
gitStatusReturnValue = false;
55+
console.log = jest.fn();
56+
process.exit = jest.fn();
57+
checkGitStatus(true);
58+
expect(console.log).toBeCalledWith(
59+
'WARNING: Git directory is not clean. Forcibly continuing.'
60+
);
61+
expect(process.exit).not.toBeCalled();
62+
});

src/cli.js renamed to src/cli/index.js

+3-63
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,11 @@
11
#!/usr/bin/env node
2-
import path from 'path';
3-
4-
import execa from 'execa';
52
import globby from 'globby';
63
import inquirer from 'inquirer';
7-
import isGitClean from 'is-git-clean';
84
import meow from 'meow';
95
import updateNotifier from 'update-notifier';
106

11-
function checkGitStatus(force) {
12-
let clean = false;
13-
let errorMessage = 'Unable to determine if git directory is clean';
14-
try {
15-
clean = isGitClean.sync(process.cwd());
16-
errorMessage = 'Git directory is not clean';
17-
} catch (err) {
18-
if (err && err.stderr && err.stderr.indexOf('Not a git repository') >= 0) {
19-
clean = true;
20-
}
21-
}
22-
23-
const ENSURE_BACKUP_MESSAGE = 'Ensure you have a backup of your tests or commit the latest changes before continuing.';
24-
25-
if (!clean) {
26-
if (force) {
27-
console.log(`WARNING: ${errorMessage}. Forcibly continuing.`, ENSURE_BACKUP_MESSAGE);
28-
} else {
29-
console.log(
30-
`ERROR: ${errorMessage}. Refusing to continue.`,
31-
ENSURE_BACKUP_MESSAGE,
32-
'You may use the --force flag to override this safety check.'
33-
);
34-
process.exit(1);
35-
}
36-
}
37-
}
38-
39-
function executeTransformation(files, flags, transformer) {
40-
const transformerPath = path.join(__dirname, 'transformers', `${transformer}.js`);
41-
42-
const args = ['-t', transformerPath].concat(files);
43-
if (flags.dry) {
44-
args.push('--dry');
45-
}
46-
if (['babel', 'babylon', 'flow'].indexOf(flags.parser) >= 0) {
47-
args.push('--parser', flags.parser);
48-
}
49-
50-
console.log(`Executing command: jscodeshift ${args.join(' ')}`);
51-
52-
const result = execa.sync(require.resolve('.bin/jscodeshift'), args, {
53-
stdio: 'inherit',
54-
stripEof: false,
55-
});
56-
57-
if (result.error) {
58-
throw result.error;
59-
}
60-
}
61-
62-
function executeTransformations(files, flags, transformers) {
63-
transformers.forEach(t => {
64-
executeTransformation(files, flags, t);
65-
});
66-
}
7+
import checkGitStatus from './git-status';
8+
import { executeTransformations } from './transformers';
679

6810
const cli = meow(
6911
{
@@ -150,8 +92,6 @@ if (cli.input.length) {
15092
}
15193

15294
const transformers = transformer === 'all' ? ['tape', 'ava'] : [transformer];
153-
transformers.forEach(t => {
154-
executeTransformation(filesExpanded, cli.flags, t);
155-
});
95+
executeTransformations(filesExpanded, cli.flags, transformers);
15696
});
15797
}

src/cli/transformers.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import path from 'path';
2+
import execa from 'execa';
3+
4+
export const transformerDirectory = path.join(__dirname, '../', 'transformers');
5+
export const jscodeshiftExecutable = require.resolve('.bin/jscodeshift');
6+
7+
function executeTransformation(files, flags, transformer) {
8+
const transformerPath = path.join(transformerDirectory, `${transformer}.js`);
9+
10+
const args = ['-t', transformerPath].concat(files);
11+
if (flags.dry) {
12+
args.push('--dry');
13+
}
14+
if (['babel', 'babylon', 'flow'].indexOf(flags.parser) >= 0) {
15+
args.push('--parser', flags.parser);
16+
}
17+
18+
console.log(`Executing command: jscodeshift ${args.join(' ')}`);
19+
20+
const result = execa.sync(jscodeshiftExecutable, args, {
21+
stdio: 'inherit',
22+
stripEof: false,
23+
});
24+
25+
if (result.error) {
26+
throw result.error;
27+
}
28+
}
29+
30+
export function executeTransformations(files, flags, transformers) {
31+
transformers.forEach(t => {
32+
executeTransformation(files, flags, t);
33+
});
34+
}

src/cli/transformers.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-env jest */
2+
3+
let execaReturnValue;
4+
jest.setMock('execa', {
5+
sync: () => execaReturnValue,
6+
});
7+
8+
const fs = require('fs');
9+
const path = require('path');
10+
const {
11+
executeTransformations, jscodeshiftExecutable, transformerDirectory,
12+
} = require('./transformers');
13+
14+
it('finds transformer directory', () => {
15+
fs.lstatSync(transformerDirectory);
16+
});
17+
18+
it('finds jscodeshift executable', () => {
19+
fs.lstatSync(jscodeshiftExecutable);
20+
});
21+
22+
it('runs jscodeshift for the given transformer', () => {
23+
execaReturnValue = { error: null };
24+
console.log = jest.fn();
25+
executeTransformations('src', {}, ['tape']);
26+
expect(console.log).toBeCalledWith(
27+
`Executing command: jscodeshift -t ${path.join(transformerDirectory, 'tape.js')} src`
28+
);
29+
});
30+
31+
it('supports jscodeshift flags', () => {
32+
execaReturnValue = { error: null };
33+
console.log = jest.fn();
34+
executeTransformations('folder', { dry: true, parser: 'flow' }, ['ava']);
35+
expect(console.log).toBeCalledWith(
36+
`Executing command: jscodeshift -t ${path.join(transformerDirectory, 'ava.js')} folder --dry --parser flow`
37+
);
38+
});
39+
40+
it('rethrows jscodeshift errors', () => {
41+
const transformerError = new Error('bum');
42+
execaReturnValue = { error: transformerError };
43+
console.log = jest.fn();
44+
expect(() => {
45+
executeTransformations('src', {}, ['tape']);
46+
}).toThrowError(transformerError);
47+
});

0 commit comments

Comments
 (0)