Skip to content

Commit adc4934

Browse files
committed
Add test files
1 parent 1d36361 commit adc4934

File tree

5 files changed

+213
-60
lines changed

5 files changed

+213
-60
lines changed

__tests__/annotation.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {expect, test} from '@jest/globals'
2+
import {Annotation} from '../src/annotation'
3+
4+
test('test Annotation.constructor with Warning', () => {
5+
const annotation = new Annotation(
6+
'Warning',
7+
'Useless parent layout: This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view',
8+
'layout.xml',
9+
11,
10+
22
11+
)
12+
expect(annotation.severityLevel).toEqual('warning')
13+
expect(annotation.message).toEqual(
14+
'Useless parent layout: This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view'
15+
)
16+
expect(annotation.properties).toEqual({
17+
file: 'layout.xml',
18+
startLine: 11,
19+
startColumn: 22
20+
})
21+
})
22+
23+
test('test Annotation.constructor with Error', () => {
24+
const annotation = new Annotation(
25+
'Error',
26+
'Ignoring results: The result of `subscribe` is not used',
27+
'Foo.kt',
28+
33,
29+
44
30+
)
31+
expect(annotation.severityLevel).toEqual('error')
32+
expect(annotation.message).toEqual(
33+
'Ignoring results: The result of `subscribe` is not used'
34+
)
35+
expect(annotation.properties).toEqual({
36+
file: 'Foo.kt',
37+
startLine: 33,
38+
startColumn: 44
39+
})
40+
})
41+
42+
test('test Annotation.constructor with Other', () => {
43+
const annotation = new Annotation('', '', 'layout.xml', 0, 0)
44+
expect(annotation.severityLevel).toEqual('warning')
45+
})

__tests__/main.test.ts

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,49 @@
1-
/**
2-
* Unit tests for the action's main functionality, src/main.ts
3-
*
4-
* To mock dependencies in ESM, you can create fixtures that export mock
5-
* functions and objects. For example, the core module is mocked in this test,
6-
* so that the actual '@actions/core' module is not imported.
7-
*/
8-
import { jest } from '@jest/globals'
9-
import * as core from '../__fixtures__/core.js'
10-
import { wait } from '../__fixtures__/wait.js'
11-
12-
// Mocks should be declared before the module being tested is imported.
13-
jest.unstable_mockModule('@actions/core', () => core)
14-
jest.unstable_mockModule('../src/wait.js', () => ({ wait }))
15-
16-
// The module being tested should be imported dynamically. This ensures that the
17-
// mocks are used in place of any actual dependencies.
18-
const { run } = await import('../src/main.js')
19-
20-
describe('main.ts', () => {
21-
beforeEach(() => {
22-
// Set the action's inputs as return values from core.getInput().
23-
core.getInput.mockImplementation(() => '500')
24-
25-
// Mock the wait function so that it does not actually wait.
26-
wait.mockImplementation(() => Promise.resolve('done!'))
27-
})
28-
29-
afterEach(() => {
30-
jest.resetAllMocks()
31-
})
32-
33-
it('Sets the time output', async () => {
34-
await run()
35-
36-
// Verify the time output was set.
37-
expect(core.setOutput).toHaveBeenNthCalledWith(
38-
1,
39-
'time',
40-
// Simple regex to match a time string in the format HH:MM:SS.
41-
expect.stringMatching(/^\d{2}:\d{2}:\d{2}/)
42-
)
43-
})
44-
45-
it('Sets a failed status', async () => {
46-
// Clear the getInput mock and return an invalid value.
47-
core.getInput.mockClear().mockReturnValueOnce('this is not a number')
48-
49-
// Clear the wait mock and return a rejected promise.
50-
wait
51-
.mockClear()
52-
.mockRejectedValueOnce(new Error('milliseconds is not a number'))
53-
54-
await run()
1+
import * as process from 'process'
2+
import * as cp from 'child_process'
3+
import * as path from 'path'
4+
import {expect, test} from '@jest/globals'
5+
6+
// shows how the runner will run a javascript action with env / stdout protocol
7+
test('test runs', () => {
8+
process.env['INPUT_REPORT-PATH'] = path.join(__dirname, 'resource', '*.xml')
9+
process.env['INPUT_FOLLOW-SYMBOLIC-LINKS'] = 'true'
10+
process.env['INPUT_IGNORE-WARNINGS'] = 'false'
11+
const np = process.execPath
12+
const ip = path.join(__dirname, '..', 'lib', 'main.js')
13+
const options: cp.ExecFileSyncOptions = {
14+
env: process.env
15+
}
16+
17+
try {
18+
const stdout = cp.execFileSync(np, [ip], options)
19+
console.log(stdout.toString())
20+
expect.assertions(1)
21+
} catch (error: any) {
22+
console.log(error.stdout.toString())
23+
expect(error.status).toEqual(1)
24+
}
25+
})
5526

56-
// Verify that the action was marked as failed.
57-
expect(core.setFailed).toHaveBeenNthCalledWith(
58-
1,
59-
'milliseconds is not a number'
60-
)
61-
})
27+
test('test runs without error', () => {
28+
process.env['INPUT_REPORT-PATH'] = path.join(
29+
__dirname,
30+
'resource',
31+
'empty-results.xml'
32+
)
33+
process.env['INPUT_FOLLOW-SYMBOLIC-LINKS'] = 'true'
34+
process.env['INPUT_IGNORE-WARNINGS'] = 'false'
35+
const np = process.execPath
36+
const ip = path.join(__dirname, '..', 'lib', 'main.js')
37+
const options: cp.ExecFileSyncOptions = {
38+
env: process.env
39+
}
40+
41+
try {
42+
const stdout = cp.execFileSync(np, [ip], options)
43+
console.log(stdout.toString())
44+
} catch (error: any) {
45+
console.log(error.status)
46+
console.log(error.stdout.toString())
47+
expect.assertions(1)
48+
}
6249
})

__tests__/parser.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import path from 'path'
2+
import {expect, test} from '@jest/globals'
3+
import {Annotation} from '../src/annotation'
4+
import {parseXmls, parseXml} from '../src/parser'
5+
6+
test('test parseXmls', () => {
7+
const file1 = path.join(__dirname, 'resource', 'lint-results.xml')
8+
const file2 = path.join(__dirname, 'resource', 'empty-results.xml')
9+
10+
const annotation1 = new Annotation(
11+
'Warning',
12+
'Useless parent layout: This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view',
13+
'layout.xml',
14+
11,
15+
22
16+
)
17+
const annotation2 = new Annotation(
18+
'Error',
19+
'Ignoring results: The result of `subscribe` is not used',
20+
'Foo.kt',
21+
33,
22+
44
23+
)
24+
25+
expect(parseXmls([file1, file2], false)).resolves.toEqual([
26+
annotation1,
27+
annotation2
28+
])
29+
})
30+
31+
test('test parseXmls and ignore warnings', () => {
32+
const file1 = path.join(__dirname, 'resource', 'lint-results.xml')
33+
const file2 = path.join(__dirname, 'resource', 'empty-results.xml')
34+
35+
const annotation2 = new Annotation(
36+
'Error',
37+
'Ignoring results: The result of `subscribe` is not used',
38+
'Foo.kt',
39+
33,
40+
44
41+
)
42+
43+
expect(parseXmls([file1, file2], true)).resolves.toEqual([annotation2])
44+
})
45+
46+
test('test parseXml with issues', () => {
47+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
48+
<issues format="6" by="lint 7.2.1">
49+
<issue
50+
id="CheckResult"
51+
severity="Error"
52+
message="The result of \`subscribe\` is not used"
53+
category="Correctness"
54+
priority="6"
55+
summary="Ignoring results"
56+
explanation="Some methods have no side effects, and calling them without doing something without the result is suspicious."
57+
errorLine1=" lifecycle.subscribe { event ->"
58+
errorLine2=" ^">
59+
<location
60+
file="Foo.kt"
61+
line="33"
62+
column="44"/>
63+
</issue>
64+
</issues>`
65+
const annotation = new Annotation(
66+
'Error',
67+
'Ignoring results: The result of `subscribe` is not used',
68+
'Foo.kt',
69+
33,
70+
44
71+
)
72+
73+
expect(parseXml(xml, false)).resolves.toEqual([annotation])
74+
})
75+
76+
test('test parseXml without issue', () => {
77+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
78+
<issues format="6" by="lint 7.2.1">
79+
</issues>`
80+
81+
expect(parseXml(xml, false)).resolves.toEqual([])
82+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<issues format="6" by="lint 7.2.1">
3+
</issues>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<issues format="6" by="lint 7.2.1">
3+
4+
<issue
5+
id="UselessParent"
6+
severity="Warning"
7+
message="This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view"
8+
category="Performance"
9+
priority="2"
10+
summary="Useless parent layout"
11+
explanation="A layout with children that has no siblings, is not a scrollview or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy."
12+
errorLine1=" &lt;RelativeLayout"
13+
errorLine2=" ~~~~~~~~~~~~~~">
14+
<location
15+
file="layout.xml"
16+
line="11"
17+
column="22"/>
18+
</issue>
19+
20+
<issue
21+
id="CheckResult"
22+
severity="Error"
23+
message="The result of `subscribe` is not used"
24+
category="Correctness"
25+
priority="6"
26+
summary="Ignoring results"
27+
explanation="Some methods have no side effects, and calling them without doing something without the result is suspicious."
28+
errorLine1=" lifecycle.subscribe { event ->"
29+
errorLine2=" ^">
30+
<location
31+
file="Foo.kt"
32+
line="33"
33+
column="44"/>
34+
</issue>
35+
36+
</issues>

0 commit comments

Comments
 (0)