-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinitial-parameters.test.ts
180 lines (159 loc) · 6.11 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
import os from 'os';
import path from 'path';
import { when } from 'jest-when';
import { buildMockProject, buildMockPackage } 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();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: '/path/to/temp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: '2022-06-22', EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/somewhere',
);
expect(config).toStrictEqual({
project,
tempDirectoryPath: '/path/to/temp',
reset: true,
today: new Date('2022-06-22'),
});
});
it('resolves the given project directory relative to the current working directory', async () => {
const project = buildMockProject({
rootPackage: buildMockPackage(),
});
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: 'project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
const readProjectSpy = jest
.spyOn(projectModule, 'readProject')
.mockResolvedValue(project);
await determineInitialParameters(['arg1', 'arg2'], '/path/to/cwd');
expect(readProjectSpy).toHaveBeenCalledWith('/path/to/cwd/project');
});
it('resolves the given temporary directory relative to the current working directory', async () => {
const project = buildMockProject();
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: 'tmp',
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);
expect(config.tempDirectoryPath).toStrictEqual('/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'),
});
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);
expect(config.tempDirectoryPath).toStrictEqual(
path.join(os.tmpdir(), 'create-release-branch', '@foo__bar'),
);
});
it('uses the current date if the TODAY environment variable was not provided', async () => {
const project = buildMockProject();
const today = new Date('2022-01-01');
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: undefined, EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
jest.setSystemTime(today);
const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);
expect(config.today).toStrictEqual(today);
});
it('uses the current date if TODAY is not a parsable date', async () => {
const project = buildMockProject();
const today = new Date('2022-01-01');
when(jest.spyOn(commandLineArgumentsModule, 'readCommandLineArguments'))
.calledWith(['arg1', 'arg2'])
.mockResolvedValue({
projectDirectory: '/path/to/project',
tempDirectory: undefined,
reset: true,
});
jest
.spyOn(envModule, 'getEnvironmentVariables')
.mockReturnValue({ TODAY: 'asdfgdasf', EDITOR: undefined });
when(jest.spyOn(projectModule, 'readProject'))
.calledWith('/path/to/project')
.mockResolvedValue(project);
jest.setSystemTime(today);
const config = await determineInitialParameters(
['arg1', 'arg2'],
'/path/to/cwd',
);
expect(config.today).toStrictEqual(today);
});
});
});