Skip to content
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

Add size property to types that conform to Location #117

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ public extension Location {
return storage.attributes[.modificationDate] as? Date
}

typealias ByteCount = Int64

/// The size of the item in bytes, or `nil` if the information is not available.
var size: ByteCount? {
guard let size = storage.attributes[.size] as? UInt64 else {
return nil
}

return ByteCount(size)
}

/// Initialize an instance of an existing location at a given path.
/// - parameter path: The absolute path of the location.
/// - throws: `LocationError` if the item couldn't be found.
Expand Down
11 changes: 11 additions & 0 deletions Tests/FilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,16 @@ class FilesTests: XCTestCase {
XCTAssertTrue(file.modificationDate.map(Calendar.current.isDateInToday) ?? false)
}
}

func testSize() {
performTest {
let subfolder = try folder.createSubfolder(named: "Folder")
XCTAssertNotNil(subfolder.size)

let file = try folder.createFile(named: "File")
XCTAssertNotNil(file.size)
}
}

func testParent() {
performTest {
Expand Down Expand Up @@ -906,6 +916,7 @@ class FilesTests: XCTestCase {
("testFirstAndLastInFileSequence", testFirstAndLastInFileSequence),
("testConvertingFileSequenceToRecursive", testConvertingFileSequenceToRecursive),
("testModificationDate", testModificationDate),
("testSize", testSize),
("testParent", testParent),
("testRootFolderParentIsNil", testRootFolderParentIsNil),
("testRootSubfolderParentIsRoot", testRootSubfolderParentIsRoot),
Expand Down