Fixes #267 Add ActionSkipper
This commit is contained in:
@@ -5,16 +5,18 @@ import {Releases} from "../src/Releases";
|
||||
import {ArtifactUploader} from "../src/ArtifactUploader";
|
||||
import {Outputs} from "../src/Outputs";
|
||||
import {ArtifactDestroyer} from "../src/ArtifactDestroyer";
|
||||
import {ActionSkipper} from "../src/ActionSkipper";
|
||||
|
||||
const applyReleaseDataMock = jest.fn()
|
||||
const artifactDestroyMock = jest.fn()
|
||||
const createMock = jest.fn()
|
||||
const deleteMock = jest.fn()
|
||||
const getMock = jest.fn()
|
||||
const listArtifactsMock = jest.fn()
|
||||
const listMock = jest.fn()
|
||||
const shouldSkipMock = jest.fn()
|
||||
const updateMock = jest.fn()
|
||||
const uploadMock = jest.fn()
|
||||
const artifactDestroyMock = jest.fn()
|
||||
|
||||
const artifacts = [
|
||||
new Artifact('a/art1'),
|
||||
@@ -45,6 +47,7 @@ describe("Action", () => {
|
||||
createMock.mockClear()
|
||||
getMock.mockClear()
|
||||
listMock.mockClear()
|
||||
shouldSkipMock.mockClear()
|
||||
updateMock.mockClear()
|
||||
uploadMock.mockClear()
|
||||
})
|
||||
@@ -150,6 +153,16 @@ describe("Action", () => {
|
||||
assertOutputApplied()
|
||||
})
|
||||
|
||||
it('skips action', async () => {
|
||||
const action = createAction(false, false, false)
|
||||
shouldSkipMock.mockResolvedValue(true)
|
||||
|
||||
await action.perform()
|
||||
|
||||
expect(createMock).not.toBeCalled()
|
||||
expect(updateMock).not.toBeCalled()
|
||||
})
|
||||
|
||||
it('throws error when create fails', async () => {
|
||||
const action = createAction(false, true)
|
||||
createMock.mockRejectedValue("error")
|
||||
@@ -353,6 +366,7 @@ describe("Action", () => {
|
||||
listMock.mockResolvedValue({
|
||||
data: []
|
||||
})
|
||||
shouldSkipMock.mockResolvedValue(false)
|
||||
updateMock.mockResolvedValue({
|
||||
data: {
|
||||
id: releaseId,
|
||||
@@ -377,6 +391,7 @@ describe("Action", () => {
|
||||
replacesArtifacts: replacesArtifacts,
|
||||
removeArtifacts: removeArtifacts,
|
||||
repo: "repo",
|
||||
skipIfReleaseExists: false,
|
||||
tag: tag,
|
||||
token: token,
|
||||
updatedDraft: updateDraft,
|
||||
@@ -401,13 +416,20 @@ describe("Action", () => {
|
||||
destroyArtifacts: artifactDestroyMock
|
||||
}
|
||||
})
|
||||
|
||||
const MockActionSkipper = jest.fn<ActionSkipper, any>(() => {
|
||||
return {
|
||||
shouldSkip: shouldSkipMock
|
||||
}
|
||||
})
|
||||
|
||||
const inputs = new MockInputs()
|
||||
const outputs = new MockOutputs()
|
||||
const releases = new MockReleases()
|
||||
const uploader = new MockUploader()
|
||||
const artifactDestroyer = new MockArtifactDestroyer()
|
||||
const actionSkipper = new MockActionSkipper()
|
||||
|
||||
return new Action(inputs, outputs, releases, uploader, artifactDestroyer)
|
||||
return new Action(inputs, outputs, releases, uploader, artifactDestroyer, actionSkipper)
|
||||
}
|
||||
})
|
||||
|
||||
44
__tests__/ActionSkipper.test.ts
Normal file
44
__tests__/ActionSkipper.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import {ActionSkipper, ReleaseActionSkipper} from "../src/ActionSkipper";
|
||||
import {Releases} from "../src/Releases";
|
||||
|
||||
describe("shouldSkip", () => {
|
||||
const getMock = jest.fn()
|
||||
const tag = "tag"
|
||||
const MockReleases = jest.fn<Releases, any>(() => {
|
||||
return {
|
||||
create: jest.fn(),
|
||||
deleteArtifact: jest.fn(),
|
||||
getByTag: getMock,
|
||||
listArtifactsForRelease: jest.fn(),
|
||||
listReleases: jest.fn(),
|
||||
update: jest.fn(),
|
||||
uploadArtifact: jest.fn()
|
||||
}
|
||||
})
|
||||
|
||||
it('should return false when skipIfReleaseExists is false', async () => {
|
||||
const actionSkipper = new ReleaseActionSkipper(false, MockReleases(), tag)
|
||||
expect(await actionSkipper.shouldSkip()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return false when error occurs', async () => {
|
||||
getMock.mockRejectedValue(new Error())
|
||||
|
||||
const actionSkipper = new ReleaseActionSkipper(true, MockReleases(), tag)
|
||||
expect(await actionSkipper.shouldSkip()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return false when release does not exist', async () => {
|
||||
getMock.mockResolvedValue({})
|
||||
|
||||
const actionSkipper = new ReleaseActionSkipper(true, MockReleases(), tag)
|
||||
expect(await actionSkipper.shouldSkip()).toBe(false)
|
||||
})
|
||||
|
||||
it('should return true when release does exist', async () => {
|
||||
getMock.mockResolvedValue({data: {}})
|
||||
|
||||
const actionSkipper = new ReleaseActionSkipper(true, MockReleases(), tag)
|
||||
expect(await actionSkipper.shouldSkip()).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
const mockGetInput = jest.fn();
|
||||
const mockGetBooleanInput = jest.fn();
|
||||
const mockGlob = jest.fn()
|
||||
const mockReadFileSync = jest.fn();
|
||||
const mockStatSync = jest.fn();
|
||||
@@ -14,7 +15,10 @@ const artifacts = [
|
||||
]
|
||||
|
||||
jest.mock('@actions/core', () => {
|
||||
return {getInput: mockGetInput};
|
||||
return {
|
||||
getInput: mockGetInput,
|
||||
getBooleanInput: mockGetBooleanInput
|
||||
};
|
||||
})
|
||||
|
||||
jest.mock('fs', () => {
|
||||
@@ -87,7 +91,7 @@ describe('Inputs', () => {
|
||||
expect(mockGlob).toBeCalledTimes(1)
|
||||
expect(mockGlob).toBeCalledWith('art1', 'contentType', true)
|
||||
})
|
||||
|
||||
|
||||
it('returns empty artifacts', () => {
|
||||
mockGetInput.mockReturnValueOnce('')
|
||||
.mockReturnValueOnce('')
|
||||
@@ -202,7 +206,7 @@ describe('Inputs', () => {
|
||||
mockGetInput.mockReturnValue('Release')
|
||||
expect(inputs.discussionCategory).toBe('Release')
|
||||
})
|
||||
|
||||
|
||||
it('returns undefined', () => {
|
||||
mockGetInput.mockReturnValue('')
|
||||
expect(inputs.discussionCategory).toBe(undefined)
|
||||
@@ -220,7 +224,7 @@ describe('Inputs', () => {
|
||||
expect(inputs.generateReleaseNotes).toBe(false)
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
describe('owner', () => {
|
||||
it('returns owner from context', function () {
|
||||
process.env.GITHUB_REPOSITORY = "owner/repo"
|
||||
@@ -278,6 +282,18 @@ describe('Inputs', () => {
|
||||
});
|
||||
})
|
||||
|
||||
describe('skipIfReleaseExists', () => {
|
||||
it('returns false', () => {
|
||||
mockGetBooleanInput.mockReturnValue(false)
|
||||
expect(inputs.skipIfReleaseExists).toBe(false)
|
||||
})
|
||||
|
||||
it('returns true', () => {
|
||||
mockGetBooleanInput.mockReturnValue(true)
|
||||
expect(inputs.skipIfReleaseExists).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('tag', () => {
|
||||
it('returns input tag', () => {
|
||||
mockGetInput.mockReturnValue('tag')
|
||||
|
||||
@@ -7,6 +7,7 @@ import * as path from "path";
|
||||
import {FileArtifactGlobber} from "../src/ArtifactGlobber";
|
||||
import {Outputs} from "../src/Outputs";
|
||||
import {GithubArtifactDestroyer} from "../src/ArtifactDestroyer";
|
||||
import {ReleaseActionSkipper} from "../src/ActionSkipper";
|
||||
|
||||
// This test is currently intended to be manually run during development. To run:
|
||||
// - Make sure you have an environment variable named GITHUB_TOKEN assigned to your token
|
||||
@@ -27,8 +28,9 @@ describe.skip('Integration Test', () => {
|
||||
inputs.artifactErrorsFailBuild,
|
||||
)
|
||||
const artifactDestroyer = new GithubArtifactDestroyer(releases)
|
||||
const actionSkipper = new ReleaseActionSkipper(inputs.skipIfReleaseExists, releases, inputs.tag)
|
||||
|
||||
action = new Action(inputs, outputs, releases, uploader, artifactDestroyer)
|
||||
action = new Action(inputs, outputs, releases, uploader, artifactDestroyer, actionSkipper)
|
||||
})
|
||||
|
||||
it('Performs action', async () => {
|
||||
@@ -52,10 +54,11 @@ describe.skip('Integration Test', () => {
|
||||
replacesArtifacts: true,
|
||||
removeArtifacts: false,
|
||||
repo: "actions-playground",
|
||||
skipIfReleaseExists: false,
|
||||
tag: "release-action-test",
|
||||
token: getToken(),
|
||||
updatedDraft: false,
|
||||
updatedReleaseBody: "This release was generated by release-action's integration test",
|
||||
updatedReleaseBody: "This release was updated by release-action's integration test",
|
||||
updatedReleaseName: "Releases Action Integration Test",
|
||||
updatedPrerelease: false,
|
||||
updateOnlyUnreleased: false
|
||||
|
||||
Reference in New Issue
Block a user