Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run unit tests also on iOS Simulator #641

Merged
merged 17 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 91 additions & 4 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

unit-tests:

name: Run unit tests
name: Run unit tests (macOS)

runs-on: macos-13-xlarge
timeout-minutes: 30
Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:
uses: mikepenz/action-junit-report@v3
if: always()
with:
check_name: Test Report
check_name: Test Report (macOS)
report_paths: tests.xml
require_tests: true

Expand Down Expand Up @@ -105,9 +105,96 @@ jobs:
author=$(gh api https://api.github.com/repos/${{ github.repository }}/commits/${head_commit} --jq .author.login)
echo "commit_author=${author}" >> $GITHUB_OUTPUT

unit-tests-ios:

name: Run unit tests (iOS)

runs-on: macos-13-xlarge
timeout-minutes: 30

steps:

- name: Check out the code
uses: actions/checkout@v3
with:
submodules: recursive

- name: Set cache key hash
run: |
has_only_tags=$(jq '[ .pins[].state | has("version") ] | all' Package.resolved)
if [[ "$has_only_tags" == "true" ]]; then
echo "cache_key_hash=${{ hashFiles('Package.resolved') }}" >> $GITHUB_ENV
else
echo "Package.resolved contains dependencies specified by branch or commit, skipping cache."
fi

- name: Cache SPM
if: env.cache_key_hash
uses: actions/cache@v3
with:
path: DerivedData/SourcePackages
key: ${{ runner.os }}-spm-ios-${{ env.cache_key_hash }}
restore-keys: |
${{ runner.os }}-spm-ios-

- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode_$(<.xcode-version).app/Contents/Developer

- name: Resolve package dependencies
run: |
while xcodebuild -resolvePackageDependencies \
-scheme BrowserServicesKit-Package \
-destination 'platform=iOS Simulator,name=iPhone 15' \
-derivedDataPath DerivedData \
2>&1 | grep Error; do :; done

# you just can't have good things
# set -o pipefail && swift package resolve | tee ios-build-log.txt | xcbeautify
# mkdir -p DerivedData/SourcePackages
# mv .build/* DerivedData/SourcePackages

- name: Run tests
run: |
set -o pipefail && xcodebuild test \
-scheme BrowserServicesKit \
-destination 'platform=iOS Simulator,name=iPhone 15' \
-derivedDataPath DerivedData \
-skipPackagePluginValidation \
CODE_SIGNING_ALLOWED=NO \
| tee -a ios-build-log.txt \
| xcbeautify --report junit --report-path . --junit-report-filename ios-unittests.xml

- name: Publish Unit Tests Report
uses: mikepenz/action-junit-report@v3
if: always()
with:
check_name: Test Report (iOS)
report_paths: ios-unittests.xml
require_tests: true

- name: Update Asana with failed unit tests
if: always() # always run even if the previous step fails
env:
ASANA_ACCESS_TOKEN: ${{ secrets.ASANA_ACCESS_TOKEN }}
WORKFLOW_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/attempts/${{ github.run_attempt }}
run: |
# Extract failed tests from the junit report
# Only keep failures unique by classname and name (column 1 and 2 of the yq output)
yq < ios-unittests.xml -p xml -o json -r \
$'[.testsuites.testsuite[].testcase] | flatten | map(select(.failure) | .+@classname + " " + .+@name + " \'" + .failure.+@message + "\' ${{ env.WORKFLOW_URL }}") | .[]' \
| sort -u -k 1,2 \
| xargs -L 1 ./scripts/report-failed-unit-test.sh -s ${{ vars.APPLE_CI_FAILING_TESTS_FAILED_TESTS_SECTION_ID }}

- name: Upload logs
uses: actions/upload-artifact@v3
if: always()
with:
name: ios-build-log.txt
path: ios-build-log.txt

create-asana-task:
name: Create Asana Task
needs: [swiftlint, unit-tests]
needs: [swiftlint, unit-tests, unit-tests-ios]

if: failure() && github.ref_name == 'main' && github.run_attempt == 1

Expand All @@ -126,7 +213,7 @@ jobs:

close-asana-task:
name: Close Asana Task
needs: [swiftlint, unit-tests]
needs: [swiftlint, unit-tests, unit-tests-ios]

if: success() && github.ref_name == 'main' && github.run_attempt > 1

Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/content-scope-scripts",
"state" : {
"revision" : "0b68b0d404d8d4f32296cd84fa160b18b0aeaf44",
"version" : "4.59.1"
"revision" : "38ee7284bac7fa12d822fcaf0677ea3969d15fb1",
"version" : "4.59.2"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// limitations under the License.
//

import Cocoa
import Foundation
import CoreData
import Persistence
Expand Down
6 changes: 4 additions & 2 deletions Tests/BookmarksTests/BookmarkDatabaseCleanerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
// limitations under the License.
//

import XCTest
import Common
import CoreData
import Foundation
import Persistence
import XCTest
@testable import Bookmarks

final class MockEventMapper: EventMapping<BookmarksCleanupError> {
Expand Down Expand Up @@ -51,7 +53,7 @@ final class BookmarkDatabaseCleanerTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()

eventMapper = MockEventMapper()
Expand Down
4 changes: 3 additions & 1 deletion Tests/BookmarksTests/BookmarkEntityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// limitations under the License.
//

import CoreData
import Foundation
import XCTest
import Persistence
@testable import Bookmarks
Expand All @@ -34,7 +36,7 @@ final class BookmarkEntityTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/BookmarksTests/BookmarkListViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import BookmarksTestsUtils
import Common
import CoreData
import Foundation
import Persistence
import XCTest
@testable import Bookmarks
Expand Down Expand Up @@ -52,7 +54,7 @@ final class BookmarkListViewModelTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()
eventMapping = MockBookmarksModelErrorEventMapping { [weak self] event in
self?.firedEvents.append(event)
Expand Down
5 changes: 3 additions & 2 deletions Tests/BookmarksTests/BookmarkUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
//

import BookmarksTestsUtils
import XCTest
import Foundation
import Persistence
import XCTest
@testable import Bookmarks

final class BookmarkUtilsTests: XCTestCase {
Expand All @@ -35,7 +36,7 @@ final class BookmarkUtilsTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//

import BookmarksTestsUtils
import CoreData
import Foundation
import Persistence
import XCTest
Expand All @@ -36,7 +37,7 @@ final class BookmarkDomainsTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class BookmarksFaviconsFetcherTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()

faviconStore = MockFaviconStore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class FaviconsFetchOperationTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()

faviconStore = MockFaviconStore()
Expand Down
3 changes: 2 additions & 1 deletion Tests/BookmarksTests/FavoriteListViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import BookmarksTestsUtils
import Common
import Foundation
import Persistence
import XCTest
@testable import Bookmarks
Expand All @@ -39,7 +40,7 @@ final class FavoriteListViewModelTests: XCTestCase {
XCTFail("Failed to load model")
return
}
bookmarksDatabase = CoreDataDatabase(name: className, containerLocation: location, model: model)
bookmarksDatabase = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: model)
bookmarksDatabase.loadStore()
eventMapping = MockBookmarksModelErrorEventMapping { [weak self] event in
self?.firedEvents.append(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class RemoteMessagingConfigMatcherTests: XCTestCase {
func testWhenDeviceMatchesAnyRuleThenReturnFirstMatch() {
let remoteConfig = RemoteConfigModel(messages: [mediumMessage(matchingRules: [1, 2], exclusionRules: [])],
rules: [1: [LocaleMatchingAttribute(value: [Locale.current.identifier], fallback: nil)],
2: [OSMatchingAttribute(min: "0", max: "15", fallback: nil)]])
2: [OSMatchingAttribute(min: "0", max: "100", fallback: nil)]])

XCTAssertEqual(matcher.evaluate(remoteConfig: remoteConfig), mediumMessage(matchingRules: [1, 2], exclusionRules: []))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.
//

import Foundation
import XCTest
@testable import BrowserServicesKit
import class Persistence.CoreDataDatabase
Expand All @@ -38,7 +39,7 @@ final class AppHTTPSUpgradeStoreTests: XCTestCase {

location = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
bloomFilterUrl = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
database = CoreDataDatabase(name: className, containerLocation: location, model: HTTPSUpgrade.managedObjectModel)
database = CoreDataDatabase(name: type(of: self).description(), containerLocation: location, model: HTTPSUpgrade.managedObjectModel)
database.loadStore { _, error in
if let e = error {
XCTFail("Could not load store: \(e.localizedDescription)")
Expand Down
4 changes: 4 additions & 0 deletions Tests/NavigationTests/ClosureNavigationResponderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// limitations under the License.
//

#if os(macOS)

import Combine
import Common
import Swifter
Expand Down Expand Up @@ -434,3 +436,5 @@ class ClosureNavigationResponderTests: DistributedNavigationDelegateTestsBase {

// swiftlint:enable unused_closure_parameter
// swiftlint:enable opening_brace

#endif
Loading
Loading