/
artifacts.spec.ts
119 lines (114 loc) · 3.62 KB
/
artifacts.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
import { exec as _exec } from 'child_process';
import _fs from 'fs-extra';
import { join } from 'upath';
import { envMock, mockExecAll } from '../../../test/execUtil';
import { mocked, platform } from '../../../test/util';
import { setExecConfig } from '../../util/exec';
import { BinarySource } from '../../util/exec/common';
import * as docker from '../../util/exec/docker';
import * as _env from '../../util/exec/env';
import * as cargo from './artifacts';
jest.mock('fs-extra');
jest.mock('child_process');
jest.mock('../../util/exec/env');
jest.mock('../../util/http');
const fs: jest.Mocked<typeof _fs> = _fs as any;
const exec: jest.Mock<typeof _exec> = _exec as any;
const env = mocked(_env);
const config = {
// `join` fixes Windows CI
localDir: join('/tmp/github/some/repo'),
dockerUser: 'foobar',
};
describe('.updateArtifacts()', () => {
beforeEach(async () => {
jest.resetAllMocks();
jest.resetModules();
env.getChildProcessEnv.mockReturnValue(envMock.basic);
await setExecConfig(config);
docker.resetPrefetchedImages();
});
it('returns null if no Cargo.lock found', async () => {
const updatedDeps = ['dep1'];
expect(
await cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '',
config,
})
).toBeNull();
});
it('returns null if updatedDeps is empty', async () => {
expect(
await cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps: [],
newPackageFileContent: '',
config,
})
).toBeNull();
});
it('returns null if unchanged', async () => {
fs.readFile.mockResolvedValueOnce('Current Cargo.lock' as any);
const execSnapshots = mockExecAll(exec);
fs.readFile.mockResolvedValueOnce('Current Cargo.lock' as any);
const updatedDeps = ['dep1'];
expect(
await cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '',
config,
})
).toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('returns updated Cargo.lock', async () => {
platform.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll(exec);
fs.readFile.mockResolvedValueOnce('New Cargo.lock' as any);
const updatedDeps = ['dep1'];
expect(
await cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('returns updated Cargo.lock with docker', async () => {
jest.spyOn(docker, 'removeDanglingContainers').mockResolvedValueOnce();
await setExecConfig({ ...config, binarySource: BinarySource.Docker });
platform.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll(exec);
fs.readFile.mockResolvedValueOnce('New Cargo.lock' as any);
const updatedDeps = ['dep1'];
expect(
await cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
});
it('catches errors', async () => {
fs.readFile.mockResolvedValueOnce('Current Cargo.lock' as any);
fs.outputFile.mockImplementationOnce(() => {
throw new Error('not found');
});
const updatedDeps = ['dep1'];
expect(
await cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
})
).toMatchSnapshot();
});
});