generated from actions/container-toolkit-action
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from Renato66/feat/tests
test: fix test for mock
- Loading branch information
Showing
23 changed files
with
357 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
"streetsidesoftware.code-spell-checker", | ||
"esbenp.prettier-vscode" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect, describe, test, mock, afterEach, jest } from 'bun:test' | ||
import * as core from '@actions/core' | ||
import { getConfigFile } from './getConfigFile' | ||
|
||
describe('getConfigFile', () => { | ||
afterEach(() => { | ||
mock.module('@actions/core', () => core) | ||
}) | ||
test('returns empty array when labels-not-allowed input is empty', () => { | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => undefined), | ||
getBooleanInput: jest.fn(() => undefined) | ||
})) | ||
const result1 = getConfigFile() | ||
expect(result1.labelsNotAllowed).toEqual([]) | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => '') | ||
})) | ||
const result2 = getConfigFile() | ||
expect(result2.labelsNotAllowed).toEqual([]) | ||
}) | ||
|
||
test('returns parsed array from labels-not-allowed input', () => { | ||
const labels = ['label1', 'label2'] | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => JSON.stringify(labels)) | ||
})) | ||
const result = getConfigFile() | ||
expect(result.labelsNotAllowed).toEqual(labels) | ||
}) | ||
|
||
test('throws error if labels-not-allowed input is not valid JSON', () => { | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => 'not-valid-json') | ||
})) | ||
expect(getConfigFile).toThrowError() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getInput } from './getInput' | ||
|
||
export const getConfigFile = () => { | ||
const labelsNotAllowed = getInput<string[]>('labels-not-allowed', []) | ||
const defaultLabels = getInput<string[]>('default-labels', []) | ||
const labelsSynonyms = getInput<Record<string, string[]>>( | ||
'labels-synonyms', | ||
{} | ||
) | ||
const ignoreComments = getInput('ignore-comments', true) | ||
|
||
return { | ||
labelsNotAllowed, | ||
defaultLabels, | ||
labelsSynonyms, | ||
ignoreComments | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { expect, describe, test, mock, afterEach, jest } from 'bun:test' | ||
import * as core from '@actions/core' | ||
import { getInput } from './getInput' | ||
|
||
describe('getInput', () => { | ||
afterEach(() => { | ||
mock.module('@actions/core', () => core) | ||
}) | ||
test('returns default value when input is empty/undefined', () => { | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => undefined) | ||
})) | ||
const result1 = getInput<string[]>('input', []) | ||
expect(result1).toEqual([]) | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => '') | ||
})) | ||
const result2 = getInput<string[]>('input', []) | ||
expect(result2).toEqual([]) | ||
}) | ||
|
||
test('returns parsed array from input', () => { | ||
const labels = ['label1', 'label2'] | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => JSON.stringify(labels)) | ||
})) | ||
const result = getInput<string[]>('input', []) | ||
expect(result).toEqual(labels) | ||
}) | ||
|
||
test('throws error if input is not valid JSON', () => { | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => 'not-valid-json') | ||
})) | ||
expect(() => getInput<string[]>('input', [])).toThrowError() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as core from '@actions/core' | ||
|
||
const getObjectInput = <T>(field: string, fallback: T): T => { | ||
const inputString = core.getInput(field) | ||
if (!inputString) return fallback | ||
try { | ||
return JSON.parse(inputString) as T | ||
} catch (error: any) { | ||
throw new Error(`"${field}": ${error.message}`) | ||
} | ||
} | ||
const getBooleanInput = (field: string, fallback: boolean): boolean => { | ||
if ([undefined, ''].includes(core.getInput(field, { trimWhitespace: true }))) | ||
return fallback | ||
return core.getBooleanInput(field) | ||
} | ||
|
||
export const getInput = <T>(field: string, fallback: T): T => { | ||
switch (typeof fallback) { | ||
case 'object': | ||
return getObjectInput(field, fallback) | ||
case 'boolean': | ||
return getBooleanInput(field, fallback) as T | ||
default: | ||
return core.getInput(field) as T | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect, describe, test } from 'bun:test' | ||
import { removeLabelsNotAllowed } from './removeLabelsNotAllowed' | ||
|
||
describe('removeLabelsNotAllowed function', () => { | ||
test('should return the same labels if labels-not-allowed is not set', () => { | ||
const labels = ['Label1', 'Label2', 'Label3'] | ||
const result = removeLabelsNotAllowed(labels, []) | ||
expect(result).toEqual(labels) | ||
}) | ||
|
||
test('should remove labels that are not allowed', () => { | ||
const labels = ['Label1', 'Label2', 'Label3'] | ||
const result = removeLabelsNotAllowed(labels, ['Label2']) | ||
expect(result).toEqual(['Label1', 'Label3']) | ||
}) | ||
|
||
test('should handle case-insensitive labels', () => { | ||
const labels = ['label1', 'Label2', 'label3'] | ||
const result = removeLabelsNotAllowed(labels, ['LABEL1']) | ||
expect(result).toEqual(['Label2', 'label3']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const removeLabelsNotAllowed = ( | ||
labels: string[], | ||
labelsNotAllowed: string[] | ||
): string[] => { | ||
if (!labelsNotAllowed || !labelsNotAllowed.length) { | ||
return labels | ||
} | ||
const labelsLC = labels.map((elem) => elem.toLocaleLowerCase()) | ||
const labelsNotAllowedLC = new Set( | ||
labelsNotAllowed.map((elem) => elem.toLocaleLowerCase()) | ||
) | ||
return labels.filter((_, index) => !labelsNotAllowedLC.has(labelsLC[index])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import { run } from './runner'; | ||
run() | ||
import { run } from './runner' | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,52 @@ | ||
import { expect, describe, test, mock, jest } from 'bun:test'; | ||
import * as core from '@actions/core'; | ||
import '@actions/github'; | ||
import { run } from './runner'; | ||
import { expect, describe, test, mock, jest } from 'bun:test' | ||
import * as core from '@actions/core' | ||
import '@actions/github' | ||
import { run } from './runner' | ||
|
||
// Mock core functions | ||
// // Mock core functions | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => 'mocked-token'), | ||
getInput: jest.fn((input) => { | ||
return input === 'repo-token' ? 'mockedToken' : undefined | ||
}), | ||
info: jest.fn(), | ||
startGroup: jest.fn(), | ||
endGroup: jest.fn(), | ||
setFailed: jest.fn(), | ||
})); | ||
setFailed: jest.fn() | ||
})) | ||
|
||
// Mock github context | ||
const mockIssue = { number: 123, body: 'Mocked issue body' }; | ||
const mockIssue = { number: 123, body: 'Mocked issue body label1' } | ||
const mockContext = { | ||
payload: { | ||
issue: mockIssue, | ||
}, | ||
}; | ||
issue: mockIssue | ||
} | ||
} | ||
mock.module('@actions/github', () => ({ | ||
getOctokit: jest.fn(), | ||
context: mockContext, | ||
})); | ||
context: mockContext | ||
})) | ||
|
||
// Mock service functions | ||
// // Mock service functions | ||
const addLabelsSpy = jest.fn() | ||
mock.module('./service/github', () => ({ | ||
getRepoLabels: jest.fn(() => []), | ||
addLabels: addLabelsSpy, | ||
})); | ||
mock.module('./domain/labelsNotAllowed', () => ({ | ||
removeLabelsNotAllowed: jest.fn((labels: string[]) => labels), | ||
})); | ||
mock.module('./scraper/text', () => ({ | ||
getIssueLabels: jest.fn(() => []), | ||
})); | ||
getRepoLabels: jest.fn(() => ['label1']), | ||
addLabels: addLabelsSpy | ||
})) | ||
|
||
describe('run function', () => { | ||
test('should run successfully without throwing errors', async () => { | ||
await run(); | ||
expect(core.setFailed).not.toHaveBeenCalled(); | ||
}); | ||
test('should add if any found label', async () => { | ||
const issueLabels = ['label', 'label2'] | ||
mock.module('./scraper/text', () => ({ | ||
getIssueLabels: jest.fn(() => issueLabels), | ||
})); | ||
await run(); | ||
// TODO: fix this test | ||
// expect(addLabelsSpy).toHaveBeenCalledWith( | ||
// expect.any(Object), // octokit | ||
// 123, // issue number | ||
// issueLabels // issue labels | ||
// ); | ||
}); | ||
}); | ||
await run() | ||
expect(core.setFailed).not.toHaveBeenCalled() | ||
expect(addLabelsSpy).toHaveBeenCalled() | ||
}) | ||
test('should add if any found label', async () => { | ||
mock.module('@actions/core', () => ({ | ||
getInput: jest.fn(() => undefined), | ||
info: jest.fn(), | ||
startGroup: jest.fn(), | ||
endGroup: jest.fn(), | ||
setFailed: jest.fn() | ||
})) | ||
expect(async () => await run()).toThrowError() | ||
}) | ||
}) |
Oops, something went wrong.