Skip to content

Commit 246a646

Browse files
authored
Merge pull request #953 from dennisameling/spawn-logic-and-path-issue
Extract spawn logic into a wrapper and add tests
2 parents 68902d3 + a806009 commit 246a646

File tree

7 files changed

+208
-89
lines changed

7 files changed

+208
-89
lines changed

.jest/set-env-vars.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
This ensures that the PATH of the machine that the tests are running on,
3+
doesn't leak into our tests.
4+
*/
5+
process.env.PATH = ''

dist/index.js

+61-44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jest.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module.exports = {
22
clearMocks: true,
33
moduleFileExtensions: ['js', 'ts'],
4+
setupFiles: ["<rootDir>/.jest/set-env-vars.js"],
45
testEnvironment: 'node',
56
testMatch: ['**/*.test.ts'],
67
testRunner: 'jest-circus/runner',
78
transform: {
89
'^.+\\.ts$': 'ts-jest'
910
},
1011
verbose: true
11-
}
12+
}

src/__tests__/git.test.ts

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import * as fs from 'fs'
2+
import * as git from '../git'
3+
import * as spawn from '../spawn'
4+
import * as core from '@actions/core'
5+
6+
// We want to mock only the rmSync method on the fs module, and leave everything
7+
// else untouched.
8+
jest.mock('fs', () => ({
9+
...jest.requireActual('fs'),
10+
rmSync: jest.fn()
11+
}))
12+
13+
describe('git', () => {
14+
// We don't want to _actually_ spawn external commands, so we mock the function
15+
let spawnSpy: jest.SpyInstance
16+
// Capture the startGroup calls
17+
let coreSpy: jest.SpyInstance
18+
// The script calls fs.rmSync, so let's mock it and verify it was called
19+
let rmSyncSpy: jest.SpyInstance
20+
21+
beforeEach(() => {
22+
coreSpy = jest.spyOn(core, 'startGroup')
23+
spawnSpy = jest.spyOn(spawn, 'spawnAndWaitForExitCode').mockResolvedValue({
24+
// 0 is the exit code for success
25+
exitCode: 0
26+
})
27+
// We don't want to _actually_ clone the repo, so we mock the function
28+
jest.spyOn(git, 'clone').mockResolvedValue()
29+
rmSyncSpy = fs.rmSync as jest.Mocked<typeof fs>['rmSync']
30+
})
31+
32+
test('getViaGit build-installers x86_64', async () => {
33+
const flavor = 'build-installers'
34+
const architecture = 'x86_64'
35+
const outputDirectory = 'outputDirectory'
36+
const {artifactName, download} = await git.getViaGit(flavor, architecture)
37+
38+
expect(artifactName).toEqual('git-sdk-64-build-installers')
39+
40+
await download(outputDirectory, true)
41+
42+
expect(coreSpy).toHaveBeenCalledWith(`Creating ${flavor} artifact`)
43+
expect(spawnSpy).toHaveBeenCalledWith(
44+
expect.stringContaining('/bash.exe'),
45+
expect.arrayContaining([
46+
'.tmp/build-extra/please.sh',
47+
'create-sdk-artifact',
48+
`--architecture=${architecture}`,
49+
`--out=${outputDirectory}`
50+
]),
51+
expect.objectContaining({
52+
env: expect.objectContaining({
53+
// We want to ensure that the correct /bin folders are in the PATH,
54+
// so that please.sh can find git.exe
55+
// https://github.com/git-for-windows/setup-git-for-windows-sdk/issues/951
56+
PATH:
57+
expect.stringContaining('/clangarm64/bin') &&
58+
expect.stringContaining('/mingw64/bin')
59+
})
60+
})
61+
)
62+
expect(rmSyncSpy).toHaveBeenCalledWith('.tmp', {recursive: true})
63+
})
64+
65+
test('getViaGit full x86_64', async () => {
66+
const flavor = 'full'
67+
const architecture = 'x86_64'
68+
const outputDirectory = 'outputDirectory'
69+
const {artifactName, download} = await git.getViaGit(flavor, architecture)
70+
71+
expect(artifactName).toEqual('git-sdk-64-full')
72+
73+
await download(outputDirectory, true)
74+
75+
expect(coreSpy).toHaveBeenCalledWith(`Checking out git-sdk-64`)
76+
expect(spawnSpy).toHaveBeenCalledWith(
77+
expect.stringContaining('/git.exe'),
78+
expect.arrayContaining([
79+
'--git-dir=.tmp',
80+
'worktree',
81+
'add',
82+
outputDirectory
83+
]),
84+
expect.any(Object)
85+
)
86+
expect(rmSyncSpy).toHaveBeenCalledWith('.tmp', {recursive: true})
87+
})
88+
})

0 commit comments

Comments
 (0)