Skip to content

Commit

Permalink
add tests for validating blank inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
rexebin committed Oct 24, 2023
1 parent 1f5e2e0 commit 62375a2
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ rules:
'semi': 'off',
'@typescript-eslint/array-type': 'error',
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/ban-ts-comment': 'error',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/consistent-type-assertions': 'error',
'@typescript-eslint/explicit-member-accessibility':
[ 'error', { 'accessibility': 'no-public' } ],
Expand Down
6 changes: 6 additions & 0 deletions __tests__/get-current-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/

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

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

describe('get current version', () => {
beforeEach(() => {
mockInputs('1', '0', 'false');
});

it('should return current version', async () => {
mockVersion('1.0.0');
const result = await getCurrentVersion('token');
Expand Down
78 changes: 45 additions & 33 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { run } from '../src/main';
import * as core from '@actions/core';
// @ts-ignore
import { mockInputs } from './mock-inputs';

function mockCurrentVersion(version: string) {
jest.spyOn(global, 'fetch').mockResolvedValue({
Expand All @@ -9,32 +11,6 @@ function mockCurrentVersion(version: string) {
ok: true
} as any as Promise<Response>);
}

function mockInputs(
majorVersion: string,
minorVersion: string,
publishBeta: string
) {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'org') {
return 'org';
}
if (name === 'packageName') {
return 'packageName';
}
if (name === 'minorVersion') {
return minorVersion;
}
if (name === 'majorVersion') {
return majorVersion;
}
if (name === 'publishBeta') {
return publishBeta;
}
throw new Error(`Unexpected input ${name}`);
});
}

afterEach(() => {
jest.restoreAllMocks();
});
Expand All @@ -50,13 +26,49 @@ describe('get current version', () => {
process.env = OLD_ENV; // Restore old environment
});

it('should throw if no token is given', async () => {
process.env = { ...OLD_ENV, ...{ GITHUB_TOKEN: undefined } };
jest.spyOn(core, 'setFailed');
await run();
expect(core.setFailed).toHaveBeenCalledWith(
'GITHUB_TOKEN not set, please set the GITHUB_TOKEN environment variable to secrets.GITHUB_TOKEN'
);
describe('inputs should be validated', () => {
it('should throw if no token is given', async () => {
process.env = { ...OLD_ENV, ...{ GITHUB_TOKEN: undefined } };
jest.spyOn(core, 'setFailed');
await run();
expect(core.setFailed).toHaveBeenCalledWith(
'GITHUB_TOKEN not set, please set the GITHUB_TOKEN environment variable to secrets.GITHUB_TOKEN'
);
});

it('should throw if no org is given', async () => {
mockInputs('1', '0', 'false', '');
jest.spyOn(core, 'setFailed');
await run();
expect(core.setFailed).toHaveBeenCalledWith('Input org is not set');
});

it('should throw if no package name is given', async () => {
mockInputs('1', '0', 'false', 'org', '');
jest.spyOn(core, 'setFailed');
await run();
expect(core.setFailed).toHaveBeenCalledWith(
'Input packageName is not set'
);
});

it('should throw if no major version is given', async () => {
mockInputs('', '0', 'false');
jest.spyOn(core, 'setFailed');
await run();
expect(core.setFailed).toHaveBeenCalledWith(
'Input majorVersion is not set'
);
});

it('should throw if no minor version is given', async () => {
mockInputs('1', '', 'false');
jest.spyOn(core, 'setFailed');
await run();
expect(core.setFailed).toHaveBeenCalledWith(
'Input minorVersion is not set'
);
});
});

describe('when current version is empty', () => {
Expand Down
28 changes: 28 additions & 0 deletions __tests__/mock-inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as core from '@actions/core';

export function mockInputs(
majorVersion: string,
minorVersion: string,
publishBeta: string,
org = 'org',
packageName = 'packageName'
) {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'org') {
return org;
}
if (name === 'packageName') {
return packageName;
}
if (name === 'minorVersion') {
return minorVersion;
}
if (name === 'majorVersion') {
return majorVersion;
}
if (name === 'publishBeta') {
return publishBeta;
}
throw new Error(`Unexpected input ${name}`);
});
}
12 changes: 12 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@
},
"devDependencies": {
"@types/jest": "^29.5.6",
"@types/node": "^20.8.7",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"@types/node": "^20.8.8",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"@vercel/ncc": "^0.38.1",
"eslint": "^8.51.0",
"eslint": "^8.52.0",
"eslint-plugin-github": "^4.10.1",
"eslint-plugin-jest": "^27.4.2",
"eslint-plugin-jest": "^27.4.3",
"eslint-plugin-jsonc": "^2.10.0",
"eslint-plugin-prettier": "^5.0.1",
"jest": "^29.7.0",
Expand Down
7 changes: 7 additions & 0 deletions src/get-current-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ interface GithubPackageVersion {

export async function getCurrentVersion(token: string): Promise<string> {
const org: string = core.getInput('org');
if (org === '') {
throw new Error(`Input org is not set`);
}

const packageName: string = core.getInput('packageName');
if (packageName === '') {
throw new Error(`Input packageName is not set`);
}

const response: Response = await fetch(
`https://api.github.com/orgs/${org}/packages/nuget/${packageName}/versions`,
Expand Down
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ export async function run(): Promise<void> {
);
}
const minorVersion = core.getInput('minorVersion');
if (minorVersion === '') {
throw new Error(`Input minorVersion is not set`);
}

const majorVersion = core.getInput('majorVersion');
if (majorVersion === '') {
throw new Error(`Input majorVersion is not set`);
}

const publishBeta = core.getInput('publishBeta').toLowerCase() === 'true';

const currentVersion = await getCurrentVersion(token);
Expand Down

0 comments on commit 62375a2

Please sign in to comment.