maciejmaciejewski / azure-pipelines-cucumber

MIT License
19 stars 19 forks source link

add ignoreBadJsonFile #44

Open dGiorgiana opened 2 years ago

dGiorgiana commented 2 years ago

I'm using the following configuration in pipeline

        - task: PublishCucumberReport@1
          condition: succeededOrFailed()
          inputs:
            jsonDir: '$(Pipeline.Workspace)/s/automation/target/cucumber-report'
            outputPath: '$(Pipeline.Workspace)/s/automation/target'
            theme: 'bootstrap'
            reportSuiteAsScenarios: true
            ignoreBadJsonFile: true
          displayName: generate cucumber report

But, from time to time I get the following error , when I aggregate in a single HTML report several cucumber Json reports :

Error: Invalid Cucumber JSON file found under /opt/vsts_agent/_work/1/s/automation/target/cucumber-report: /opt/vsts_agent/_work/1/s/automation/target/cucumber-report/69.json at mergeJSONS (/opt/vsts_agent/_work/_tasks/PublishCucumberReport_83c82c-532-11ea-8fab-bbeffcf287/1..7/reporter/node_modules/cucumber-html-reporter/lib/jsonDir.js:26:19) at Array.map () at Object.collectJSONS (/opt/vsts_agent/_work/_tasks/PublishCucumberReport_83c82c-532-11ea-8fab-bbeffcf287/1..7/reporter/node_modules/cucumber-html-reporter/lib/jsonDir.js:48:11) at Object.generate (/opt/vsts_agent/_work/_tasks/PublishCucumberReport_83c82c-532-11ea-8fab-bbeffcf287/1..7/reporter/node_modules/cucumber-html-reporter/lib/reporter.js:537:17) at generateReport (/opt/vsts_agent/_work/_tasks/PublishCucumberReport_83c82c-532-11ea-8fab-bbeffcf287/1..7/reporter/node_modules/cucumber-html-reporter/index.js:3:21) at Object. (/opt/vsts_agent/_work/_tasks/PublishCucumberReport_83c82c-532-11ea-8fab-bbeffcf287/1..7/reporter/script.js:2:1) at Module._compile (/modules/cjs/loader.js:172:14) at Object.Module._extensions..js (/modules/cjs/loader.js:111:1) at Module.load (/modules/cjs/loader.js:937:32) at Function.Module._load (***/modules/cjs/loader.js:778:12)

The file in question is an empty Array [] and the error makes sense .

But this a blocker for generating a HML report with all other correct json files .

I'm thinking that a solution is to add ignoreBadJsonFile option, but i'm not sure. It's default value in cucumber-html-report is false.

Thank you for your time

maciejmaciejewski commented 2 years ago

This is actually a great idea, as the third party report generator looks for all json files in given dir which causes the issue.

Ruchika-Jha commented 9 months ago

Thanks for the hint in the problem where empty array[] causes the problem. I came up with the approach below, and it is working for me. In my pipeline I am including a task to run bash script

I am addidng a script to ignore the empty array files,

!/bin/bash

Change directory to the target folder

cd /path to folder where files are kept

Loop through JSON files recursively

while IFS= read -r -d '' file; do if [ -s "$file" ]; then

Check if file is not empty

    echo "$file is not empty"

    # Check if file contents are not []
    if ! jq -e '.[]' "$file" >/dev/null 2>&1; then
        echo "Empty array found in $file"
        echo "Deleting $file..."
        rm "$file"
    fi
else
    # Delete empty file
    echo "$file is empty, deleting..."
    rm "$file"
fi

done < <(find . -type f -name '*.json' -print0)

In this script:

It checks if a file is not empty using [ -s "$file" ]. If the file is not empty, it checks if the file contents are not [] using jq -e '.[]' "$file". If jq returns successfully (i.e., no [] found), it means the file contains other content, and it proceeds to the next file. If jq fails (i.e., [] found), it means the file contains an empty array, and it gets deleted. If the file is empty, it gets deleted directly. This script ensures that only JSON files with non-empty content or non-empty arrays are retained, and all other files (including empty ones or those containing only []) are deleted.