Skip to content

Commit 26bae46

Browse files
Merge pull request #24 from codecov/joseph/control-char-splitting
feat: use ascii control char US to separate the class name and test name when parsing junit
2 parents 5fc0212 + 1507de2 commit 26bae46

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

src/junit.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ use crate::testrun::{Outcome, Testrun};
1111
// from https://gist.github.com/scott-codecov/311c174ecc7de87f7d7c50371c6ef927#file-cobertura-rs-L18-L31
1212
fn attributes_map(attributes: Attributes) -> Result<HashMap<String, String>, pyo3::PyErr> {
1313
let mut attr_map = HashMap::new();
14-
for attribute in attributes {
15-
if let Ok(attr) = attribute {
16-
let bytes = attr.value.into_owned();
17-
let value = String::from_utf8(bytes)?;
18-
let key = String::from_utf8(attr.key.into_inner().to_vec())?;
19-
attr_map.insert(key, value);
20-
}
14+
for attribute in attributes.flatten() {
15+
let bytes = attribute.value.into_owned();
16+
let value = String::from_utf8(bytes)?;
17+
let key = String::from_utf8(attribute.key.into_inner().to_vec())?;
18+
attr_map.insert(key, value);
2119
}
2220
Ok(attr_map)
2321
}
2422

2523
fn populate(attr_hm: &HashMap<String, String>, testsuite: String) -> Result<Testrun, pyo3::PyErr> {
2624
let name = format!(
27-
"{}::{}",
25+
"{}{}{}",
2826
attr_hm
2927
.get("classname")
3028
.ok_or(ParserError::new_err("No classname found"))?,
29+
'\x1f',
3130
attr_hm
3231
.get("name")
3332
.ok_or(ParserError::new_err("No name found"))?
@@ -107,7 +106,7 @@ pub fn parse_junit_xml(file_bytes: Vec<u8>) -> PyResult<Vec<Testrun>> {
107106

108107
curr_testsuite = attr_hm?
109108
.get("name")
110-
.ok_or(ParserError::new_err(format!("Error getting name",)))?
109+
.ok_or(ParserError::new_err("Error getting name".to_string()))?
111110
.to_string();
112111
}
113112
_ => {}
@@ -123,15 +122,14 @@ pub fn parse_junit_xml(file_bytes: Vec<u8>) -> PyResult<Vec<Testrun>> {
123122
b"failure" => in_failure = false,
124123
_ => (),
125124
},
126-
Ok(Event::Empty(e)) => match e.name().as_ref() {
127-
b"testcase" => {
125+
Ok(Event::Empty(e)) => {
126+
if e.name().as_ref() == b"testcase" {
128127
let attr_hm = attributes_map(e.attributes());
129128
list_of_test_runs.push(populate(&attr_hm?, curr_testsuite.clone())?);
130129
}
131-
_ => (),
132-
},
130+
}
133131
Ok(Event::Text(x)) => {
134-
if in_failure == true {
132+
if in_failure {
135133
let mut testrun = saved_testrun
136134
.ok_or(ParserError::new_err("Error accessing saved testrun"))?;
137135

tests/test_junit.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from test_results_parser import parse_junit_xml, Testrun, Outcome
2+
from test_results_parser import Outcome, Testrun, parse_junit_xml
33

44

55
class TestParsers:
@@ -10,7 +10,7 @@ class TestParsers:
1010
"./tests/junit.xml",
1111
[
1212
Testrun(
13-
"tests.test_parsers.TestParsers::test_junit[junit.xml--True]",
13+
"tests.test_parsers.TestParsers\x1ftest_junit[junit.xml--True]",
1414
0.001,
1515
Outcome.Failure,
1616
"pytest",
@@ -32,7 +32,7 @@ def test_junit(self, filename, expected, check):
3232
tests/test_parsers.py:16: AssertionError""",
3333
),
3434
Testrun(
35-
"tests.test_parsers.TestParsers::test_junit[jest-junit.xml--False]",
35+
"tests.test_parsers.TestParsers\x1ftest_junit[jest-junit.xml--False]",
3636
0.064,
3737
Outcome.Pass,
3838
"pytest",
@@ -44,28 +44,28 @@ def test_junit(self, filename, expected, check):
4444
"./tests/jest-junit.xml",
4545
[
4646
Testrun(
47-
"Title when rendered renders pull title::Title when rendered renders pull title",
47+
"Title when rendered renders pull title\x1fTitle when rendered renders pull title",
4848
0.036,
4949
Outcome.Pass,
5050
"Title",
5151
None,
5252
),
5353
Testrun(
54-
"Title when rendered renders pull author::Title when rendered renders pull author",
54+
"Title when rendered renders pull author\x1fTitle when rendered renders pull author",
5555
0.005,
5656
Outcome.Pass,
5757
"Title",
5858
None,
5959
),
6060
Testrun(
61-
"Title when rendered renders pull updatestamp::Title when rendered renders pull updatestamp",
61+
"Title when rendered renders pull updatestamp\x1fTitle when rendered renders pull updatestamp",
6262
0.002,
6363
Outcome.Pass,
6464
"Title",
6565
None,
6666
),
6767
Testrun(
68-
"Title when rendered for first pull request renders pull title::Title when rendered for first pull request renders pull title",
68+
"Title when rendered for first pull request renders pull title\x1fTitle when rendered for first pull request renders pull title",
6969
0.006,
7070
Outcome.Pass,
7171
"Title",
@@ -77,15 +77,15 @@ def test_junit(self, filename, expected, check):
7777
"./tests/vitest-junit.xml",
7878
[
7979
Testrun(
80-
"__tests__/test-file-1.test.ts::first test file &gt; 2 + 2 should equal 4",
80+
"__tests__/test-file-1.test.ts\x1ffirst test file &gt; 2 + 2 should equal 4",
8181
0.01,
8282
Outcome.Failure,
8383
"__tests__/test-file-1.test.ts",
8484
"""AssertionError: expected 5 to be 4 // Object.is equality
8585
❯ __tests__/test-file-1.test.ts:20:28""",
8686
),
8787
Testrun(
88-
"__tests__/test-file-1.test.ts::first test file &gt; 4 - 2 should equal 2",
88+
"__tests__/test-file-1.test.ts\x1ffirst test file &gt; 4 - 2 should equal 2",
8989
0,
9090
Outcome.Pass,
9191
"__tests__/test-file-1.test.ts",
@@ -97,20 +97,20 @@ def test_junit(self, filename, expected, check):
9797
"./tests/empty_failure.junit.xml",
9898
[
9999
Testrun(
100-
"test.test::test.test works",
100+
"test.test\x1ftest.test works",
101101
0.234,
102102
Outcome.Pass,
103103
"test",
104-
None
104+
None,
105105
),
106-
Testrun(
107-
"test.test::test.test fails",
106+
Testrun(
107+
"test.test\x1ftest.test fails",
108108
1,
109109
Outcome.Failure,
110110
"test",
111-
"TestError"
111+
"TestError",
112112
),
113-
]
113+
],
114114
),
115115
],
116116
)
@@ -120,4 +120,3 @@ def test_junit(self, filename, expected):
120120
assert len(res) == len(expected)
121121
for restest, extest in zip(res, expected):
122122
assert restest == extest
123-

0 commit comments

Comments
 (0)