cli icon indicating copy to clipboard operation
cli copied to clipboard

apex:test:run --resultformat junit Generates Invalid XML if a Class Requires Compilation

Open hlopetz opened this issue 4 years ago • 6 comments

Summary

sfdx output cannot be parsed by CI (Jenkins in my case) if one of the test classes requires a recompilation.

Steps To Reproduce:

  1. create an APEX class with a method
  2. write a test class relying on the method
  3. delete the method or change its signature and save the APEX class (that will invalidate the test class)
  4. run tests as sfdx force:apex:test:run -u yourInstance --resultformat junit -t TestClass
  5. find test-result.xml is invalid

Jenkins shows the following message:

Failed to read test report file …/test-result.xml
org.dom4j.DocumentException: Error on line 108 of document  : The value of
attribute "name" associated with an element type "testcase" must not contain the '<' character.

Expected result

valid XML output

Actual result

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
    <testsuite name="force.apex" failures="1" ... >
        <properties>…</properties>
        <testcase name="<compile>" classname="TestClass" time="0.00">
            <failure message="line 10, column 1: Dependent class is invalid and needs recompilation:
 Class ApexClass : Method not defined"></failure>
        </testcase>
    </testsuite>
</testsuites>

that "<compile>" is the reason. please avoid using special/reserved characters without escaping them.

System Information

{
	"cliVersion": "sfdx-cli/7.116.2",
	"architecture": "linux-x64",
	"nodeVersion": "node-v14.17.5",
	"pluginVersions": [
		"@oclif/plugin-autocomplete 0.3.0 (core)",
		"@oclif/plugin-commands 1.3.0 (core)",
		"@oclif/plugin-help 3.2.2 (core)",
		"@oclif/plugin-not-found 1.2.4 (core)",
		"@oclif/plugin-plugins 1.10.1 (core)",
		"@oclif/plugin-update 1.4.0-3 (core)",
		"@oclif/plugin-warn-if-update-available 1.7.0 (core)",
		"@oclif/plugin-which 1.0.3 (core)",
		"@salesforce/sfdx-plugin-lwc-test 0.1.7 (core)",
		"@salesforce/sfdx-trust 3.6.0 (core)",
		"alias 1.1.10 (core)",
		"apex 0.2.8 (core)",
		"auth 1.7.1 (core)",
		"config 1.2.24 (core)",
		"custom-metadata 1.0.12 (core)",
		"data 0.6.1 (core)",
		"generator 1.1.7 (core)",
		"limits 1.2.1 (core)",
		"org 1.7.0 (core)",
		"salesforce-alm 52.3.1 (core)",
		"schema 1.0.8 (core)",
		"sfdx-cli 7.116.2 (core)",
		"source 1.0.12 (core)",
		"telemetry 1.2.3 (core)",
		"templates 52.1.0 (core)",
		"user 1.4.0 (core)"
	],
	"osVersion": "Linux 4.4.0-186-generic"
}

hlopetz avatar Sep 09 '21 15:09 hlopetz

Thank you for filing this issue. We appreciate your feedback and will review the issue as soon as possible. Remember, however, that GitHub isn't a mechanism for receiving support under any agreement or SLA. If you require immediate assistance, contact Salesforce Customer Support.

github-actions[bot] avatar Sep 09 '21 15:09 github-actions[bot]

Confirming I'm seeing this issue as well, but for when HTML is embedded into the result report. Oddly, it looks like if sfdx-cli is installed from package, we don't see this issue, but if sfdx-cli is installed using npm, this issue presents!

esmcelroy-rci avatar Dec 01 '21 16:12 esmcelroy-rci

i believe that's homebrew way in my case.

hlopetz avatar Dec 01 '21 17:12 hlopetz

Interesting! We tried two different install methods in our Jenkins:

  • npm install -g sfdx-cli
  • wget https://developer.salesforce.com/media/salesforce-cli/sfdx-linux-amd64.tar.xz && mkdir sfdx && tar xf sfdx-linux-amd64.tar.xz -C sfdx --strip-components 1 && ./sfdx/install

With the second method, in the same sandbox, we get a valid report file.... but not with the npm method...

esmcelroy-rci avatar Dec 01 '21 17:12 esmcelroy-rci

probably, homebrew uses the npm one.

hlopetz avatar Dec 01 '21 17:12 hlopetz

XML metacharacters are not escaped in the failure element's message attribute either. Can be reproduced by having a test which throws an exception with a message containing XML metacharacters, e.g. <, > or "

geoffswift avatar Sep 01 '22 09:09 geoffswift

This is fixed by: https://github.com/forcedotcom/salesforcedx-apex/pull/285 I believe/hope

gitfish avatar Nov 07 '22 21:11 gitfish