Skip to content

Commit 52dd912

Browse files
authored
Merge pull request #11 from hidakatsuya/fix-loading-xml-file-has-no-linenumber
Fix cannot load properly a XML file containing issue with no lineNumber and errorLine attributes
2 parents 9c85564 + 3dae002 commit 52dd912

File tree

9 files changed

+143
-35
lines changed

9 files changed

+143
-35
lines changed

__tests__/check.test.js

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,56 @@ describe("invalid XML file", () => {
100100
})
101101
})
102102

103-
test("XML file containing issues with multiple locations", async () => {
104-
const results = await check({ pathPattern: path.join(__dirname, "xml/failure3.xml") })
103+
describe("other XML file formats", () => {
104+
test("<issue> with multiple location attributes", async () => {
105+
const results = await check({ pathPattern: path.join(__dirname, "xml/failure3.xml") })
105106

106-
expect(results.status).toEqual("warning")
107-
expect(results.failures.length).toEqual(1)
108-
expect(results.results.length).toEqual(1)
107+
expect(results.status).toEqual("warning")
108+
expect(results.failures.length).toEqual(1)
109+
expect(results.results.length).toEqual(1)
110+
111+
const { result } = results.results[0]
112+
113+
expect(result.status).toEqual("warning")
114+
expect(result.issues.length).toEqual(2)
115+
116+
expect(result.warnings.length).toEqual(2)
117+
expect(result.warnings[0].file).toEqual("/path/to/root/app/src/main/res/values/colors1.xml")
118+
expect(result.warnings[1].file).toEqual("/path/to/root/app/src/main/res/values/colors2.xml")
119+
120+
expect(result.errors.length).toEqual(0)
121+
})
109122

110-
const { result } = results.results[0]
123+
test("No errorLine and lineNumber attributes", async () => {
124+
const results = await check({ pathPattern: path.join(__dirname, "xml/failure4.xml") })
111125

112-
expect(result.status).toEqual("warning")
113-
expect(result.issues.length).toEqual(2)
126+
expect(results.status).toEqual("warning")
127+
expect(results.failures.length).toEqual(1)
128+
expect(results.results.length).toEqual(1)
129+
130+
const { result } = results.results[0]
131+
132+
expect(result.status).toEqual("warning")
133+
expect(result.issues.length).toEqual(1)
134+
135+
expect(result.warnings.length).toEqual(1)
136+
expect(result.errors.length).toEqual(0)
114137

115-
expect(result.warnings.length).toEqual(2)
116-
expect(result.warnings[0].file).toEqual("/path/to/root/app/src/main/res/values/colors1.xml")
117-
expect(result.warnings[1].file).toEqual("/path/to/root/app/src/main/res/values/colors2.xml")
138+
const issue = result.issues[0]
118139

119-
expect(result.errors.length).toEqual(0)
140+
expect(issue.id).toEqual("UnusedResources")
141+
expect(issue.errorLine1).toBeUndefined()
142+
expect(issue.errorLine2).toBeUndefined()
143+
expect(issue.lineNumber).toBeUndefined()
144+
})
120145
})
121146

122147
describe("basic glob path pattern", () => {
123148
test("**/xml/failure*.xml", async () => {
124149
const results = await check({ pathPattern: "**/xml/failure*.xml" })
125150

126151
expect(results.status).toEqual("error")
127-
expect(results.failures.length).toEqual(3)
128-
expect(results.results.length).toEqual(3)
152+
expect(results.failures.length).toEqual(4)
153+
expect(results.results.length).toEqual(4)
129154
})
130155
})

__tests__/report.test.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ const { Issue, Result, Results } = require("../src/check")
44

55
const baseDir = "/path/to"
66

7-
function buildIssue({ severity, identifier = 1 }) {
7+
function buildIssue({
8+
severity,
9+
identifier = 1,
10+
lineNumber = identifier.toString(),
11+
errorLine1 = "line1",
12+
errorLine2 = "line2"
13+
}) {
814
return new Issue({
915
id: "SomeError",
1016
file: `/path/to/app/src/source${identifier}.kt`,
11-
lineNumber: `${identifier}`,
17+
lineNumber,
1218
severity: severity,
1319
message: `Error message${identifier}`,
1420
summary: "Some Error",
15-
errorLine1: "line1",
16-
errorLine2: "line2"
21+
errorLine1,
22+
errorLine2
1723
})
1824
}
1925

@@ -154,3 +160,30 @@ test("multiple results", async () => {
154160
"</details>"
155161
].join("\n"))
156162
})
163+
164+
describe("other cases", () => {
165+
test("no lineNumber and errorLine attribute", async () => {
166+
const issue = buildIssue({
167+
severity: "Error",
168+
identifier: 1,
169+
lineNumber: null,
170+
errorLine1: null,
171+
errorLine2: null
172+
})
173+
const result = new Result([issue])
174+
const results = new Results([{ path: "/path/to/app/build/result.xml", result }])
175+
176+
await report({ results, core, baseDir })
177+
178+
const summary = core.summary.stringify()
179+
180+
expect(summary).toMatch([
181+
"<h2>❌ Android Lint</h2>",
182+
"<h3>app/build/result.xml</h3>",
183+
"<details><summary>❌ Errors</summary>",
184+
"",
185+
"#### app/src/source1.kt (1 issues)",
186+
"* SomeError: Error message1"
187+
].join("\n"))
188+
})
189+
})

__tests__/xml/failure1.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- contains both warning and errors -->
23
<issues format="6" by="lint 8.1.0">
34

45
<issue

__tests__/xml/failure2.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- contains only warning issues -->
23
<issues format="6" by="lint 8.1.0">
34

45
<issue

__tests__/xml/failure3.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- <issue> with multiple location attributes -->
23
<issues format="6" by="lint 8.1.0">
34

45
<issue

__tests__/xml/failure4.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- no errorLine and lineNumber attributes -->
3+
<issues format="6" by="lint 8.1.0">
4+
5+
<issue
6+
id="UnusedResources"
7+
severity="Warning"
8+
message="The resource `R.color.purple_700` appears to be unused"
9+
category="Performance"
10+
priority="3"
11+
summary="Unused resources"
12+
explanation="Unused resources make applications larger and slow down builds.&#xA;&#xA;&#xA;The unused resource check can ignore tests. If you want to include resources that are only referenced from tests, consider packaging them in a test source set instead.&#xA;&#xA;You can include test sources in the unused resource check by setting the system property lint.unused-resources.include-tests =true, and to exclude them (usually for performance reasons), use lint.unused-resources.exclude-tests =true.&#xA;,">
13+
<location
14+
file="/path/to/root/app/src/main/res/values/colors1.xml"/>
15+
</issue>
16+
17+
</issues>

dist/index.js

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

dist/index.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.

src/report.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,35 @@ function buildDetails(issuesEachFile, baseDir) {
1212
)
1313

1414
issues.forEach(issue => {
15-
summaries.push(
16-
`* **Line#${issue.lineNumber}** - ${issue.id}: ${issue.message}`,
17-
" ```",
18-
` ${issue.errorLine1}`,
19-
` ${issue.errorLine2}`,
20-
" ```",
21-
""
22-
)
15+
summaries.push(buildIssueDetail(issue))
2316
})
2417
}
2518

2619
return summaries.join("\n")
2720
}
2821

22+
function buildIssueDetail(issue) {
23+
const detail = []
24+
25+
if (issue.lineNumber) {
26+
detail.push(`* **Line#${issue.lineNumber}** - ${issue.id}: ${issue.message}`)
27+
} else {
28+
detail.push(`* ${issue.id}: ${issue.message}`)
29+
}
30+
31+
if (issue.errorLine1 && issue.errorLine2) {
32+
detail.push(
33+
" ```",
34+
` ${issue.errorLine1}`,
35+
` ${issue.errorLine2}`,
36+
" ```"
37+
)
38+
}
39+
detail.push("")
40+
41+
return detail.join("\n")
42+
}
43+
2944
function resultIcon(resultStatus) {
3045
switch (resultStatus) {
3146
case "error":

0 commit comments

Comments
 (0)