Add mocks to tests

This commit is contained in:
Nick Cipollo
2019-08-26 20:35:39 -04:00
parent 4e291a9639
commit 028c2af361
7 changed files with 137 additions and 36 deletions

View File

@@ -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))
}
}
}

View File

@@ -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')
}
}

View File

@@ -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)

View File

@@ -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