Skip to content

SR-11922: [FileHandle] Make the function signature the same as DarwinFoundation. #2582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2019
Merged
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
6 changes: 3 additions & 3 deletions Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ open class FileHandle : NSObject {
}

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

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

@available(swift, deprecated: 100000, renamed: "truncate(toOffset:)")
@available(swift, deprecated: 100000, renamed: "truncate(atOffset:)")
open func truncateFile(atOffset offset: UInt64) {
try! truncate(toOffset: offset)
try! truncate(atOffset: offset)
}

@available(swift, deprecated: 100000, renamed: "synchronize()")
Expand Down
49 changes: 49 additions & 0 deletions TestFoundation/TestFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ class TestFileHandle : XCTestCase {
return fh!
}

func createFileHandleForUpdating() -> FileHandle {
let url = createTemporaryFile(containing: content)

var fh: FileHandle!
expectDoesNotThrow({ fh = try FileHandle(forUpdating: url) }, "Couldn't create file handle.")

allHandles.append(fh)
return fh
}

#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
func createFileHandleForSeekErrors() -> FileHandle {
#if os(Windows)
Expand Down Expand Up @@ -380,6 +390,44 @@ class TestFileHandle : XCTestCase {
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, Data([0, 0, 0, 0, 0, 1, 2, 3, 4, 5]))
}

func test_truncate() throws {
// `func truncate(atOffset offset: UInt64) throws` is introduced in Swift 5.
// See also https://bugs.swift.org/browse/SR-11922

let fh = createFileHandleForUpdating()

try fh.truncate(atOffset: 50)
XCTAssertEqual(fh.offsetInFile, 50)

try fh.truncate(atOffset: 0)
XCTAssertEqual(fh.offsetInFile, 0)

try fh.truncate(atOffset: 100)
XCTAssertEqual(fh.offsetInFile, 100)

fh.write(Data([1, 2]))
XCTAssertEqual(fh.offsetInFile, 102)

try fh.seek(toOffset: 4)
XCTAssertEqual(fh.offsetInFile, 4)

(0..<20).forEach { fh.write(Data([$0])) }
XCTAssertEqual(fh.offsetInFile, 24)

fh.seekToEndOfFile()
XCTAssertEqual(fh.offsetInFile, 102)

try fh.truncate(atOffset: 10)
XCTAssertEqual(fh.offsetInFile, 10)

try fh.seek(toOffset: 0)
XCTAssertEqual(fh.offsetInFile, 0)

let data = fh.readDataToEndOfFile()
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, Data([0, 0, 0, 0, 0, 1, 2, 3, 4, 5]))
}

func test_readabilityHandlerCloseFileRace() throws {
for _ in 0..<10 {
Expand Down Expand Up @@ -541,6 +589,7 @@ class TestFileHandle : XCTestCase {
("testWritingWithMultiregionData", testWritingWithMultiregionData),
("test_constants", test_constants),
("test_truncateFile", test_truncateFile),
("test_truncate", test_truncate),
("test_readabilityHandlerCloseFileRace", test_readabilityHandlerCloseFileRace),
("test_readabilityHandlerCloseFileRaceWithError", test_readabilityHandlerCloseFileRaceWithError),
("test_availableData", test_availableData),
Expand Down