Skip to content

Commit

Permalink
feat: add outputs and better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Aug 12, 2020
1 parent 4d588a6 commit 4e513b6
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 125 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
10 changes: 9 additions & 1 deletion __tests__/getConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import getConfig from '../src/getConfig'
import * as path from 'path'
import * as fs from 'fs'
import nock from 'nock'
import fetch from 'node-fetch'

describe('getConfig', () => {
Expand All @@ -16,10 +17,17 @@ describe('getConfig', () => {
// TODO: change this to point to the new relic repo when it goes public
const url =
'https://raw.githubusercontent.com/aperture-science-incorporated/.github/master/repolinter.json'
const expected = await (await fetch(url)).json()
const filepath = path.resolve(__dirname, 'testconfig.json')
const expected = JSON.parse(await fs.promises.readFile(filepath, 'utf8'))
const scope = nock('https://raw.githubusercontent.com')
.get('/aperture-science-incorporated/.github/master/repolinter.json')
.replyWithFile(200, filepath)

const res = await getConfig({configUrl: url})

expect(res).toMatchObject(expected)

scope.done()
})

test('getConfig fails with an invalid file', async () => {
Expand Down
102 changes: 102 additions & 0 deletions __tests__/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import * as cp from 'child_process'
import * as path from 'path'
import {Inputs} from '../src/inputs'

async function execAsync(
command: string,
opts: cp.ExecOptions
): Promise<{out: string; err: string; code: number}> {
return new Promise((resolve, reject) => {
cp.exec(command, opts, (err, outstd, errstd) =>
err !== null && err.code === undefined
? reject(err)
: resolve({
out: outstd,
err: errstd,
code: err !== null ? (err.code as number) : 0
})
)
})
}

async function runAction(
env: NodeJS.ProcessEnv
): Promise<{out: string; err: string; code: number}> {
const ip = path.join(__dirname, '..', 'dist', 'index.js')
return execAsync(`node ${ip}`, {env})
}

function getInputName(input: string): string {
return `INPUT_${input.replace(/ /g, '_').toUpperCase()}`
}

describe('integration', () => {
beforeEach(() => {
process.env[getInputName(Inputs.REPO)] = 'newrelic/repolinter-action'
process.env[getInputName(Inputs.OUTPUT_TYPE)] = 'exit-code'
process.env[getInputName(Inputs.OUTPUT_NAME)] = 'Open Source Policy Issues'
process.env[getInputName(Inputs.LABEL_NAME)] = 'repolinter'
process.env[getInputName(Inputs.LABEL_COLOR)] = 'fbca04'
process.env['GITHUB_ACTION'] = 'true'
delete process.env[getInputName(Inputs.CONFIG_FILE)]
delete process.env[getInputName(Inputs.CONFIG_URL)]
delete process.env['GITHUB_TOKEN']
delete process.env['INPUT_GITHUB_TOKEN']
delete process.env[getInputName(Inputs.TOKEN)]
})

// TODO: fix tests to reflect new exit code logic
// TODO: add tests for outputs

test('runs a failing test config', async () => {
process.env[getInputName(Inputs.CONFIG_FILE)] = path.resolve(
__dirname,
'testconfig.json'
)

const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).not.toEqual(0)
// TODO: outputs
expect(out).toContain('testconfig.json')
expect(out).not.toContain('undefined')
})

test('runs a URL config', async () => {
process.env[getInputName(Inputs.CONFIG_URL)] =
'https://raw.githubusercontent.com/aperture-science-incorporated/.github/master/repolinter.json'

const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).not.toEqual(0)
expect(out).toContain(
'https://raw.githubusercontent.com/aperture-science-incorporated/.github/master/repolinter.json'
)
expect(out).not.toContain('undefined')
})

test('runs the default config', async () => {
const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).not.toEqual(0)
expect(out).toContain('default')
expect(out).not.toContain('undefined')
})

test('runs a passing config', async () => {
process.env[getInputName(Inputs.CONFIG_FILE)] = path.resolve(
__dirname,
'passingtestconfig.json'
)

const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).toEqual(0)
expect(out).toContain('passingtestconfig.json')
expect(out).not.toContain('undefined')
})
})
Loading

0 comments on commit 4e513b6

Please sign in to comment.