Skip to content

Commit d069c32

Browse files
author
Sergio
committed
feat: introduced lingui-codemod CLI!
1 parent cefe6aa commit d069c32

7 files changed

+1670
-31
lines changed

bin/__tests__/lingui-codemod.test.ts

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
let gitStatusReturnValue;
2+
let execaReturnValue;
3+
4+
jest.setMock('execa', {
5+
sync: () => execaReturnValue
6+
});
7+
8+
jest.setMock('is-git-clean', {
9+
sync: () => {
10+
if (typeof gitStatusReturnValue === 'boolean') {
11+
return gitStatusReturnValue;
12+
}
13+
throw gitStatusReturnValue;
14+
}
15+
});
16+
17+
import fs from "fs";
18+
import path from "path";
19+
import {
20+
runTransform,
21+
jscodeshiftExecutable,
22+
transformerDirectory,
23+
checkGitStatus
24+
} from '../cli';
25+
26+
describe('check-git-status', () => {
27+
it('does not exit and output any logs when git repo is clean', () => {
28+
gitStatusReturnValue = true;
29+
console.log = jest.fn();
30+
// @ts-ignore
31+
process.exit = jest.fn();
32+
checkGitStatus();
33+
expect(console.log).not.toBeCalled();
34+
expect(process.exit).not.toBeCalled();
35+
});
36+
37+
it('does not exit and output any logs when not a git repo', () => {
38+
const err = new Error();
39+
// @ts-ignore
40+
err.stderr = 'Not a git repository';
41+
gitStatusReturnValue = err;
42+
console.log = jest.fn();
43+
// @ts-ignore
44+
process.exit = jest.fn();
45+
checkGitStatus();
46+
expect(console.log).not.toBeCalled();
47+
expect(process.exit).not.toBeCalled();
48+
});
49+
50+
it('exits and output logs when git repo is dirty', () => {
51+
gitStatusReturnValue = false;
52+
console.log = jest.fn();
53+
// @ts-ignore
54+
process.exit = jest.fn();
55+
checkGitStatus();
56+
expect(console.log).toBeCalled();
57+
expect(process.exit).toBeCalled();
58+
});
59+
60+
it('exits and output logs when git detection fail', () => {
61+
gitStatusReturnValue = new Error('bum');
62+
console.log = jest.fn();
63+
// @ts-ignore
64+
process.exit = jest.fn();
65+
checkGitStatus();
66+
expect(console.log).toBeCalled();
67+
expect(process.exit).toBeCalled();
68+
});
69+
70+
it('does not exit when git repo is dirty and force flag is given', () => {
71+
gitStatusReturnValue = false;
72+
console.log = jest.fn();
73+
// @ts-ignore
74+
process.exit = jest.fn();
75+
checkGitStatus(true);
76+
expect(console.log).toBeCalledWith(
77+
'WARNING: Git directory is not clean. Forcibly continuing.'
78+
);
79+
expect(process.exit).not.toBeCalled();
80+
});
81+
});
82+
83+
describe('runTransform', () => {
84+
it('finds transformer directory', () => {
85+
fs.lstatSync(transformerDirectory);
86+
});
87+
88+
it('finds jscodeshift executable', () => {
89+
fs.lstatSync(jscodeshiftExecutable);
90+
});
91+
92+
it('runs jscodeshift for the given transformer', () => {
93+
execaReturnValue = { error: null };
94+
console.log = jest.fn();
95+
runTransform({
96+
files: 'src',
97+
flags: {},
98+
parser: 'flow',
99+
transformer: 'rename-unsafe-xyz'
100+
});
101+
expect(console.log).toBeCalledWith(
102+
// eslint-disable-next-line max-len
103+
`Executing command: jscodeshift --verbose=2 --ignore-pattern=**/node_modules/** --parser flow --extensions=jsx,js --transform ${path.join(
104+
transformerDirectory,
105+
'rename-unsafe-xyz.js'
106+
)} src`
107+
);
108+
});
109+
110+
it('supports jscodeshift flags', () => {
111+
execaReturnValue = { error: null };
112+
console.log = jest.fn();
113+
runTransform({
114+
files: 'folder',
115+
flags: { dry: true },
116+
parser: 'flow',
117+
transformer: 'v2-to-v3'
118+
});
119+
expect(console.log).toBeCalledWith(
120+
// eslint-disable-next-line max-len
121+
`Executing command: jscodeshift --dry --verbose=2 --ignore-pattern=**/node_modules/** --parser flow --extensions=jsx,js --transform ${path.join(
122+
transformerDirectory,
123+
'v2-to-v3.js'
124+
)} folder`
125+
);
126+
});
127+
128+
it('supports typescript parser', () => {
129+
execaReturnValue = { error: null };
130+
console.log = jest.fn();
131+
runTransform({
132+
files: 'folder',
133+
flags: { dry: true },
134+
parser: 'tsx',
135+
transformer: 'rename-unsafe-lifecycles'
136+
});
137+
expect(console.log).toBeCalledWith(
138+
// eslint-disable-next-line max-len
139+
`Executing command: jscodeshift --dry --verbose=2 --ignore-pattern=**/node_modules/** --parser tsx --extensions=tsx,ts,jsx,js --transform ${path.join(
140+
transformerDirectory,
141+
'rename-unsafe-lifecycles.js'
142+
)} folder`
143+
);
144+
});
145+
146+
it('supports jscodeshift custom arguments', () => {
147+
execaReturnValue = { error: null };
148+
console.log = jest.fn();
149+
runTransform({
150+
files: 'folder',
151+
flags: {
152+
dry: true,
153+
jscodeshift: 'verbose=2 --printOptions=\'{"quote":"double"}\''
154+
},
155+
parser: 'babel',
156+
transformer: 'v2-to-v3'
157+
});
158+
expect(console.log).toBeCalledWith(
159+
// eslint-disable-next-line max-len
160+
`Executing command: jscodeshift --dry --verbose=2 --ignore-pattern=**/node_modules/** --parser babel --extensions=jsx,js --transform ${path.join(
161+
transformerDirectory,
162+
'v2-to-v3.js'
163+
)} verbose=2 --printOptions='{"quote":"double"}' folder`
164+
);
165+
});
166+
167+
it('rethrows jscodeshift errors', () => {
168+
const transformerError = new Error('bum');
169+
execaReturnValue = { error: transformerError };
170+
console.log = jest.fn();
171+
expect(() => {
172+
runTransform({
173+
files: 'src',
174+
flags: {},
175+
parser: 'flow',
176+
transformers: ['tape']
177+
});
178+
}).toThrowError(transformerError);
179+
});
180+
});

0 commit comments

Comments
 (0)