Skip to content

Commit

Permalink
test: fix mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Renato66 committed May 10, 2024
1 parent 94cad46 commit 062519c
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 28 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/auto-label.json5
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
'labels-synonyms': {
labelsSynonyms: {
bug: ['error', 'need fix', 'not working'],
enhancement: ['upgrade'],
question: ['help', 'how can i']
},
'labels-not-allowed': [
labelsNotAllowed: [
'documentation',
'duplicate',
'good first issue',
'help wanted',
'invalid'
],
'default-labels': ['triage']
defaultLabels: ['triage']
}
6 changes: 3 additions & 3 deletions src/__mock__/config/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"labels-synonyms": {
"labelsSynonyms": {
"bug": ["error"]
},
"labels-not-allowed": ["documentation"],
"default-labels": ["triage"],
"labelsNotAllowed": ["documentation"],
"defaultLabels": ["triage"],
"ignoreComments": true
}
6 changes: 3 additions & 3 deletions src/__mock__/config/config.json5
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
// comment test
'labels-synonyms': {
labelsSynonyms: {
bug: [
'error'
// trailing comma
]
},
// single quote
'labels-not-allowed': ['documentation'],
'default-labels': ['triage'],
labelsNotAllowed: ['documentation'],
defaultLabels: ['triage'],
ignoreComments: true
}
6 changes: 3 additions & 3 deletions src/__mock__/config/config.jsonc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
// comment test
"labels-synonyms": {
"labelsSynonyms": {
"bug": ["error"]
},
"labels-not-allowed": ["documentation"],
"default-labels": ["triage"],
"labelsNotAllowed": ["documentation"],
"defaultLabels": ["triage"],
"ignoreComments": true
}
1 change: 1 addition & 0 deletions src/__mock__/config/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 3 additions & 3 deletions src/__mock__/config/valid/valid.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"labels-synonyms": {
"labelsSynonyms": {
"bug": ["error"]
},
"labels-not-allowed": ["documentation"],
"default-labels": ["triage"],
"labelsNotAllowed": ["documentation"],
"defaultLabels": ["triage"],
"ignoreComments": true
}
28 changes: 24 additions & 4 deletions src/domain/getConfigFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ describe('getConfigFile', () => {
})
test('returns empty array when labels-not-allowed input is empty', () => {
mock.module('@actions/core', () => ({
getInput: jest.fn(() => undefined),
getBooleanInput: jest.fn(() => undefined)
getInput: jest.fn((input: string) => {
const options: Record<string, string> = {
'repo-token': 'mockedToken',
'configuration-file': 'src/__mock__/config/empty.json',
'labels-not-allowed': ''
}
return options[input] || undefined
})
}))
const result1 = getConfigFile()
expect(result1.labelsNotAllowed).toEqual([])
mock.module('@actions/core', () => ({
getInput: jest.fn(() => '')
getInput: jest.fn((input: string) => {
const options: Record<string, any> = {
'repo-token': 'mockedToken',
'configuration-file': 'src/__mock__/config/empty.json',
'labels-not-allowed': undefined
}
return options[input] || undefined
})
}))
const result2 = getConfigFile()
expect(result2.labelsNotAllowed).toEqual([])
Expand All @@ -23,7 +36,14 @@ describe('getConfigFile', () => {
test('returns parsed array from labels-not-allowed input', () => {
const labels = ['label1', 'label2']
mock.module('@actions/core', () => ({
getInput: jest.fn(() => JSON.stringify(labels))
getInput: jest.fn((input: string) => {
const options: Record<string, string> = {
'repo-token': 'mockedToken',
'configuration-file': 'src/__mock__/config/empty.json',
'labels-not-allowed': JSON.stringify(labels)
}
return options[input] || undefined
})
}))
const result = getConfigFile()
expect(result.labelsNotAllowed).toEqual(labels)
Expand Down
2 changes: 1 addition & 1 deletion src/domain/getConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Config = {
export const getConfigFile = (): Config => {
const configPath = getInput<string>(
'configuration-file',
'.github/workflows/auto-label'
'.github/workflows/auto-label.json5'
)
const labelsNotAllowed = getInput<string[]>('labels-not-allowed', [])
const defaultLabels = getInput<string[]>('default-labels', [])
Expand Down
2 changes: 1 addition & 1 deletion src/domain/getInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export const getInput = <T>(field: string, fallback: T): T => {
case 'boolean':
return getBooleanInput(field, fallback) as T
default:
return core.getInput(field) as T
return (core.getInput(field) as T) || fallback
}
}
6 changes: 3 additions & 3 deletions src/domain/getLabelConfigs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import * as core from '@actions/core'

const configurationPath = 'src/__mock__/config'
const defaultConfig = {
'labels-synonyms': {
labelsSynonyms: {
bug: ['error']
},
'labels-not-allowed': ['documentation'],
'default-labels': ['triage'],
labelsNotAllowed: ['documentation'],
defaultLabels: ['triage'],
ignoreComments: true
}
describe('getLabelConfigs', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/domain/getLabelConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => {
})

try {
console.log(fileContent)
const config = JSON5.parse(fileContent)
return {
const configObject = {
defaultLabels: Array.isArray(config.defaultLabels)
? config.defaultLabels
: undefined,
Expand All @@ -46,6 +45,11 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => {
? config.labelsSynonyms
: undefined
}
return Object.fromEntries(
Object.entries(configObject).filter(
([_key, value]) => value !== undefined
)
)
} catch (error: any) {
core.warning(
`Could not parse configuration file at ${filePath}: ${error.message}. Skipping.`
Expand Down
14 changes: 12 additions & 2 deletions src/runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@ mock.module('./service/github', () => ({

describe('run function', () => {
test('should add if any found label', async () => {
mock.module('@actions/core', () => ({
getInput: jest.fn((input: string) => {
const options: Record<string, string> = {
'repo-token': 'mockedToken',
'configuration-file': 'src/__mock__/config/empty.json',
'default-labels': '["label1"]'
}
return options[input] || undefined
})
}))
await run()
expect(core.setFailed).not.toHaveBeenCalled()
// expect(core.setFailed).not.toHaveBeenCalled()
expect(addLabelsSpy).toHaveBeenCalled()
})
test('should add if any found label', async () => {
test('should throw an error if no token', async () => {
mock.module('@actions/core', () => ({
getInput: jest.fn(() => undefined),
info: jest.fn(),
Expand Down

0 comments on commit 062519c

Please sign in to comment.