Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/Replay/HAR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,13 @@ extension HAR {
/// with fractional seconds and pretty-printed output.
public static func save(_ log: Log, to url: URL) throws {
let data = try encode(log)

// Ensure the directory exists
let directory = url.deletingLastPathComponent()
if !FileManager.default.fileExists(atPath: directory.path) {
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
}

try data.write(to: url)
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/ReplayTests/HARTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,24 @@ struct HARTests {
#expect(log.creator.name == "MyApp/2.0")
}

@Test("save to non existing directory")
func saveToNonExistingDirectory() throws {
var log = HAR.create()
log.entries.append(makeTestEntry())
log.comment = "Test HAR file"

let tempSubdirURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("subdir")
let tempURL = tempSubdirURL
.appendingPathComponent("HARTests_saveAndLoad.har")

try HAR.save(log, to: tempURL)

#expect(FileManager.default.fileExists(atPath: tempURL.path))

try? FileManager.default.removeItem(at: tempSubdirURL)
}

@Test("save and load roundtrip")
func saveAndLoad() throws {
var log = HAR.create()
Expand Down