mirror of
https://code.forgejo.org/actions/checkout.git
synced 2024-11-24 18:34:51 +01:00
switch to spyOn for mocks (#152)
This commit is contained in:
parent
f95f2a3856
commit
61fd8fd0c7
2 changed files with 47 additions and 48 deletions
|
@ -1,47 +1,44 @@
|
|||
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 {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
|
||||
// Inputs for 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
|
||||
// Shallow clone original @actions/github context
|
||||
let originalContext = {...github.context}
|
||||
|
||||
describe('input-helper tests', () => {
|
||||
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
|
||||
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(() => {
|
||||
|
@ -50,14 +47,18 @@ describe('input-helper tests', () => {
|
|||
})
|
||||
|
||||
afterAll(() => {
|
||||
// Reset GitHub workspace
|
||||
// Restore GitHub workspace
|
||||
delete process.env['GITHUB_WORKSPACE']
|
||||
if (originalGitHubWorkspace) {
|
||||
process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
|
||||
}
|
||||
|
||||
// Reset modules
|
||||
jest.resetModules()
|
||||
// Restore @actions/github context
|
||||
github.context.ref = originalContext.ref
|
||||
github.context.sha = originalContext.sha
|
||||
|
||||
// Restore
|
||||
jest.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('sets defaults', () => {
|
||||
|
@ -76,16 +77,15 @@ describe('input-helper tests', () => {
|
|||
})
|
||||
|
||||
it('qualifies ref', () => {
|
||||
let originalContext = mockGitHub.context
|
||||
let originalRef = github.context.ref
|
||||
try {
|
||||
mockGitHub.context = {...originalContext} // Shallow clone
|
||||
mockGitHub.context.ref = 'some-unqualified-ref'
|
||||
github.context.ref = 'some-unqualified-ref'
|
||||
const settings: ISourceSettings = inputHelper.getInputs()
|
||||
expect(settings).toBeTruthy()
|
||||
expect(settings.commit).toBe('1234567890123456789012345678901234567890')
|
||||
expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
|
||||
} finally {
|
||||
mockGitHub.context = originalContext
|
||||
github.context.ref = originalRef
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
const mockCore = jest.genMockFromModule('@actions/core') as any
|
||||
mockCore.info = (message: string) => {
|
||||
info.push(message)
|
||||
}
|
||||
import * as core from '@actions/core'
|
||||
import {RetryHelper} from '../lib/retry-helper'
|
||||
|
||||
let info: string[]
|
||||
let retryHelper: any
|
||||
|
||||
describe('retry-helper tests', () => {
|
||||
beforeAll(() => {
|
||||
// Mocks
|
||||
jest.setMock('@actions/core', mockCore)
|
||||
// Mock @actions/core info()
|
||||
jest.spyOn(core, 'info').mockImplementation((message: string) => {
|
||||
info.push(message)
|
||||
})
|
||||
|
||||
// Now import
|
||||
const retryHelperModule = require('../lib/retry-helper')
|
||||
retryHelper = new retryHelperModule.RetryHelper(3, 0, 0)
|
||||
retryHelper = new RetryHelper(3, 0, 0)
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
|
@ -21,8 +20,8 @@ describe('retry-helper tests', () => {
|
|||
})
|
||||
|
||||
afterAll(() => {
|
||||
// Reset modules
|
||||
jest.resetModules()
|
||||
// Restore
|
||||
jest.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('first attempt succeeds', async () => {
|
||||
|
|
Loading…
Reference in a new issue