Skip to content

Commit

Permalink
Add coverage for ignoring unchanged files
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardiman committed Jul 22, 2019
1 parent 36b2196 commit 12542ba
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
20 changes: 16 additions & 4 deletions Sources/Config/ConfigGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Foundation

public class ConfigGenerator {

var printer: Printing = Printer()

public init() {}

public func run(_ arguments: [String]) throws {
Expand Down Expand Up @@ -44,15 +46,15 @@ public class ConfigGenerator {
if newData == currentData {
shouldWrite = false
} else {
print("Existing file different from new file, writing \(url.lastPathComponent)\nExisting: \(currentData), New: \(newData)")
printer.print(message: "Existing file different from new file, writing \(url.lastPathComponent)\nExisting: \(currentData), New: \(newData)")
}
} else {
print("Existing file not present, writing \(url.lastPathComponent)")
printer.print(message: "Existing file not present, writing \(url.lastPathComponent)")
}
if shouldWrite == false {
print("Ignoring \(url.lastPathComponent) as it has not changed")
printer.print(message: "Ignoring \(url.lastPathComponent) as it has not changed")
} else {
print("Wrote \(url.lastPathComponent)")
printer.print(message: "Wrote \(url.lastPathComponent)")
try configurationFile.description.write(to: swiftOutput, atomically: true, encoding: .utf8)
}
}
Expand All @@ -62,3 +64,13 @@ public class ConfigGenerator {
return Arguments.Option.all.compactMap { $0.usage }.joined(separator: "\n")
}
}

protocol Printing {
func print(message: String)
}

struct Printer: Printing {
func print(message: String) {
Swift.print(message)
}
}
74 changes: 72 additions & 2 deletions Tests/ConfigTests/ConfigGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ConfigGeneratorTests: XCTestCase {
let generator = ConfigGenerator()
try generator.run(validOptions())
try configFixtures.forEach {
let contents = try String(contentsOf: tempURL.appendingPathComponent(filenames[$0.key] ?? $0.key).appendingPathExtension("ext.swift"), encoding: .utf8)
let contents = try String(contentsOf: url(for: $0.key), encoding: .utf8)
expect(contents).to(equal(expectedStrings[$0.key]))
}
}
Expand All @@ -61,6 +61,52 @@ class ConfigGeneratorTests: XCTestCase {
}
}

func testItDoesNotWriteIfTheConfigHasNotChanged() throws {
let generator = ConfigGenerator()
let mockPrinter = MockPrinter()
generator.printer = mockPrinter
try givenAConfigFile((key: "standard", value: generatorConfiguration))
try generator.run(validOptions())
let outputURL = url(for: "standard")
let inputURL = configURL(for: "standard")
let currentTouchDate = try FileManager.default.attributesOfItem(atPath: outputURL.path)[.modificationDate] as? Date
expect(mockPrinter.receivedMessages.first).to(equal("Existing file not present, writing \(inputURL.lastPathComponent)"))
expect(mockPrinter.receivedMessages.last).to(equal("Wrote \(inputURL.lastPathComponent)"))
mockPrinter.reset()
try generator.run(validOptions())
let newTouchDate = try FileManager.default.attributesOfItem(atPath: outputURL.path)[.modificationDate] as? Date
expect(currentTouchDate).to(equal(newTouchDate))
expect(mockPrinter.receivedMessages.first).to(equal("Ignoring \(inputURL.lastPathComponent) as it has not changed"))
}

func testItDoesWriteIfTheConfigHasChanged() throws {
let generator = ConfigGenerator()
let outputURL = url(for: "standard")
let inputURL = configURL(for: "standard")

try givenAConfigFile((key: "standard", value: generatorConfiguration))
try generator.run(validOptions())
let currentTouchDate = try FileManager.default.attributesOfItem(atPath: outputURL.path)[.modificationDate] as? Date

let mockPrinter = MockPrinter()
generator.printer = mockPrinter

try givenAConfigFile((key: "standard", value: enumConfiguration))
try generator.run(validOptions())
let newTouchDate = try FileManager.default.attributesOfItem(atPath: outputURL.path)[.modificationDate] as? Date
expect(currentTouchDate).to(beLessThan(newTouchDate))
expect(mockPrinter.receivedMessages.first).to(equal("Existing file different from new file, writing \(inputURL.lastPathComponent)\nExisting: \(expectedStrings["standard"]!), New: \(expectedStrings["enumconfig"]!.replacingOccurrences(of: "enumconfig", with: "standard"))"))
}

func testItCanPrintItsUsage() {
let generator = ConfigGenerator()
expect(generator.usage).to(equal("""
\(Arguments.Option.scheme.usage)
\(Arguments.Option.configPath.usage)
\(Arguments.Option.additionalExtension.usage)
"""))
}

func validOptions() -> [String] {
return [
"",
Expand All @@ -70,16 +116,28 @@ class ConfigGeneratorTests: XCTestCase {
]
}

func givenAConfigFile(_ config: (key: String, value: [String: Any])) throws {
try write(object: config.value, toConfigFileNamed: config.key)
}

func givenSomeConfigFiles() throws {
try configFixtures.forEach {
try write(object: $0.value, toConfigFileNamed: $0.key)
try givenAConfigFile($0)
}
}

func write(object: Any, toConfigFileNamed name: String) throws {
let data = try JSONSerialization.data(withJSONObject: object, options: [])
try data.write(to: tempURL.appendingPathComponent(name).appendingPathExtension("config"))
}

private func url(for key: String) -> URL {
return tempURL.appendingPathComponent(filenames[key] ?? key).appendingPathExtension("ext.swift")
}

private func configURL(for key: String) -> URL {
return tempURL.appendingPathComponent(filenames[key] ?? key).appendingPathExtension("config")
}
}

private let configFixtures = [
Expand Down Expand Up @@ -147,3 +205,15 @@ private let extensionConfiguration: [String: Any] = [
"extensionOn": "UIColor"
]
]

private class MockPrinter: Printing {
var receivedMessages = [String]()

func print(message: String) {
receivedMessages.append(message)
}

func reset() {
receivedMessages.removeAll()
}
}

0 comments on commit 12542ba

Please sign in to comment.