Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract spawn logic into a wrapper and add tests #953

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jest/setEnvVars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
This ensures that the PATH of the machine that the tests are running on,
doesn't leak into our tests.
*/
process.env.PATH = ''
105 changes: 61 additions & 44 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
setupFiles: ["<rootDir>/.jest/setEnvVars.ts"],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
Expand Down
88 changes: 88 additions & 0 deletions src/__tests__/git.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as fs from 'fs'
import * as git from '../git'
import * as spawn from '../spawn'
import * as core from '@actions/core'

// We want to mock only the rmSync method on the fs module, and leave everything
// else untouched.
jest.mock('fs', () => ({
...jest.requireActual('fs'),
rmSync: jest.fn()
}))

describe('git', () => {
// We don't want to _actually_ spawn external commands, so we mock the function
let spawnSpy: jest.SpyInstance
// Capture the startGroup calls
let coreSpy: jest.SpyInstance
// The script calls fs.rmSync, so let's mock it and verify it was called
let rmSyncSpy: jest.SpyInstance

beforeEach(() => {
coreSpy = jest.spyOn(core, 'startGroup')
spawnSpy = jest.spyOn(spawn, 'spawnAndWaitForExitCode').mockResolvedValue({
// 0 is the exit code for success
exitCode: 0
})
// We don't want to _actually_ clone the repo, so we mock the function
jest.spyOn(git, 'clone').mockResolvedValue()
rmSyncSpy = fs.rmSync as jest.Mocked<typeof fs>['rmSync']
})

test('getViaGit build-installers x86_64', async () => {
const flavor = 'build-installers'
const architecture = 'x86_64'
const outputDirectory = 'outputDirectory'
const {artifactName, download} = await git.getViaGit(flavor, architecture)

expect(artifactName).toEqual('git-sdk-64-build-installers')

await download(outputDirectory, true)

expect(coreSpy).toHaveBeenCalledWith(`Creating ${flavor} artifact`)
expect(spawnSpy).toHaveBeenCalledWith(
expect.stringContaining('/bash.exe'),
expect.arrayContaining([
'.tmp/build-extra/please.sh',
'create-sdk-artifact',
`--architecture=${architecture}`,
`--out=${outputDirectory}`
]),
expect.objectContaining({
env: expect.objectContaining({
// We want to ensure that the correct /bin folders are in the PATH,
// so that please.sh can find git.exe
// https://github.com/git-for-windows/setup-git-for-windows-sdk/issues/951
PATH:
expect.stringContaining('/clangarm64/bin') &&
expect.stringContaining('/mingw64/bin')
})
})
)
expect(rmSyncSpy).toHaveBeenCalledWith('.tmp', {recursive: true})
})

test('getViaGit full x86_64', async () => {
const flavor = 'full'
const architecture = 'x86_64'
const outputDirectory = 'outputDirectory'
const {artifactName, download} = await git.getViaGit(flavor, architecture)

expect(artifactName).toEqual('git-sdk-64-full')

await download(outputDirectory, true)

expect(coreSpy).toHaveBeenCalledWith(`Checking out git-sdk-64`)
expect(spawnSpy).toHaveBeenCalledWith(
expect.stringContaining('/git.exe'),
expect.arrayContaining([
'--git-dir=.tmp',
'worktree',
'add',
outputDirectory
]),
expect.any(Object)
)
expect(rmSyncSpy).toHaveBeenCalledWith('.tmp', {recursive: true})
})
})
Loading