From 2d2925f7d43ee8155351e3ef7e724c73611c2339 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Sun, 12 Nov 2017 11:12:03 -0500 Subject: [PATCH] Add Travis CI for linux testing! (#33) * wip * all tests * work around for suffix * Revert "work around for suffix" This reverts commit 489d50af845aa16056a2865d67275330f1e79490. * import foundation * Revert "Revert "work around for suffix"" This reverts commit 85ec24c5b9cfc99d1848c5039578b874c67696e7. * attachment stuff * wip * wip * omg * omgomg * wip * wip * clean up * clean up * does this work * pr feedback * clean * another linux note --- .dockerignore | 5 +++ .swift-version | 1 + .travis.yml | 14 ++++++++ Dockerfile | 9 ++++++ Makefile | 2 ++ README.md | 4 ++- Sources/Diff/Diff.swift | 10 +++--- Sources/SnapshotTesting/Diffable.swift | 13 ++++++-- Sources/SnapshotTesting/SnapshotTesting.swift | 32 +++++++++++-------- Tests/LinuxMain.swift | 6 ++++ .../SnapshotTestingTests.swift | 12 +++++++ 11 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 .dockerignore create mode 100644 .swift-version create mode 100644 .travis.yml create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 Tests/LinuxMain.swift diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..0116b686 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.git/ +.build/ +!.build/checkouts/ +!.build/repositories/ +!.build/workspace-state.json diff --git a/.swift-version b/.swift-version new file mode 100644 index 00000000..5186d070 --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +4.0 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..e31bcba0 --- /dev/null +++ b/.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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..78c6272e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM swift:4.0 + +WORKDIR /package + +COPY . ./ + +RUN swift package resolve +RUN swift package clean +CMD swift test --parallel diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..7af2c281 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +test: + docker build --tag snapshot-testing . && docker run --rm snapshot-testing diff --git a/README.md b/README.md index 41771490..c949054c 100644 --- a/README.md +++ b/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. diff --git a/Sources/Diff/Diff.swift b/Sources/Diff/Diff.swift index e5d2c10b..a21e7de4 100644 --- a/Sources/Diff/Diff.swift +++ b/Sources/Diff/Diff.swift @@ -1,3 +1,5 @@ +import Foundation + public enum DiffType { case first case second @@ -83,8 +85,8 @@ public struct Hunk { } } -public func chunk(diff diffs: [Diff], context ctx: Int = 4) -> [Hunk] { - func prepending(_ prefix: String) -> (S) -> String { +public func chunk(diff diffs: [Diff], 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) }) } @@ -102,11 +104,11 @@ public func chunk(diff diffs: [Diff], context ctx: Int = 4 fstLen: ctx, sndIdx: current.sndIdx + current.sndLen + len - ctx, sndLen: ctx, - lines: (diff.elements.suffix(ctx) as ArraySlice).map(prepending(figureSpace)) + lines: (diff.elements.suffix(ctx) as ArraySlice).map(prepending(figureSpace)) ) return (next, changed(hunk) ? hunks + [hunk] : hunks) case .both where current.lines.isEmpty: - let lines = (diff.elements.suffix(ctx) as ArraySlice).map(prepending(figureSpace)) + let lines = (diff.elements.suffix(ctx) as ArraySlice).map(prepending(figureSpace)) let count = lines.count return (current + Hunk(idx: len - count, len: count, lines: lines), hunks) case .both: diff --git a/Sources/SnapshotTesting/Diffable.swift b/Sources/SnapshotTesting/Diffable.swift index d4a53b89..ae840e66 100644 --- a/Sources/SnapshotTesting/Diffable.swift +++ b/Sources/SnapshotTesting/Diffable.swift @@ -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])? @@ -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") diff --git a/Sources/SnapshotTesting/SnapshotTesting.swift b/Sources/SnapshotTesting/SnapshotTesting.swift index 176156de..d648e9db 100644 --- a/Sources/SnapshotTesting/SnapshotTesting.swift +++ b/Sources/SnapshotTesting/SnapshotTesting.swift @@ -48,7 +48,7 @@ public func assertSnapshot( }() let testName: String = { - let testIdentifier = "\(snapshotDirectoryUrl):\(function)" + let testIdentifier = "\(snapshotDirectoryUrl.absoluteString):\(function)" counter[testIdentifier, default: 0] += 1 return "\(function.dropLast(2)).\(counter[testIdentifier]!)" }() @@ -60,26 +60,32 @@ public func assertSnapshot( 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 { diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 00000000..6189df32 --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,6 @@ +import XCTest +@testable import SnapshotTestingTests + +XCTMain([ + testCase(SnapshotTestingTests.allTests), +]) diff --git a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift index b9f5d3ce..d679e3df 100644 --- a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift +++ b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift @@ -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