Skip to content

Commit

Permalink
Android support (#939)
Browse files Browse the repository at this point in the history
* Android support

* Android support

* CI updates

* Run swift-format

* CI updates

* CI updates

* Run swift-format

* CI updates

* CI updates

* CI updates

* Run swift-format

* CI updates

* CI updates

* Run swift-format

* CI fixes

* Run swift-format

* CI fixes

* Run swift-format

* CI fixes

* Update AssertSnapshot.swift

---------

Co-authored-by: marcprux <marcprux@users.noreply.github.com>
Co-authored-by: Stephen Celis <stephen@stephencelis.com>
Co-authored-by: Stephen Celis <stephen.celis@gmail.com>
  • Loading branch information
4 people authored Jan 24, 2025
1 parent a5dbd5d commit de5f975
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
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

0 comments on commit de5f975

Please sign in to comment.