Skip to content

Commit

Permalink
Add Travis CI for linux testing! (#33)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mbrandonw committed Nov 12, 2017
1 parent d9fdff4 commit 2d2925f
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
@@ -0,0 +1,5 @@
.git/
.build/
!.build/checkouts/
!.build/repositories/
!.build/workspace-state.json
1 change: 1 addition & 0 deletions .swift-version
@@ -0,0 +1 @@
4.0
14 changes: 14 additions & 0 deletions .travis.yml
@@ -0,0 +1,14 @@
os:
- linux
- osx
env:
language: generic
sudo: required
dist: trusty
osx_image: xcode9.1
install:
- if [ $TRAVIS_OS_NAME = linux ]; then
eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)";
fi
script:
- swift test
9 changes: 9 additions & 0 deletions Dockerfile
@@ -0,0 +1,9 @@
FROM swift:4.0

WORKDIR /package

COPY . ./

RUN swift package resolve
RUN swift package clean
CMD swift test --parallel
2 changes: 2 additions & 0 deletions Makefile
@@ -0,0 +1,2 @@
test:
docker build --tag snapshot-testing . && docker run --rm snapshot-testing
4 changes: 3 additions & 1 deletion README.md
@@ -1,4 +1,6 @@
# swift-snapshot-testing [![CircleCI](https://circleci.com/gh/pointfreeco/swift-snapshot-testing.svg?style=svg)](https://circleci.com/gh/pointfreeco/swift-snapshot-testing)
# swift-snapshot-testing

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)

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.

Expand Down
10 changes: 6 additions & 4 deletions Sources/Diff/Diff.swift
@@ -1,3 +1,5 @@
import Foundation

public enum DiffType {
case first
case second
Expand Down Expand Up @@ -83,8 +85,8 @@ public struct Hunk {
}
}

public func chunk<S: StringProtocol>(diff diffs: [Diff<S>], context ctx: Int = 4) -> [Hunk] {
func prepending(_ prefix: String) -> (S) -> String {
public func chunk(diff diffs: [Diff<String>], context ctx: Int = 4) -> [Hunk] {
func prepending(_ prefix: String) -> (String) -> String {
return { prefix + $0 + ($0.hasSuffix(" ") ? "¬" : "") }
}
let changed: (Hunk) -> Bool = { $0.lines.contains(where: { $0.hasPrefix(minus) || $0.hasPrefix(plus) }) }
Expand All @@ -102,11 +104,11 @@ public func chunk<S: StringProtocol>(diff diffs: [Diff<S>], context ctx: Int = 4
fstLen: ctx,
sndIdx: current.sndIdx + current.sndLen + len - ctx,
sndLen: ctx,
lines: (diff.elements.suffix(ctx) as ArraySlice<S>).map(prepending(figureSpace))
lines: (diff.elements.suffix(ctx) as ArraySlice<String>).map(prepending(figureSpace))
)
return (next, changed(hunk) ? hunks + [hunk] : hunks)
case .both where current.lines.isEmpty:
let lines = (diff.elements.suffix(ctx) as ArraySlice<S>).map(prepending(figureSpace))
let lines = (diff.elements.suffix(ctx) as ArraySlice<String>).map(prepending(figureSpace))
let count = lines.count
return (current + Hunk(idx: len - count, len: count, lines: lines), hunks)
case .both:
Expand Down
13 changes: 11 additions & 2 deletions Sources/SnapshotTesting/Diffable.swift
Expand Up @@ -2,6 +2,15 @@ import Diff
import Foundation
import XCTest

#if os(Linux)
// NB: Linux doesn't have XCTAttachment, so stubbing out the minimal interface.
public struct XCTAttachment {}
extension XCTAttachment {
public init(string: String) {
}
}
#endif

public protocol Diffable {
static var diffablePathExtension: String? { get }
static func diffableDiff(_ fst: Self, _ snd: Self) -> (String, [XCTAttachment])?
Expand Down Expand Up @@ -39,8 +48,8 @@ extension String: Diffable {
guard fst != snd else { return nil }

let hunks = chunk(diff: diff(
fst.split(separator: "\n", omittingEmptySubsequences: false),
snd.split(separator: "\n", omittingEmptySubsequences: false)
fst.split(separator: "\n", omittingEmptySubsequences: false).map(String.init),
snd.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
))
let failure = hunks.flatMap { [$0.patchMark] + $0.lines }.joined(separator: "\n")

Expand Down
32 changes: 19 additions & 13 deletions Sources/SnapshotTesting/SnapshotTesting.swift
Expand Up @@ -48,7 +48,7 @@ public func assertSnapshot<S: Snapshot>(
}()

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

defer {
staleSnapshots[snapshotDirectoryUrl, default: Set(
try! fileManager.contentsOfDirectory(
at: snapshotDirectoryUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles
)
)].remove(snapshotFileUrl)
_ = trackSnapshots
// NB: Linux doesn't have file manager enumeration capabilities, so we skip this work on Linux.
#if !os(Linux)
staleSnapshots[snapshotDirectoryUrl, default: Set(
try! fileManager.contentsOfDirectory(
at: snapshotDirectoryUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles
)
)].remove(snapshotFileUrl)
_ = trackSnapshots
#endif
}

let format = snapshot.snapshotFormat
if !recording && fileManager.fileExists(atPath: snapshotFileUrl.path) {
let reference = S.Format.fromDiffableData(try! Data(contentsOf: snapshotFileUrl))
if let (failure, attachments) = S.Format.diffableDiff(reference, format) {
XCTFail(failure, file: file, line: line)
if !attachments.isEmpty {
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
attachments.forEach {
$0.lifetime = .deleteOnSuccess
activity.add($0)
// NB: Linux doesn't have XCTAttachment, and we don't even need it, so can skip all of this work.
#if !os(Linux)
XCTContext.runActivity(named: "Attached Failure Diff") { activity in
attachments.forEach {
$0.lifetime = .deleteOnSuccess
activity.add($0)
}
}
}
#endif
}
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions Tests/LinuxMain.swift
@@ -0,0 +1,6 @@
import XCTest
@testable import SnapshotTestingTests

XCTMain([
testCase(SnapshotTestingTests.allTests),
])
12 changes: 12 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Expand Up @@ -39,3 +39,15 @@ class SnapshotTestingTests: XCTestCase {
assertSnapshot(matching: [1, 2])
}
}

#if os(Linux)
extension SnapshotTestingTests {
static var allTests : [(String, (SnapshotTestingTests) -> () throws -> Void)] {
return [
("testWithAny", testWithAny),
("testWithNSObject", testWithNSObject),
("testMultipleSnapshots", testMultipleSnapshots),
]
}
}
#endif

0 comments on commit 2d2925f

Please sign in to comment.