scolladon / sfdx-git-delta

Generate the sfdx content in source format from two git commits
Other
445 stars 114 forks source link

How to run Specified test class in bitbucket pipeline #902

Closed shreyagoyal06 closed 3 months ago

shreyagoyal06 commented 3 months ago

Issue verification check:

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

scolladon commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

Okay, thanks for your time and support.