Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove invalid ESC control sequence from XML output #230

Merged
merged 3 commits into from
Nov 18, 2022

Conversation

stevenxu-db
Copy link
Contributor

@stevenxu-db stevenxu-db commented Oct 21, 2022

This change forcibly deletes any occurrences of the ESC control sequence in the output XML file.

RTL emits the ESC control sequence \u001b or \x1B sometimes, and the xml package doesn't escape or exclude it. This is not valid XML and causes some parsers, including the one used in this package's unit tests, to fail. #197 was a previous report of this.

Without this fix, the newly added test fails with an XML parse error.

Screenshot 2022-10-21 at 12 17 02 PM

We could also fix in xml but that project appears less actively maintained, and given nobody has reported this it may not be a widespread problem worth messing with.

This also fixes leaking state between some tests.

@amirgalor-gong
Copy link

Would be great if this can be reviewed and possibly merged - i'm seeing some flaky-ness using this :(

@stevenxu-db
Copy link
Contributor Author

Here's our patch on 14.0.1 while we're awaiting upstream review and fix/release.

diff --git a/index.js b/index.js
index 13b7394b622396e6357eb318c1a71fc8d7db27f7..a80595e98b40d4edf62b4ae6b51c268e1bdcc9fe 100644
--- a/index.js
+++ b/index.js
@@ -34,8 +34,12 @@ const processor = (report, reporterOptions = {}, jestRootDir = null) => {
   // Ensure output path exists
   mkdirp.sync(path.dirname(outputPath));

+  // Clean ESC character, which is invalid XML and some libraries include in error messages
+  const xmlString = xml(jsonResults, { indent: '  ', declaration: true });
+  const cleanedXmlString = xmlString.replace(/\u001b/g, '');
+
   // Write data to file
-  fs.writeFileSync(outputPath, xml(jsonResults, { indent: '  ', declaration: true }));
+  fs.writeFileSync(outputPath, cleanedXmlString);

   // Jest 18 compatibility
   return report;

Copy link
Collaborator

@palmerj3 palmerj3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay reviewing this. I was traveling for work and then was ill.

I left a comment for you to review.

I would like to avoid simple string replacements and try to utilize strip-ansi or something similar. I already have logic that attempts to remove these characters from failure tags so perhaps that logic needs to be adjusted.

index.js Outdated
@@ -34,8 +34,12 @@ const processor = (report, reporterOptions = {}, jestRootDir = null) => {
// Ensure output path exists
mkdirp.sync(path.dirname(outputPath));

// Clean ESC character, which is invalid XML and some libraries include in error messages
const xmlString = xml(jsonResults, { indent: ' ', declaration: true });
const cleanedXmlString = xmlString.replace(/\u001b/g, '');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing a simple string replace can you see if you can utilize something more generic like strip-ansi?

We use that package when generating the failure tag. So perhaps it needs to be included here or tweaked.

See https://github.com/jest-community/jest-junit/blob/master/utils/buildJsonResults.js#L77

Copy link
Contributor Author

@stevenxu-db stevenxu-db Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pointer. I've collocated my regexp replace with the existing function.

strip-ansi is already operating on the offending string, but it appears that strip-ansi strips only ANSI escape codes, which are ESC plus some characters. But it doesn't also strip plain ASCII control characters like ESC, which are illegal in the XML spec.

Screenshot 2022-11-10 at 6 03 03 PM

I failed to find a suitable package on the npm registry. I did find a snippet https://gist.github.com/john-doherty/b9195065884cdbfd2017a4756e6409cc from @john-doherty that appears to be more robust than my naive approach. I initially wanted to keep the scope of the change small, but if you prefer, I can inline a copy with attribution or work with the author to publish it as a separate package with appropriate license. @palmerj3, please just let me know as I'm happy to follow your preference.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool thanks for clarifying. Your implementation looks fine to me so let's roll with that.

Running the CI job now. If all looks good I'll merge and cut a release in the morning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the checks passed.

Sorry for the ping. It's not urgent for us since we've already patched locally, but it looks like there's one other PR that intersects and a couple of downstream projects that could use this fix. Thanks for your attention here and hope you're doing better!

@stevenxu-db stevenxu-db requested a review from palmerj3 November 11, 2022 02:14
@palmerj3 palmerj3 merged commit 22db501 into jest-community:master Nov 18, 2022
@palmerj3
Copy link
Collaborator

Thanks for the hard work!

This is now published in jest-junit 15.0.0

trevor-scheer referenced this pull request in apollographql/apollo-server Nov 19, 2022
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [jest-junit](https://togithub.com/jest-community/jest-junit) |
[`14.0.1` ->
`15.0.0`](https://renovatebot.com/diffs/npm/jest-junit/14.0.1/15.0.0) |
[![age](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/compatibility-slim/14.0.1)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/confidence-slim/14.0.1)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>jest-community/jest-junit</summary>

###
[`v15.0.0`](https://togithub.com/jest-community/jest-junit/releases/tag/v15.0.0)

[Compare
Source](https://togithub.com/jest-community/jest-junit/compare/v14.0.1...v15.0.0)

Remove invalid ESC control sequence from XML output
[https://github.com/jest-community/jest-junit/pull/230](https://togithub.com/jest-community/jest-junit/pull/230)
- by [@&#8203;stevenxu-db](https://togithub.com/stevenxu-db)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/apollographql/apollo-server).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yNy4zIiwidXBkYXRlZEluVmVyIjoiMzQuMjcuMyJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
creature-water-valley referenced this pull request in ws-4020/mobile-app-crib-notes Dec 27, 2022
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [jest-junit](https://togithub.com/jest-community/jest-junit) |
[`^14.0.0` ->
`^15.0.0`](https://renovatebot.com/diffs/npm/jest-junit/14.0.1/15.0.0) |
[![age](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/compatibility-slim/14.0.1)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/jest-junit/15.0.0/confidence-slim/14.0.1)](https://docs.renovatebot.com/merge-confidence/)
|

---

### ⚠ Dependency Lookup Warnings ⚠

Warnings were logged while processing this repo. Please check the
Dependency Dashboard for more information.

---

### Release Notes

<details>
<summary>jest-community/jest-junit</summary>

###
[`v15.0.0`](https://togithub.com/jest-community/jest-junit/releases/tag/v15.0.0)

[Compare
Source](https://togithub.com/jest-community/jest-junit/compare/v14.0.1...v15.0.0)

Remove invalid ESC control sequence from XML output
[https://github.com/jest-community/jest-junit/pull/230](https://togithub.com/jest-community/jest-junit/pull/230)
- by [@&#8203;stevenxu-db](https://togithub.com/stevenxu-db)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/ws-4020/mobile-app-crib-notes).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yNy4zIiwidXBkYXRlZEluVmVyIjoiMzQuMjcuMyJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants