From 98252fd2b10b75b97fc5de02778b7b3d36fea846 Mon Sep 17 00:00:00 2001 From: AJ Lauer Barinov Date: Tue, 5 Dec 2023 11:09:43 -0800 Subject: [PATCH] ci: run tests on push (#100) * test: adjust expected values for event payloads to account for differences in float precision --- .github/workflows/run-tests.yml | 16 ++++ .../Upload Tests/ReporterTests.swift | 78 +++++++++++++++---- scripts/run-unit-tests.sh | 44 +++++++++++ 3 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/run-tests.yml create mode 100755 scripts/run-unit-tests.sh diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 00000000..a5bb0085 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,16 @@ +name: Run Tests + +on: + push: + +jobs: + unit-tests: + name: Run Unit Tests + runs-on: macos-13 + steps: + - name: Install xcbeautify + run: brew install xcbeautify + - name: Checkout + uses: actions/checkout@v3 + - name: Run Tests + run: "scripts/run-unit-tests.sh MuxUploadSDK" diff --git a/Tests/MuxUploadSDKTests/Upload Tests/ReporterTests.swift b/Tests/MuxUploadSDKTests/Upload Tests/ReporterTests.swift index 0a726945..e2d8a9a1 100644 --- a/Tests/MuxUploadSDKTests/Upload Tests/ReporterTests.swift +++ b/Tests/MuxUploadSDKTests/Upload Tests/ReporterTests.swift @@ -9,7 +9,12 @@ import XCTest class ReporterTests: XCTestCase { - enum ExpectedJSONStrings { + /// There was a change in JSONEncoder behavior between + /// iOS 16 and 17 that affected precision when encoding + /// floats. It's immaterial in terms of data but does + /// require slightly different expected values when + /// testing. + enum ExpectedJSONStringsiOS16AndBelow { static let inputStandardizationFailed = #"{"data":{"app_name":"AcmeApp","app_version":"3.2.1","device_model":"iPad","error_description":"foo","input_duration":3.1400000000000001,"input_size":1500000,"maximum_resolution":"default","non_standard_input_reasons":[],"platform_name":"iPadOS","platform_version":"15.0.0","sdk_version":"0.4.1","standardization_end_time":"2023-07-07T03:43:58Z","standardization_start_time":"2023-07-07T03:38:58Z","upload_canceled":false,"upload_url":"https:\/\/www.example.com"},"session_id":"xyz789","type":"upload_input_standardization_failed","version":"1"}"# static let inputStandardizationSucceeded = #"{"data":{"app_name":"AcmeApp","app_version":"3.2.1","device_model":"iPad","input_duration":3.1400000000000001,"input_size":1500000,"maximum_resolution":"default","non_standard_input_reasons":[],"platform_name":"iPadOS","platform_version":"15.0.0","sdk_version":"0.4.1","standardization_end_time":"2023-07-07T03:43:58Z","standardization_start_time":"2023-07-07T03:38:58Z","upload_url":"https:\/\/www.example.com"},"session_id":"jkl567","type":"upload_input_standardization_succeeded","version":"1"}"# @@ -19,6 +24,16 @@ class ReporterTests: XCTestCase { static let uploadSucceeded = #"{"data":{"app_name":"AcmeApp","app_version":"3.2.1","device_model":"iPad","input_duration":3.1400000000000001,"input_size":1500000,"input_standardization_requested":true,"platform_name":"iPadOS","platform_version":"16.2.0","region_code":"US","sdk_version":"0.3.0","upload_end_time":"2023-07-07T04:12:48Z","upload_start_time":"2023-07-07T04:12:18Z","upload_url":"https:\/\/www.example.com"},"session_id":"abc123","type":"upload_succeeded","version":"1"}"# } + enum ExpectedJSONStringsiOS17 { + static let inputStandardizationFailed = #"{"data":{"app_name":"AcmeApp","app_version":"3.2.1","device_model":"iPad","error_description":"foo","input_duration":3.14,"input_size":1500000,"maximum_resolution":"default","non_standard_input_reasons":[],"platform_name":"iPadOS","platform_version":"15.0.0","sdk_version":"0.4.1","standardization_end_time":"2023-07-07T03:43:58Z","standardization_start_time":"2023-07-07T03:38:58Z","upload_canceled":false,"upload_url":"https:\/\/www.example.com"},"session_id":"xyz789","type":"upload_input_standardization_failed","version":"1"}"# + + static let inputStandardizationSucceeded = #"{"data":{"app_name":"AcmeApp","app_version":"3.2.1","device_model":"iPad","input_duration":3.14,"input_size":1500000,"maximum_resolution":"default","non_standard_input_reasons":[],"platform_name":"iPadOS","platform_version":"15.0.0","sdk_version":"0.4.1","standardization_end_time":"2023-07-07T03:43:58Z","standardization_start_time":"2023-07-07T03:38:58Z","upload_url":"https:\/\/www.example.com"},"session_id":"jkl567","type":"upload_input_standardization_succeeded","version":"1"}"# + + static let uploadFailed = #"{"data":{"app_name":"AcmeApp","app_version":"3.2.1","device_model":"iPad","error_description":"foo","input_duration":3.14,"input_size":1500000,"input_standardization_requested":false,"platform_name":"iPadOS","platform_version":"16.2.0","region_code":"US","sdk_version":"0.3.0","upload_end_time":"2023-07-07T04:12:48Z","upload_start_time":"2023-07-07T04:12:18Z","upload_url":"https:\/\/www.example.com"},"session_id":"abc123","type":"upload_failed","version":"1"}"# + + static let uploadSucceeded = #"{"data":{"app_name":"AcmeApp","app_version":"3.2.1","device_model":"iPad","input_duration":3.14,"input_size":1500000,"input_standardization_requested":true,"platform_name":"iPadOS","platform_version":"16.2.0","region_code":"US","sdk_version":"0.3.0","upload_end_time":"2023-07-07T04:12:48Z","upload_start_time":"2023-07-07T04:12:18Z","upload_url":"https:\/\/www.example.com"},"session_id":"abc123","type":"upload_succeeded","version":"1"}"# + } + var jsonEncoder = Reporter().jsonEncoder func testInputStandardizationFailedEventSerialization() throws { @@ -52,10 +67,18 @@ class ReporterTests: XCTestCase { ) ) - XCTAssertEqual( - json, - ExpectedJSONStrings.inputStandardizationFailed - ) + if #available(iOS 17, *) { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS17.inputStandardizationFailed + ) + } else { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS16AndBelow.inputStandardizationFailed + ) + } + } func testInputStandardizationSucceededEventSerialization() throws { @@ -87,10 +110,17 @@ class ReporterTests: XCTestCase { ) ) - XCTAssertEqual( - json, - ExpectedJSONStrings.inputStandardizationSucceeded - ) + if #available(iOS 17, *) { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS17.inputStandardizationSucceeded + ) + } else { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS16AndBelow.inputStandardizationSucceeded + ) + } } func testUploadFailedEventSerialization() throws { @@ -123,10 +153,17 @@ class ReporterTests: XCTestCase { ) ) - XCTAssertEqual( - json, - ExpectedJSONStrings.uploadFailed - ) + if #available(iOS 17, *) { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS17.uploadFailed + ) + } else { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS16AndBelow.uploadFailed + ) + } } func testUploadSucceededEventSerialization() throws { @@ -159,10 +196,17 @@ class ReporterTests: XCTestCase { ) ) - XCTAssertEqual( - json, - ExpectedJSONStrings.uploadSucceeded - ) + if #available(iOS 17, *) { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS17.uploadSucceeded + ) + } else { + XCTAssertEqual( + json, + ExpectedJSONStringsiOS16AndBelow.uploadSucceeded + ) + } } } diff --git a/scripts/run-unit-tests.sh b/scripts/run-unit-tests.sh new file mode 100755 index 00000000..e474b425 --- /dev/null +++ b/scripts/run-unit-tests.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +readonly XCODE=$(xcodebuild -version | grep Xcode | cut -d " " -f2) + +set -euo pipefail + +if [ $# -ne 1 ]; then + echo "▸ Usage: $0 SCHEME" + exit 1 +fi + +readonly SCHEME="$1" + +if ! command -v xcbeautify &> /dev/null +then + echo -e "\033[1;31m ERROR: xcbeautify could not be found please install it... \033[0m" + exit 1 +fi + +echo "▸ Selecting Xcode 15" + +sudo xcode-select -s /Applications/Xcode_15.0.app/Contents/Developer + +echo "▸ Using Xcode Version: ${XCODE}" + +echo "▸ Available Xcode SDKs" + +xcodebuild -showsdks + +echo "▸ Resolve Package Dependencies" + +xcodebuild -resolvePackageDependencies + +echo "▸ Available Schemes" + +xcodebuild -list -json + +echo "▸ Test ${SCHEME}" + +xcodebuild clean test \ + -scheme $SCHEME \ + -destination 'platform=iOS Simulator,OS=17.0.1,name=iPhone 15' \ + -sdk iphonesimulator17.0 \ + | xcbeautify