Skip to content

Commit

Permalink
feat: add a custom-parameters parameter in the test job and comma…
Browse files Browse the repository at this point in the history
…nd. (#57)
  • Loading branch information
EricRibeiro authored Aug 25, 2023
1 parent 892859c commit 5270e57
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 50 deletions.
53 changes: 53 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ workflows:
filters: *filters
context: orb-testing-unity
pre-steps: *mono
- unity/test:
name: "test-linux-with-custom-parameters"
step-name: "Check if test filter in custom parameter works"
unity-license-var-name: "UNITY_ENCODED_LICENSE_2021"
unity-username-var-name: "UNITY_USERNAME"
unity-password-var-name: "UNITY_PASSWORD"
executor:
name: "unity/ubuntu"
target_platform: "linux-il2cpp"
editor_version: "2021.3.1f1"
resource_class: "medium"
project-path: "Unity2D-Demo-Game-CI-CD/src"
custom-parameters: "-testFilter EnemyMovesLeft -customParam1 potato -customParam2 $CIRCLE_WORKFLOW_ID"
test-platform: "playmode"
filters: *filters
context: orb-testing-unity
pre-steps: *mono
- unity/test:
name: "test-windows"
step-name: "Check if the tests run and results are uploaded"
Expand All @@ -117,6 +134,23 @@ workflows:
filters: *filters
context: orb-testing-unity
pre-steps: *mono
- unity/test:
name: "test-windows-with-custom-parameters"
step-name: "Check if test filter in custom parameter works"
unity-license-var-name: "UNITY_ENCODED_LICENSE_2021"
unity-username-var-name: "UNITY_USERNAME"
unity-password-var-name: "UNITY_PASSWORD"
executor:
name: "unity/windows-2019"
size: "large"
editor_version: "2021.3.2f1"
target_platform: "windows-il2cpp"
project-path: "Unity2D-Demo-Game-CI-CD/src"
custom-parameters: "-testFilter EnemyMovesLeft -customParam1 potato -customParam2 $CIRCLE_WORKFLOW_ID"
test-platform: "playmode"
filters: *filters
context: orb-testing-unity
pre-steps: *mono
- unity/test:
name: "test-osx"
step-name: "Check if the tests run and results are uploaded"
Expand All @@ -132,6 +166,22 @@ workflows:
filters: *filters
context: orb-testing-unity
pre-steps: *mono
- unity/test:
name: "test-osx-with-custom-parameters"
step-name: "Check if test filter in custom parameter works"
unity-license-var-name: "UNITY_ENCODED_LICENSE_2021"
unity-username-var-name: "UNITY_USERNAME"
unity-password-var-name: "UNITY_PASSWORD"
executor:
name: "unity/macos"
editor_version: "2021.3.1f1"
resource_class: "large"
project-path: "Unity2D-Demo-Game-CI-CD/src"
custom-parameters: "-testFilter EnemyMovesLeft -customParam1 potato -customParam2 $CIRCLE_WORKFLOW_ID"
test-platform: "playmode"
filters: *filters
context: orb-testing-unity
pre-steps: *mono

# IL2CPP Builds
- unity/build:
Expand Down Expand Up @@ -428,8 +478,11 @@ workflows:
requires:
- orb-tools/pack
- test-linux
- test-linux-with-custom-parameters
- test-windows
- test-windows-with-custom-parameters
- test-osx
- test-osx-with-custom-parameters
- build-linux64-il2cpp
- build-Windows64-il2cpp
- build-osx-il2cpp
Expand Down
9 changes: 9 additions & 0 deletions src/commands/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ parameters:
type: string
default: "10m"
description: Elapsed time the command can run without output.
custom-parameters:
type: string
default: ""
description: |
Additional arguments for the Unity CLI.
Use it to pass arguments to Unity's test options. Environment variables are supported.
The parameters must be separated by space and must be in the format "-key value" or "-key" for booleans.
Example: '-testFilter "MyNamespace.Something.MyTest" -assemblyNames "MyUnitTestAssembly" -testCategory "Unit;Integration"'.
steps:
- run:
Expand All @@ -42,6 +50,7 @@ steps:
environment:
PARAM_PROJECT_PATH: << parameters.project-path >>
PARAM_TEST_PLATFORM: << parameters.test-platform >>
PARAM_CUSTOM_PARAMETERS: << parameters.custom-parameters >>
SCRIPT_TEST_LINUX: << include(scripts/linux/test.sh) >>
SCRIPT_TEST_WINDOWS: << include(scripts/windows/test.sh) >>
SCRIPT_TEST_MACOS: << include(scripts/macos/test.sh) >>
Expand Down
9 changes: 9 additions & 0 deletions src/jobs/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ parameters:
type: string
default: "20m"
description: Elapsed time the command can run without output.
custom-parameters:
type: string
default: ""
description: |
Additional arguments for the Unity CLI.
Use it to pass arguments to Unity's test options. Environment variables are supported.
The parameters must be separated by space and must be in the format "-key value" or "-key" for booleans.
Example: '-testFilter "MyNamespace.Something.MyTest" -assemblyNames "MyUnitTestAssembly" -testCategory "Unit;Integration"'.
executor: << parameters.executor >>

Expand All @@ -73,6 +81,7 @@ steps:
test-platform: << parameters.test-platform >>
project-path: << parameters.project-path >>
no_output_timeout: << parameters.no_output_timeout>>
custom-parameters: << parameters.custom-parameters >>
- when:
condition: <<parameters.return-license>>
steps:
Expand Down
91 changes: 45 additions & 46 deletions src/scripts/linux/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,54 @@
# shellcheck shell=bash
# shellcheck disable=SC2154

trap_test_script_exit() {
local -r exit_status="$?"

if [ "$exit_status" -ne 0 ]; then
printf '%s\n' 'The script did not complete successfully.'
printf '%s\n' "The exit code was $exit_status"

rm -rf "$gameci_sample_project_dir"
exit "$exit_status"
fi

parse_results_to_junit() {
# Intall dependencies for the JUnit parser.
apt-get update && apt-get install -y default-jre libsaxonb-java

# Parse Unity's results xml to JUnit format.
printf '%s\n' "$DEPENDENCY_NUNIT_TRANSFORM" > "$base_dir/nunit3-junit.xslt"
saxonb-xslt -s "$UNITY_DIR/$TEST_PLATFORM-results.xml" -xsl "$base_dir/nunit3-junit.xslt" > "$UNITY_DIR/$TEST_PLATFORM-junit-results.xml"

cat "$UNITY_DIR/$TEST_PLATFORM-junit-results.xml"

# Clean up.
rm -rf "$gameci_sample_project_dir"
printf '%s\n' "$DEPENDENCY_NUNIT_TRANSFORM" >"$base_dir/nunit3-junit.xslt"
saxonb-xslt -s "$unity_project_full_path/$PARAM_TEST_PLATFORM-results.xml" -xsl "$base_dir/nunit3-junit.xslt" >"$unity_project_full_path/$PARAM_TEST_PLATFORM-junit-results.xml"
}

# Download test script.
curl --silent --location \
--request GET \
--url "https://gitlab.com/game-ci/unity3d-gitlab-ci-example/-/raw/main/ci/test.sh" \
--header 'Accept: application/vnd.github.v3+json' \
--output "$base_dir/test.sh"

chmod +x "$base_dir/test.sh"

# Name variables as required by the "test.sh" script.
readonly TEST_PLATFORM="$PARAM_TEST_PLATFORM"
readonly TESTING_TYPE="JUNIT"
readonly UNITY_DIR="$unity_project_full_path"
readonly CI_PROJECT_DIR="$gameci_sample_project_dir"
readonly CI_PROJECT_NAME="$CIRCLE_PROJECT_REPONAME"

export TEST_PLATFORM
export TESTING_TYPE
export UNITY_DIR
export CI_PROJECT_DIR
export CI_PROJECT_NAME

# Trap "test.sh" exit otherwise it won't be possible to parse the results to JUnit format.
trap trap_test_script_exit EXIT

# Run the test script.
# shellcheck source=/dev/null
source "$base_dir/test.sh"
set -x
# shellcheck disable=SC2086 # $custom_parameters needs to be unquoted. Otherwise it will be treated as a single parameter.
${UNITY_EXECUTABLE:-xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' unity-editor} \
-projectPath "$unity_project_full_path" \
-runTests \
-testPlatform "$PARAM_TEST_PLATFORM" \
-testResults "$unity_project_full_path"/"$PARAM_TEST_PLATFORM"-results.xml \
-logFile /dev/stdout \
-batchmode \
-nographics \
-enableCodeCoverage \
-coverageResultsPath "$unity_project_full_path"/"$PARAM_TEST_PLATFORM"-coverage \
-coverageOptions "generateAdditionalMetrics;generateHtmlReport;generateHtmlReportHistory;generateBadgeReport;" \
-debugCodeOptimization \
$custom_parameters
unity_exit_code=$?
set +x

if [ "$unity_exit_code" -eq 0 ] || [ "$unity_exit_code" -eq 2 ]; then
printf '%s\n' "Run succeeded. Exit code $unity_exit_code"
parse_results_to_junit
# Print the results to the console.
cat "$unity_project_full_path/$PARAM_TEST_PLATFORM-junit-results.xml"
else
printf '%s\n' "Run failed. Exit code $unity_exit_code"
fi

code_coverage_package="com.unity.testtools.codecoverage"
package_manifest_path="$unity_project_full_path/Packages/manifest.json"

# Check if the Code Coverage package is installed and move the coverage results to the root of the project.
if grep -q "$code_coverage_package" "$package_manifest_path"; then
grep "$unity_project_full_path"/"$PARAM_TEST_PLATFORM"-coverage/Report/Summary.xml Linecoverage
mv "$unity_project_full_path"/"$PARAM_TEST_PLATFORM"-coverage/"$CIRCLE_PROJECT_REPONAME"-opencov/*Mode/TestCoverageResults_*.xml "$unity_project_full_path"/"$PARAM_TEST_PLATFORM"-coverage/coverage.xml
rm -r "$unity_project_full_path"/"$PARAM_TEST_PLATFORM"-coverage/"$CIRCLE_PROJECT_REPONAME"-opencov/
else
{
echo -e "\033[33mCode Coverage package not found in $package_manifest_path. Please install the package \"Code Coverage\" through Unity's Package Manager to enable coverage reports.\033[0m"
} 2>/dev/null
fi

exit "$unity_exit_code"
4 changes: 3 additions & 1 deletion src/scripts/macos/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ parse_xml_to_junit() {

set -x
# Run the tests.
# shellcheck disable=SC2086 # $custom_parameters needs to be unquoted. Otherwise it will be treated as a single parameter.
"$UNITY_EDITOR_PATH" \
-batchmode \
-nographics \
-projectPath "$unity_project_full_path" \
-runTests \
-testPlatform "$PARAM_TEST_PLATFORM" \
-testResults "$base_dir/results.xml" \
-logfile /dev/stdout
-logfile /dev/stdout \
$custom_parameters

unity_exit_code=$?
set +x
Expand Down
3 changes: 3 additions & 0 deletions src/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ eval "$SCRIPT_UTILS"
# Detect host OS.
detect-os

# Expand custom parameters, if any.
custom_parameters="$(eval echo "$PARAM_CUSTOM_PARAMETERS")" && readonly custom_parameters

if [ "$PLATFORM" = "linux" ]; then eval "$SCRIPT_TEST_LINUX";
elif [ "$PLATFORM" = "macos" ]; then eval "$SCRIPT_TEST_MACOS";
elif [ "$PLATFORM" = "windows" ]; then eval "$SCRIPT_TEST_WINDOWS";
Expand Down
20 changes: 17 additions & 3 deletions src/scripts/windows/test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/false
# shellcheck shell=bash
# shellcheck disable=SC2154
# shellcheck disable=SC2154,SC2016

trap_exit() {
local exit_status="$?"
Expand All @@ -16,11 +16,25 @@ trap_exit() {
}
trap trap_exit EXIT

# Add the build target and build name in the environment variables.
# Add necessary values in the environment variables.
docker exec "$CONTAINER_NAME" powershell "[System.Environment]::SetEnvironmentVariable('TEST_PLATFORM','$PARAM_TEST_PLATFORM', [System.EnvironmentVariableTarget]::Machine)"
docker exec "$CONTAINER_NAME" powershell "[System.Environment]::SetEnvironmentVariable('CUSTOM_PARAMS','$custom_parameters', [System.EnvironmentVariableTarget]::Machine)"

test_args=(
'-batchmode'
'-nographics'
'-projectPath $Env:PROJECT_PATH'
'-runTests'
'-testPlatform $Env:TEST_PLATFORM'
'-testResults "C:/test/results.xml"'
)

[ -n "$custom_parameters" ] && test_args+=( '$Env:CUSTOM_PARAMS.split()' )

# Run the tests.
docker exec "$CONTAINER_NAME" powershell '& "C:\Program Files\Unity\Hub\Editor\*\Editor\Unity.exe" -batchmode -nographics -projectPath $Env:PROJECT_PATH -runTests -testPlatform $Env:TEST_PLATFORM -testResults "C:/test/results.xml" -logfile | Out-Host'
set -x
docker exec "$CONTAINER_NAME" powershell "& 'C:\Program Files\Unity\Hub\Editor\*\Editor\Unity.exe' ${test_args[*]} -logfile | Out-Host"
set +x

# Install JDK to run Saxon.
docker exec "$CONTAINER_NAME" powershell 'choco upgrade jdk8 --no-progress -y'
Expand Down

0 comments on commit 5270e57

Please sign in to comment.