Skip to content

Commit

Permalink
Merge pull request #27 from mxcl/codecov
Browse files Browse the repository at this point in the history
More coverage
  • Loading branch information
mxcl authored Jan 31, 2019
2 parents ec6c011 + c456081 commit 6b52932
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
33 changes: 25 additions & 8 deletions Sources/Path+FileManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation
#if os(Linux)
import Glibc
#endif

public extension Path {
//MARK: File Management
Expand All @@ -25,6 +28,11 @@ public extension Path {
if overwrite, to.isFile, isFile {
try FileManager.default.removeItem(at: to.url)
}
#if os(Linux) && !swift(>=5.1) // check if fixed
if !overwrite, to.isFile {
throw CocoaError.error(.fileWriteFileExists)
}
#endif
try FileManager.default.copyItem(atPath: string, toPath: to.string)
return to
}
Expand Down Expand Up @@ -53,20 +61,16 @@ public extension Path {
@discardableResult
func copy(into: Path, overwrite: Bool = false) throws -> Path {
if !into.exists {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: into.url, withIntermediateDirectories: true)
}
let rv = into/basename()
if overwrite, rv.isFile {
try rv.delete()
}
#if os(Linux)
#if swift(>=5.1)
// check if fixed
#else
#if os(Linux) && !swift(>=5.1) // check if fixed
if !overwrite, rv.isFile {
throw CocoaError.error(.fileWriteFileExists)
}
#endif
#endif
try FileManager.default.copyItem(at: url, to: rv.url)
return rv
Expand Down Expand Up @@ -142,13 +146,26 @@ public extension Path {
}

/**
Creates an empty file at this path.
Creates an empty file at this path or if the file exists, updates its modification time.
- Returns: `self` to allow chaining.
*/
@inlinable
@discardableResult
func touch() throws -> Path {
return try "".write(to: self)
if !exists {
guard FileManager.default.createFile(atPath: string, contents: nil) else {
throw CocoaError.error(.fileWriteUnknown)
}
} else {
#if os(Linux)
let fd = open(string, O_WRONLY)
defer { close(fd) }
futimens(fd, nil)
#else
try FileManager.default.setAttributes([.modificationDate: Date()], ofItemAtPath: string)
#endif
}
return self
}

/**
Expand Down
58 changes: 51 additions & 7 deletions Tests/PathTests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class PathTests: XCTestCase {
try root.foo.touch().copy(to: root.bar)
XCTAssert(root.foo.isFile)
XCTAssert(root.bar.isFile)
XCTAssertThrowsError(try root.foo.copy(to: root.bar))
try root.foo.copy(to: root.bar, overwrite: true)
}
}

Expand All @@ -182,14 +184,25 @@ class PathTests: XCTestCase {
XCTAssertTrue(bar1.exists)
XCTAssertTrue(bar2.exists)
}

// test creates intermediary directories
try bar1.copy(into: root1.create.directories)

// test doesn’t replace file if “copy into” a file
let d = try root1.fuz.touch()
XCTAssertThrowsError(try root1.baz.touch().copy(into: d))
XCTAssert(d.isFile)
XCTAssert(root1.baz.isFile)
}
}

func testMoveTo() throws {
try Path.mktemp { root in
try root.foo.touch().move(to: root.bar)
XCTAssertFalse(root.foo.exists)
XCTAssert(root.bar.isFile)
try Path.mktemp { tmpdir in
try tmpdir.foo.touch().move(to: tmpdir.bar)
XCTAssertFalse(tmpdir.foo.exists)
XCTAssert(tmpdir.bar.isFile)
XCTAssertThrowsError(try tmpdir.foo.touch().move(to: tmpdir.bar))
try tmpdir.foo.move(to: tmpdir.bar, overwrite: true)
}
}

Expand All @@ -203,6 +216,17 @@ class PathTests: XCTestCase {
XCTAssertFalse(bar1.exists)
XCTAssertTrue(bar2.exists)
}

// test creates intermediary directories
try root1.baz.touch().move(into: root1.create.directories)
XCTAssertFalse(root1.baz.exists)
XCTAssert(root1.create.directories.baz.isFile)

// test doesn’t replace file if “move into” a file
let d = try root1.fuz.touch()
XCTAssertThrowsError(try root1.baz.touch().move(into: d))
XCTAssert(d.isFile)
XCTAssert(root1.baz.isFile)
}
}

Expand All @@ -225,7 +249,7 @@ class PathTests: XCTestCase {
XCTAssertEqual(Path.root.string, "/")
XCTAssertEqual(Path.home.string, NSHomeDirectory())
XCTAssertEqual(Path.documents.string, NSHomeDirectory() + "/Documents")
#if os(macOS)
#if !os(Linux)
XCTAssertEqual(Path.caches.string, NSHomeDirectory() + "/Library/Caches")
XCTAssertEqual(Path.cwd.string, FileManager.default.currentDirectoryPath)
XCTAssertEqual(Path.applicationSupport.string, NSHomeDirectory() + "/Library/Application Support")
Expand Down Expand Up @@ -298,6 +322,9 @@ class PathTests: XCTestCase {
let now2 = Date().timeIntervalSince1970.rounded(.down)
XCTAssertNotEqual(now1, now2)
XCTAssertEqual(foo.mtime?.timeIntervalSince1970.rounded(.down), now2) //FIXME flakey

XCTAssertNil(tmpdir.void.mtime)
XCTAssertNil(tmpdir.void.ctime)
}
}

Expand Down Expand Up @@ -334,14 +361,15 @@ class PathTests: XCTestCase {
_ = Bundle.main.sharedFrameworks
}

func testDataExentsions() throws {
func testDataExtensions() throws {
let data = try Data(contentsOf: Path(#file)!)
try Path.mktemp { tmpdir in
_ = try data.write(to: tmpdir.foo)
_ = try data.write(to: tmpdir.foo, atomically: true)
}
}

func testStringExentsions() throws {
func testStringExtensions() throws {
let string = try String(contentsOf: Path(#file)!)
try Path.mktemp { tmpdir in
_ = try string.write(to: tmpdir.foo)
Expand All @@ -353,4 +381,20 @@ class PathTests: XCTestCase {
_ = try FileHandle(forWritingAt: Path(#file)!)
_ = try FileHandle(forUpdatingAt: Path(#file)!)
}

func testSort() {
XCTAssertEqual([Path.root.a, Path.root.c, Path.root.b].sorted(), [Path.root.a, Path.root.b, Path.root.c])
}

func testLock() throws {
#if !os(Linux)
try Path.mktemp { tmpdir in
let bar = try tmpdir.bar.touch()
try bar.lock()
XCTAssertThrowsError(try bar.touch())
try bar.unlock()
try bar.touch()
}
#endif
}
}
6 changes: 4 additions & 2 deletions Tests/PathTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension PathTests {
("testConcatenation", testConcatenation),
("testCopyInto", testCopyInto),
("testCopyTo", testCopyTo),
("testDataExentsions", testDataExentsions),
("testDataExtensions", testDataExtensions),
("testDelete", testDelete),
("testDynamicMember", testDynamicMember),
("testEnumeration", testEnumeration),
Expand All @@ -20,6 +20,7 @@ extension PathTests {
("testFilesystemAttributes", testFilesystemAttributes),
("testIsDirectory", testIsDirectory),
("testJoin", testJoin),
("testLock", testLock),
("testMkpathIfExists", testMkpathIfExists),
("testMktemp", testMktemp),
("testMoveInto", testMoveInto),
Expand All @@ -28,8 +29,9 @@ extension PathTests {
("testRelativePathCodable", testRelativePathCodable),
("testRelativeTo", testRelativeTo),
("testRename", testRename),
("testSort", testSort),
("testStringConvertibles", testStringConvertibles),
("testStringExentsions", testStringExentsions),
("testStringExtensions", testStringExtensions),
("testTimes", testTimes),
]
}
Expand Down

0 comments on commit 6b52932

Please sign in to comment.