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 mockReadFileSync = jest.fn();
|
||||||
const mockStatSync = jest.fn();
|
const mockStatSync = jest.fn();
|
||||||
|
|
||||||
import { Inputs } from "../src/Inputs";
|
import { Inputs, CoreInputs } from "../src/Inputs";
|
||||||
import { Context } from "@actions/github/lib/context";
|
import { Context } from "@actions/github/lib/context";
|
||||||
|
|
||||||
|
|
||||||
jest.mock('@actions/core', () => {
|
jest.mock('@actions/core', () => {
|
||||||
return { getInput: mockGetInput };
|
return { getInput: mockGetInput };
|
||||||
});
|
})
|
||||||
|
|
||||||
jest.mock('fs', () => {
|
jest.mock('fs', () => {
|
||||||
return {
|
return {
|
||||||
readFileSync: mockReadFileSync,
|
readFileSync: mockReadFileSync,
|
||||||
statSync: mockStatSync
|
statSync: mockStatSync
|
||||||
};
|
};
|
||||||
});
|
})
|
||||||
|
|
||||||
describe('Inputs', () => {
|
describe('Inputs', () => {
|
||||||
let context: Context;
|
let context: Context;
|
||||||
@@ -23,7 +23,7 @@ describe('Inputs', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockGetInput.mockReturnValue(null)
|
mockGetInput.mockReturnValue(null)
|
||||||
context = new Context()
|
context = new Context()
|
||||||
inputs = new Inputs(context)
|
inputs = new CoreInputs(context)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns artifact', () => {
|
it('returns artifact', () => {
|
||||||
@@ -58,21 +58,32 @@ describe('Inputs', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('description', () => {
|
describe('body', () => {
|
||||||
it('returns input description', () => {
|
it('returns input body', () => {
|
||||||
mockGetInput.mockReturnValue("description")
|
mockGetInput.mockReturnValue("body")
|
||||||
expect(inputs.description).toBe("description")
|
expect(inputs.body).toBe("body")
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns description file contents', () => {
|
it('returns body file contents', () => {
|
||||||
mockGetInput.mockReturnValueOnce("").mockReturnValueOnce("a/path")
|
mockGetInput.mockReturnValueOnce("").mockReturnValueOnce("a/path")
|
||||||
mockReadFileSync.mockReturnValue("file")
|
mockReadFileSync.mockReturnValue("file")
|
||||||
|
|
||||||
expect(inputs.description).toBe("file")
|
expect(inputs.body).toBe("file")
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns empty', () => {
|
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:
|
artifactContentType:
|
||||||
description: 'The content type of the artifact. Defaults to raw'
|
description: 'The content type of the artifact. Defaults to raw'
|
||||||
default: ''
|
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:
|
commit:
|
||||||
description: 'An optional commit reference. This will be used to create the tag if it doesn't exist.'
|
description: 'An optional commit reference. This will be used to create the tag if it doesn't exist.'
|
||||||
default: ''
|
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:
|
name:
|
||||||
description: 'An optional name for the release. If this is omitted the tag will be used.'
|
description: 'An optional name for the release. If this is omitted the tag will be used.'
|
||||||
default: ''
|
default: ''
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Inputs } from "./Inputs";
|
import { Inputs } from "./Inputs";
|
||||||
import { Releases } from "./Releases";
|
import { Releases } from "./Releases";
|
||||||
|
import { basename } from "path";
|
||||||
|
|
||||||
export class Action {
|
export class Action {
|
||||||
private inputs: Inputs
|
private inputs: Inputs
|
||||||
@@ -9,4 +10,22 @@ export class Action {
|
|||||||
this.inputs = inputs
|
this.inputs = inputs
|
||||||
this.releases = releases
|
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 { Context } from "@actions/github/lib/context";
|
||||||
import { readFileSync, statSync } from 'fs';
|
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
|
private context: Context
|
||||||
|
|
||||||
constructor(context: Context) {
|
constructor(context: Context) {
|
||||||
@@ -26,22 +38,27 @@ export class Inputs {
|
|||||||
return statSync(this.artifact).size
|
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 {
|
get commit(): string {
|
||||||
return core.getInput('commit')
|
return core.getInput('commit')
|
||||||
}
|
}
|
||||||
|
|
||||||
get description(): string {
|
get draft(): boolean {
|
||||||
const description = core.getInput('description')
|
const draft = core.getInput('draft')
|
||||||
if (description) {
|
return draft == 'true'
|
||||||
return description
|
|
||||||
}
|
|
||||||
|
|
||||||
const descriptionFile = core.getInput('descriptionFile')
|
|
||||||
if (descriptionFile) {
|
|
||||||
return this.stringFromFile(descriptionFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get name(): string {
|
get name(): string {
|
||||||
@@ -72,7 +89,7 @@ export class Inputs {
|
|||||||
return core.getInput('token', { required: true })
|
return core.getInput('token', { required: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
private stringFromFile(path: string): string {
|
stringFromFile(path: string): string {
|
||||||
return readFileSync(path, 'utf-8')
|
return readFileSync(path, 'utf-8')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
import * as github from '@actions/github';
|
import * as github from '@actions/github';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import { Inputs } from './Inputs';
|
import { Inputs, CoreInputs } from './Inputs';
|
||||||
import { Releases } from './Releases';
|
import { Releases, GithubReleases } from './Releases';
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
try {
|
try {
|
||||||
const token = core.getInput('token');
|
const token = core.getInput('token');
|
||||||
const context = github.context
|
const context = github.context
|
||||||
const git = new github.GitHub(token);
|
const git = new github.GitHub(token);
|
||||||
const releases = new Releases(context, git)
|
const releases = new GithubReleases(context, git)
|
||||||
const inputs = new Inputs(context)
|
const inputs = new CoreInputs(context)
|
||||||
await releases.create(inputs.tag)
|
await releases.create(inputs.tag)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
core.warning(error)
|
core.warning(error)
|
||||||
|
|||||||
@@ -2,7 +2,25 @@ import { Context } from "@actions/github/lib/context";
|
|||||||
import { GitHub } from "@actions/github";
|
import { GitHub } from "@actions/github";
|
||||||
import { AnyResponse, Response, ReposCreateReleaseResponse } from "@octokit/rest";
|
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
|
context: Context
|
||||||
git: GitHub
|
git: GitHub
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user