Skip to content

Commit

Permalink
refactor: use getMultilineInput() (#495)
Browse files Browse the repository at this point in the history
* refactor: use getMultilineInput()
* chore(deps-dev): remove unused packages
* build: remove typedef import
  • Loading branch information
nogic1008 authored Jun 11, 2021
1 parent 06542fa commit bacc22b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 66 deletions.
49 changes: 10 additions & 39 deletions __tests__/input-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { getInput } from '@actions/core'
import { readFileSync } from 'fs'
import { load as yamlLoad } from 'js-yaml'
import { join as pathJoin } from 'path'
import { getInput, getMultilineInput } from '@actions/core'
import { mocked } from 'ts-jest/utils'

import { getInputParams } from '../src/input-helper'
Expand All @@ -12,25 +9,20 @@ jest.mock('@actions/core')
describe('input-helper.ts', () => {
describe('getInputParams()', () => {
const createMockedGetInput =
(token: string, imageUrl: string, searchPattern?: string | undefined) =>
(token: string, imageUrl: string) =>
(name: string): string =>
name === 'token'
? token
: name === 'image-url'
? imageUrl
: name === 'search-pattern'
? searchPattern ?? ''
: ''
beforeEach(() => mocked(getInput).mockReset())
name === 'token' ? token : name === 'image-url' ? imageUrl : ''
beforeEach(() => {
mocked(getInput).mockReset()
mocked(getMultilineInput).mockReset()
})

test('returns getInput() values', () => {
// Arrange
const token = random(10)
const imageUrl = random(30)
const searchPattern = '^LGTM$\n^lgtm$'
mocked(getInput).mockImplementation(
createMockedGetInput(token, imageUrl, searchPattern)
)
mocked(getInput).mockImplementation(createMockedGetInput(token, imageUrl))
mocked(getMultilineInput).mockReturnValue(['^LGTM$', '^lgtm$'])

// Act
const params = getInputParams()
Expand All @@ -45,6 +37,7 @@ describe('input-helper.ts', () => {
const token = random(10)
const imageUrl = random(30)
mocked(getInput).mockImplementation(createMockedGetInput(token, imageUrl))
mocked(getMultilineInput).mockReturnValue([])

// Act
const params = getInputParams()
Expand All @@ -54,27 +47,5 @@ describe('input-helper.ts', () => {
expect(params.imageUrl).toBe(imageUrl)
expect(params.searchPattern).toStrictEqual([/^(lgtm|LGTM)$/m])
})
test('uses all input parameters defined by action.yml', () => {
// Arrange
// Load action.yml settings
const path = pathJoin(__dirname, '..', 'action.yml')
const yamlText = readFileSync(path, 'utf8')
const actionSettings = yamlLoad(yamlText) as {
inputs: Record<string, { required?: boolean }>
}
const expectedInputs = Object.entries(actionSettings.inputs).map(
([key, { required }]) => [key, required ? { required } : undefined]
)
mocked(getInput).mockReturnValue('1\n2\n3')

// Act
getInputParams()

// Assert
expect(getInput).toHaveBeenCalledTimes(expectedInputs.length)
for (const [key, value] of expectedInputs) {
expect(getInput).toHaveBeenCalledWith(key, value)
}
})
})
})
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"devDependencies": {
"@octokit/webhooks-types": "^3.77.1",
"@types/jest": "^26.0.23",
"@types/js-yaml": "^4.0.1",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"@vercel/ncc": "^0.28.6",
Expand All @@ -50,7 +49,6 @@
"eslint-plugin-simple-import-sort": "^7.0.0",
"husky": "^6.0.0",
"jest": "^27.0.4",
"js-yaml": "^4.1.0",
"lint-staged": "^11.0.0",
"npm-run-all2": "^5.0.2",
"prettier": "^2.3.1",
Expand Down
16 changes: 9 additions & 7 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInput } from '@actions/core'
import { getInput, getMultilineInput } from '@actions/core'

type InputParameter = {
/** Your GitHub Token */
Expand All @@ -13,12 +13,14 @@ type InputParameter = {

/** Gets the value of inputs. */
export function getInputParams(): InputParameter {
const token = getInput('token')
const imageUrl = getInput('image-url', { required: true })
const pattern = getMultilineInput('search-pattern')
return {
token: getInput('token', undefined),
imageUrl: getInput('image-url', { required: true }),
searchPattern: (getInput('search-pattern', undefined) || '^(lgtm|LGTM)$')
.split('\n')
.filter((x) => x !== '')
.map((x) => new RegExp(x, 'm'))
token,
imageUrl,
searchPattern: pattern.length
? pattern.map((x) => new RegExp(x, 'm'))
: [/^(lgtm|LGTM)$/m]
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"esModuleInterop": true
},
"exclude": ["node_modules", "__tests__", "jest.config.ts"],
"types": ["jest", "js-yaml", "node"]
"types": ["jest"]
}
17 changes: 0 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,6 @@
jest-diff "^26.0.0"
pretty-format "^26.0.0"

"@types/js-yaml@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.1.tgz#5544730b65a480b18ace6b6ce914e519cec2d43b"
integrity sha512-xdOvNmXmrZqqPy3kuCQ+fz6wA0xU5pji9cd1nDrflWaAWtYLLGk5ykW0H6yg5TVyehHP1pfmuuSaZkhP+kspVA==

"@types/json-schema@^7.0.7":
version "7.0.7"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
Expand Down Expand Up @@ -987,11 +982,6 @@ argparse@^1.0.7:
dependencies:
sprintf-js "~1.0.2"

argparse@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

array-union@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d"
Expand Down Expand Up @@ -2490,13 +2480,6 @@ js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
dependencies:
argparse "^2.0.1"

jsdom@^16.6.0:
version "16.6.0"
resolved "https://registry.yarnpkg.com/jsdom/-/jsdom-16.6.0.tgz#f79b3786682065492a3da6a60a4695da983805ac"
Expand Down

0 comments on commit bacc22b

Please sign in to comment.