Skip to content

Commit 10919d5

Browse files
GHA-93 Add UTs for OctokitAction input and load (#230)
1 parent 88f6440 commit 10919d5

File tree

7 files changed

+345
-4
lines changed

7 files changed

+345
-4
lines changed

.github/workflows/Build.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
uses: SonarSource/vault-action-wrapper@v3
5555
with:
5656
secrets: |
57+
development/github/token/{REPO_OWNER_NAME_DASH}-jira token | GITHUB_TOKEN;
5758
development/artifactory/token/{REPO_OWNER_NAME_DASH}-private-reader access_token | REPOX_TOKEN;
5859
development/kv/data/jira user | JIRA_USER;
5960
development/kv/data/jira token | JIRA_TOKEN;
@@ -67,6 +68,7 @@ jobs:
6768

6869
- name: Run tests
6970
env:
70-
JIRA_USER: ${{ fromJSON(steps.secrets.outputs.vault).JIRA_USER }}
71-
JIRA_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).JIRA_TOKEN }}
71+
GITHUB_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).GITHUB_TOKEN }}
72+
JIRA_USER: ${{ fromJSON(steps.secrets.outputs.vault).JIRA_USER }}
73+
JIRA_TOKEN: ${{ fromJSON(steps.secrets.outputs.vault).JIRA_TOKEN }}
7274
run: npm run test

dist/lib/OctokitAction.test.js

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

dist/lib/OctokitAction.test.js.map

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

dist/tests/OctokitRestStub.js

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

dist/tests/OctokitRestStub.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/OctokitAction.test.ts

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import { LogTester } from '../tests/LogTester';
2+
import { jiraClientStub } from '../tests/JiraClientStub';
3+
import { createOctokitRestStub } from '../tests/OctokitRestStub';
4+
import { OctokitAction } from './OctokitAction';
5+
import { fail } from 'assert';
6+
7+
class TestOctokitAction extends OctokitAction {
8+
9+
constructor() {
10+
super();
11+
(this as any).jira = jiraClientStub;
12+
(this as any).rest = createOctokitRestStub('PR title');
13+
}
14+
15+
async execute(): Promise<void> {
16+
this.log('Invoked execute()');
17+
}
18+
}
19+
20+
describe('OctokitAction', () => {
21+
const originalKeys = Object.keys(process.env);
22+
let logTester: LogTester;
23+
let sut: any; // TestOctokitAction, but with access to protected methods
24+
25+
beforeEach(() => {
26+
logTester = new LogTester();
27+
const token = process.env["GITHUB_TOKEN"];
28+
if (token) {
29+
for (const key of Object.keys(process.env)) {
30+
if (!originalKeys.includes(key)) {
31+
// Otherwise, changes form previous UT are propagated to the next one
32+
delete process.env[key];
33+
}
34+
}
35+
process.env['GITHUB_REPOSITORY'] = 'test-owner/test-repo';
36+
process.env['INPUT_GITHUB-TOKEN'] = token;
37+
sut = new TestOctokitAction();
38+
} else {
39+
fail("OctokitAction tests require GITHUB_TOKEN environment variables to be set.");
40+
}
41+
});
42+
43+
afterEach(() => {
44+
logTester.afterEach();
45+
});
46+
47+
it('isEngXpSquad is true', async () => {
48+
process.env['INPUT_IS-ENG-XP-SQUAD'] = 'true';
49+
const action = new TestOctokitAction();
50+
expect((action as any).isEngXpSquad).toBe(true);
51+
});
52+
53+
it('isEngXpSquad is false', async () => {
54+
const action = new TestOctokitAction();
55+
expect((action as any).isEngXpSquad).toBe(false);
56+
});
57+
58+
it('sendGraphQL', async () => {
59+
const result = await sut.sendGraphQL(`
60+
query {
61+
repository (owner: "SonarSource", name: "gh-action-lt-backlog") {
62+
id
63+
name
64+
}
65+
}`);
66+
expect(result).toEqual({
67+
repository: {
68+
id: "R_kgDOGZ6k-Q",
69+
name: "gh-action-lt-backlog"
70+
}
71+
})
72+
});
73+
74+
it('inputString', async () => {
75+
process.env['INPUT_KEY'] = 'Value'; // KEY must be upper case, otherwise Ubuntu in CI doesn't work
76+
expect(sut.inputString('key')).toBe('Value');
77+
expect(sut.inputString('missing')).toBe('');
78+
});
79+
80+
it('inputNumber', async () => {
81+
process.env['INPUT_KEY'] = '42';
82+
process.env['INPUT_STRING'] = 'Lorem Ipsum';
83+
expect(sut.inputNumber('key')).toBe(42);
84+
expect(() => sut.inputNumber('string')).toThrow("Value of input 'string' is not a number: Lorem Ipsum")
85+
});
86+
87+
it('inputBoolean true', async () => {
88+
for (const value of ['true', 'True', 'TRUE', 'tRuE']) {
89+
process.env['INPUT_KEY'] = value;
90+
expect(sut.inputBoolean('key')).toBe(true);
91+
}
92+
});
93+
94+
it('inputBoolean false', async () => {
95+
for (const value of ['', 'false', 'False', '0', 'no']) {
96+
process.env['INPUT_KEY'] = value;
97+
expect(sut.inputBoolean('key')).toBe(false);
98+
}
99+
});
100+
101+
it('loadPullRequest', async () => {
102+
const pr = await sut.loadPullRequest(42);
103+
expect(pr).toMatchObject({ number: 42, title: "PR title" }); // Plus the remainig properties from scaffolding
104+
expect(logTester.logsParams).toStrictEqual(["Loading PR #42"]);
105+
});
106+
107+
it('loadIssue', async () => {
108+
const issue = await sut.loadIssue(24);
109+
expect(issue).toMatchObject({ number: 24, title: "Issue title" }); // Plus the remainig properties from scaffolding
110+
expect(logTester.logsParams).toStrictEqual(["Loading issue #24"]);
111+
});
112+
113+
it.skip('findFixedIssues no body', async () => {
114+
115+
});
116+
117+
it.skip('findFixedIssues no issue', async () => {
118+
119+
});
120+
121+
it.skip('findFixedIssues with issues in body', async () => {
122+
123+
});
124+
125+
it.skip('findFixedIssues renovate no issue', async () => {
126+
127+
});
128+
129+
it.skip('findFixedIssues renovate with issue', async () => {
130+
131+
});
132+
133+
it.skip('addComment', async () => {
134+
135+
});
136+
137+
it.skip('listComments', async () => {
138+
139+
});
140+
141+
it.skip('updateIssueTitle', async () => {
142+
143+
});
144+
145+
it.skip('updatePullRequestTitle', async () => {
146+
147+
});
148+
149+
it.skip('updatePullRequestDescription', async () => {
150+
151+
});
152+
153+
it.skip('findEmail returns first', async () => {
154+
155+
});
156+
157+
it.skip('findEmail no results', async () => {
158+
159+
});
160+
161+
it.skip('findEmail bad token', async () => {
162+
163+
});
164+
165+
it.skip('sendSlackMessage', async () => {
166+
167+
});
168+
169+
it.skip('processRequestReview', async () => {
170+
171+
});
172+
173+
it.skip('addJiraComponent success', async () => {
174+
175+
});
176+
177+
it.skip('addJiraComponent fails to create component', async () => {
178+
179+
});
180+
181+
it.skip('addJiraComponent fails to assign component', async () => {
182+
183+
});
184+
});

tests/OctokitRestStub.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
type RestStub = {
22
issues: {
33
createComment: (params: any) => void,
4+
get: (params: any) => any,
45
listComments: (params: any) => any
56
}
67
pulls: {
@@ -15,6 +16,14 @@ export function createOctokitRestStub(title: string, body?: string, login: strin
1516
createComment(params: any): void {
1617
console.log(`Invoked rest.issues.createComment(${JSON.stringify(params)})`);
1718
},
19+
get(params: any): any {
20+
return {
21+
data: {
22+
number: 24,
23+
title: "Issue title"
24+
}
25+
}
26+
},
1827
listComments(params: any): any {
1928
return {
2029
data: []
@@ -38,7 +47,7 @@ export function createOctokitRestStub(title: string, body?: string, login: strin
3847
login,
3948
}
4049
}
41-
}
50+
};
4251
},
4352
update(params: any): void {
4453
console.log(`Invoked rest.pulls.update(${JSON.stringify(params)})`);

0 commit comments

Comments
 (0)