Skip to content

Commit 36f02ca

Browse files
committed
Refactor progress file parsing and improve test validation
1 parent 3e9e091 commit 36f02ca

File tree

3 files changed

+5
-29
lines changed

3 files changed

+5
-29
lines changed

Sources/Swiftly/JsonFileProgressReporter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ struct JsonFileProgressReporter: ProgressAnimationProtocol {
3131
return
3232
}
3333

34-
self.fileHandle.seekToEndOfFile()
3534
self.fileHandle.write(jsonData)
3635
self.fileHandle.write("\n".data(using: .utf8) ?? Data())
37-
self.fileHandle.synchronizeFile()
36+
try? self.fileHandle.synchronize()
3837
}
3938

4039
func update(step: Int, total: Int, text: String) {
41-
assert(step <= total)
40+
guard total > 0 && step <= total else {
41+
return
42+
}
4243
self.writeProgress(
4344
ProgressInfo.step(
4445
timestamp: Date(),
@@ -52,8 +53,7 @@ struct JsonFileProgressReporter: ProgressAnimationProtocol {
5253
}
5354

5455
func clear() {
55-
self.fileHandle.truncateFile(atOffset: 0)
56-
self.fileHandle.synchronizeFile()
56+
// not implemented for JSON file reporter
5757
}
5858

5959
func close() throws {

Sources/SwiftlyCore/FileManager+FilePath.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,6 @@ extension String {
190190
try self.write(to: URL(fileURLWithPath: path.string), atomically: atomically, encoding: enc)
191191
}
192192

193-
public func append(to path: FilePath, encoding enc: String.Encoding = .utf8) throws {
194-
if !FileManager.default.fileExists(atPath: path.string) {
195-
try self.write(to: path, atomically: true, encoding: enc)
196-
return
197-
}
198-
}
199-
200193
public init(contentsOf path: FilePath, encoding enc: String.Encoding = .utf8) throws {
201194
try self.init(contentsOf: URL(fileURLWithPath: path.string), encoding: enc)
202195
}

Tests/SwiftlyTests/JsonFileProgressReporterTests.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,6 @@ import Testing
100100
try FileManager.default.removeItem(atPath: tempFile.string)
101101
}
102102

103-
@Test("Test clear method truncates the file")
104-
func testClearTruncatesFile() async throws {
105-
let tempFile = fs.mktemp(ext: ".json")
106-
try await fs.create(.mode(Int(0o644)), file: tempFile)
107-
defer { try? FileManager.default.removeItem(atPath: tempFile.string) }
108-
let reporter = try JsonFileProgressReporter(SwiftlyTests.ctx, filePath: tempFile)
109-
defer { try? reporter.close() }
110-
111-
reporter.update(step: 1, total: 2, text: "Test")
112-
113-
#expect(try String(contentsOf: tempFile).lengthOfBytes(using: String.Encoding.utf8) > 0)
114-
115-
reporter.clear()
116-
117-
#expect(try String(contentsOf: tempFile).lengthOfBytes(using: String.Encoding.utf8) == 0)
118-
}
119-
120103
@Test("Test multiple progress updates create multiple lines")
121104
func testMultipleUpdatesCreateMultipleLines() async throws {
122105
let tempFile = fs.mktemp(ext: ".json")

0 commit comments

Comments
 (0)