Skip to content

Commit

Permalink
Inline bashscript as js
Browse files Browse the repository at this point in the history
  • Loading branch information
alsi-lawr committed Aug 31, 2024
1 parent 0e7be34 commit f0d8603
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
62 changes: 61 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26158,6 +26158,64 @@ function version(uuid) {
var _default = version;
exports["default"] = _default;

/***/ }),

/***/ 74:
/***/ ((module) => {

const script = `#!/bin/bash

echo "Running unit tests for $UNIT_TEST_PROJECT"
echo "Dotnet version used: "
dotnet --version

# Show parameters
echo "UNIT_TEST_PROJECT: $UNIT_TEST_PROJECT"
echo "UNIT_TEST_COVERAGE_THRESHOLD: $UNIT_TEST_COVERAGE_THRESHOLD"

# Export .NET tools
export PATH="$PATH:/root/.dotnet/tools"

# Install report generator tool
dotnet tool install -g dotnet-reportgenerator-globaltool

# Install required packages
dotnet add $UNIT_TEST_PROJECT package coverlet.msbuild
dotnet add $UNIT_TEST_PROJECT package coverlet.collector

# Restore unit test project
dotnet restore -s "https://api.nuget.org/v3/index.json" $UNIT_TEST_PROJECT

# Sanitise Exclude Modules
if [ -z "$UNIT_TEST_EXCLUDE_MODULES" ]; then
UNIT_TEST_EXCLUDE_MODULES="[xunit.*]*"
else
UNIT_TEST_EXCLUDE_MODULES="[xunit.*]*,$UNIT_TEST_EXCLUDE_MODULES"
fi

# Run unit tests and collect code coverage
dotnet build $UNIT_TEST_PROJECT
export IS_CI="true"
dotnet test $UNIT_TEST_PROJECT \
/p:CollectCoverage=true \
/p:CoverletOutputFormat=cobertura \
/p:CoverletOutput=lcov \
/p:Threshold=$UNIT_TEST_COVERAGE_THRESHOLD \
/p:ThresholdType=line \
/p:ThresholdStat=total \
/p:Exclude=\"$UNIT_TEST_EXCLUDE_MODULES\" \
/p:ExcludeByFile=\"$UNIT_TEST_EXCLUDE_FILES\"

# Generate code coverage report
reportgenerator "-reports:${UNIT_TEST_PROJECT}/lcov*.cobertura.xml" "-targetdir:${UNIT_TEST_PROJECT}/report" "-reporttypes:Html"

# Copy the cobertura xml file for inline coverage analysis
cp ${UNIT_TEST_PROJECT}/*.cobertura.xml ${UNIT_TEST_PROJECT}/report
`;

module.exports = script;


/***/ }),

/***/ 9491:
Expand Down Expand Up @@ -28068,6 +28126,7 @@ var __webpack_exports__ = {};
(() => {
const core = __nccwpck_require__(2186);
const exec = __nccwpck_require__(1514);
const script = __nccwpck_require__(74);

async function run() {
try {
Expand All @@ -28093,9 +28152,10 @@ async function run() {
'-e', `UNIT_TEST_EXCLUDE_FILES=${excludeFiles}`,
'-e', `UNIT_TEST_EXCLUDE_MODULES=${excludeModules}`,
'-e', `UNIT_TEST_COVERAGE_THRESHOLD=${threshold}`,
'-e', `UNIT_TEST_PROJECT=${project}`,
'-w', '/workspace',
imageName,
'bash', '-c', `/ci/test.sh /workspace/${project}`
'bash', '-c', script
]);

// Optionally, set an output for the action (e.g., path to coverage report)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "dotnet-test-coverlet",
"version": "1.0.0",
"description": "This reusable action runs unit tests on a .NET project and generates a code coverage report. It supports configuration for custom test project paths, exclusion of files and modules from coverage, and setting a coverage threshold.",
"main": "index.js",
"main": "dist/index.js",
"directories": {
"doc": "docs",
"test": "test"
},
"scripts": {
"build": "ncc build index.js -o dist",
"build": "ncc build src/index.js -o dist",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
Expand Down
4 changes: 3 additions & 1 deletion index.js → src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const core = require("@actions/core");
const exec = require("@actions/exec");
const script = require("./script");

async function run() {
try {
Expand All @@ -25,9 +26,10 @@ async function run() {
'-e', `UNIT_TEST_EXCLUDE_FILES=${excludeFiles}`,
'-e', `UNIT_TEST_EXCLUDE_MODULES=${excludeModules}`,
'-e', `UNIT_TEST_COVERAGE_THRESHOLD=${threshold}`,
'-e', `UNIT_TEST_PROJECT=${project}`,
'-w', '/workspace',
imageName,
'bash', '-c', `/ci/test.sh /workspace/${project}`
'bash', '-c', script
]);

// Optionally, set an output for the action (e.g., path to coverage report)
Expand Down
7 changes: 4 additions & 3 deletions src/test.sh → src/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/bin/bash

UNIT_TEST_PROJECT=$1
const script = `#!/bin/bash
echo "Running unit tests for $UNIT_TEST_PROJECT"
echo "Dotnet version used: "
Expand Down Expand Up @@ -48,3 +46,6 @@ reportgenerator "-reports:${UNIT_TEST_PROJECT}/lcov*.cobertura.xml" "-targetdir:
# Copy the cobertura xml file for inline coverage analysis
cp ${UNIT_TEST_PROJECT}/*.cobertura.xml ${UNIT_TEST_PROJECT}/report
`;

module.exports = script;

0 comments on commit f0d8603

Please sign in to comment.