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

How to run Specified test class in bitbucket pipeline #902

Closed
1 task
shreyagoyal06 opened this issue Aug 21, 2024 · 6 comments
Closed
1 task

How to run Specified test class in bitbucket pipeline #902

shreyagoyal06 opened this issue Aug 21, 2024 · 6 comments
Assignees
Labels
CI/CD CI/CD context question Further information is requested

Comments

@shreyagoyal06
Copy link

shreyagoyal06 commented Aug 21, 2024

Issue verification check:

  • is the current repository fully deployable at the commit SHA provided with the 'from' parameter of the command?

What is the problem?

How do I run only specified test classes while deploying to SF environment?

Its mentioned in the doc to use the 'xq' command but when I'm trying to install yq its installs correctly and shows the yq version as well after it when running the xq command provided in the documentation it shows "command not found"

below is the sample yml

image: salesforce/cli:latest-full

pipelines:
  pull-requests:
    feature/*:
      - stage:
          name: Build and validate
          steps:
          - step:
              name: 'Install delta plugin and Create delta package'
              script:
                - echo "y" | sfdx plugins:install sfdx-git-delta
                - sfdx plugins
                - TARGET_BRANCH_COMMIT=$(git ls-remote origin refs/heads/$BITBUCKET_PR_DESTINATION_BRANCH | awk '{print $1}')
                - sfdx sgd:source:delta --to "HEAD" --from $TARGET_BRANCH_COMMIT --output .
                - echo "Installing yq"
                - apt-get update && apt-get install -y wget
                - wget https://github.com/mikefarah/yq/releases/download/v4.18.1/yq_linux_amd64 -O /usr/bin/yq
                - chmod +x /usr/bin/yq
                - yq --version
                - xq --version
                - xq . < package/package.xml | jq '.Package.types | [.] | flatten | map(select(.name=="ApexClass")) | .[] | .members | [.] | flatten | map(select(. | index("*") | not)) | unique | join(",")' > testclass.txt
                - cat testclass.txt
                - cat package/package.xml
                - cat destructiveChanges/destructiveChanges.xml

Provide the command and the parameters you used
Provided the yml above

What is the expected result?

to get test class names in testclass.txt file

What is the actual result?

xq:command not found

@shreyagoyal06 shreyagoyal06 added the bug Something isn't working label Aug 21, 2024
@scolladon scolladon added question Further information is requested CI/CD CI/CD context and removed bug Something isn't working labels Aug 21, 2024
@scolladon
Copy link
Owner

Hi @shreyagoyal06,

My guess here is the xq command is not in the PATH.
I think it is because you install yq from its binary and so it only copie yq and not the other executable.
Could you try installing yq using apt get and see if it copies all the executable and put them in the PATH ?

@shreyagoyal06
Copy link
Author

shreyagoyal06 commented Aug 21, 2024

Hi @scolladon
The sudo command was not working either so I had to update the command like this
curl -L https://github.com/mikefarah/yq/releases/download/v4.34.2/yq_linux_amd64 -o yq
chmod +x yq
mkdir -p $HOME/bin
mv yq $HOME/bin/
export PATH=$HOME/bin:$PATH
yq --version
yq eval -P -o=json package/package.xml | jq '.Package.types | [.] | flatten | map(select(.name=="ApexClass")) | .[] | .members | [.] | flatten | map(select(. | index("*") | not)) | unique | join(",")' > testclass.txt

But the output of this command is just the name of the apex classes that are present in my package.xml. Its not providing the test class names to input into the SF command for RunSpecifiedTest attribute.
Can you please enlighten me on this?

@scolladon
Copy link
Owner

Hi @shreyagoyal06, maybe you want to get only the test classes from the package.xml following a file pattern or you want to deduce the test classes to run from the one in the package.xml following a file pattern.
In this case you need to update a bit the query and here are some examples.

@shreyagoyal06
Copy link
Author

Hi @scolladon Thank you so much for the solution provided.
It resolved one of the test class issue. I used the 3rd pattern provided by you
.Package.types | [.] | flatten | map(select(.name=="ApexClass")) | .[] | .members | [.] | flatten | map(select(. | index("*") | not)) | map(. | sub("TEST$" ; "") + "TEST") | unique | join(",")

But instead of appending "Test" to the classes which does not have a test class in the package.xml, is it possible somehow that we can get its test class name from the force-app/main/default/classes folder instead?
Because though it is a standard practice for test class naming convention, there might be a possibility that the test classes are named differently.

@scolladon
Copy link
Owner

How the script would know what test class to add depending on classes found in the package.xml ?
I think there could be multiple answers to this question and all involved having to script something more complicated than the jq script to fulfil this requirement.

My bet is your better off renaming the test classes to follow a pattern and use the jq script 👍

@shreyagoyal06
Copy link
Author

Okay, thanks for your time and support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/CD CI/CD context question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants