Skip to content

Commit 4afe65d

Browse files
committed
Improve JSON progress file handling and error reporting
1 parent b1655f4 commit 4afe65d

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

Sources/SwiftlyCore/FileManager+FilePath.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,6 @@ extension String {
195195
try self.write(to: path, atomically: true, encoding: enc)
196196
return
197197
}
198-
199-
let fileHandle = try FileHandle(forWritingTo: URL(fileURLWithPath: path.string))
200-
defer { fileHandle.closeFile() }
201-
fileHandle.seekToEndOfFile()
202-
if let data = self.data(using: enc) {
203-
fileHandle.write(data)
204-
} else {
205-
throw SwiftlyError(message: "Failed to convert string to data with encoding \(enc)")
206-
}
207198
}
208199

209200
public init(contentsOf path: FilePath, encoding enc: String.Encoding = .utf8) throws {

Tests/SwiftlyTests/JsonFileProgressReporterTests.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import Foundation
2-
@testable import Swiftly
3-
@testable import SwiftlyCore
42
import SystemPackage
53
import Testing
64

@@ -102,18 +100,21 @@ import Testing
102100
try FileManager.default.removeItem(atPath: tempFile.string)
103101
}
104102

105-
@Test("Test clear method removes file")
106-
func testClearRemovesFile() throws {
103+
@Test("Test clear method truncates the file")
104+
func testClearTruncatesFile() async throws {
107105
let tempFile = fs.mktemp(ext: ".json")
108-
let reporter = JsonFileProgressReporter(filePath: tempFile)
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() }
109110

110111
reporter.update(step: 1, total: 2, text: "Test")
111112

112-
#expect(FileManager.default.fileExists(atPath: tempFile.string))
113+
#expect(try String(contentsOf: tempFile).lengthOfBytes(using: String.Encoding.utf8) > 0)
113114

114115
reporter.clear()
115116

116-
#expect(!FileManager.default.fileExists(atPath: tempFile.string))
117+
#expect(try String(contentsOf: tempFile).lengthOfBytes(using: String.Encoding.utf8) == 0)
117118
}
118119

119120
@Test("Test multiple progress updates create multiple lines")
@@ -129,8 +130,6 @@ import Testing
129130
reporter.update(step: 50, total: 100, text: "Processing item 50")
130131
reporter.update(step: 100, total: 100, text: "Processing item 100")
131132

132-
reporter.update(step: 1, total: 3, text: "Step 1")
133-
reporter.update(step: 2, total: 3, text: "Step 2")
134133
reporter.complete(success: true)
135134
try? reporter.close()
136135

0 commit comments

Comments
 (0)