Skip to content

Commit 1d5d427

Browse files
committed
Fix tests
1 parent 26cd82a commit 1d5d427

File tree

3 files changed

+168
-150
lines changed

3 files changed

+168
-150
lines changed

__tests__/annotation.test.ts

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect} from '@jest/globals'
22
import {Annotation} from '../src/annotation.js'
33

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
4+
describe('annotation.ts', () => {
5+
it('test Annotation.constructor with Warning', () => {
6+
const annotation = new Annotation(
7+
'Warning',
8+
'Useless parent layout: This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view',
9+
'layout.xml',
10+
11,
11+
22
12+
)
13+
expect(annotation.severityLevel).toEqual('warning')
14+
expect(annotation.message).toEqual(
15+
'Useless parent layout: This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view'
16+
)
17+
expect(annotation.properties).toEqual({
18+
file: 'layout.xml',
19+
startLine: 11,
20+
startColumn: 22
21+
})
2022
})
21-
})
2223

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
24+
it('test Annotation.constructor with Error', () => {
25+
const annotation = new Annotation(
26+
'Error',
27+
'Ignoring results: The result of `subscribe` is not used',
28+
'Foo.kt',
29+
33,
30+
44
31+
)
32+
expect(annotation.severityLevel).toEqual('error')
33+
expect(annotation.message).toEqual(
34+
'Ignoring results: The result of `subscribe` is not used'
35+
)
36+
expect(annotation.properties).toEqual({
37+
file: 'Foo.kt',
38+
startLine: 33,
39+
startColumn: 44
40+
})
3941
})
40-
})
4142

42-
test('test Annotation.constructor with Other', () => {
43-
const annotation = new Annotation('', '', 'layout.xml', 0, 0)
44-
expect(annotation.severityLevel).toEqual('warning')
43+
it('test Annotation.constructor with Other', () => {
44+
const annotation = new Annotation('', '', 'layout.xml', 0, 0)
45+
expect(annotation.severityLevel).toEqual('warning')
46+
})
4547
})

__tests__/main.test.ts

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,60 @@
1-
import * as process from 'process'
2-
import * as cp from 'child_process'
1+
import { jest } from '@jest/globals'
32
import * as path from 'path'
4-
import {expect, test} from '@jest/globals'
3+
import url from 'url'
4+
import {expect} from '@jest/globals'
5+
import * as core from '../__fixtures__/core.js'
6+
7+
jest.unstable_mockModule('@actions/core', () => core)
8+
9+
const { run } = await import('../src/main.js')
510

611
// 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-
})
12+
describe('main.ts', () => {
13+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
14+
15+
beforeEach(() => {
16+
core.getBooleanInput.mockImplementation((name) => {
17+
switch (name) {
18+
case 'follow-symbolic-links':
19+
return true
20+
case 'ignore-warnings':
21+
return false
22+
default:
23+
return false
24+
}
25+
})
26+
})
27+
28+
afterEach(() => {
29+
jest.resetAllMocks()
30+
})
31+
32+
it('test runs', async () => {
33+
core.getInput.mockImplementation((name) => {
34+
switch (name) {
35+
case 'report-path':
36+
return path.join(__dirname, 'resource', '*.xml')
37+
default:
38+
return ''
39+
}
40+
})
41+
42+
await run()
43+
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Android Lint with 1 error')
44+
})
45+
46+
it('test runs without error', async () => {
47+
core.getInput.mockImplementation((name) => {
48+
switch (name) {
49+
case 'report-path':
50+
return path.join(__dirname, 'resource', 'empty-results.xml')
51+
default:
52+
return ''
53+
}
54+
})
55+
56+
await run()
2657

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-
}
58+
expect(core.setFailed).not.toHaveBeenCalled()
59+
})
4960
})

__tests__/parser.test.ts

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,87 @@
11
import path from 'path'
2-
import {expect, test} from '@jest/globals'
2+
import url from 'url'
3+
import {expect} from '@jest/globals'
34
import {Annotation} from '../src/annotation.js'
45
import {parseXmls, parseXml} from '../src/parser.js'
56

6-
test('test parseXmls', () => {
7-
const file1 = path.join(__dirname, 'resource', 'lint-results.xml')
8-
const file2 = path.join(__dirname, 'resource', 'empty-results.xml')
7+
describe('parser.ts', () => {
8+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
99

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-
)
10+
it('test parseXmls', () => {
11+
const file1 = path.join(__dirname, 'resource', 'lint-results.xml')
12+
const file2 = path.join(__dirname, 'resource', 'empty-results.xml')
2413

25-
expect(parseXmls([file1, file2], false)).resolves.toEqual([
26-
annotation1,
27-
annotation2
28-
])
29-
})
14+
const annotation1 = new Annotation(
15+
'Warning',
16+
'Useless parent layout: This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view',
17+
'layout.xml',
18+
11,
19+
22
20+
)
21+
const annotation2 = new Annotation(
22+
'Error',
23+
'Ignoring results: The result of `subscribe` is not used',
24+
'Foo.kt',
25+
33,
26+
44
27+
)
3028

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')
29+
expect(parseXmls([file1, file2], false)).resolves.toEqual([
30+
annotation1,
31+
annotation2
32+
])
33+
})
3434

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-
)
35+
it('test parseXmls and ignore warnings', () => {
36+
const file1 = path.join(__dirname, 'resource', 'lint-results.xml')
37+
const file2 = path.join(__dirname, 'resource', 'empty-results.xml')
4238

43-
expect(parseXmls([file1, file2], true)).resolves.toEqual([annotation2])
44-
})
39+
const annotation2 = new Annotation(
40+
'Error',
41+
'Ignoring results: The result of `subscribe` is not used',
42+
'Foo.kt',
43+
33,
44+
44
45+
)
4546

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-
)
47+
expect(parseXmls([file1, file2], true)).resolves.toEqual([annotation2])
48+
})
7249

73-
expect(parseXml(xml, false)).resolves.toEqual([annotation])
74-
})
50+
it('test parseXml with issues', () => {
51+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
52+
<issues format="6" by="lint 7.2.1">
53+
<issue
54+
id="CheckResult"
55+
severity="Error"
56+
message="The result of \`subscribe\` is not used"
57+
category="Correctness"
58+
priority="6"
59+
summary="Ignoring results"
60+
explanation="Some methods have no side effects, and calling them without doing something without the result is suspicious."
61+
errorLine1=" lifecycle.subscribe { event ->"
62+
errorLine2=" ^">
63+
<location
64+
file="Foo.kt"
65+
line="33"
66+
column="44"/>
67+
</issue>
68+
</issues>`
69+
const annotation = new Annotation(
70+
'Error',
71+
'Ignoring results: The result of `subscribe` is not used',
72+
'Foo.kt',
73+
33,
74+
44
75+
)
76+
77+
expect(parseXml(xml, false)).resolves.toEqual([annotation])
78+
})
7579

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+
it('test parseXml without issue', () => {
81+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
82+
<issues format="6" by="lint 7.2.1">
83+
</issues>`
8084

81-
expect(parseXml(xml, false)).resolves.toEqual([])
85+
expect(parseXml(xml, false)).resolves.toEqual([])
86+
})
8287
})

0 commit comments

Comments
 (0)