Skip to content

Commit 2d2925f

Browse files
authored
Add Travis CI for linux testing! (pointfreeco#33)
* wip * all tests * work around for suffix * Revert "work around for suffix" This reverts commit 489d50a. * import foundation * Revert "Revert "work around for suffix"" This reverts commit 85ec24c. * attachment stuff * wip * wip * omg * omgomg * wip * wip * clean up * clean up * does this work * pr feedback * clean * another linux note
1 parent d9fdff4 commit 2d2925f

File tree

11 files changed

+88
-20
lines changed

11 files changed

+88
-20
lines changed

.dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.git/
2+
.build/
3+
!.build/checkouts/
4+
!.build/repositories/
5+
!.build/workspace-state.json

.swift-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.0

.travis.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
os:
2+
- linux
3+
- osx
4+
env:
5+
language: generic
6+
sudo: required
7+
dist: trusty
8+
osx_image: xcode9.1
9+
install:
10+
- if [ $TRAVIS_OS_NAME = linux ]; then
11+
eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)";
12+
fi
13+
script:
14+
- swift test

Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM swift:4.0
2+
3+
WORKDIR /package
4+
5+
COPY . ./
6+
7+
RUN swift package resolve
8+
RUN swift package clean
9+
CMD swift test --parallel

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
docker build --tag snapshot-testing . && docker run --rm snapshot-testing

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# swift-snapshot-testing [![CircleCI](https://circleci.com/gh/pointfreeco/swift-snapshot-testing.svg?style=svg)](https://circleci.com/gh/pointfreeco/swift-snapshot-testing)
1+
# swift-snapshot-testing
2+
3+
macOS [![CircleCI](https://circleci.com/gh/pointfreeco/swift-snapshot-testing.svg?style=svg)](https://circleci.com/gh/pointfreeco/swift-snapshot-testing) Linux [![Build Status](https://travis-ci.org/pointfreeco/swift-snapshot-testing.svg?branch=master)](https://travis-ci.org/pointfreeco/swift-snapshot-testing)
24

35
A library that records app data into test assertions. Because snapshot tests can capture the entirety of a data structure, they can cover far more surface area than a typical unit test.
46

Sources/Diff/Diff.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
public enum DiffType {
24
case first
35
case second
@@ -83,8 +85,8 @@ public struct Hunk {
8385
}
8486
}
8587

86-
public func chunk<S: StringProtocol>(diff diffs: [Diff<S>], context ctx: Int = 4) -> [Hunk] {
87-
func prepending(_ prefix: String) -> (S) -> String {
88+
public func chunk(diff diffs: [Diff<String>], context ctx: Int = 4) -> [Hunk] {
89+
func prepending(_ prefix: String) -> (String) -> String {
8890
return { prefix + $0 + ($0.hasSuffix(" ") ? "¬" : "") }
8991
}
9092
let changed: (Hunk) -> Bool = { $0.lines.contains(where: { $0.hasPrefix(minus) || $0.hasPrefix(plus) }) }
@@ -102,11 +104,11 @@ public func chunk<S: StringProtocol>(diff diffs: [Diff<S>], context ctx: Int = 4
102104
fstLen: ctx,
103105
sndIdx: current.sndIdx + current.sndLen + len - ctx,
104106
sndLen: ctx,
105-
lines: (diff.elements.suffix(ctx) as ArraySlice<S>).map(prepending(figureSpace))
107+
lines: (diff.elements.suffix(ctx) as ArraySlice<String>).map(prepending(figureSpace))
106108
)
107109
return (next, changed(hunk) ? hunks + [hunk] : hunks)
108110
case .both where current.lines.isEmpty:
109-
let lines = (diff.elements.suffix(ctx) as ArraySlice<S>).map(prepending(figureSpace))
111+
let lines = (diff.elements.suffix(ctx) as ArraySlice<String>).map(prepending(figureSpace))
110112
let count = lines.count
111113
return (current + Hunk(idx: len - count, len: count, lines: lines), hunks)
112114
case .both:

Sources/SnapshotTesting/Diffable.swift

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import Diff
22
import Foundation
33
import XCTest
44

5+
#if os(Linux)
6+
// NB: Linux doesn't have XCTAttachment, so stubbing out the minimal interface.
7+
public struct XCTAttachment {}
8+
extension XCTAttachment {
9+
public init(string: String) {
10+
}
11+
}
12+
#endif
13+
514
public protocol Diffable {
615
static var diffablePathExtension: String? { get }
716
static func diffableDiff(_ fst: Self, _ snd: Self) -> (String, [XCTAttachment])?
@@ -39,8 +48,8 @@ extension String: Diffable {
3948
guard fst != snd else { return nil }
4049

4150
let hunks = chunk(diff: diff(
42-
fst.split(separator: "\n", omittingEmptySubsequences: false),
43-
snd.split(separator: "\n", omittingEmptySubsequences: false)
51+
fst.split(separator: "\n", omittingEmptySubsequences: false).map(String.init),
52+
snd.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
4453
))
4554
let failure = hunks.flatMap { [$0.patchMark] + $0.lines }.joined(separator: "\n")
4655

Sources/SnapshotTesting/SnapshotTesting.swift

+19-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public func assertSnapshot<S: Snapshot>(
4848
}()
4949

5050
let testName: String = {
51-
let testIdentifier = "\(snapshotDirectoryUrl):\(function)"
51+
let testIdentifier = "\(snapshotDirectoryUrl.absoluteString):\(function)"
5252
counter[testIdentifier, default: 0] += 1
5353
return "\(function.dropLast(2)).\(counter[testIdentifier]!)"
5454
}()
@@ -60,26 +60,32 @@ public func assertSnapshot<S: Snapshot>(
6060
try! fileManager.createDirectory(at: snapshotDirectoryUrl, withIntermediateDirectories: true)
6161

6262
defer {
63-
staleSnapshots[snapshotDirectoryUrl, default: Set(
64-
try! fileManager.contentsOfDirectory(
65-
at: snapshotDirectoryUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles
66-
)
67-
)].remove(snapshotFileUrl)
68-
_ = trackSnapshots
63+
// NB: Linux doesn't have file manager enumeration capabilities, so we skip this work on Linux.
64+
#if !os(Linux)
65+
staleSnapshots[snapshotDirectoryUrl, default: Set(
66+
try! fileManager.contentsOfDirectory(
67+
at: snapshotDirectoryUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles
68+
)
69+
)].remove(snapshotFileUrl)
70+
_ = trackSnapshots
71+
#endif
6972
}
70-
73+
7174
let format = snapshot.snapshotFormat
7275
if !recording && fileManager.fileExists(atPath: snapshotFileUrl.path) {
7376
let reference = S.Format.fromDiffableData(try! Data(contentsOf: snapshotFileUrl))
7477
if let (failure, attachments) = S.Format.diffableDiff(reference, format) {
7578
XCTFail(failure, file: file, line: line)
7679
if !attachments.isEmpty {
77-
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
78-
attachments.forEach {
79-
$0.lifetime = .deleteOnSuccess
80-
activity.add($0)
80+
// NB: Linux doesn't have XCTAttachment, and we don't even need it, so can skip all of this work.
81+
#if !os(Linux)
82+
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
83+
attachments.forEach {
84+
$0.lifetime = .deleteOnSuccess
85+
activity.add($0)
86+
}
8187
}
82-
}
88+
#endif
8389
}
8490
}
8591
} else {

Tests/LinuxMain.swift

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import XCTest
2+
@testable import SnapshotTestingTests
3+
4+
XCTMain([
5+
testCase(SnapshotTestingTests.allTests),
6+
])

Tests/SnapshotTestingTests/SnapshotTestingTests.swift

+12
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@ class SnapshotTestingTests: XCTestCase {
3939
assertSnapshot(matching: [1, 2])
4040
}
4141
}
42+
43+
#if os(Linux)
44+
extension SnapshotTestingTests {
45+
static var allTests : [(String, (SnapshotTestingTests) -> () throws -> Void)] {
46+
return [
47+
("testWithAny", testWithAny),
48+
("testWithNSObject", testWithNSObject),
49+
("testMultipleSnapshots", testMultipleSnapshots),
50+
]
51+
}
52+
}
53+
#endif

0 commit comments

Comments
 (0)