generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git.ts: extract spawn logic into a wrapper and add tests
Currently, it's a bit hard to ensure that the code in git.exe actually does what we expect it to do, especially considering that we're calling things like please.sh and assume that some executables are available on the PATH. Let's ensure we add some tests for these code paths, which we can easily extend with more tests if needed. Signed-off-by: Dennis Ameling <[email protected]>
- Loading branch information
1 parent
8310ae7
commit 704b06a
Showing
5 changed files
with
140 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {mkdirSync, rmdirSync, existsSync} from 'fs' | ||
import * as git from '../git' | ||
import * as spawn from '../spawn' | ||
import * as core from '@actions/core' | ||
|
||
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 | ||
|
||
beforeEach(() => { | ||
coreSpy = jest.spyOn(core, 'startGroup') | ||
spawnSpy = jest.spyOn(spawn, 'spawn').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() | ||
|
||
// The script removes the .tmp folder at the end, so let's create it | ||
mkdirSync('.tmp') | ||
}) | ||
|
||
afterEach(() => { | ||
// Clean up the .tmp folder if the script didn't already do it | ||
if (existsSync('.tmp')) { | ||
rmdirSync('.tmp', {recursive: true}) | ||
} | ||
}) | ||
|
||
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') | ||
}) | ||
}) | ||
) | ||
}) | ||
|
||
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) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
spawn as SpawnInternal, | ||
SpawnOptionsWithStdioTuple, | ||
StdioNull, | ||
StdioPipe | ||
} from 'child_process' | ||
|
||
export type SpawnReturnArgs = { | ||
exitCode: number | null | ||
} | ||
|
||
/** | ||
* Simple wrapper around NodeJS's "child_process.spawn" function. | ||
* Since we only use the exit code, we only expose that. | ||
*/ | ||
export async function spawn( | ||
command: string, | ||
args: readonly string[], | ||
options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull> | ||
): Promise<SpawnReturnArgs> { | ||
const child = SpawnInternal(command, args, options) | ||
|
||
return new Promise<SpawnReturnArgs>((resolve, reject) => { | ||
child.on('error', reject) | ||
child.on('close', code => { | ||
resolve({exitCode: code}) | ||
}) | ||
}) | ||
} |