Skip to content

Commit 52e5404

Browse files
authored
Merge pull request #2582 from YOCKOW/sr-11922
SR-11922: [FileHandle] Make the function signature the same as DarwinFoundation.
2 parents 5a26031 + 09d0b3a commit 52e5404

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

Foundation/FileHandle.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ open class FileHandle : NSObject {
570570
}
571571

572572
@available(swift 5.0)
573-
public func truncate(toOffset offset: UInt64) throws {
573+
public func truncate(atOffset offset: UInt64) throws {
574574
guard self != FileHandle._nulldeviceFileHandle else { return }
575575

576576
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue) }
@@ -683,9 +683,9 @@ open class FileHandle : NSObject {
683683
try! seek(toOffset: offset)
684684
}
685685

686-
@available(swift, deprecated: 100000, renamed: "truncate(toOffset:)")
686+
@available(swift, deprecated: 100000, renamed: "truncate(atOffset:)")
687687
open func truncateFile(atOffset offset: UInt64) {
688-
try! truncate(toOffset: offset)
688+
try! truncate(atOffset: offset)
689689
}
690690

691691
@available(swift, deprecated: 100000, renamed: "synchronize()")

TestFoundation/TestFileHandle.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ class TestFileHandle : XCTestCase {
8484
return fh!
8585
}
8686

87+
func createFileHandleForUpdating() -> FileHandle {
88+
let url = createTemporaryFile(containing: content)
89+
90+
var fh: FileHandle!
91+
expectDoesNotThrow({ fh = try FileHandle(forUpdating: url) }, "Couldn't create file handle.")
92+
93+
allHandles.append(fh)
94+
return fh
95+
}
96+
8797
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
8898
func createFileHandleForSeekErrors() -> FileHandle {
8999
#if os(Windows)
@@ -380,6 +390,44 @@ class TestFileHandle : XCTestCase {
380390
XCTAssertEqual(data.count, 10)
381391
XCTAssertEqual(data, Data([0, 0, 0, 0, 0, 1, 2, 3, 4, 5]))
382392
}
393+
394+
func test_truncate() throws {
395+
// `func truncate(atOffset offset: UInt64) throws` is introduced in Swift 5.
396+
// See also https://bugs.swift.org/browse/SR-11922
397+
398+
let fh = createFileHandleForUpdating()
399+
400+
try fh.truncate(atOffset: 50)
401+
XCTAssertEqual(fh.offsetInFile, 50)
402+
403+
try fh.truncate(atOffset: 0)
404+
XCTAssertEqual(fh.offsetInFile, 0)
405+
406+
try fh.truncate(atOffset: 100)
407+
XCTAssertEqual(fh.offsetInFile, 100)
408+
409+
fh.write(Data([1, 2]))
410+
XCTAssertEqual(fh.offsetInFile, 102)
411+
412+
try fh.seek(toOffset: 4)
413+
XCTAssertEqual(fh.offsetInFile, 4)
414+
415+
(0..<20).forEach { fh.write(Data([$0])) }
416+
XCTAssertEqual(fh.offsetInFile, 24)
417+
418+
fh.seekToEndOfFile()
419+
XCTAssertEqual(fh.offsetInFile, 102)
420+
421+
try fh.truncate(atOffset: 10)
422+
XCTAssertEqual(fh.offsetInFile, 10)
423+
424+
try fh.seek(toOffset: 0)
425+
XCTAssertEqual(fh.offsetInFile, 0)
426+
427+
let data = fh.readDataToEndOfFile()
428+
XCTAssertEqual(data.count, 10)
429+
XCTAssertEqual(data, Data([0, 0, 0, 0, 0, 1, 2, 3, 4, 5]))
430+
}
383431

384432
func test_readabilityHandlerCloseFileRace() throws {
385433
for _ in 0..<10 {
@@ -541,6 +589,7 @@ class TestFileHandle : XCTestCase {
541589
("testWritingWithMultiregionData", testWritingWithMultiregionData),
542590
("test_constants", test_constants),
543591
("test_truncateFile", test_truncateFile),
592+
("test_truncate", test_truncate),
544593
("test_readabilityHandlerCloseFileRace", test_readabilityHandlerCloseFileRace),
545594
("test_readabilityHandlerCloseFileRaceWithError", test_readabilityHandlerCloseFileRaceWithError),
546595
("test_availableData", test_availableData),

0 commit comments

Comments
 (0)