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

Automatic capture of relevant Test classes or any Dependencies in package.xml when Apex classes are Updated #241

Closed
akshay4570 opened this issue Jan 27, 2022 · 7 comments
Assignees
Labels
docker Anything related to docker enhancement New feature or request test classes Anything related to apex test class

Comments

@akshay4570
Copy link

Is your proposal related to a problem?


Hi Team,
Git Delta is a great product and have been using it for quit a while but to run RunSpecifiedTests with only the Testcases that are relevant to a particular Apex class which is changed rather than running all the test classes there seems to be no option. Could it be possible to pick all the dependencies including Test classes, Objects etc. to be picked as part of package.xml when there is any updated Apex Class.

Describe the solution you'd like


Lets say I have a class ApexClass1 and it has a dependency over an Object Object1 and Testclass ApexClass1_Test. The solution I would like is whenever there is changes on the ApexClass1 file the dependencies like Object1, ApexClass1_Test should also be captured in the package.xml so that we can run a script and loop through these files and run only the specified test cases.
By updating ApexClass1 the package.xml should look like this,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>ApexClass1</members>
        <members>ApexClass1_Test</members>
        <name>ApexClass</name>
    </types>
    <types>
        <members>Object1</members>
        <name>CustomObject</name>
    </types>
    <version>52.0</version>
</Package>

Describe alternatives you've considered


Till now, I am not able to find a way to capture the dependencies.

Additional context


@akshay4570 akshay4570 added the enhancement New feature or request label Jan 27, 2022
@scolladon
Copy link
Owner

Hi @akshay4570 !

Thanks for this suggestion !

From what I understand, you need to be able to generate the SpecifiedTest classes, from the content of the package.xml, based on a test class naming convention.

I think we got you covered 😉 , We explained a way to parse Generate a comma-separated list of the added and modified Apex classes in our README.md.
This jq command gives you the list of classes from the package.xml.
You just need to tweak it a little bit in order to postfix the element with your naming convention before joining with "," using jq map operator (something like | map(.+"<test class convention>")) => xq . < package.xml | jq '.Package.types | if type=="array" then .[] else . end | select(.name=="ApexClass") | .members | map(.+"Test") | join(",")'

You may want to do something for the test classes already present in the package.xml because they also will be postfix here.
So you probably want to exclude them from the map (map if the string does not end by the test class convention)

@akshay4570
Copy link
Author

Hi @scolladon,
The approach you suggested is for the test class already present in the package.xml, But what I am suggesting is based on the Apex class modified I wanted the test classes to be automatically captured in the package.xml. Please can you go through my problem statement once again.

@scolladon
Copy link
Owner

scolladon commented Jan 27, 2022

I think this is what I proposed...
My suggestion is to deduce the apex test class based on a naming convention.
If ApexClass1 is in the package.xml then I want to deploy with SpecifiedTest using ApexClass1_Test apex test class.

What you are suggesting is a little bit more smarter from what I understand.
You want to be able to add to the package.xml other thing base on dependency (for the first level I guess) :

  • What depends on what is listed in the package.xml => ApexClass1_Test apex class depends on ApexClass1 apex class then I want ApexClass1_Test to be added to the package.xml
  • What the package.xml content depends on => ApexClass1 class depends on Object1 then I want Object1 to be added to the package.xml

I think it could be done pretty easily using the Dependency API and/or Tooling API symbol table

If that's what you have in mind, then I think it is a great idea, and I think this plugin may not be the right place to deal with that kind of problem.
It should be at least another command or another plugin by itself, something dedicated on driving the test based on the content of the package.xml and/or the repo.

@mehdicherf
Copy link
Collaborator

mehdicherf commented Feb 2, 2022

Hi @akshay4570 and thank you for taking the time to describe your enhancement!

In addition to the answers from @scolladon, I would add that dealing with this kind of dependency tree is not trivial. For example, I'm not sure how we would identify the relevant Test classes that apply to a modified Apex class? Naming conventions (will not always work)? Downstream dependency (will not always work either, at least not if we limit the depth)?

You also mention upstream dependencies, but if an SObject is not modified in the repo, I do not see the rationale for adding it just because it's referenced in a test class. What would be the reason for adding that dependency to the package?

So I'm afraid it would be hard to find a solution that fit everyone's needs in a reliable way. For now I would close this issue, but please let us know if you feel that we missed your point!

@tetardv
Copy link

tetardv commented Mar 16, 2023

Hello,

I am trying to implement a CI/CD using SFDX cli. I found your very nice plugin to create a package.xml with only changes for source to target branch which is exactly what I need.
Furthermore, I found this topic which is exactly answering my needs, which is to run only specified tests which is related to apex classes found in the package.xml previously.
I am facing issue running the command : xq . < package.xml | jq '.Package.types | if type=="array" then .[] else . end | select(.name=="ApexClass") | .members | map(.+"Test") | join(",")' and the one in the README as well.

Could you please provide the environment you are running this command ? is that a linux ubuntu as well ?
If it can help, i keep having the error parse error: Invalid numeric literal at line 1, column 5 .
Thanks for the help, and this really nice plugin.

@scolladon scolladon added the test classes Anything related to apex test class label Mar 16, 2023
@scolladon
Copy link
Owner

Hi @tetardv,

Personally I'm on mac.
We have a lot of discussion around this topic, you can find those by filtering the issues with the label "test classes".
Here is a list of query we have built around test classes if you are more interested in a pointer.

In order to install xq, you need to install yq.
That's a bit odd, I know.
Then you will have the right version of xq (and maybe other problems but that would be progress I hope)

@tetardv
Copy link

tetardv commented Mar 16, 2023

Hi @scolladon ,
Ok i understand now that i have installed a xq version which only prettify the xml.
I needed to install the good version as you provided on your previous comment.

I will share my dockerfile if it can be of any help for anyone :
`FROM heroku/heroku:20

ENV DEBIAN_FRONTEND=noninteractive
ARG SALESFORCE_CLI_VERSION=latest
ARG SF_CLI_VERSION=latest

RUN echo '95bdaaf92265eefd40d2055fb9b5cd6cbc3cb2c4495e3ebd4b1b501822d69731 ./nodejs.tar.gz' > node-file-lock.sha
&& curl -s -o nodejs.tar.gz https://nodejs.org/dist/v18.14.2/node-v18.14.2-linux-x64.tar.gz
&& shasum --check node-file-lock.sha
RUN mkdir /usr/local/lib/nodejs
&& tar xf nodejs.tar.gz -C /usr/local/lib/nodejs/ --strip-components 1
&& rm nodejs.tar.gz node-file-lock.sha

ENV PATH=/usr/local/lib/nodejs/bin:$PATH
RUN npm install --global sfdx-cli@${SALESFORCE_CLI_VERSION} --ignore-scripts
RUN npm install --global @salesforce/cli@${SF_CLI_VERSION}

RUN echo y | sfdx plugins:install sfdx-git-delta

RUN apt-get update && apt-get install --assume-yes openjdk-11-jdk-headless jq python3-pip python3.5
RUN apt-get autoremove --assume-yes
&& apt-get clean --assume-yes
&& rm -rf /var/lib/apt/lists/*

RUN pip install yq

ENV SFDX_CONTAINER_MODE true
ENV DEBIAN_FRONTEND=dialog
ENV SHELL /bin/bash`

Thanks !

@scolladon scolladon added the docker Anything related to docker label Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker Anything related to docker enhancement New feature or request test classes Anything related to apex test class
Projects
None yet
Development

No branches or pull requests

4 participants