Skip to content

Commit e5d2006

Browse files
committed
add tests to cover 100%
add case for not found
1 parent f48e1db commit e5d2006

15 files changed

Lines changed: 211 additions & 173 deletions

.github/linters/.eslintrc.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ rules:
5050
'@typescript-eslint/ban-ts-comment': 'error',
5151
'@typescript-eslint/consistent-type-assertions': 'error',
5252
'@typescript-eslint/explicit-member-accessibility':
53-
['error', { 'accessibility': 'no-public' }],
54-
# '@typescript-eslint/explicit-function-return-type':
55-
# ['error', { 'allowExpressions': true, 'allowTypedFunctionExpressions': true }],
56-
'@typescript-eslint/func-call-spacing': ['error', 'never'],
53+
[ 'error', { 'accessibility': 'no-public' } ],
54+
# '@typescript-eslint/explicit-function-return-type':
55+
# ['error', { 'allowExpressions': true, 'allowTypedFunctionExpressions': true }],
56+
'@typescript-eslint/func-call-spacing': [ 'error', 'never' ],
5757
'@typescript-eslint/no-array-constructor': 'error',
5858
'@typescript-eslint/no-empty-interface': 'error',
5959
'@typescript-eslint/no-explicit-any': 'off',
@@ -76,7 +76,6 @@ rules:
7676
'@typescript-eslint/promise-function-async': 'error',
7777
'@typescript-eslint/require-array-sort-compare': 'error',
7878
'@typescript-eslint/restrict-plus-operands': 'error',
79-
'@typescript-eslint/semi': ['error', 'never'],
8079
'@typescript-eslint/space-before-function-paren': 'off',
8180
'@typescript-eslint/type-annotation-spacing': 'error',
8281
'@typescript-eslint/unbound-method': 'error'

.github/linters/tsconfig.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
"compilerOptions": {
55
"noEmit": true
66
},
7-
"include": ["../../__tests__/**/*", "../../src/**/*"],
8-
"exclude": ["../../dist", "../../node_modules", "../../coverage", "*.json"]
7+
"include": [
8+
"../../__tests__/**/*",
9+
"../../src/**/*"
10+
],
11+
"exclude": [
12+
"../../dist",
13+
"../../node_modules",
14+
"../../coverage",
15+
"*.json"
16+
]
917
}

.prettierrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"printWidth": 80,
33
"tabWidth": 2,
44
"useTabs": false,
5-
"semi": false,
5+
"semi": true,
66
"singleQuote": true,
77
"quoteProps": "as-needed",
88
"jsxSingleQuote": false,

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
# GitHub Action for calculate next nuget version from Github Packages
22

3+
[![GitHub Super-Linter](https://github.com/rexebin/next-nuget-package-version/actions/workflows/linter.yml/badge.svg)](https://github.com/super-linter/super-linter)
34
![CI](https://github.com/rexebin/next-nuget-package-version/actions/workflows/ci.yml/badge.svg)
45

5-
This github action calls GitHub API `https://api.github.com/orgs/${org}/packages/nuget/${packageName}/versions` with `${{ secrets.GITHUB_TOKEN }}`, to get the latest version and calculate the next version number from inputs such as:
6+
This github action calls GitHub API `https://api.github.com/orgs/${org}/packages/nuget/${packageName}/versions`
7+
with `${{ secrets.GITHUB_TOKEN }}`, to get the latest version and calculate the next version number from inputs such as:
68

79
1. main version
810
2. minor version
911
3. publish beta or not
1012

1113
## Version Calculations
1214

13-
|Main Version|Minor Version|Last Version|Publish Beta|Next Version| Note
14-
|---------|------|----|----|-|-
15-
|1|0|''|true|1.0.0-beta.1| first version
16-
|1|0|''|false|1.0.0|first version
17-
|1|0|'1.0.1'|true|1.0.2.beta.1| create beta 1 for next patch version
18-
|1|0|'1.0.1'|false|1.0.2| increment patch version
19-
|1|0|'1.0.1.beta.1'|true|1.0.1.beta.2| increment beta version
20-
|1|0|'1.0.1.beta.1'|false|1.0.1| create next version from beta
21-
|1|1|'1.0.20'|false|1.1.0|last version doesn't match input minor version, bump minor version
22-
|1|1|'1.0.20'|true|1.1.0.beta.1|last version doesn't match input minor version, bump minor version
23-
|2|0|'1.0.20.beta.1'|false|2.0.0|last version doesn't match input main version, bump main version
24-
|2|0|'1.0.20.beta.1'|false|2.0.0.beta.1|last version doesn't match input main version, bump main version
15+
| Main Version | Minor Version | Last Version | Publish Beta | Next Version | Note
16+
|--------------|---------------|-----------------|--------------|--------------|--------------------------------------------------------------------
17+
| 1 | 0 | '' | true | 1.0.0-beta.1 | first version
18+
| 1 | 0 | '' | false | 1.0.0 | first version
19+
| 1 | 0 | '1.0.1' | true | 1.0.2.beta.1 | create beta 1 for next patch version
20+
| 1 | 0 | '1.0.1' | false | 1.0.2 | increment patch version
21+
| 1 | 0 | '1.0.1.beta.1' | true | 1.0.1.beta.2 | increment beta version
22+
| 1 | 0 | '1.0.1.beta.1' | false | 1.0.1 | create next version from beta
23+
| 1 | 1 | '1.0.20' | false | 1.1.0 | last version doesn't match input minor version, bump minor version
24+
| 1 | 1 | '1.0.20' | true | 1.1.0.beta.1 | last version doesn't match input minor version, bump minor version
25+
| 2 | 0 | '1.0.20.beta.1' | false | 2.0.0 | last version doesn't match input main version, bump main version
26+
| 2 | 0 | '1.0.20.beta.1' | false | 2.0.0.beta.1 | last version doesn't match input main version, bump main version
2527

2628
## Usage
2729

__tests__/get-current-version.test.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,55 @@
22
* Unit tests for src/wait.ts
33
*/
44

5-
import { getCurrentVersion } from '../src/get-current-version'
5+
import { getCurrentVersion } from '../src/get-current-version';
66

77
function mockVersion(version: string) {
88
jest.spyOn(global, 'fetch').mockResolvedValue({
99
json: jest
1010
.fn()
1111
.mockResolvedValue(version === '' ? [] : [{ name: version }]),
1212
ok: true
13-
} as any as Promise<Response>)
13+
} as any as Promise<Response>);
1414
}
1515

1616
afterEach(() => {
17-
jest.restoreAllMocks()
18-
})
17+
jest.restoreAllMocks();
18+
});
1919

2020
describe('get current version', () => {
2121
it('should return current version', async () => {
22-
mockVersion('1.0.0')
23-
const result = await getCurrentVersion('token')
24-
expect(result).toBe('1.0.0')
25-
})
22+
mockVersion('1.0.0');
23+
const result = await getCurrentVersion('token');
24+
expect(result).toBe('1.0.0');
25+
});
2626

2727
it('should return empty string if no version', async () => {
28-
mockVersion('')
29-
const result = await getCurrentVersion('token')
30-
expect(result).toBe('')
31-
})
32-
})
28+
mockVersion('');
29+
const result = await getCurrentVersion('token');
30+
expect(result).toBe('');
31+
});
32+
33+
it('should return empty string if Github api returns 404 not found', async () => {
34+
jest.spyOn(global, 'fetch').mockResolvedValue({
35+
json: jest.fn(),
36+
ok: false,
37+
status: 404,
38+
statusText: 'Not Found'
39+
} as any as Promise<Response>);
40+
const result = await getCurrentVersion('token');
41+
expect(result).toBe('');
42+
});
43+
44+
it('should throw if Github API return not Ok and not 404 not found', async () => {
45+
jest.spyOn(global, 'fetch').mockResolvedValue({
46+
json: jest.fn(),
47+
ok: false,
48+
status: 500,
49+
statusText: 'Internal Server Error'
50+
} as any as Promise<Response>);
51+
const result = getCurrentVersion('token');
52+
await expect(result).rejects.toThrow(
53+
'Failed to get current version: Internal Server Error'
54+
);
55+
});
56+
});

__tests__/get-next-version.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22
* Unit tests for src/wait.ts
33
*/
44

5-
import { expect } from '@jest/globals'
6-
import { getNextVersion } from '../src/get-next-version'
5+
import { expect } from '@jest/globals';
6+
import { getNextVersion } from '../src/get-next-version';
77

88
describe('get next version', () => {
99
it('should increment beta version from last beta version', () => {
10-
const result = getNextVersion('1.0.0-beta.1', true)
11-
expect(result).toBe('1.0.0-beta.2')
12-
})
10+
const result = getNextVersion('1.0.0-beta.1', true);
11+
expect(result).toBe('1.0.0-beta.2');
12+
});
1313

1414
it('should publish main version from last beta', () => {
15-
const result = getNextVersion('1.0.0-beta.1', false)
16-
expect(result).toBe('1.0.0')
17-
})
15+
const result = getNextVersion('1.0.0-beta.1', false);
16+
expect(result).toBe('1.0.0');
17+
});
1818

1919
it('should increment main version and create beta 1 for next patch version', () => {
20-
const result = getNextVersion('1.0.0', true)
21-
expect(result).toBe('1.0.1-beta.1')
22-
})
20+
const result = getNextVersion('1.0.0', true);
21+
expect(result).toBe('1.0.1-beta.1');
22+
});
2323

2424
it('should increment main version from last main version', () => {
25-
const result = getNextVersion('1.0.0', false)
26-
expect(result).toBe('1.0.1')
27-
})
28-
})
25+
const result = getNextVersion('1.0.0', false);
26+
expect(result).toBe('1.0.1');
27+
});
28+
});

__tests__/index.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
* Unit tests for the action's entrypoint, src/index.ts
33
*/
44

5-
import * as main from '../src/main'
5+
import * as main from '../src/main';
66

77
// Mock the action's entrypoint
8-
const runMock = jest.spyOn(main, 'run').mockImplementation()
8+
const runMock = jest.spyOn(main, 'run').mockImplementation();
99

1010
describe('index', () => {
1111
it('calls run when imported', async () => {
1212
// eslint-disable-next-line @typescript-eslint/no-require-imports
13-
require('../src/index')
13+
require('../src/index');
1414

15-
expect(runMock).toHaveBeenCalled()
16-
})
17-
})
15+
expect(runMock).toHaveBeenCalled();
16+
});
17+
});

__tests__/main.test.ts

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { run } from '../src/main'
2-
import * as core from '@actions/core'
1+
import { run } from '../src/main';
2+
import * as core from '@actions/core';
33

44
function mockCurrentVersion(version: string) {
55
jest.spyOn(global, 'fetch').mockResolvedValue({
66
json: jest
77
.fn()
88
.mockResolvedValue(version === '' ? [] : [{ name: version }]),
99
ok: true
10-
} as any as Promise<Response>)
10+
} as any as Promise<Response>);
1111
}
1212

1313
function mockInputs(
@@ -17,38 +17,47 @@ function mockInputs(
1717
) {
1818
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
1919
if (name === 'org') {
20-
return 'org'
20+
return 'org';
2121
}
2222
if (name === 'packageName') {
23-
return 'packageName'
23+
return 'packageName';
2424
}
2525
if (name === 'minorVersion') {
26-
return minorVersion
26+
return minorVersion;
2727
}
2828
if (name === 'mainVersion') {
29-
return mainVersion
29+
return mainVersion;
3030
}
3131
if (name === 'publishBeta') {
32-
return publishBeta
32+
return publishBeta;
3333
}
34-
throw new Error(`Unexpected input ${name}`)
35-
})
34+
throw new Error(`Unexpected input ${name}`);
35+
});
3636
}
3737

3838
afterEach(() => {
39-
jest.restoreAllMocks()
40-
})
39+
jest.restoreAllMocks();
40+
});
4141

4242
describe('get current version', () => {
43-
const OLD_ENV = process.env
43+
const OLD_ENV = process.env;
4444
beforeEach(() => {
45-
jest.resetModules() // Most important - it clears the cache
46-
process.env = { ...OLD_ENV, ...{ GITHUB_TOKEN: 'mytoken' } } // Make a copy
47-
})
45+
jest.resetModules(); // Most important - it clears the cache
46+
process.env = { ...OLD_ENV, ...{ GITHUB_TOKEN: 'mytoken' } };
47+
});
4848

4949
afterAll(() => {
50-
process.env = OLD_ENV // Restore old environment
51-
})
50+
process.env = OLD_ENV; // Restore old environment
51+
});
52+
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+
);
60+
});
5261

5362
describe('when current version is empty', () => {
5463
it.each([
@@ -60,14 +69,14 @@ describe('get current version', () => {
6069
])(
6170
'when main version is %s, minor version is %s, beta: %s, should return %s',
6271
async (mainVersion, minorVersion, publishBeta, expectedVersion) => {
63-
mockCurrentVersion('')
64-
mockInputs(mainVersion, minorVersion, publishBeta)
65-
const spy = jest.spyOn(core, 'setOutput')
66-
await run()
67-
expect(spy).toHaveBeenCalledWith('version', expectedVersion)
72+
mockCurrentVersion('');
73+
mockInputs(mainVersion, minorVersion, publishBeta);
74+
const spy = jest.spyOn(core, 'setOutput');
75+
await run();
76+
expect(spy).toHaveBeenCalledWith('version', expectedVersion);
6877
}
69-
)
70-
})
78+
);
79+
});
7180

7281
describe('when current version does not match given main or minor version', () => {
7382
it.each([
@@ -78,14 +87,14 @@ describe('get current version', () => {
7887
])(
7988
'when current: %s, publish beta: %s, should return: %s',
8089
async (currentVersion, publishBeta, expectedVersion) => {
81-
mockCurrentVersion(currentVersion)
82-
mockInputs('1', '0', publishBeta)
83-
const spy = jest.spyOn(core, 'setOutput')
84-
await run()
85-
expect(spy).toHaveBeenCalledWith('version', expectedVersion)
90+
mockCurrentVersion(currentVersion);
91+
mockInputs('1', '0', publishBeta);
92+
const spy = jest.spyOn(core, 'setOutput');
93+
await run();
94+
expect(spy).toHaveBeenCalledWith('version', expectedVersion);
8695
}
87-
)
88-
})
96+
);
97+
});
8998

9099
describe('when current version match the given main or minor version', () => {
91100
it.each([
@@ -111,12 +120,12 @@ describe('get current version', () => {
111120
])(
112121
'when current: %s, publish beta: %s, should return: %s',
113122
async (currentVersion, publishBeta, expectedVersion) => {
114-
mockCurrentVersion(currentVersion)
115-
mockInputs('1', '0', publishBeta)
116-
const spy = jest.spyOn(core, 'setOutput')
117-
await run()
118-
expect(spy).toHaveBeenCalledWith('version', expectedVersion)
123+
mockCurrentVersion(currentVersion);
124+
mockInputs('1', '0', publishBeta);
125+
const spy = jest.spyOn(core, 'setOutput');
126+
await run();
127+
expect(spy).toHaveBeenCalledWith('version', expectedVersion);
119128
}
120-
)
121-
})
122-
})
129+
);
130+
});
131+
});

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)