Skip to content

Commit de5f975

Browse files
Android support (#939)
* 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>
1 parent a5dbd5d commit de5f975

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

.github/workflows/ci.yml

+14
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ jobs:
6363
- uses: actions/checkout@v4
6464
- run: swift build
6565
- run: swift test
66+
67+
android:
68+
strategy:
69+
matrix:
70+
swift:
71+
- "6.0.2"
72+
name: Android
73+
runs-on: ubuntu-latest
74+
steps:
75+
- uses: actions/checkout@v4
76+
- uses: skiptools/swift-android-action@v2
77+
with:
78+
swift-version: ${{ matrix.swift }}
79+
copy-files: ${GITHUB_WORKSPACE}/Tests/SnapshotTestingTests/__Snapshots__

Sources/SnapshotTesting/AssertSnapshot.swift

+16-9
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,17 @@ public func verifySnapshot<Value, Format>(
296296
let fileUrl = URL(fileURLWithPath: "\(filePath)", isDirectory: false)
297297
let fileName = fileUrl.deletingPathExtension().lastPathComponent
298298

299+
#if os(Android)
300+
// When running tests on Android, the CI script copies the Tests/SnapshotTestingTests/__Snapshots__ up to the temporary folder
301+
let snapshotsBaseUrl = URL(
302+
fileURLWithPath: "/data/local/tmp/android-xctest", isDirectory: true)
303+
#else
304+
let snapshotsBaseUrl = fileUrl.deletingLastPathComponent()
305+
#endif
306+
299307
let snapshotDirectoryUrl =
300308
snapshotDirectory.map { URL(fileURLWithPath: $0, isDirectory: true) }
301-
?? fileUrl
302-
.deletingLastPathComponent()
303-
.appendingPathComponent("__Snapshots__")
304-
.appendingPathComponent(fileName)
309+
?? snapshotsBaseUrl.appendingPathComponent("__Snapshots__").appendingPathComponent(fileName)
305310

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

318323
let testName = sanitizePathComponent(testName)
319-
let snapshotFileUrl =
324+
var snapshotFileUrl =
320325
snapshotDirectoryUrl
321326
.appendingPathComponent("\(testName).\(identifier)")
322-
.appendingPathExtension(snapshotting.pathExtension ?? "")
327+
if let ext = snapshotting.pathExtension {
328+
snapshotFileUrl = snapshotFileUrl.appendingPathExtension(ext)
329+
}
323330
let fileManager = FileManager.default
324331
try fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)
325332

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

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

448455
if !attachments.isEmpty {
449-
#if !os(Linux) && !os(Windows)
456+
#if !os(Linux) && !os(Android) && !os(Windows)
450457
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS"),
451458
!isSwiftTesting
452459
{
@@ -501,7 +508,7 @@ func sanitizePathComponent(_ string: String) -> String {
501508
.replacingOccurrences(of: "^-|-$", with: "", options: .regularExpression)
502509
}
503510

504-
#if !os(Linux) && !os(Windows)
511+
#if !os(Android) && !os(Linux) && !os(Windows)
505512
import CoreServices
506513

507514
func uniformTypeIdentifier(fromExtension pathExtension: String) -> String? {

Sources/SnapshotTesting/Common/XCTAttachment.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if os(Linux) || os(Windows)
1+
#if os(Linux) || os(Android) || os(Windows)
22
import Foundation
33

44
public struct XCTAttachment {

Sources/SnapshotTesting/Internal/Deprecations.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public func _verifyInlineSnapshot<Value>(
136136

137137
/// Did not successfully record, so we will fail.
138138
if !attachments.isEmpty {
139-
#if !os(Linux) && !os(Windows)
139+
#if !os(Linux) && !os(Android) && !os(Windows)
140140
if ProcessInfo.processInfo.environment.keys.contains("__XCODE_BUILT_PRODUCTS_DIR_PATHS") {
141141
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
142142
attachments.forEach {

Tests/SnapshotTestingTests/RecordTests.swift

+3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ class RecordTests: XCTestCase {
162162
#endif
163163

164164
func testRecordFailed_NoFailure() throws {
165+
#if os(Android)
166+
throw XCTSkip("cannot save next to file on Android")
167+
#endif
165168
try Data("42".utf8).write(to: snapshotURL)
166169
let modifiedDate =
167170
try FileManager.default

0 commit comments

Comments
 (0)