mirror of
https://gitee.com/actions-mirror/checkout.git
synced 2024-11-27 08:10:42 +08:00
switch to spyOn for mocks (#152)
This commit is contained in:
parent
f95f2a3856
commit
61fd8fd0c7
@ -1,47 +1,44 @@
|
|||||||
import * as assert from 'assert'
|
import * as assert from 'assert'
|
||||||
|
import * as core from '@actions/core'
|
||||||
|
import * as fsHelper from '../lib/fs-helper'
|
||||||
|
import * as github from '@actions/github'
|
||||||
|
import * as inputHelper from '../lib/input-helper'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import {ISourceSettings} from '../lib/git-source-provider'
|
import {ISourceSettings} from '../lib/git-source-provider'
|
||||||
|
|
||||||
const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
|
const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
|
||||||
const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
|
const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
|
||||||
|
|
||||||
// Late bind
|
// Inputs for mock @actions/core
|
||||||
let inputHelper: any
|
|
||||||
|
|
||||||
// Mock @actions/core
|
|
||||||
let inputs = {} as any
|
let inputs = {} as any
|
||||||
const mockCore = jest.genMockFromModule('@actions/core') as any
|
|
||||||
mockCore.getInput = (name: string) => {
|
|
||||||
return inputs[name]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mock @actions/github
|
// Shallow clone original @actions/github context
|
||||||
const mockGitHub = jest.genMockFromModule('@actions/github') as any
|
let originalContext = {...github.context}
|
||||||
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', () => {
|
describe('input-helper tests', () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
|
// Mock @actions/core getInput()
|
||||||
|
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
||||||
|
return inputs[name]
|
||||||
|
})
|
||||||
|
|
||||||
|
// Mock @actions/github context
|
||||||
|
jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
|
||||||
|
return {
|
||||||
|
owner: 'some-owner',
|
||||||
|
repo: 'some-repo'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
github.context.ref = 'refs/heads/some-ref'
|
||||||
|
github.context.sha = '1234567890123456789012345678901234567890'
|
||||||
|
|
||||||
|
// Mock ./fs-helper directoryExistsSync()
|
||||||
|
jest
|
||||||
|
.spyOn(fsHelper, 'directoryExistsSync')
|
||||||
|
.mockImplementation((path: string) => path == gitHubWorkspace)
|
||||||
|
|
||||||
// GitHub workspace
|
// GitHub workspace
|
||||||
process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
|
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(() => {
|
beforeEach(() => {
|
||||||
@ -50,14 +47,18 @@ describe('input-helper tests', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
// Reset GitHub workspace
|
// Restore GitHub workspace
|
||||||
delete process.env['GITHUB_WORKSPACE']
|
delete process.env['GITHUB_WORKSPACE']
|
||||||
if (originalGitHubWorkspace) {
|
if (originalGitHubWorkspace) {
|
||||||
process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
|
process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset modules
|
// Restore @actions/github context
|
||||||
jest.resetModules()
|
github.context.ref = originalContext.ref
|
||||||
|
github.context.sha = originalContext.sha
|
||||||
|
|
||||||
|
// Restore
|
||||||
|
jest.restoreAllMocks()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sets defaults', () => {
|
it('sets defaults', () => {
|
||||||
@ -76,16 +77,15 @@ describe('input-helper tests', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('qualifies ref', () => {
|
it('qualifies ref', () => {
|
||||||
let originalContext = mockGitHub.context
|
let originalRef = github.context.ref
|
||||||
try {
|
try {
|
||||||
mockGitHub.context = {...originalContext} // Shallow clone
|
github.context.ref = 'some-unqualified-ref'
|
||||||
mockGitHub.context.ref = 'some-unqualified-ref'
|
|
||||||
const settings: ISourceSettings = inputHelper.getInputs()
|
const settings: ISourceSettings = inputHelper.getInputs()
|
||||||
expect(settings).toBeTruthy()
|
expect(settings).toBeTruthy()
|
||||||
expect(settings.commit).toBe('1234567890123456789012345678901234567890')
|
expect(settings.commit).toBe('1234567890123456789012345678901234567890')
|
||||||
expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
|
expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
|
||||||
} finally {
|
} finally {
|
||||||
mockGitHub.context = originalContext
|
github.context.ref = originalRef
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
const mockCore = jest.genMockFromModule('@actions/core') as any
|
import * as core from '@actions/core'
|
||||||
mockCore.info = (message: string) => {
|
import {RetryHelper} from '../lib/retry-helper'
|
||||||
info.push(message)
|
|
||||||
}
|
|
||||||
let info: string[]
|
let info: string[]
|
||||||
let retryHelper: any
|
let retryHelper: any
|
||||||
|
|
||||||
describe('retry-helper tests', () => {
|
describe('retry-helper tests', () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
// Mocks
|
// Mock @actions/core info()
|
||||||
jest.setMock('@actions/core', mockCore)
|
jest.spyOn(core, 'info').mockImplementation((message: string) => {
|
||||||
|
info.push(message)
|
||||||
|
})
|
||||||
|
|
||||||
// Now import
|
retryHelper = new RetryHelper(3, 0, 0)
|
||||||
const retryHelperModule = require('../lib/retry-helper')
|
|
||||||
retryHelper = new retryHelperModule.RetryHelper(3, 0, 0)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -21,8 +20,8 @@ describe('retry-helper tests', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
// Reset modules
|
// Restore
|
||||||
jest.resetModules()
|
jest.restoreAllMocks()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('first attempt succeeds', async () => {
|
it('first attempt succeeds', async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user