/
migrated-data.spec.ts
194 lines (174 loc) · 6.86 KB
/
migrated-data.spec.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
import detectIndent from 'detect-indent';
import { Fixtures } from '../../../../../test/fixtures';
import { mockedFunction, scm } from '../../../../../test/util';
import { migrateConfig } from '../../../../config/migration';
import { logger } from '../../../../logger';
import { readLocalFile } from '../../../../util/fs';
import { detectRepoFileConfig } from '../../init/merge';
import { MigratedDataFactory } from './migrated-data';
jest.mock('../../../../config/migration');
jest.mock('../../../../util/git');
jest.mock('../../../../util/fs');
jest.mock('../../init/merge');
jest.mock('detect-indent');
const rawNonMigrated = Fixtures.get('./renovate.json');
const rawNonMigratedJson5 = Fixtures.get('./renovate.json5');
const migratedData = Fixtures.getJson('./migrated-data.json');
const migratedDataJson5 = Fixtures.getJson('./migrated-data.json5');
const migratedConfigObj = Fixtures.getJson('./migrated.json');
const formattedMigratedData = Fixtures.getJson(
'./migrated-data-formatted.json'
);
describe('workers/repository/config-migration/branch/migrated-data', () => {
describe('MigratedDataFactory.getAsync', () => {
beforeEach(() => {
jest.resetAllMocks();
mockedFunction(detectIndent).mockReturnValue({
type: 'space',
amount: 2,
indent: ' ',
});
mockedFunction(detectRepoFileConfig).mockResolvedValue({
configFileName: 'renovate.json',
configFileRaw: rawNonMigrated,
});
mockedFunction(migrateConfig).mockReturnValue({
isMigrated: true,
migratedConfig: migratedConfigObj,
});
});
it('Calls getAsync a first when migration not needed', async () => {
mockedFunction(migrateConfig).mockReturnValueOnce({
isMigrated: false,
migratedConfig: {},
});
await expect(MigratedDataFactory.getAsync()).resolves.toBeNull();
});
it('Calls getAsync a first time to initialize the factory', async () => {
await expect(MigratedDataFactory.getAsync()).resolves.toEqual(
migratedData
);
expect(detectRepoFileConfig).toHaveBeenCalledTimes(1);
});
it('Calls getAsync a second time to get the saved data from before', async () => {
await expect(MigratedDataFactory.getAsync()).resolves.toEqual(
migratedData
);
expect(detectRepoFileConfig).toHaveBeenCalledTimes(0);
});
describe('MigratedData class', () => {
it('gets the filename from the class instance', async () => {
const data = await MigratedDataFactory.getAsync();
expect(data?.filename).toBe('renovate.json');
});
it('gets the content from the class instance', async () => {
const data = await MigratedDataFactory.getAsync();
expect(data?.content).toBe(migratedData.content);
});
});
it('Resets the factory and gets a new value', async () => {
MigratedDataFactory.reset();
await expect(MigratedDataFactory.getAsync()).resolves.toEqual(
migratedData
);
});
it('Resets the factory and gets a new value with default indentation', async () => {
const indent = {
type: undefined,
amount: 0,
// TODO: incompatible types (#7154)
indent: null as never,
};
mockedFunction(detectIndent).mockReturnValueOnce(indent);
MigratedDataFactory.reset();
await expect(MigratedDataFactory.getAsync()).resolves.toEqual({
...migratedData,
indent,
});
});
it('Migrate a JSON5 config file', async () => {
mockedFunction(detectRepoFileConfig).mockResolvedValueOnce({
configFileName: 'renovate.json5',
configFileRaw: rawNonMigratedJson5,
});
MigratedDataFactory.reset();
await expect(MigratedDataFactory.getAsync()).resolves.toEqual(
migratedDataJson5
);
});
it('Returns nothing due to detectRepoFileConfig throwing', async () => {
const err = new Error('error-message');
mockedFunction(detectRepoFileConfig).mockRejectedValueOnce(err);
MigratedDataFactory.reset();
await expect(MigratedDataFactory.getAsync()).resolves.toBeNull();
expect(logger.debug).toHaveBeenCalledWith(
{ err },
'MigratedDataFactory.getAsync() Error initializing renovate MigratedData'
);
});
});
describe('MigratedDataFactory.applyPrettierFormatting', () => {
beforeAll(() => {
mockedFunction(detectIndent).mockReturnValueOnce({
type: 'space',
amount: 2,
indent: ' ',
});
mockedFunction(detectRepoFileConfig).mockResolvedValueOnce({
configFileName: 'renovate.json',
configFileRaw: rawNonMigrated,
});
mockedFunction(migrateConfig).mockReturnValueOnce({
isMigrated: true,
migratedConfig: migratedConfigObj,
});
MigratedDataFactory.reset();
});
beforeEach(() => {
mockedFunction(scm.getFileList).mockResolvedValue([]);
});
it('does not format when no prettier config is present', async () => {
const { content: unformatted } = migratedData;
mockedFunction(readLocalFile).mockResolvedValueOnce(null);
await MigratedDataFactory.getAsync();
await expect(
MigratedDataFactory.applyPrettierFormatting(migratedData)
).resolves.toEqual(unformatted);
});
it('does not format when failing to fetch package.json file', async () => {
const { content: unformatted } = migratedData;
mockedFunction(readLocalFile).mockRejectedValueOnce(null);
await MigratedDataFactory.getAsync();
await expect(
MigratedDataFactory.applyPrettierFormatting(migratedData)
).resolves.toEqual(unformatted);
});
it('does not format when there is an invalid package.json file', async () => {
const { content: unformatted } = migratedData;
mockedFunction(readLocalFile).mockResolvedValueOnce('invalid json');
await MigratedDataFactory.getAsync();
await expect(
MigratedDataFactory.applyPrettierFormatting(migratedData)
).resolves.toEqual(unformatted);
});
it('formats when prettier config file is found', async () => {
const formatted = formattedMigratedData.content;
mockedFunction(scm.getFileList).mockResolvedValue(['.prettierrc']);
await MigratedDataFactory.getAsync();
await expect(
MigratedDataFactory.applyPrettierFormatting(migratedData)
).resolves.toEqual(formatted);
});
it('formats when finds prettier config inside the package.json file', async () => {
const formatted = formattedMigratedData.content;
mockedFunction(detectRepoFileConfig).mockResolvedValueOnce({
configFileName: 'renovate.json',
});
mockedFunction(readLocalFile).mockResolvedValueOnce('{"prettier":{}}');
await MigratedDataFactory.getAsync();
await expect(
MigratedDataFactory.applyPrettierFormatting(migratedData)
).resolves.toEqual(formatted);
});
});
});