Compare commits

..

14 Commits

Author SHA1 Message Date
repo-ranger[bot]
6c84754ad8 Merge pull request #33 from mxcl/bundle-private-frameworks
Bundle.privateFrameworks
2019-02-09 18:32:02 +00:00
Max Howell
8469565b06 Bundle.privateFrameworks 2019-02-09 13:24:57 -05:00
Max Howell
ed45d10179 Docs updates; CocoaPods 16.0 release; [ci skip] 2019-02-08 09:14:23 -05:00
Max Howell
8033ae49b4 Add [pathos]
[pathos]: https://github.com/dduan/Pathos

[skip ci]
2019-02-04 12:05:14 -05:00
Max Howell
b290173486 Update travis scripts 2019-02-04 12:04:48 -05:00
Max Howell
8248354a80 Some documentation improvements 2019-02-01 15:18:15 -05:00
Max Howell
14963e48f5 Merge pull request #30 from mxcl/codecov
More coverage
2019-02-01 10:26:59 -05:00
Max Howell
7f5340bc19 More coverage
Though I can hardly test these functions, at least we can verify they run
without crashing etc.
2019-02-01 10:17:37 -05:00
Max Howell
74074c634f Merge pull request #29 from mxcl/codecov
More coverage
2019-01-31 21:33:50 -05:00
Max Howell
74656bbfcd More coverage 2019-01-31 21:17:37 -05:00
Max Howell
cced2af2cd Merge pull request #28 from mxcl/codecov
More coverage
2019-01-31 19:17:43 -05:00
Max Howell
b9abd07318 More coverage 2019-01-31 19:09:36 -05:00
Max Howell
6b52932e7b Merge pull request #27 from mxcl/codecov
More coverage
2019-01-31 16:29:08 -05:00
Max Howell
c456081e65 More coverage 2019-01-31 14:26:09 -05:00
8 changed files with 200 additions and 87 deletions

View File

@@ -90,12 +90,15 @@ jobs:
tags: true tags: true
- name: CocoaPods - name: CocoaPods
before_install: | before_install: export TRAVIS_REPO_NAME=${TRAVIS_REPO_SLUG#*/}
install: gem install cocoapods
before_script: |
export DESCRIPTION=$(swift - <<\ \ EOF export DESCRIPTION=$(swift - <<\ \ EOF
import Foundation import Foundation
struct Response: Decodable { let description: String } struct Response: Decodable { let description: String }
let token = ProcessInfo.processInfo.environment["GITHUB_TOKEN"]! let token = ProcessInfo.processInfo.environment["GITHUB_TOKEN"]!
let url = URL(string: "https://api.github.com/repos/mxcl/Path.swift")! let slug = ProcessInfo.processInfo.environment["TRAVIS_REPO_SLUG"]!
let url = URL(string: "https://api.github.com/repos/\(slug)")!
var rq = URLRequest(url: url) var rq = URLRequest(url: url)
rq.setValue("token \(token)", forHTTPHeaderField: "Authorization") rq.setValue("token \(token)", forHTTPHeaderField: "Authorization")
let semaphore = DispatchSemaphore(value: 0) let semaphore = DispatchSemaphore(value: 0)
@@ -108,15 +111,15 @@ jobs:
let rsp = try JSONDecoder().decode(Response.self, from: data) let rsp = try JSONDecoder().decode(Response.self, from: data)
print(rsp.description, terminator: "") print(rsp.description, terminator: "")
EOF) EOF)
cat <<\ \ EOF> Path.swift.podspec cat <<\ \ EOF> $TRAVIS_REPO_NAME.podspec
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'Path.swift' s.name = ENV['TRAVIS_REPO_NAME']
s.version = ENV['TRAVIS_TAG'] s.version = ENV['TRAVIS_TAG']
s.summary = ENV['DESCRIPTION'] s.summary = ENV['DESCRIPTION']
s.homepage = 'https://github.com/mxcl/Path.swift' s.homepage = "https://github.com/#{ENV['TRAVIS_REPO_SLUG']}"
s.license = { :type => 'Unlicense', :file => 'LICENSE.md' } s.license = { type: 'Unlicense', file: 'LICENSE.md' }
s.author = { 'mxcl' => 'mxcl@me.com' } s.author = { mxcl: 'mxcl@me.com' }
s.source = { :git => 'https://github.com/mxcl/Path.swift.git', :tag => s.version.to_s } s.source = { git: "https://github.com/#{ENV['TRAVIS_REPO_SLUG']}.git", tag: s.version }
s.social_media_url = 'https://twitter.com/mxcl' s.social_media_url = 'https://twitter.com/mxcl'
s.osx.deployment_target = '10.10' s.osx.deployment_target = '10.10'
s.ios.deployment_target = '8.0' s.ios.deployment_target = '8.0'
@@ -126,5 +129,4 @@ jobs:
s.swift_version = '4.2' s.swift_version = '4.2'
end end
EOF EOF
install: gem install cocoapods --pre
script: pod trunk push script: pod trunk push

View File

@@ -1,4 +1,4 @@
# Path.swift ![badge-platforms][] ![badge-languages][] [![badge-ci][]][travis] [![badge-jazzy][]][docs] [![badge-codecov][]][codecov] ![badge-version][] # Path.swift ![badge-platforms][] ![badge-languages][] [![badge-ci][]][travis] [![badge-jazzy][]][docs] [![badge-codecov][]][codecov] [![badge-version][]][cocoapods]
A file-system pathing library focused on developer experience and robust end A file-system pathing library focused on developer experience and robust end
results. results.
@@ -36,7 +36,7 @@ print(foo.isFile) // => true
let prefs = Path.home.Library.Preferences // => /Users/mxcl/Library/Preferences let prefs = Path.home.Library.Preferences // => /Users/mxcl/Library/Preferences
// a practical example: installing a helper executable // a practical example: installing a helper executable
try Bundle.resources.join("helper").copy(into: Path.home.join(".local/bin").mkdir(.p)).chmod(0o500) try Bundle.resources.helper.copy(into: Path.root.usr.local.bin).chmod(0o500)
``` ```
We emphasize safety and correctness, just like Swift, and also (again like We emphasize safety and correctness, just like Swift, and also (again like
@@ -57,7 +57,8 @@ help me continue my work, I appreciate it x
# Handbook # Handbook
Our [online API documentation][docs] is automatically updated for new releases. Our [online API documentation][docs] covers 100% of our public API and is
automatically updated for new releases.
## Codable ## Codable
@@ -229,7 +230,7 @@ for that as the check was deemed too expensive to be worthwhile.
SwiftPM: SwiftPM:
```swift ```swift
package.append(.package(url: "https://github.com/mxcl/Path.swift", from: "0.5.0")) package.append(.package(url: "https://github.com/mxcl/Path.swift.git", from: "0.5.0"))
``` ```
CocoaPods: CocoaPods:
@@ -253,6 +254,7 @@ https://codebasesaga.com/canopy/
# Alternatives # Alternatives
* [Pathos](https://github.com/dduan/Pathos) by Daniel Duan
* [PathKit](https://github.com/kylef/PathKit) by Kyle Fuller * [PathKit](https://github.com/kylef/PathKit) by Kyle Fuller
* [Files](https://github.com/JohnSundell/Files) by John Sundell * [Files](https://github.com/JohnSundell/Files) by John Sundell
* [Utility](https://github.com/apple/swift-package-manager) by Apple * [Utility](https://github.com/apple/swift-package-manager) by Apple
@@ -267,3 +269,4 @@ https://codebasesaga.com/canopy/
[travis]: https://travis-ci.com/mxcl/Path.swift [travis]: https://travis-ci.com/mxcl/Path.swift
[codecov]: https://codecov.io/gh/mxcl/Path.swift [codecov]: https://codecov.io/gh/mxcl/Path.swift
[badge-version]: https://img.shields.io/cocoapods/v/Path.swift.svg?label=version [badge-version]: https://img.shields.io/cocoapods/v/Path.swift.svg?label=version
[cocoapods]: https://cocoapods.org/pods/Path.swift

View File

@@ -9,32 +9,25 @@ public extension Bundle {
return str.flatMap(Path.init) return str.flatMap(Path.init)
} }
/// Returns the path for the shared-frameworks directory in this bundle. /**
Returns the path for the shared-frameworks directory in this bundle.
- Note: This is typically `ShareFrameworks`
*/
var sharedFrameworks: Path { var sharedFrameworks: Path {
var `default`: Path { return sharedFrameworksPath.flatMap(Path.init) ?? defaultSharedFrameworksPath
#if os(macOS) }
return path.join("Contents/Frameworks")
#elseif os(Linux) /**
return path.join("lib") Returns the path for the private-frameworks directory in this bundle.
#else - Note: This is typically `Frameworks`
return path.join("Frameworks") */
#endif var privateFrameworks: Path {
} return privateFrameworksPath.flatMap(Path.init) ?? defaultSharedFrameworksPath
return sharedFrameworksPath.flatMap(Path.init) ?? `default`
} }
/// Returns the path for the resources directory in this bundle. /// Returns the path for the resources directory in this bundle.
var resources: Path { var resources: Path {
var `default`: Path { return resourcePath.flatMap(Path.init) ?? defaultResourcesPath
#if os(macOS)
return path.join("Contents/Resources")
#elseif os(Linux)
return path.join("share")
#else
return path
#endif
}
return resourcePath.flatMap(Path.init) ?? `default`
} }
/// Returns the path for this bundle. /// Returns the path for this bundle.
@@ -107,3 +100,25 @@ public extension FileHandle {
try self.init(forUpdating: path.url) try self.init(forUpdating: path.url)
} }
} }
internal extension Bundle {
var defaultSharedFrameworksPath: Path {
#if os(macOS)
return path.join("Contents/Frameworks")
#elseif os(Linux)
return path.join("lib")
#else
return path.join("Frameworks")
#endif
}
var defaultResourcesPath: Path {
#if os(macOS)
return path.join("Contents/Resources")
#elseif os(Linux)
return path.join("share")
#else
return path
#endif
}
}

View File

@@ -1,7 +1,4 @@
import Foundation import Foundation
//#if os(Linux)
//import func Glibc.chmod
//#endif
public extension Path { public extension Path {
//MARK: Filesystem Attributes //MARK: Filesystem Attributes
@@ -40,15 +37,12 @@ public extension Path {
*/ */
@discardableResult @discardableResult
func chmod(_ octal: Int) throws -> Path { func chmod(_ octal: Int) throws -> Path {
// #if os(Linux)
// Glibc.chmod(string, __mode_t(octal))
// #else
try FileManager.default.setAttributes([.posixPermissions: octal], ofItemAtPath: string) try FileManager.default.setAttributes([.posixPermissions: octal], ofItemAtPath: string)
// #endif
return self return self
} }
/** /**
Applies the macOS filesystem lock attribute.
- Note: If file is already locked, does nothing. - Note: If file is already locked, does nothing.
- Note: If file doesnt exist, throws. - Note: If file doesnt exist, throws.
- Important: On Linux does nothing. - Important: On Linux does nothing.
@@ -70,6 +64,7 @@ public extension Path {
- Note: If file isnt locked, does nothing. - Note: If file isnt locked, does nothing.
- Note: If file doesnt exist, does nothing. - Note: If file doesnt exist, does nothing.
- Important: On Linux does nothing. - Important: On Linux does nothing.
- SeeAlso: `lock()`
*/ */
@discardableResult @discardableResult
func unlock() throws -> Path { func unlock() throws -> Path {

View File

@@ -47,18 +47,7 @@ extension Path {
fatalError() fatalError()
} }
#else #else
guard let pathString = FileManager.default.urls(for: searchPath, in: .userDomainMask).first?.path else { guard let pathString = FileManager.default.urls(for: searchPath, in: .userDomainMask).first?.path else { return defaultUrl(for: searchPath) }
switch searchPath {
case .documentDirectory:
return Path.home/"Documents"
case .applicationSupportDirectory:
return Path.home/"Library/Application Support"
case .cachesDirectory:
return Path.home/"Library/Caches"
default:
fatalError()
}
}
return Path(string: pathString) return Path(string: pathString)
#endif #endif
} }
@@ -90,3 +79,19 @@ extension Path {
return path(for: .applicationSupportDirectory) return path(for: .applicationSupportDirectory)
} }
} }
#if !os(Linux)
func defaultUrl(for searchPath: FileManager.SearchPathDirectory) -> Path {
switch searchPath {
case .documentDirectory:
return Path.home/"Documents"
case .applicationSupportDirectory:
return Path.home/"Library/Application Support"
case .cachesDirectory:
return Path.home/"Library/Caches"
default:
fatalError()
}
}
#endif

View File

@@ -1,4 +1,7 @@
import Foundation import Foundation
#if os(Linux)
import Glibc
#endif
public extension Path { public extension Path {
//MARK: File Management //MARK: File Management
@@ -25,6 +28,11 @@ public extension Path {
if overwrite, to.isFile, isFile { if overwrite, to.isFile, isFile {
try FileManager.default.removeItem(at: to.url) 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) try FileManager.default.copyItem(atPath: string, toPath: to.string)
return to return to
} }
@@ -38,7 +46,8 @@ public extension Path {
// Create ~/.local/bin, copy `ls` there and make the new copy executable // Create ~/.local/bin, copy `ls` there and make the new copy executable
try Path.root.join("bin/ls").copy(into: Path.home.join(".local/bin").mkdir(.p)).chmod(0o500) try Path.root.join("bin/ls").copy(into: Path.home.join(".local/bin").mkdir(.p)).chmod(0o500)
If the destination does not exist, this function creates the directory first. If the destination does not exist, this function creates the directory
(including intermediary directories if necessary) first.
- Parameter into: Destination directory - Parameter into: Destination directory
- Parameter overwrite: If true overwrites any file that already exists at `into`. - Parameter overwrite: If true overwrites any file that already exists at `into`.
@@ -48,25 +57,21 @@ public extension Path {
`self` because even though *Path.swifts* policy is to noop if the desired `self` because even though *Path.swifts* policy is to noop if the desired
end result preexists, checking for this condition is too expensive a end result preexists, checking for this condition is too expensive a
trade-off. trade-off.
- SeeAlso: `copy(into:overwrite:)` - SeeAlso: `copy(to:overwrite:)`
*/ */
@discardableResult @discardableResult
func copy(into: Path, overwrite: Bool = false) throws -> Path { func copy(into: Path, overwrite: Bool = false) throws -> Path {
if !into.exists { if !into.exists {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) try into.mkdir(.p)
} }
let rv = into/basename() let rv = into/basename()
if overwrite, rv.isFile { if overwrite, rv.isFile {
try rv.delete() try rv.delete()
} }
#if os(Linux) #if os(Linux) && !swift(>=5.1) // check if fixed
#if swift(>=5.1)
// check if fixed
#else
if !overwrite, rv.isFile { if !overwrite, rv.isFile {
throw CocoaError.error(.fileWriteFileExists) throw CocoaError.error(.fileWriteFileExists)
} }
#endif
#endif #endif
try FileManager.default.copyItem(at: url, to: rv.url) try FileManager.default.copyItem(at: url, to: rv.url)
return rv return rv
@@ -86,7 +91,7 @@ public extension Path {
`self` because even though *Path.swifts* policy is to noop if the desired `self` because even though *Path.swifts* policy is to noop if the desired
end result preexists, checking for this condition is too expensive a end result preexists, checking for this condition is too expensive a
trade-off. trade-off.
- SeeAlso: move(into:overwrite:) - SeeAlso: `move(into:overwrite:)`
*/ */
@discardableResult @discardableResult
func move(to: Path, overwrite: Bool = false) throws -> Path { func move(to: Path, overwrite: Bool = false) throws -> Path {
@@ -103,13 +108,14 @@ public extension Path {
try Path.root.join("bar").move(into: .home) try Path.root.join("bar").move(into: .home)
// => "/Users/mxcl/bar" // => "/Users/mxcl/bar"
If the destination does not exist, this function creates the directory first. If the destination does not exist, this function creates the directory
(including intermediary directories if necessary) first.
- Parameter into: Destination directory - Parameter into: Destination directory
- Parameter overwrite: If true *overwrites* any file that already exists at `into`. - Parameter overwrite: If true *overwrites* any file that already exists at `into`.
- Note: `throws` if `into` is a file. - Note: `throws` if `into` is a file.
- Returns: The `Path` of destination filename. - Returns: The `Path` of destination filename.
- SeeAlso: move(into:overwrite:) - SeeAlso: `move(to:overwrite:)`
*/ */
@discardableResult @discardableResult
func move(into: Path, overwrite: Bool = false) throws -> Path { func move(into: Path, overwrite: Bool = false) throws -> Path {
@@ -142,13 +148,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. - Returns: `self` to allow chaining.
*/ */
@inlinable @inlinable
@discardableResult @discardableResult
func touch() throws -> Path { 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
} }
/** /**

View File

@@ -1,5 +1,5 @@
@testable import Path
import XCTest import XCTest
import Path
class PathTests: XCTestCase { class PathTests: XCTestCase {
func testConcatenation() { func testConcatenation() {
@@ -169,6 +169,8 @@ class PathTests: XCTestCase {
try root.foo.touch().copy(to: root.bar) try root.foo.touch().copy(to: root.bar)
XCTAssert(root.foo.isFile) XCTAssert(root.foo.isFile)
XCTAssert(root.bar.isFile) XCTAssert(root.bar.isFile)
XCTAssertThrowsError(try root.foo.copy(to: root.bar))
try root.foo.copy(to: root.bar, overwrite: true)
} }
} }
@@ -182,14 +184,25 @@ class PathTests: XCTestCase {
XCTAssertTrue(bar1.exists) XCTAssertTrue(bar1.exists)
XCTAssertTrue(bar2.exists) XCTAssertTrue(bar2.exists)
} }
// test creates intermediary directories
try bar1.copy(into: root1.create.directories)
// test doesnt 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 { func testMoveTo() throws {
try Path.mktemp { root in try Path.mktemp { tmpdir in
try root.foo.touch().move(to: root.bar) try tmpdir.foo.touch().move(to: tmpdir.bar)
XCTAssertFalse(root.foo.exists) XCTAssertFalse(tmpdir.foo.exists)
XCTAssert(root.bar.isFile) XCTAssert(tmpdir.bar.isFile)
XCTAssertThrowsError(try tmpdir.foo.touch().move(to: tmpdir.bar))
try tmpdir.foo.move(to: tmpdir.bar, overwrite: true)
} }
} }
@@ -203,6 +216,17 @@ class PathTests: XCTestCase {
XCTAssertFalse(bar1.exists) XCTAssertFalse(bar1.exists)
XCTAssertTrue(bar2.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 doesnt 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)
} }
} }
@@ -225,10 +249,14 @@ class PathTests: XCTestCase {
XCTAssertEqual(Path.root.string, "/") XCTAssertEqual(Path.root.string, "/")
XCTAssertEqual(Path.home.string, NSHomeDirectory()) XCTAssertEqual(Path.home.string, NSHomeDirectory())
XCTAssertEqual(Path.documents.string, NSHomeDirectory() + "/Documents") XCTAssertEqual(Path.documents.string, NSHomeDirectory() + "/Documents")
#if os(macOS) #if !os(Linux)
XCTAssertEqual(Path.caches.string, NSHomeDirectory() + "/Library/Caches") XCTAssertEqual(Path.caches.string, NSHomeDirectory() + "/Library/Caches")
XCTAssertEqual(Path.cwd.string, FileManager.default.currentDirectoryPath) XCTAssertEqual(Path.cwd.string, FileManager.default.currentDirectoryPath)
XCTAssertEqual(Path.applicationSupport.string, NSHomeDirectory() + "/Library/Application Support") XCTAssertEqual(Path.applicationSupport.string, NSHomeDirectory() + "/Library/Application Support")
_ = defaultUrl(for: .documentDirectory)
_ = defaultUrl(for: .cachesDirectory)
_ = defaultUrl(for: .applicationSupportDirectory)
#endif #endif
} }
@@ -298,6 +326,9 @@ class PathTests: XCTestCase {
let now2 = Date().timeIntervalSince1970.rounded(.down) let now2 = Date().timeIntervalSince1970.rounded(.down)
XCTAssertNotEqual(now1, now2) XCTAssertNotEqual(now1, now2)
XCTAssertEqual(foo.mtime?.timeIntervalSince1970.rounded(.down), now2) //FIXME flakey XCTAssertEqual(foo.mtime?.timeIntervalSince1970.rounded(.down), now2) //FIXME flakey
XCTAssertNil(tmpdir.void.mtime)
XCTAssertNil(tmpdir.void.ctime)
} }
} }
@@ -325,23 +356,37 @@ class PathTests: XCTestCase {
XCTAssertThrowsError(try JSONDecoder().decode([Path].self, from: data)) XCTAssertThrowsError(try JSONDecoder().decode([Path].self, from: data))
} }
func testBundleExtensions() { func testBundleExtensions() throws {
XCTAssertTrue(Bundle.main.path.isDirectory)
XCTAssertTrue(Bundle.main.resources.isDirectory)
// dont exist in tests
_ = Bundle.main.path(forResource: "foo", ofType: "bar")
_ = Bundle.main.sharedFrameworks
}
func testDataExentsions() throws {
let data = try Data(contentsOf: Path(#file)!)
try Path.mktemp { tmpdir in try Path.mktemp { tmpdir in
_ = try data.write(to: tmpdir.foo) let bndl = Bundle(path: tmpdir.string)!
XCTAssertEqual(bndl.path, tmpdir)
XCTAssertEqual(bndl.sharedFrameworks, tmpdir.SharedFrameworks)
XCTAssertEqual(bndl.privateFrameworks, tmpdir.Frameworks)
XCTAssertEqual(bndl.resources, tmpdir)
XCTAssertNil(bndl.path(forResource: "foo", ofType: "bar"))
#if os(macOS)
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.Contents.Frameworks)
XCTAssertEqual(bndl.defaultResourcesPath, tmpdir.Contents.Resources)
#elseif os(tvOS) || os(iOS)
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.Frameworks)
XCTAssertEqual(bndl.defaultResourcesPath, tmpdir)
#else
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.lib)
XCTAssertEqual(bndl.defaultResourcesPath, tmpdir.share)
#endif
} }
} }
func testStringExentsions() 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 testStringExtensions() throws {
let string = try String(contentsOf: Path(#file)!) let string = try String(contentsOf: Path(#file)!)
try Path.mktemp { tmpdir in try Path.mktemp { tmpdir in
_ = try string.write(to: tmpdir.foo) _ = try string.write(to: tmpdir.foo)
@@ -353,4 +398,30 @@ class PathTests: XCTestCase {
_ = try FileHandle(forWritingAt: Path(#file)!) _ = try FileHandle(forWritingAt: Path(#file)!)
_ = try FileHandle(forUpdatingAt: 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()
// a non existant file is already unlocked
try tmpdir.nonExit.unlock()
}
#endif
}
func testTouchThrowsIfCannotWrite() throws {
try Path.mktemp { tmpdir in
try tmpdir.chmod(0o000)
XCTAssertThrowsError(try tmpdir.bar.touch())
}
}
} }

View File

@@ -9,7 +9,7 @@ extension PathTests {
("testConcatenation", testConcatenation), ("testConcatenation", testConcatenation),
("testCopyInto", testCopyInto), ("testCopyInto", testCopyInto),
("testCopyTo", testCopyTo), ("testCopyTo", testCopyTo),
("testDataExentsions", testDataExentsions), ("testDataExtensions", testDataExtensions),
("testDelete", testDelete), ("testDelete", testDelete),
("testDynamicMember", testDynamicMember), ("testDynamicMember", testDynamicMember),
("testEnumeration", testEnumeration), ("testEnumeration", testEnumeration),
@@ -20,6 +20,7 @@ extension PathTests {
("testFilesystemAttributes", testFilesystemAttributes), ("testFilesystemAttributes", testFilesystemAttributes),
("testIsDirectory", testIsDirectory), ("testIsDirectory", testIsDirectory),
("testJoin", testJoin), ("testJoin", testJoin),
("testLock", testLock),
("testMkpathIfExists", testMkpathIfExists), ("testMkpathIfExists", testMkpathIfExists),
("testMktemp", testMktemp), ("testMktemp", testMktemp),
("testMoveInto", testMoveInto), ("testMoveInto", testMoveInto),
@@ -28,9 +29,11 @@ extension PathTests {
("testRelativePathCodable", testRelativePathCodable), ("testRelativePathCodable", testRelativePathCodable),
("testRelativeTo", testRelativeTo), ("testRelativeTo", testRelativeTo),
("testRename", testRename), ("testRename", testRename),
("testSort", testSort),
("testStringConvertibles", testStringConvertibles), ("testStringConvertibles", testStringConvertibles),
("testStringExentsions", testStringExentsions), ("testStringExtensions", testStringExtensions),
("testTimes", testTimes), ("testTimes", testTimes),
("testTouchThrowsIfCannotWrite", testTouchThrowsIfCannotWrite),
] ]
} }