Skip to content

Commit

Permalink
Add additional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardiman committed Sep 25, 2019
1 parent e462436 commit 2949240
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 13 additions & 5 deletions Sources/Config/ConfigGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,31 @@ public class ConfigGenerator {
swiftOutput.appendPathExtension("swift")
let newData = configurationFile.description
var shouldWrite = true
if let currentData = try? String(contentsOf: swiftOutput) {
if arguments.verbose {
printer.print(message: "Checking existing file at \(swiftOutput.path)")
}
if let existingData = try? Data(contentsOf: swiftOutput), let currentData = String(data: existingData, encoding: .utf8) {
if arguments.verbose {
printer.print(message: "Existing file contents: \(currentData)")
}
if newData == currentData {
shouldWrite = false
} else {
printer.print(message: "Existing file different from new file, writing \(url.lastPathComponent)\nExisting: \(currentData), New: \(newData)")
printer.print(message: "Existing file at \(swiftOutput.path) different from new file, writing \(url.lastPathComponent)\nExisting: \(currentData), New: \(newData)")
}
} else {
printer.print(message: "Existing file not present, writing \(url.lastPathComponent)")
printer.print(message: "Existing file not present at \(swiftOutput.path), writing \(url.lastPathComponent)")
}
if shouldWrite == false {
printer.print(message: "Ignoring \(url.lastPathComponent) as it has not changed")
} else {
printer.print(message: "Wrote \(url.lastPathComponent)")
try configurationFile.description.write(to: swiftOutput, atomically: true, encoding: .utf8)
do {
try configurationFile.description.write(to: swiftOutput, atomically: true, encoding: .utf8)
printer.print(message: "Wrote \(url.lastPathComponent) to \(swiftOutput.path)")
} catch {
printer.print(message: "Failed to write to \(swiftOutput.path)")
throw error
}
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions Tests/ConfigTests/ConfigGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class ConfigGeneratorTests: XCTestCase {

override func setUp() {
super.setUp()
tempURL = URL(fileURLWithPath: NSTemporaryDirectory())
let url = URL(fileURLWithPath: NSTemporaryDirectory())
let amendedURL = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: []).first?.deletingLastPathComponent()
tempURL = amendedURL ?? url
}

override func tearDown() {
Expand Down Expand Up @@ -70,8 +72,8 @@ class ConfigGeneratorTests: XCTestCase {
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)"))
expect(mockPrinter.receivedMessages.first).to(equal("Existing file not present at \(outputURL.path), writing \(inputURL.lastPathComponent)"))
expect(mockPrinter.receivedMessages.last).to(equal("Wrote \(inputURL.lastPathComponent) to \(outputURL.path)"))
mockPrinter.reset()
try generator.run(validOptions())
let newTouchDate = try FileManager.default.attributesOfItem(atPath: outputURL.path)[.modificationDate] as? Date
Expand All @@ -95,7 +97,7 @@ class ConfigGeneratorTests: XCTestCase {
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"))"))
expect(mockPrinter.receivedMessages.first).to(equal("Existing file at \(outputURL.path) different from new file, writing \(inputURL.lastPathComponent)\nExisting: \(expectedStrings["standard"]!), New: \(expectedStrings["enumconfig"]!.replacingOccurrences(of: "enumconfig", with: "standard"))"))
}

func testItCanPrintItsUsage() {
Expand Down

0 comments on commit 2949240

Please sign in to comment.