diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 576b2c3..7745c24 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -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" @@ -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" @@ -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: @@ -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 diff --git a/src/commands/test.yml b/src/commands/test.yml index 74cbe9a..32e6325 100644 --- a/src/commands/test.yml +++ b/src/commands/test.yml @@ -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: @@ -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) >> diff --git a/src/jobs/test.yml b/src/jobs/test.yml index 632f73e..9a21adf 100644 --- a/src/jobs/test.yml +++ b/src/jobs/test.yml @@ -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 >> @@ -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: <> steps: diff --git a/src/scripts/linux/test.sh b/src/scripts/linux/test.sh index 0008a52..0d4de6b 100644 --- a/src/scripts/linux/test.sh +++ b/src/scripts/linux/test.sh @@ -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" \ No newline at end of file +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" diff --git a/src/scripts/macos/test.sh b/src/scripts/macos/test.sh index dacd4ba..9ed053f 100644 --- a/src/scripts/macos/test.sh +++ b/src/scripts/macos/test.sh @@ -71,6 +71,7 @@ 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 \ @@ -78,7 +79,8 @@ set -x -runTests \ -testPlatform "$PARAM_TEST_PLATFORM" \ -testResults "$base_dir/results.xml" \ - -logfile /dev/stdout + -logfile /dev/stdout \ + $custom_parameters unity_exit_code=$? set +x diff --git a/src/scripts/test.sh b/src/scripts/test.sh index 4f8b2ce..203615b 100644 --- a/src/scripts/test.sh +++ b/src/scripts/test.sh @@ -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"; diff --git a/src/scripts/windows/test.sh b/src/scripts/windows/test.sh index db7bf85..6d217c4 100644 --- a/src/scripts/windows/test.sh +++ b/src/scripts/windows/test.sh @@ -1,6 +1,6 @@ #!/bin/false # shellcheck shell=bash -# shellcheck disable=SC2154 +# shellcheck disable=SC2154,SC2016 trap_exit() { local exit_status="$?" @@ -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'