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

Android support #939

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ jobs:
- uses: actions/checkout@v4
- run: swift build
- run: swift test

android:
strategy:
matrix:
swift:
- "6.0.2"
name: Android
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: skiptools/swift-android-action@v2
with:
swift-version: ${{ matrix.swift }}
copy-files: ${GITHUB_WORKSPACE}/Tests/SnapshotTestingTests/__Snapshots__
25 changes: 16 additions & 9 deletions Sources/SnapshotTesting/AssertSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,17 @@ public func verifySnapshot<Value, Format>(
let fileUrl = URL(fileURLWithPath: "\(filePath)", isDirectory: false)
let fileName = fileUrl.deletingPathExtension().lastPathComponent

#if os(Android)
// When running tests on Android, the CI script copies the Tests/SnapshotTestingTests/__Snapshots__ up to the temporary folder
let snapshotsBaseUrl = URL(
fileURLWithPath: "/data/local/tmp/android-xctest", isDirectory: true)
#else
let snapshotsBaseUrl = fileUrl.deletingLastPathComponent()
#endif

let snapshotDirectoryUrl =
snapshotDirectory.map { URL(fileURLWithPath: $0, isDirectory: true) }
?? fileUrl
.deletingLastPathComponent()
.appendingPathComponent("__Snapshots__")
.appendingPathComponent(fileName)
?? snapshotsBaseUrl.appendingPathComponent("__Snapshots__").appendingPathComponent(fileName)

let identifier: String
if let name = name {
Expand All @@ -316,10 +321,12 @@ public func verifySnapshot<Value, Format>(
}

let testName = sanitizePathComponent(testName)
let snapshotFileUrl =
var snapshotFileUrl =
snapshotDirectoryUrl
.appendingPathComponent("\(testName).\(identifier)")
.appendingPathExtension(snapshotting.pathExtension ?? "")
if let ext = snapshotting.pathExtension {
snapshotFileUrl = snapshotFileUrl.appendingPathExtension(ext)
}
let fileManager = FileManager.default
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)

Expand Down Expand Up @@ -359,7 +366,7 @@ public func verifySnapshot<Value, Format>(
try snapshotData.write(to: snapshotFileUrl)
}

#if !os(Linux) && !os(Windows)
#if !os(Android) && !os(Linux) && !os(Windows)
if !isSwiftTesting,
ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS")
{
Expand Down Expand Up @@ -446,7 +453,7 @@ public func verifySnapshot<Value, Format>(
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)

if !attachments.isEmpty {
#if !os(Linux) && !os(Windows)
#if !os(Linux) && !os(Android) && !os(Windows)
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS"),
!isSwiftTesting
{
Expand Down Expand Up @@ -501,7 +508,7 @@ func sanitizePathComponent(_ string: String) -> String {
.replacingOccurrences(of: "^-|-$", with: "", options: .regularExpression)
}

#if !os(Linux) && !os(Windows)
#if !os(Android) && !os(Linux) && !os(Windows)
import CoreServices

func uniformTypeIdentifier(fromExtension pathExtension: String) -> String? {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Common/XCTAttachment.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if os(Linux) || os(Windows)
#if os(Linux) || os(Android) || os(Windows)
import Foundation

public struct XCTAttachment {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SnapshotTesting/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public func _verifyInlineSnapshot<Value>(

/// Did not successfully record, so we will fail.
if !attachments.isEmpty {
#if !os(Linux) && !os(Windows)
#if !os(Linux) && !os(Android) && !os(Windows)
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") {
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
attachments.forEach {
Expand Down
3 changes: 3 additions & 0 deletions Tests/SnapshotTestingTests/RecordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class RecordTests: XCTestCase {
#endif

func testRecordFailed_NoFailure() throws {
#if os(Android)
throw XCTSkip("cannot save next to file on Android")
#endif
try Data("42".utf8).write(to: snapshotURL)
let modifiedDate =
try FileManager.default
Expand Down
Loading