-
Notifications
You must be signed in to change notification settings - Fork 542
/
initUtils.test.ts
217 lines (177 loc) · 6.2 KB
/
initUtils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { promises as fs } from 'fs';
import childProcess from 'child_process';
import pathUtils from 'path';
import { resetFileSystem } from '../../test-utils';
import {
cloneTemplate,
gitInit,
isGitInstalled,
isInGitRepository,
prepareWorkingDirectory,
yarnInstall,
TEMPLATE_GIT_URL,
} from './initUtils';
jest.mock('fs');
jest.mock('process', () => ({
...jest.requireActual('process'),
cwd: jest.fn(),
}));
jest.mock('child_process', () => ({
execSync: jest.fn(),
}));
describe('initUtils', () => {
beforeEach(async () => {
await resetFileSystem();
});
describe('prepareWorkingDirectory', () => {
it('creates a new directory if needed', async () => {
// eslint-disable-next-line jest/no-restricted-matchers
await expect(prepareWorkingDirectory('foo')).resolves.toBeUndefined();
});
it("does not create a directory if it's using the current working directory", async () => {
await prepareWorkingDirectory(process.cwd());
expect(await fs.readdir(process.cwd())).toStrictEqual([]);
});
it('throws an error if it fails to create a new directory', async () => {
jest.spyOn(fs, 'mkdir').mockImplementation(() => {
throw new Error('error message');
});
await expect(prepareWorkingDirectory('bar')).rejects.toThrow(
'Init Error: Failed to prepare working directory with message: Init Error: Failed to create new directory.',
);
});
it('throws if the folder is not empty', async () => {
const folderPath = 'bar';
const filePath = pathUtils.join(folderPath, 'foo.txt');
await fs.mkdir(folderPath);
await fs.appendFile(filePath, 'test');
await expect(prepareWorkingDirectory('bar')).rejects.toThrow(
'Init Error: Failed to prepare working directory with message: Directory bar not empty.',
);
});
});
describe('cloneTemplate', () => {
it('passes if the command is ran successfully', async () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => Buffer.from([]));
await cloneTemplate('foo');
expect(execSyncMock).toHaveBeenCalledTimes(1);
expect(execSyncMock).toHaveBeenCalledWith(
`git clone --depth=1 ${TEMPLATE_GIT_URL} foo`,
{
stdio: [2],
},
);
});
it('throws if the command fails', async () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => {
throw new Error('error message');
});
await expect(cloneTemplate('foo')).rejects.toThrow(
'Init Error: Failed to clone the template.',
);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
describe('isGitInstalled', () => {
it('returns true if git is installed', () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => Buffer.from([]));
const result = isGitInstalled();
expect(result).toStrictEqual(true);
expect(execSyncMock).toHaveBeenCalledTimes(1);
expect(execSyncMock).toHaveBeenCalledWith('git --version', {
stdio: 'ignore',
});
});
it('returns false if git is not installed', () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => {
throw new Error('error message');
});
const result = isGitInstalled();
expect(result).toStrictEqual(false);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
describe('isInGitRepository', () => {
it('returns true if the directory is a git repository', () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => Buffer.from([]));
const result = isInGitRepository('foo');
expect(result).toStrictEqual(true);
expect(execSyncMock).toHaveBeenCalledTimes(1);
expect(execSyncMock).toHaveBeenCalledWith(
'git rev-parse --is-inside-work-tree',
{
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, 'foo'),
},
);
});
it('returns false if the directory is not a git repository', () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => {
throw new Error('error message');
});
const result = isInGitRepository('foo');
expect(result).toStrictEqual(false);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
describe('gitInit', () => {
it('init a new repository', async () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => Buffer.from([]));
await gitInit('foo');
expect(execSyncMock).toHaveBeenCalledTimes(1);
expect(execSyncMock).toHaveBeenCalledWith('git init', {
stdio: 'ignore',
cwd: pathUtils.resolve(__dirname, 'foo'),
});
});
it('throws an error if it fails to init a new repository', async () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => {
throw new Error('error message');
});
await expect(gitInit('foo')).rejects.toThrow(
'Init Error: Failed to init a new git repository.',
);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
describe('yarnInstall', () => {
it('run yarn and yarn install commands', async () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => Buffer.from([]));
await yarnInstall('foo');
expect(execSyncMock).toHaveBeenCalledTimes(1);
expect(execSyncMock).toHaveBeenCalledWith('yarn install', {
stdio: [0, 1, 2],
cwd: pathUtils.resolve(__dirname, 'foo'),
});
});
it('throws an error if it fails to run a command', async () => {
const execSyncMock = jest
.spyOn(childProcess, 'execSync')
.mockImplementation(() => {
throw new Error('error message');
});
await expect(yarnInstall('foo')).rejects.toThrow(
'Init Error: Failed to install dependencies.',
);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
});