Add mocks to tests
This commit is contained in:
36
__tests__/Action.test.ts
Normal file
36
__tests__/Action.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Action } from "../src/Action";
|
||||
import { Inputs } from "../src/Inputs";
|
||||
import { Releases } from "../src/Releases";
|
||||
|
||||
|
||||
describe("Action", () => {
|
||||
|
||||
it('verifies my sanity', () => {
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
function createAction(): Action {
|
||||
const MockReleases = jest.fn<Releases, any>(() => {
|
||||
return {
|
||||
create: jest.fn(),
|
||||
uploadArtifact: jest.fn()
|
||||
}
|
||||
})
|
||||
const MockInputs = jest.fn<Inputs, any>(() => {
|
||||
return {
|
||||
artifact: "foo",
|
||||
artifactContentType: "foo",
|
||||
artifactContentLength: 22,
|
||||
body: "bar",
|
||||
commit: "foo",
|
||||
name: "name",
|
||||
tag: "foo",
|
||||
token: "foo"
|
||||
}
|
||||
})
|
||||
const inputs = new MockInputs()
|
||||
const releases = new MockReleases()
|
||||
|
||||
return new Action(inputs, releases)
|
||||
}
|
||||
@@ -2,20 +2,20 @@ const mockGetInput = jest.fn();
|
||||
const mockReadFileSync = jest.fn();
|
||||
const mockStatSync = jest.fn();
|
||||
|
||||
import { Inputs } from "../src/Inputs";
|
||||
import { Inputs, CoreInputs } from "../src/Inputs";
|
||||
import { Context } from "@actions/github/lib/context";
|
||||
|
||||
|
||||
jest.mock('@actions/core', () => {
|
||||
return { getInput: mockGetInput };
|
||||
});
|
||||
})
|
||||
|
||||
jest.mock('fs', () => {
|
||||
return {
|
||||
readFileSync: mockReadFileSync,
|
||||
statSync: mockStatSync
|
||||
};
|
||||
});
|
||||
})
|
||||
|
||||
describe('Inputs', () => {
|
||||
let context: Context;
|
||||
@@ -23,7 +23,7 @@ describe('Inputs', () => {
|
||||
beforeEach(() => {
|
||||
mockGetInput.mockReturnValue(null)
|
||||
context = new Context()
|
||||
inputs = new Inputs(context)
|
||||
inputs = new CoreInputs(context)
|
||||
})
|
||||
|
||||
it('returns artifact', () => {
|
||||
@@ -58,21 +58,32 @@ describe('Inputs', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('description', () => {
|
||||
it('returns input description', () => {
|
||||
mockGetInput.mockReturnValue("description")
|
||||
expect(inputs.description).toBe("description")
|
||||
describe('body', () => {
|
||||
it('returns input body', () => {
|
||||
mockGetInput.mockReturnValue("body")
|
||||
expect(inputs.body).toBe("body")
|
||||
})
|
||||
|
||||
it('returns description file contents', () => {
|
||||
it('returns body file contents', () => {
|
||||
mockGetInput.mockReturnValueOnce("").mockReturnValueOnce("a/path")
|
||||
mockReadFileSync.mockReturnValue("file")
|
||||
|
||||
expect(inputs.description).toBe("file")
|
||||
expect(inputs.body).toBe("file")
|
||||
})
|
||||
|
||||
it('returns empty', () => {
|
||||
expect(inputs.description).toBe("")
|
||||
expect(inputs.body).toBe("")
|
||||
})
|
||||
})
|
||||
|
||||
describe('draft', () => {
|
||||
it('returns false', () => {
|
||||
expect(inputs.draft).toBe(false)
|
||||
})
|
||||
|
||||
it('returns true', () => {
|
||||
mockGetInput.mockReturnValue("true")
|
||||
expect(inputs.draft).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
12
action.yml
12
action.yml
@@ -8,15 +8,15 @@ inputs:
|
||||
artifactContentType:
|
||||
description: 'The content type of the artifact. Defaults to raw'
|
||||
default: ''
|
||||
body:
|
||||
description: 'An optional body for the release.'
|
||||
default: ''
|
||||
bodyFile:
|
||||
description: 'An optional body file for the release. This should be the path to the file'
|
||||
default: ''
|
||||
commit:
|
||||
description: 'An optional commit reference. This will be used to create the tag if it doesn't exist.'
|
||||
default: ''
|
||||
description:
|
||||
description: 'An optional description for the release.'
|
||||
default: ''
|
||||
descriptionFile:
|
||||
description: 'An optional description file for the release. This should be the path to the file'
|
||||
default: ''
|
||||
name:
|
||||
description: 'An optional name for the release. If this is omitted the tag will be used.'
|
||||
default: ''
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Inputs } from "./Inputs";
|
||||
import { Releases } from "./Releases";
|
||||
import { basename } from "path";
|
||||
|
||||
export class Action {
|
||||
private inputs: Inputs
|
||||
@@ -9,4 +10,22 @@ export class Action {
|
||||
this.inputs = inputs
|
||||
this.releases = releases
|
||||
}
|
||||
|
||||
async perform() {
|
||||
const createResult = await this.releases.create(
|
||||
this.inputs.tag,
|
||||
this.inputs.body,
|
||||
this.inputs.commit,
|
||||
this.inputs.draft,
|
||||
this.inputs.name)
|
||||
|
||||
if (this.inputs.artifact) {
|
||||
await this.releases.uploadArtifact(
|
||||
createResult.data.assets_url,
|
||||
this.inputs.artifactContentLength,
|
||||
this.inputs.artifactContentType,
|
||||
this.inputs.artifact,
|
||||
basename(this.inputs.artifact))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,19 @@ import * as core from '@actions/core';
|
||||
import { Context } from "@actions/github/lib/context";
|
||||
import { readFileSync, statSync } from 'fs';
|
||||
|
||||
export class Inputs {
|
||||
export interface Inputs {
|
||||
readonly artifact: string
|
||||
readonly artifactContentType: string
|
||||
readonly artifactContentLength: number
|
||||
readonly body: string
|
||||
readonly commit: string
|
||||
readonly draft: boolean
|
||||
readonly name: string
|
||||
readonly tag: string
|
||||
readonly token: string
|
||||
}
|
||||
|
||||
export class CoreInputs implements Inputs {
|
||||
private context: Context
|
||||
|
||||
constructor(context: Context) {
|
||||
@@ -26,22 +38,27 @@ export class Inputs {
|
||||
return statSync(this.artifact).size
|
||||
}
|
||||
|
||||
get body(): string {
|
||||
const body = core.getInput('body')
|
||||
if (body) {
|
||||
return body
|
||||
}
|
||||
|
||||
const bodyFile = core.getInput('bodyFile')
|
||||
if (bodyFile) {
|
||||
return this.stringFromFile(bodyFile)
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
get commit(): string {
|
||||
return core.getInput('commit')
|
||||
}
|
||||
|
||||
get description(): string {
|
||||
const description = core.getInput('description')
|
||||
if (description) {
|
||||
return description
|
||||
}
|
||||
|
||||
const descriptionFile = core.getInput('descriptionFile')
|
||||
if (descriptionFile) {
|
||||
return this.stringFromFile(descriptionFile)
|
||||
}
|
||||
|
||||
return ''
|
||||
get draft(): boolean {
|
||||
const draft = core.getInput('draft')
|
||||
return draft == 'true'
|
||||
}
|
||||
|
||||
get name(): string {
|
||||
@@ -72,7 +89,7 @@ export class Inputs {
|
||||
return core.getInput('token', { required: true })
|
||||
}
|
||||
|
||||
private stringFromFile(path: string): string {
|
||||
stringFromFile(path: string): string {
|
||||
return readFileSync(path, 'utf-8')
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
import * as github from '@actions/github';
|
||||
import * as core from '@actions/core';
|
||||
import { Inputs } from './Inputs';
|
||||
import { Releases } from './Releases';
|
||||
import { Inputs, CoreInputs } from './Inputs';
|
||||
import { Releases, GithubReleases } from './Releases';
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
const token = core.getInput('token');
|
||||
const context = github.context
|
||||
const git = new github.GitHub(token);
|
||||
const releases = new Releases(context, git)
|
||||
const inputs = new Inputs(context)
|
||||
const releases = new GithubReleases(context, git)
|
||||
const inputs = new CoreInputs(context)
|
||||
await releases.create(inputs.tag)
|
||||
.catch(error => {
|
||||
core.warning(error)
|
||||
|
||||
@@ -2,7 +2,25 @@ import { Context } from "@actions/github/lib/context";
|
||||
import { GitHub } from "@actions/github";
|
||||
import { AnyResponse, Response, ReposCreateReleaseResponse } from "@octokit/rest";
|
||||
|
||||
export class Releases {
|
||||
export interface Releases {
|
||||
create(
|
||||
tag: string,
|
||||
body?: string,
|
||||
commitHash?: string,
|
||||
draft?: boolean,
|
||||
name?: string
|
||||
): Promise<Response<ReposCreateReleaseResponse>>
|
||||
|
||||
uploadArtifact(
|
||||
assetUrl: string,
|
||||
contentLength: number,
|
||||
contentType: string,
|
||||
file: string,
|
||||
name: string
|
||||
): Promise<Response<AnyResponse>>
|
||||
}
|
||||
|
||||
export class GithubReleases implements Releases{
|
||||
context: Context
|
||||
git: GitHub
|
||||
|
||||
|
||||
Reference in New Issue
Block a user