Skip to content

Commit 62375a2

Browse files
committed
add tests for validating blank inputs
1 parent 1f5e2e0 commit 62375a2

File tree

8 files changed

+112
-39
lines changed

8 files changed

+112
-39
lines changed

.github/linters/.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ rules:
4747
'semi': 'off',
4848
'@typescript-eslint/array-type': 'error',
4949
'@typescript-eslint/await-thenable': 'error',
50-
'@typescript-eslint/ban-ts-comment': 'error',
50+
'@typescript-eslint/ban-ts-comment': 'off',
5151
'@typescript-eslint/consistent-type-assertions': 'error',
5252
'@typescript-eslint/explicit-member-accessibility':
5353
[ 'error', { 'accessibility': 'no-public' } ],

__tests__/get-current-version.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44

55
import { getCurrentVersion } from '../src/get-current-version';
6+
// @ts-ignore
7+
import { mockInputs } from './mock-inputs';
68

79
function mockVersion(version: string) {
810
jest.spyOn(global, 'fetch').mockResolvedValue({
@@ -18,6 +20,10 @@ afterEach(() => {
1820
});
1921

2022
describe('get current version', () => {
23+
beforeEach(() => {
24+
mockInputs('1', '0', 'false');
25+
});
26+
2127
it('should return current version', async () => {
2228
mockVersion('1.0.0');
2329
const result = await getCurrentVersion('token');

__tests__/main.test.ts

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { run } from '../src/main';
22
import * as core from '@actions/core';
3+
// @ts-ignore
4+
import { mockInputs } from './mock-inputs';
35

46
function mockCurrentVersion(version: string) {
57
jest.spyOn(global, 'fetch').mockResolvedValue({
@@ -9,32 +11,6 @@ function mockCurrentVersion(version: string) {
911
ok: true
1012
} as any as Promise<Response>);
1113
}
12-
13-
function mockInputs(
14-
majorVersion: string,
15-
minorVersion: string,
16-
publishBeta: string
17-
) {
18-
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
19-
if (name === 'org') {
20-
return 'org';
21-
}
22-
if (name === 'packageName') {
23-
return 'packageName';
24-
}
25-
if (name === 'minorVersion') {
26-
return minorVersion;
27-
}
28-
if (name === 'majorVersion') {
29-
return majorVersion;
30-
}
31-
if (name === 'publishBeta') {
32-
return publishBeta;
33-
}
34-
throw new Error(`Unexpected input ${name}`);
35-
});
36-
}
37-
3814
afterEach(() => {
3915
jest.restoreAllMocks();
4016
});
@@ -50,13 +26,49 @@ describe('get current version', () => {
5026
process.env = OLD_ENV; // Restore old environment
5127
});
5228

53-
it('should throw if no token is given', async () => {
54-
process.env = { ...OLD_ENV, ...{ GITHUB_TOKEN: undefined } };
55-
jest.spyOn(core, 'setFailed');
56-
await run();
57-
expect(core.setFailed).toHaveBeenCalledWith(
58-
'GITHUB_TOKEN not set, please set the GITHUB_TOKEN environment variable to secrets.GITHUB_TOKEN'
59-
);
29+
describe('inputs should be validated', () => {
30+
it('should throw if no token is given', async () => {
31+
process.env = { ...OLD_ENV, ...{ GITHUB_TOKEN: undefined } };
32+
jest.spyOn(core, 'setFailed');
33+
await run();
34+
expect(core.setFailed).toHaveBeenCalledWith(
35+
'GITHUB_TOKEN not set, please set the GITHUB_TOKEN environment variable to secrets.GITHUB_TOKEN'
36+
);
37+
});
38+
39+
it('should throw if no org is given', async () => {
40+
mockInputs('1', '0', 'false', '');
41+
jest.spyOn(core, 'setFailed');
42+
await run();
43+
expect(core.setFailed).toHaveBeenCalledWith('Input org is not set');
44+
});
45+
46+
it('should throw if no package name is given', async () => {
47+
mockInputs('1', '0', 'false', 'org', '');
48+
jest.spyOn(core, 'setFailed');
49+
await run();
50+
expect(core.setFailed).toHaveBeenCalledWith(
51+
'Input packageName is not set'
52+
);
53+
});
54+
55+
it('should throw if no major version is given', async () => {
56+
mockInputs('', '0', 'false');
57+
jest.spyOn(core, 'setFailed');
58+
await run();
59+
expect(core.setFailed).toHaveBeenCalledWith(
60+
'Input majorVersion is not set'
61+
);
62+
});
63+
64+
it('should throw if no minor version is given', async () => {
65+
mockInputs('1', '', 'false');
66+
jest.spyOn(core, 'setFailed');
67+
await run();
68+
expect(core.setFailed).toHaveBeenCalledWith(
69+
'Input minorVersion is not set'
70+
);
71+
});
6072
});
6173

6274
describe('when current version is empty', () => {

__tests__/mock-inputs.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as core from '@actions/core';
2+
3+
export function mockInputs(
4+
majorVersion: string,
5+
minorVersion: string,
6+
publishBeta: string,
7+
org = 'org',
8+
packageName = 'packageName'
9+
) {
10+
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
11+
if (name === 'org') {
12+
return org;
13+
}
14+
if (name === 'packageName') {
15+
return packageName;
16+
}
17+
if (name === 'minorVersion') {
18+
return minorVersion;
19+
}
20+
if (name === 'majorVersion') {
21+
return majorVersion;
22+
}
23+
if (name === 'publishBeta') {
24+
return publishBeta;
25+
}
26+
throw new Error(`Unexpected input ${name}`);
27+
});
28+
}

dist/index.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@
6969
},
7070
"devDependencies": {
7171
"@types/jest": "^29.5.6",
72-
"@types/node": "^20.8.7",
73-
"@typescript-eslint/eslint-plugin": "^6.8.0",
74-
"@typescript-eslint/parser": "^6.8.0",
72+
"@types/node": "^20.8.8",
73+
"@typescript-eslint/eslint-plugin": "^6.9.0",
74+
"@typescript-eslint/parser": "^6.9.0",
7575
"@vercel/ncc": "^0.38.1",
76-
"eslint": "^8.51.0",
76+
"eslint": "^8.52.0",
7777
"eslint-plugin-github": "^4.10.1",
78-
"eslint-plugin-jest": "^27.4.2",
78+
"eslint-plugin-jest": "^27.4.3",
7979
"eslint-plugin-jsonc": "^2.10.0",
8080
"eslint-plugin-prettier": "^5.0.1",
8181
"jest": "^29.7.0",

src/get-current-version.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ interface GithubPackageVersion {
77

88
export async function getCurrentVersion(token: string): Promise<string> {
99
const org: string = core.getInput('org');
10+
if (org === '') {
11+
throw new Error(`Input org is not set`);
12+
}
13+
1014
const packageName: string = core.getInput('packageName');
15+
if (packageName === '') {
16+
throw new Error(`Input packageName is not set`);
17+
}
1118

1219
const response: Response = await fetch(
1320
`https://api.github.com/orgs/${org}/packages/nuget/${packageName}/versions`,

src/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ export async function run(): Promise<void> {
2424
);
2525
}
2626
const minorVersion = core.getInput('minorVersion');
27+
if (minorVersion === '') {
28+
throw new Error(`Input minorVersion is not set`);
29+
}
30+
2731
const majorVersion = core.getInput('majorVersion');
32+
if (majorVersion === '') {
33+
throw new Error(`Input majorVersion is not set`);
34+
}
35+
2836
const publishBeta = core.getInput('publishBeta').toLowerCase() === 'true';
2937

3038
const currentVersion = await getCurrentVersion(token);

0 commit comments

Comments
 (0)