Implemented the "init(at: )" initialiser, the "pathString" computed property, and the "appendingPath(_: )" function for the URL+Extensions extension in the library target.
This commit is contained in:
parent
3a447b4f71
commit
a1ad391baa
35
Sources/Library/Extensions/URL+Extensions.swift
Normal file
35
Sources/Library/Extensions/URL+Extensions.swift
Normal file
@ -0,0 +1,35 @@
|
||||
import Foundation
|
||||
|
||||
extension URL {
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(at filePath: String) {
|
||||
if #available(macOS 13.0, *) {
|
||||
self = URL(filePath: filePath)
|
||||
} else {
|
||||
self = URL(fileURLWithPath: filePath)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
var pathString: String {
|
||||
if #available(macOS 13.0, *) {
|
||||
path()
|
||||
} else {
|
||||
path
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func appendingPath(_ path: String) -> URL {
|
||||
if #available(macOS 13.0, *) {
|
||||
appending(path: path)
|
||||
} else {
|
||||
appendingPathComponent(path)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
extension URL {
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(at filePath: String) {
|
||||
if #available(macOS 13.0, *) {
|
||||
self = URL(filePath: filePath)
|
||||
} else {
|
||||
self = URL(fileURLWithPath: filePath)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -54,25 +54,9 @@ public struct FileService: FileServicing {
|
||||
throw FileServiceError.urlNotFileURL
|
||||
}
|
||||
|
||||
let filePath = getPath(for: url)
|
||||
let filePath = url.pathString
|
||||
|
||||
return fileManager.fileExists(atPath: filePath)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension FileService {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func getPath(for url: URL) -> String {
|
||||
if #available(macOS 13.0, *) {
|
||||
return url.path()
|
||||
} else {
|
||||
return url.path
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,12 +27,8 @@ public struct CreateRootFolderTask {
|
||||
} else {
|
||||
await fileService.currentFolder
|
||||
}
|
||||
|
||||
let newFolder = if #available(macOS 13.0, *) {
|
||||
rootFolder.appending(path: name)
|
||||
} else {
|
||||
rootFolder.appendingPathComponent(name)
|
||||
}
|
||||
|
||||
let newFolder = rootFolder.appendingPath(name)
|
||||
|
||||
try await fileService.createFolder(at: newFolder)
|
||||
|
||||
|
76
Tests/Library/Cases/Extensions/URL+ExtensionsTests.swift
Normal file
76
Tests/Library/Cases/Extensions/URL+ExtensionsTests.swift
Normal file
@ -0,0 +1,76 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct URL_ExtensionsTests {
|
||||
|
||||
// MARK: Initialisers tests
|
||||
|
||||
@Test(arguments: zip([String.someFilePath, .dotPath, .tildePath],
|
||||
[URL.someFile, .dotFile, .tildeFile]))
|
||||
func initAt(
|
||||
with filePath: String,
|
||||
expects url: URL
|
||||
) async throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let result = URL(at: filePath)
|
||||
|
||||
// THEN
|
||||
#expect(result == url)
|
||||
#expect(result.isFileURL == true)
|
||||
}
|
||||
|
||||
// MARK: Computed tests
|
||||
|
||||
@Test(arguments: zip([URL.someFile, .dotFile, .tildeFile, .someURL],
|
||||
[String.someFilePath, .dotPath, .tildePath, .empty]))
|
||||
func pathString(
|
||||
with url: URL,
|
||||
expects path: String
|
||||
) async throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let result = url.pathString
|
||||
|
||||
// THEN
|
||||
#expect(result == path)
|
||||
}
|
||||
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test(arguments: zip([URL.dotFile, .tildeFile, .someFile],
|
||||
[".\(String.someFilePath)", "~\(String.someFilePath)", "\(String.someFilePath)\(String.someFilePath)"]))
|
||||
func appendingPath(
|
||||
with url: URL,
|
||||
expects path: String
|
||||
) async throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let result = url.appendingPath(.someFilePath)
|
||||
|
||||
// THEN
|
||||
#expect(result.pathString == path)
|
||||
#expect(result.isFileURL == true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let dotPath = "."
|
||||
static let empty = ""
|
||||
static let tildePath = "~"
|
||||
static let someFilePath = "/some/file/path"
|
||||
}
|
||||
|
||||
// MARK: - URL+Constants
|
||||
|
||||
private extension URL {
|
||||
static let dotFile = URL(at: .dotPath)
|
||||
static let someFile = URL(at: .someFilePath)
|
||||
static let someURL = URL(string: "https://some.url.path")!
|
||||
static let tildeFile = URL(at: .tildePath)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user