-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinitial-parameters.test.ts
196 lines (175 loc) · 6.58 KB
/
initial-parameters.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
import os from 'os';
import path from 'path';
import { when } from 'jest-when';
import {
buildMockProject,
buildMockPackage,
createNoopWriteStream,
} from '../tests/unit/helpers';
import { determineInitialParameters } from './initial-parameters';
import * as commandLineArgumentsModule from './command-line-arguments';
import * as envModule from './env';
import * as projectModule from './project';
jest.mock('./command-line-arguments');
jest.mock('./env');
jest.mock('./project');
describe('initial-parameters', () => {
describe('determineInitialParameters', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('returns an object derived from command-line arguments and environment variables that contains data necessary to run the workflow', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);
const initialParameters = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
stderr,
});
expect(initialParameters).toStrictEqual({
project,
tempDirectoryPath: '/path/to/temp',
reset: true,
});
});
it('resolves the given project directory relative to the current working directory', async () => {
const project = buildMockProject({
rootPackage: buildMockPackage(),
});
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: 'project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
const readProjectSpy = jest
.spyOn(projectModule, 'readProject')
.mockResolvedValue(project);
await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/cwd',
stderr,
});
expect(readProjectSpy).toHaveBeenCalledWith('/path/to/cwd/project', {
stderr,
});
});
it('resolves the given temporary directory relative to the current working directory', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: 'tmp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);
const initialParameters = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/cwd',
stderr,
});
expect(initialParameters.tempDirectoryPath).toBe('/path/to/cwd/tmp');
});
it('uses a default temporary directory based on the name of the package if no temporary directory was given', async () => {
const project = buildMockProject({
rootPackage: buildMockPackage('@foo/bar'),
});
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);
const initialParameters = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/cwd',
stderr,
});
expect(initialParameters.tempDirectoryPath).toStrictEqual(
path.join(os.tmpdir(), 'create-release-branch', '@foo__bar'),
);
});
it('returns initial parameters including reset: true, derived from a command-line argument of "--reset true"', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);
const initialParameters = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
stderr,
});
expect(initialParameters.reset).toBe(true);
});
it('returns initial parameters including reset: false, derived from a command-line argument of "--reset false"', async () => {
const project = buildMockProject();
const stderr = createNoopWriteStream();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: false,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project', { stderr })
.mockResolvedValue(project);
const initialParameters = await determineInitialParameters({
argv: ['arg1', 'arg2'],
cwd: '/path/to/somewhere',
stderr,
});
expect(initialParameters.reset).toBe(false);
});
});
});