diff options
| author | eric sciple <ericsciple@users.noreply.github.com> | 2019-12-03 10:28:59 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-03 10:28:59 -0500 |
| commit | e404ba5989baa550f631fb1262539d1375a856b7 (patch) | |
| tree | 4cc911589e7c9a4c976bc99eb4dcb886d2b2d8a8 /__test__ | |
| parent | 6a11491f2eea6739d1296d604c5402cffa43a1f1 (diff) | |
Convert checkout to a regular action (#70)
Diffstat (limited to '__test__')
| -rw-r--r-- | __test__/git-version.test.ts | 45 | ||||
| -rw-r--r-- | __test__/input-helper.test.ts | 120 | ||||
| -rwxr-xr-x | __test__/modify-work-tree.sh | 10 | ||||
| -rw-r--r-- | __test__/ref-helper.test.ts | 168 | ||||
| -rwxr-xr-x | __test__/verify-basic.sh | 10 | ||||
| -rwxr-xr-x | __test__/verify-clean.sh | 13 | ||||
| -rwxr-xr-x | __test__/verify-lfs.sh | 11 | ||||
| -rwxr-xr-x | __test__/verify-no-unstaged-changes.sh | 17 | ||||
| -rwxr-xr-x | __test__/verify-side-by-side.sh | 11 | ||||
| -rwxr-xr-x | __test__/verify-submodules-not-checked-out.sh | 11 |
10 files changed, 416 insertions, 0 deletions
diff --git a/__test__/git-version.test.ts b/__test__/git-version.test.ts new file mode 100644 index 0000000..aa3ae4b --- /dev/null +++ b/__test__/git-version.test.ts @@ -0,0 +1,45 @@ +import {GitVersion} from '../lib/git-version' + +describe('git-version tests', () => { + it('basics', async () => { + let version = new GitVersion('') + expect(version.isValid()).toBeFalsy() + + version = new GitVersion('asdf') + expect(version.isValid()).toBeFalsy() + + version = new GitVersion('1.2') + expect(version.isValid()).toBeTruthy() + expect(version.toString()).toBe('1.2') + + version = new GitVersion('1.2.3') + expect(version.isValid()).toBeTruthy() + expect(version.toString()).toBe('1.2.3') + }) + + it('check minimum', async () => { + let version = new GitVersion('4.5') + expect(version.checkMinimum(new GitVersion('3.6'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('3.6.7'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.4'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.5'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.5.0'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.6'))).toBeFalsy() + expect(version.checkMinimum(new GitVersion('4.6.0'))).toBeFalsy() + expect(version.checkMinimum(new GitVersion('5.1'))).toBeFalsy() + expect(version.checkMinimum(new GitVersion('5.1.2'))).toBeFalsy() + + version = new GitVersion('4.5.6') + expect(version.checkMinimum(new GitVersion('3.6'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('3.6.7'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.4'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.5'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.5.5'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.5.6'))).toBeTruthy() + expect(version.checkMinimum(new GitVersion('4.5.7'))).toBeFalsy() + expect(version.checkMinimum(new GitVersion('4.6'))).toBeFalsy() + expect(version.checkMinimum(new GitVersion('4.6.0'))).toBeFalsy() + expect(version.checkMinimum(new GitVersion('5.1'))).toBeFalsy() + expect(version.checkMinimum(new GitVersion('5.1.2'))).toBeFalsy() + }) +}) diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts new file mode 100644 index 0000000..6010e11 --- /dev/null +++ b/__test__/input-helper.test.ts @@ -0,0 +1,120 @@ +import * as assert from 'assert' +import * as path from 'path' +import {ISourceSettings} from '../lib/git-source-provider' + +const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE'] +const gitHubWorkspace = path.resolve('/checkout-tests/workspace') + +// Late bind +let inputHelper: any + +// Mock @actions/core +let inputs = {} as any +const mockCore = jest.genMockFromModule('@actions/core') as any +mockCore.getInput = (name: string) => { + return inputs[name] +} + +// Mock @actions/github +const mockGitHub = jest.genMockFromModule('@actions/github') as any +mockGitHub.context = { + repo: { + owner: 'some-owner', + repo: 'some-repo' + }, + ref: 'refs/heads/some-ref', + sha: '1234567890123456789012345678901234567890' +} + +// Mock ./fs-helper +const mockFSHelper = jest.genMockFromModule('../lib/fs-helper') as any +mockFSHelper.directoryExistsSync = (path: string) => path == gitHubWorkspace + +describe('input-helper tests', () => { + beforeAll(() => { + // GitHub workspace + process.env['GITHUB_WORKSPACE'] = gitHubWorkspace + + // Mocks + jest.setMock('@actions/core', mockCore) + jest.setMock('@actions/github', mockGitHub) + jest.setMock('../lib/fs-helper', mockFSHelper) + + // Now import + inputHelper = require('../lib/input-helper') + }) + + beforeEach(() => { + // Reset inputs + inputs = {} + }) + + afterAll(() => { + // Reset GitHub workspace + delete process.env['GITHUB_WORKSPACE'] + if (originalGitHubWorkspace) { + process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace + } + + // Reset modules + jest.resetModules() + }) + + it('sets defaults', () => { + const settings: ISourceSettings = inputHelper.getInputs() + expect(settings).toBeTruthy() + expect(settings.accessToken).toBeFalsy() + expect(settings.clean).toBe(true) + expect(settings.commit).toBeTruthy() + expect(settings.commit).toBe('1234567890123456789012345678901234567890') + expect(settings.fetchDepth).toBe(1) + expect(settings.lfs).toBe(false) + expect(settings.ref).toBe('refs/heads/some-ref') + expect(settings.repositoryName).toBe('some-repo') + expect(settings.repositoryOwner).toBe('some-owner') + expect(settings.repositoryPath).toBe(gitHubWorkspace) + }) + + it('requires qualified repo', () => { + inputs.repository = 'some-unqualified-repo' + assert.throws(() => { + inputHelper.getInputs() + }, /Invalid repository 'some-unqualified-repo'/) + }) + + it('roots path', () => { + inputs.path = 'some-directory/some-subdirectory' + const settings: ISourceSettings = inputHelper.getInputs() + expect(settings.repositoryPath).toBe( + path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory') + ) + }) + + it('sets correct default ref/sha for other repo', () => { + inputs.repository = 'some-owner/some-other-repo' + const settings: ISourceSettings = inputHelper.getInputs() + expect(settings.ref).toBe('refs/heads/master') + expect(settings.commit).toBeFalsy() + }) + + it('sets ref to empty when explicit sha', () => { + inputs.ref = '1111111111222222222233333333334444444444' + const settings: ISourceSettings = inputHelper.getInputs() + expect(settings.ref).toBeFalsy() + expect(settings.commit).toBe('1111111111222222222233333333334444444444') + }) + + it('sets sha to empty when explicit ref', () => { + inputs.ref = 'refs/heads/some-other-ref' + const settings: ISourceSettings = inputHelper.getInputs() + expect(settings.ref).toBe('refs/heads/some-other-ref') + expect(settings.commit).toBeFalsy() + }) + + it('gives good error message for submodules input', () => { + inputs.submodules = 'true' + assert.throws(() => { + inputHelper.getInputs() + }, /The input 'submodules' is not supported/) + }) +}) diff --git a/__test__/modify-work-tree.sh b/__test__/modify-work-tree.sh new file mode 100755 index 0000000..89447eb --- /dev/null +++ b/__test__/modify-work-tree.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ ! -f "./basic/basic-file.txt" ]; then + echo "Expected basic file does not exist" + exit 1 +fi + +echo hello >> ./basic/basic-file.txt +echo hello >> ./basic/new-file.txt +git -C ./basic status
\ No newline at end of file diff --git a/__test__/ref-helper.test.ts b/__test__/ref-helper.test.ts new file mode 100644 index 0000000..9911eff --- /dev/null +++ b/__test__/ref-helper.test.ts @@ -0,0 +1,168 @@ +import * as assert from 'assert' +import * as refHelper from '../lib/ref-helper' +import {IGitCommandManager} from '../lib/git-command-manager' + +const commit = '1234567890123456789012345678901234567890' +let git: IGitCommandManager + +describe('ref-helper tests', () => { + beforeEach(() => { + git = ({} as unknown) as IGitCommandManager + }) + + it('getCheckoutInfo requires git', async () => { + const git = (null as unknown) as IGitCommandManager + try { + await refHelper.getCheckoutInfo(git, 'refs/heads/my/branch', commit) + throw new Error('Should not reach here') + } catch (err) { + expect(err.message).toBe('Arg git cannot be empty') + } + }) + + it('getCheckoutInfo requires ref or commit', async () => { + try { + await refHelper.getCheckoutInfo(git, '', '') + throw new Error('Should not reach here') + } catch (err) { + expect(err.message).toBe('Args ref and commit cannot both be empty') + } + }) + + it('getCheckoutInfo sha only', async () => { + const checkoutInfo = await refHelper.getCheckoutInfo(git, '', commit) + expect(checkoutInfo.ref).toBe(commit) + expect(checkoutInfo.startPoint).toBeFalsy() + }) + + it('getCheckoutInfo refs/heads/', async () => { + const checkoutInfo = await refHelper.getCheckoutInfo( + git, + 'refs/heads/my/branch', + commit + ) + expect(checkoutInfo.ref).toBe('my/branch') + expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch') + }) + + it('getCheckoutInfo refs/pull/', async () => { + const checkoutInfo = await refHelper.getCheckoutInfo( + git, + 'refs/pull/123/merge', + commit + ) + expect(checkoutInfo.ref).toBe('refs/remotes/pull/123/merge') + expect(checkoutInfo.startPoint).toBeFalsy() + }) + + it('getCheckoutInfo refs/tags/', async () => { + const checkoutInfo = await refHelper.getCheckoutInfo( + git, + 'refs/tags/my-tag', + commit + ) + expect(checkoutInfo.ref).toBe('refs/tags/my-tag') + expect(checkoutInfo.startPoint).toBeFalsy() + }) + + it('getCheckoutInfo unqualified branch only', async () => { + git.branchExists = jest.fn(async (remote: boolean, pattern: string) => { + return true + }) + + const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my/branch', '') + + expect(checkoutInfo.ref).toBe('my/branch') + expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch') + }) + + it('getCheckoutInfo unqualified tag only', async () => { + git.branchExists = jest.fn(async (remote: boolean, pattern: string) => { + return false + }) + git.tagExists = jest.fn(async (pattern: string) => { + return true + }) + + const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my-tag', '') + + expect(checkoutInfo.ref).toBe('refs/tags/my-tag') + expect(checkoutInfo.startPoint).toBeFalsy() + }) + + it('getCheckoutInfo unqualified ref only, not a branch or tag', async () => { + git.branchExists = jest.fn(async (remote: boolean, pattern: string) => { + return false + }) + git.tagExists = jest.fn(async (pattern: string) => { + return false + }) + + try { + await refHelper.getCheckoutInfo(git, 'my-ref', '') + throw new Error('Should not reach here') + } catch (err) { + expect(err.message).toBe( + "A branch or tag with the name 'my-ref' could not be found" + ) + } + }) + + it('getRefSpec requires ref or commit', async () => { + assert.throws( + () => refHelper.getRefSpec('', ''), + /Args ref and commit cannot both be empty/ + ) + }) + + it('getRefSpec sha + refs/heads/', async () => { + const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit) + expect(refSpec.length).toBe(1) + expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`) + }) + + it('getRefSpec sha + refs/pull/', async () => { + const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit) + expect(refSpec.length).toBe(1) + expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`) + }) + + it('getRefSpec sha + refs/tags/', async () => { + const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit) + expect(refSpec.length).toBe(1) + expect(refSpec[0]).toBe(`+${commit}:refs/tags/my-tag`) + }) + + it('getRefSpec sha only', async () => { + const refSpec = refHelper.getRefSpec('', commit) + expect(refSpec.length).toBe(1) + expect(refSpec[0]).toBe(commit) + }) + + it('getRefSpec unqualified ref only', async () => { + const refSpec = refHelper.getRefSpec('my-ref', '') + expect(refSpec.length).toBe(2) + expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*') + expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*') + }) + + it('getRefSpec refs/heads/ only', async () => { + const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '') + expect(refSpec.length).toBe(1) + expect(refSpec[0]).toBe( + '+refs/heads/my/branch:refs/remotes/origin/my/branch' + ) + }) + + it('getRefSpec refs/pull/ only', async () => { + const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '') + expect(refSpec.length).toBe(1) + expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge') + }) + + it('getRefSpec refs/tags/ only', async () => { + const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '') + expect(refSpec.length).toBe(1) + expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag') + }) +}) diff --git a/__test__/verify-basic.sh b/__test__/verify-basic.sh new file mode 100755 index 0000000..b5d09f0 --- /dev/null +++ b/__test__/verify-basic.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ ! -f "./basic/basic-file.txt" ]; then + echo "Expected basic file does not exist" + exit 1 +fi + +# Verify auth token +cd basic +git fetch
\ No newline at end of file diff --git a/__test__/verify-clean.sh b/__test__/verify-clean.sh new file mode 100755 index 0000000..86bf9d6 --- /dev/null +++ b/__test__/verify-clean.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +if [[ "$(git -C ./basic status --porcelain)" != "" ]]; then + echo ---------------------------------------- + echo git status + echo ---------------------------------------- + git status + echo ---------------------------------------- + echo git diff + echo ---------------------------------------- + git diff + exit 1 +fi diff --git a/__test__/verify-lfs.sh b/__test__/verify-lfs.sh new file mode 100755 index 0000000..b0463f1 --- /dev/null +++ b/__test__/verify-lfs.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +if [ ! -f "./lfs/regular-file.txt" ]; then + echo "Expected regular file does not exist" + exit 1 +fi + +if [ ! -f "./lfs/lfs-file.bin" ]; then + echo "Expected lfs file does not exist" + exit 1 +fi diff --git a/__test__/verify-no-unstaged-changes.sh b/__test__/verify-no-unstaged-changes.sh new file mode 100755 index 0000000..9fe6173 --- /dev/null +++ b/__test__/verify-no-unstaged-changes.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +if [[ "$(git status --porcelain)" != "" ]]; then + echo ---------------------------------------- + echo git status + echo ---------------------------------------- + git status + echo ---------------------------------------- + echo git diff + echo ---------------------------------------- + git diff + echo ---------------------------------------- + echo Troubleshooting + echo ---------------------------------------- + echo "::error::Unstaged changes detected. Locally try running: git clean -ffdx && npm ci && npm run all" + exit 1 +fi diff --git a/__test__/verify-side-by-side.sh b/__test__/verify-side-by-side.sh new file mode 100755 index 0000000..35de29a --- /dev/null +++ b/__test__/verify-side-by-side.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +if [ ! -f "./side-by-side-1/side-by-side-test-file-1.txt" ]; then + echo "Expected file 1 does not exist" + exit 1 +fi + +if [ ! -f "./side-by-side-2/side-by-side-test-file-2.txt" ]; then + echo "Expected file 2 does not exist" + exit 1 +fi diff --git a/__test__/verify-submodules-not-checked-out.sh b/__test__/verify-submodules-not-checked-out.sh new file mode 100755 index 0000000..dcc0487 --- /dev/null +++ b/__test__/verify-submodules-not-checked-out.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +if [ ! -f "./submodules-not-checked-out/regular-file.txt" ]; then + echo "Expected regular file does not exist" + exit 1 +fi + +if [ -f "./submodules-not-checked-out/submodule-level-1/submodule-file.txt" ]; then + echo "Unexpected submodule file exists" + exit 1 +fi |
