Skip to content

Commit cd32dc4

Browse files
committed
Add tests
1 parent eed5e47 commit cd32dc4

File tree

7 files changed

+559
-70
lines changed

7 files changed

+559
-70
lines changed

__tests__/fixtures/api/latest.json

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
3+
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
4+
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
5+
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
6+
"tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
7+
"zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
8+
"discussion_url": "https://github.com/octocat/Hello-World/discussions/90",
9+
"id": 1,
10+
"node_id": "MDc6UmVsZWFzZTE=",
11+
"tag_name": "v1.0.0",
12+
"target_commitish": "master",
13+
"name": "v1.0.0",
14+
"body": "Description of the release",
15+
"draft": false,
16+
"prerelease": false,
17+
"created_at": "2013-02-27T19:35:32Z",
18+
"published_at": "2013-02-27T19:35:32Z",
19+
"author": {
20+
"login": "octocat",
21+
"id": 1,
22+
"node_id": "MDQ6VXNlcjE=",
23+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
24+
"gravatar_id": "",
25+
"url": "https://api.github.com/users/octocat",
26+
"html_url": "https://github.com/octocat",
27+
"followers_url": "https://api.github.com/users/octocat/followers",
28+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
29+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
30+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
31+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
32+
"organizations_url": "https://api.github.com/users/octocat/orgs",
33+
"repos_url": "https://api.github.com/users/octocat/repos",
34+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
35+
"received_events_url": "https://api.github.com/users/octocat/received_events",
36+
"type": "User",
37+
"site_admin": false
38+
},
39+
"assets": [
40+
{
41+
"url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1",
42+
"browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
43+
"id": 1,
44+
"node_id": "MDEyOlJlbGVhc2VBc3NldDE=",
45+
"name": "example.zip",
46+
"label": "short description",
47+
"state": "uploaded",
48+
"content_type": "application/zip",
49+
"size": 1024,
50+
"download_count": 42,
51+
"created_at": "2013-02-27T19:35:32Z",
52+
"updated_at": "2013-02-27T19:35:32Z",
53+
"uploader": {
54+
"login": "octocat",
55+
"id": 1,
56+
"node_id": "MDQ6VXNlcjE=",
57+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
58+
"gravatar_id": "",
59+
"url": "https://api.github.com/users/octocat",
60+
"html_url": "https://github.com/octocat",
61+
"followers_url": "https://api.github.com/users/octocat/followers",
62+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
63+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
64+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
65+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
66+
"organizations_url": "https://api.github.com/users/octocat/orgs",
67+
"repos_url": "https://api.github.com/users/octocat/repos",
68+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
69+
"received_events_url": "https://api.github.com/users/octocat/received_events",
70+
"type": "User",
71+
"site_admin": false
72+
}
73+
}
74+
]
75+
}

__tests__/fixtures/api/tag.json

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
3+
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
4+
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
5+
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
6+
"tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
7+
"zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
8+
"discussion_url": "https://github.com/octocat/Hello-World/discussions/90",
9+
"id": 1,
10+
"node_id": "MDc6UmVsZWFzZTE=",
11+
"tag_name": "v1.0.0",
12+
"target_commitish": "master",
13+
"name": "v1.0.0",
14+
"body": "Description of the release",
15+
"draft": false,
16+
"prerelease": false,
17+
"created_at": "2013-02-27T19:35:32Z",
18+
"published_at": "2013-02-27T19:35:32Z",
19+
"author": {
20+
"login": "octocat",
21+
"id": 1,
22+
"node_id": "MDQ6VXNlcjE=",
23+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
24+
"gravatar_id": "",
25+
"url": "https://api.github.com/users/octocat",
26+
"html_url": "https://github.com/octocat",
27+
"followers_url": "https://api.github.com/users/octocat/followers",
28+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
29+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
30+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
31+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
32+
"organizations_url": "https://api.github.com/users/octocat/orgs",
33+
"repos_url": "https://api.github.com/users/octocat/repos",
34+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
35+
"received_events_url": "https://api.github.com/users/octocat/received_events",
36+
"type": "User",
37+
"site_admin": false
38+
},
39+
"assets": [
40+
{
41+
"url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1",
42+
"browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
43+
"id": 1,
44+
"node_id": "MDEyOlJlbGVhc2VBc3NldDE=",
45+
"name": "example.zip",
46+
"label": "short description",
47+
"state": "uploaded",
48+
"content_type": "application/zip",
49+
"size": 1024,
50+
"download_count": 42,
51+
"created_at": "2013-02-27T19:35:32Z",
52+
"updated_at": "2013-02-27T19:35:32Z",
53+
"uploader": {
54+
"login": "octocat",
55+
"id": 1,
56+
"node_id": "MDQ6VXNlcjE=",
57+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
58+
"gravatar_id": "",
59+
"url": "https://api.github.com/users/octocat",
60+
"html_url": "https://github.com/octocat",
61+
"followers_url": "https://api.github.com/users/octocat/followers",
62+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
63+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
64+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
65+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
66+
"organizations_url": "https://api.github.com/users/octocat/orgs",
67+
"repos_url": "https://api.github.com/users/octocat/repos",
68+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
69+
"received_events_url": "https://api.github.com/users/octocat/received_events",
70+
"type": "User",
71+
"site_admin": false
72+
}
73+
}
74+
]
75+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "1",
3+
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
4+
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
5+
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
6+
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
7+
"name": "v1.0.0",
8+
"tag_name": "v1.0.0",
9+
"draft": "false",
10+
"prerelease": "false",
11+
"target_commitish": "master",
12+
"created_at": "2013-02-27T19:35:32Z",
13+
"published_at": "2013-02-27T19:35:32Z"
14+
}

__tests__/fixtures/output/tag.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "1",
3+
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
4+
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
5+
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
6+
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
7+
"name": "v1.0.0",
8+
"tag_name": "v1.0.0",
9+
"draft": "false",
10+
"prerelease": "false",
11+
"target_commitish": "master",
12+
"created_at": "2013-02-27T19:35:32Z",
13+
"published_at": "2013-02-27T19:35:32Z"
14+
}

__tests__/main.test.ts

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,66 @@
1-
import { wait } from '../src/wait';
2-
import * as process from 'process';
3-
import * as cp from 'child_process';
4-
import * as path from 'path';
5-
6-
test('throws invalid number', async () => {
7-
const input = parseInt('foo', 10);
8-
await expect(wait(input)).rejects.toThrow('milliseconds not a number');
9-
});
1+
// @ts-ignore
2+
import { run } from '@jonabc/actions-mocks';
3+
import path from 'path';
4+
import fs from 'fs';
105

11-
test('wait 500 ms', async () => {
12-
const start = new Date();
13-
await wait(500);
14-
const end = new Date();
15-
var delta = Math.abs(end.getTime() - start.getTime());
16-
expect(delta).toBeGreaterThan(450);
17-
});
6+
const script = path.join(__dirname, '../lib/main');
7+
const env = {
8+
GITHUB_REPOSITORY: 'foo/bar',
9+
INPUT_TOKEN: 'dummy',
10+
};
11+
12+
function parseOutput(raw: string): Record<string, string> {
13+
return raw
14+
.split('\n')
15+
.filter((line) => line.match(/^::set-output/))
16+
.map((line) => line.replace('::set-output name=', '').split('::'))
17+
.reduce((result, [key, value]) => ({ ...result, [key]: value }), {});
18+
}
19+
20+
describe('main', function () {
21+
test('can fetch latest release', async () => {
22+
const mocks = {
23+
github: [
24+
{
25+
method: 'GET',
26+
uri: '/repos/foo/bar/releases/latest',
27+
file: path.join(__dirname, 'fixtures/api/latest.json'),
28+
headers: {
29+
'content-type': 'application/json; charset=utf-8',
30+
},
31+
},
32+
],
33+
};
34+
35+
const { out, status } = await run(script, { env: { ...env, INPUT_LATEST: 'true' }, mocks });
36+
const output = parseOutput(out);
37+
38+
expect(status).toEqual(0);
39+
expect(output).toEqual(
40+
JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures/output/latest.json'), { encoding: 'utf-8' }))
41+
);
42+
});
43+
44+
test('can fetch release by tag', async () => {
45+
const mocks = {
46+
github: [
47+
{
48+
method: 'GET',
49+
uri: '/repos/foo/bar/releases/tags/v1.0.0',
50+
file: path.join(__dirname, 'fixtures/api/tag.json'),
51+
headers: {
52+
'content-type': 'application/json; charset=utf-8',
53+
},
54+
},
55+
],
56+
};
57+
58+
const { out, status } = await run(script, { env: { ...env, INPUT_TAG_NAME: 'v1.0.0' }, mocks });
59+
const output = parseOutput(out);
1860

19-
// shows how the runner will run a javascript action with env / stdout protocol
20-
test('test runs', () => {
21-
process.env['INPUT_MILLISECONDS'] = '500';
22-
const np = process.execPath;
23-
const ip = path.join(__dirname, '..', 'lib', 'main.js');
24-
const options: cp.ExecFileSyncOptions = {
25-
env: process.env,
26-
};
27-
console.log(cp.execFileSync(np, [ip], options).toString());
61+
expect(status).toEqual(0);
62+
expect(output).toEqual(
63+
JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures/output/tag.json'), { encoding: 'utf-8' }))
64+
);
65+
});
2866
});

0 commit comments

Comments
 (0)