Skip to content

Commit

Permalink
Code Coverage Support (#182)
Browse files Browse the repository at this point in the history
* Added basic framework for enable code coverage

* Added basic coverage results building and combination

* fixed ENABLE_CODE_COVERAGE to be `true` or `false`

* Added code coverage project to manifest

* Updated to add more tests for code coverage build

* Updated coverage parameter documentation

* Fixed small syntax error

* Enabled code coverage flag for code coverage tests

* Fixed error in test file build

* Updated project run settings

* Fixed error when creating combined code coverage results

* Updated testing workflows

* updated test workflows

* Updated parameters and added tests

* Updated tests and bash script for running

* Updated run_tests.sh script to simplfy some parameters

* Updated run_tests to remove incorrect ';'

* Updated run_tests script

* Fixed small syntax error

* Fixed for loop in run_tests.sh

* Updated run_tests syntax error for '=' operator

* Fixed runTests variable assignment

* Fixed parameters for running tests via bash

* Corrected bash arguments

* Updated test cases in main.yml

* Updated parameter names and default values for code coverage

* Fixed broken paths for coverage results upload in main.yml

* Corrected names of coverage results artifacts
  • Loading branch information
nicholas-maltbie authored Apr 21, 2022
1 parent ec4f392 commit 9656246
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 107 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ jobs:
projectPath: ${{ matrix.projectPath }}
unityVersion: ${{ matrix.unityVersion }}
testMode: all
coverageOptions: 'enableCyclomaticComplexity;generateHtmlReport;generateBadgeReport;assemblyFilters:+MyScripts'
# Test implicit artifactsPath, by not setting it

# Upload artifacts
Expand All @@ -115,6 +116,14 @@ jobs:
path: ${{ steps.allTests.outputs.artifactsPath }}
retention-days: 14

# Upload coverage
- name: Upload coverage results
uses: actions/upload-artifact@v3
with:
name: Coverage results (all)
path: ${{ steps.allTests.outputs.coveragePath }}
retention-days: 14

testRunnerInEditMode:
name: Test edit mode 📝
runs-on: ubuntu-latest
Expand Down Expand Up @@ -162,6 +171,14 @@ jobs:
path: ${{ steps.editMode.outputs.artifactsPath }}
retention-days: 14

# Upload coverage
- name: Upload coverage results
uses: actions/upload-artifact@v3
with:
name: Coverage results (edit mode)
path: ${{ steps.editMode.outputs.coveragePath }}
retention-days: 14

testRunnerInPlayMode:
name: Test play mode 📺
runs-on: ubuntu-latest
Expand Down Expand Up @@ -209,6 +226,14 @@ jobs:
path: ${{ steps.playMode.outputs.artifactsPath }}
retention-days: 14

# Upload coverage
- name: Upload coverage results
uses: actions/upload-artifact@v3
with:
name: Coverage results (play mode)
path: ${{ steps.playMode.outputs.coveragePath }}
retention-days: 14

testEachModeSequentially:
name: Test each mode sequentially 👩‍👩‍👧‍👦 # don't try this at home (it's much slower)
runs-on: ubuntu-latest
Expand Down
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
required: false
default: 'all'
description: 'The type of tests to be run by the test runner.'
coverageOptions:
required: false
default: 'enableCyclomaticComplexity;generateHtmlReport;generateBadgeReport'
description: 'Optional coverage parameters for the -coverageOptions argument.'
artifactsPath:
required: false
default: 'artifacts'
Expand All @@ -46,7 +50,9 @@ inputs:
description: 'Name for the check run that is created when a github token is provided.'
outputs:
artifactsPath:
description: 'Path where the artifacts are stored'
description: 'Path where the artifacts are stored.'
coveragePath:
description: 'Path where the code coverage results are stored.'
branding:
icon: 'box'
color: 'gray-dark'
Expand Down
18 changes: 15 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

136 changes: 35 additions & 101 deletions dist/steps/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,18 @@ echo "Using project path \"$UNITY_PROJECT_PATH\"."
echo "Using artifacts path \"$ARTIFACTS_PATH\" to save test results."
FULL_ARTIFACTS_PATH=$GITHUB_WORKSPACE/$ARTIFACTS_PATH

#
# Set and display the coverage results path
#

echo "Using coverage results path \"$COVERAGE_RESULTS_PATH\" to save test coverage results."
FULL_COVERAGE_RESULTS_PATH=$GITHUB_WORKSPACE/$COVERAGE_RESULTS_PATH

#
# Display custom parameters
#
echo "Using custom parameters $CUSTOM_PARAMETERS."

# Set the modes for testing
case $TEST_MODE in
editmode)
echo "Edit mode selected for testing."
EDIT_MODE=true
;;
playmode)
echo "Play mode selected for testing."
PLAY_MODE=true
;;
*)
echo "All modes selected for testing."
EDIT_MODE=true
PLAY_MODE=true
;;
esac
echo "Using custom parameters $CUSTOM_PARAMETERS."

# The following tests are 2019 mode (requires Unity 2019.2.11f1 or later)
# Reference: https://docs.unity3d.com/2019.3/Documentation/Manual/CommandLineArguments.html
Expand Down Expand Up @@ -65,118 +56,61 @@ echo ""
ls -alh $UNITY_PROJECT_PATH

#
# Testing in EditMode
# Testing for each platform
#
EDIT_MODE_EXIT_CODE=0
if [ "$EDIT_MODE" = "true" ]; then
for platform in ${TEST_PLATFORMS//;/ }; do
echo ""
echo "###########################"
echo "# Testing in EditMode #"
echo "# Testing in $platform #"
echo "###########################"
echo ""
unity-editor \
-batchmode \
-logFile "$FULL_ARTIFACTS_PATH/editmode.log" \
-projectPath "$UNITY_PROJECT_PATH" \
-runTests \
-testPlatform editmode \
-testResults "$FULL_ARTIFACTS_PATH/editmode-results.xml" \
$CUSTOM_PARAMETERS

# Catch exit code
EDIT_MODE_EXIT_CODE=$?

# Print unity log output
cat "$FULL_ARTIFACTS_PATH/editmode.log"

# Display results
if [ $EDIT_MODE_EXIT_CODE -eq 0 ]; then
echo "Run succeeded, no failures occurred";
elif [ $EDIT_MODE_EXIT_CODE -eq 2 ]; then
echo "Run succeeded, some tests failed";
elif [ $EDIT_MODE_EXIT_CODE -eq 3 ]; then
echo "Run failure (other failure)";
if [[ "$platform" != "COMBINE_RESULTS" ]]; then
runTests="-runTests -testPlatform $platform -testResults $FULL_ARTIFACTS_PATH/$platform-results.xml"
else
echo "Unexpected exit code $EDIT_MODE_EXIT_CODE";
runTests="-quit"
fi
fi

#
# Testing in PlayMode
#
PLAY_MODE_EXIT_CODE=0
if [ "$PLAY_MODE" = "true" ]; then
echo ""
echo "###########################"
echo "# Testing in PlayMode #"
echo "###########################"
echo ""
unity-editor \
-batchmode \
-logFile "$FULL_ARTIFACTS_PATH/playmode.log" \
-logFile "$FULL_ARTIFACTS_PATH/$platform.log" \
-projectPath "$UNITY_PROJECT_PATH" \
-runTests \
-testPlatform playmode \
-testResults "$FULL_ARTIFACTS_PATH/playmode-results.xml" \
-coverageResultsPath "$FULL_COVERAGE_RESULTS_PATH" \
$runTests \
-enableCodeCoverage \
-debugCodeOptimization \
-coverageOptions "$COVERAGE_OPTIONS" \
$CUSTOM_PARAMETERS

# Catch exit code
PLAY_MODE_EXIT_CODE=$?
TEST_EXIT_CODE=$?

# Print unity log output
cat "$FULL_ARTIFACTS_PATH/playmode.log"
cat "$FULL_ARTIFACTS_PATH/$platform.log"

# Display results
if [ $PLAY_MODE_EXIT_CODE -eq 0 ]; then
if [ $TEST_EXIT_CODE -eq 0 ]; then
echo "Run succeeded, no failures occurred";
elif [ $PLAY_MODE_EXIT_CODE -eq 2 ]; then
elif [ $TEST_EXIT_CODE -eq 2 ]; then
echo "Run succeeded, some tests failed";
elif [ $PLAY_MODE_EXIT_CODE -eq 3 ]; then
elif [ $TEST_EXIT_CODE -eq 3 ]; then
echo "Run failure (other failure)";
else
echo "Unexpected exit code $PLAY_MODE_EXIT_CODE";
echo "Unexpected exit code $TEST_EXIT_CODE";
fi
fi

#
# Results
#

echo ""
echo "###########################"
echo "# Project directory #"
echo "###########################"
echo ""
ls -alh $UNITY_PROJECT_PATH

if [ "$EDIT_MODE" = "true" ]; then
echo ""
echo "###########################"
echo "# Edit Mode Results #"
echo "###########################"
echo ""
cat "$FULL_ARTIFACTS_PATH/editmode-results.xml"
cat "$FULL_ARTIFACTS_PATH/editmode-results.xml" | grep test-run | grep Passed
fi
if [ $TEST_EXIT_CODE -ne 0 ]; then
TEST_RUNNER_EXIT_CODE=$TEST_EXIT_CODE
fi

if [ "$PLAY_MODE" = "true" ]; then
echo ""
echo "###########################"
echo "# Play Mode Results #"
echo "# $platform Results #"
echo "###########################"
echo ""
cat "$FULL_ARTIFACTS_PATH/playmode-results.xml"
cat "$FULL_ARTIFACTS_PATH/playmode-results.xml" | grep test-run | grep Passed
fi

#
# Exit
#

if [ $EDIT_MODE_EXIT_CODE -gt 0 ]; then
TEST_RUNNER_EXIT_CODE=$EDIT_MODE_EXIT_CODE
fi

if [ $PLAY_MODE_EXIT_CODE -gt 0 ]; then
TEST_RUNNER_EXIT_CODE=$PLAY_MODE_EXIT_CODE
fi
if [[ "$platform" != "COMBINE_RESULTS" ]]; then
cat "$FULL_ARTIFACTS_PATH/$platform-results.xml"
cat "$FULL_ARTIFACTS_PATH/$platform-results.xml" | grep test-run | grep Passed
fi
done
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async function run() {
projectPath,
customParameters,
testMode,
coverageOptions,
artifactsPath,
useHostNetwork,
sshAgent,
Expand All @@ -30,6 +31,7 @@ async function run() {
projectPath,
customParameters,
testMode,
coverageOptions,
artifactsPath,
useHostNetwork,
sshAgent,
Expand All @@ -39,6 +41,7 @@ async function run() {
});
} finally {
await Output.setArtifactsPath(artifactsPath);
await Output.setCoveragePath('CodeCoverage');
}

if (githubToken) {
Expand Down
8 changes: 7 additions & 1 deletion src/model/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Docker = {
projectPath,
customParameters,
testMode,
coverageOptions,
artifactsPath,
useHostNetwork,
sshAgent,
Expand All @@ -23,6 +24,9 @@ const Docker = {
if (!existsSync(githubHome)) mkdirSync(githubHome);
const githubWorkflow = path.join(runnerTemporaryPath, '_github_workflow');
if (!existsSync(githubWorkflow)) mkdirSync(githubWorkflow);
const testPlatforms = (
testMode === 'all' ? ['playmode', 'editmode', 'COMBINE_RESULTS'] : [testMode]
).join(';');

const command = `docker run \
--workdir /github/workspace \
Expand All @@ -35,7 +39,9 @@ const Docker = {
--env UNITY_VERSION="${editorVersion}" \
--env PROJECT_PATH="${projectPath}" \
--env CUSTOM_PARAMETERS="${customParameters}" \
--env TEST_MODE="${testMode}" \
--env TEST_PLATFORMS="${testPlatforms}" \
--env COVERAGE_OPTIONS="${coverageOptions}" \
--env COVERAGE_RESULTS_PATH="CodeCoverage" \
--env ARTIFACTS_PATH="${artifactsPath}" \
--env GITHUB_REF \
--env GITHUB_SHA \
Expand Down
Loading

0 comments on commit 9656246

Please sign in to comment.