How to use DirectoryPath class

Best Mockingbird code snippet using .DirectoryPath

Run Mockingbird automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

URL+Cockoo.swift

Source: URL+Cockoo.swift Github

copy
1import struct Foundation.NSURL.URL
2import class Foundation.NSFileManager.FileManager
3
4internal extension URL {
5  
6  init(directoryPath: String) {
7    var directoryPath = directoryPath
8    if directoryPath.hasPrefix("~/") {
9      directoryPath = FileManager.default.homeDirectoryForCurrentUser.absoluteString + String(directoryPath[directoryPath.index(directoryPath.startIndex, offsetBy: 2)..<directoryPath.endIndex])
10    } else { /**/ }
11    guard
12      !directoryPath.isEmpty,
13      FileManager.default.directoryExists(atPath: directoryPath),
14      let directoryURL = URL(string: "file://\(directoryPath)")
15      else { fatalError("Invalid or missing directory: \(directoryPath)") } // TODO: error?
16    self = directoryURL
17  }
18  
19  init(filePath: String) {
20    var filePath = filePath
21    if filePath.hasPrefix("~/") {
22      filePath = FileManager.default.homeDirectoryForCurrentUser.absoluteString + String(filePath[filePath.index(filePath.startIndex, offsetBy: 2)..<filePath.endIndex])
23    } else { /**/ }
24    guard
25      !filePath.isEmpty,
26      FileManager.default.fileExists(atPath: filePath),
27      let directoryURL = URL(string: "file://\(filePath)")
28      else { fatalError("Invalid or missing file: \(filePath)") } // TODO: error?
29    self = directoryURL
30  }
31}
32
Full Screen

TemplateRepositoryPathTests.swift

Source: TemplateRepositoryPathTests.swift Github

copy
1// The MIT License
2//
3// Copyright (c) 2015 Gwendal Roué
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21// THE SOFTWARE.
22
23
24import XCTest
25import Mustache
26
27class TemplateRepositoryPathTests: XCTestCase {
28    
29    func testTemplateRepositoryWithURL() {
30        let testBundle = Bundle(for: type(of: self))
31        let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_UTF8", ofType: nil)!
32        let repo = TemplateRepository(directoryPath: directoryPath)
33        var template: Template
34        var rendering: String
35        
36        do {
37            _ = try repo.template(named: "notFound")
38            XCTAssert(false)
39        } catch {
40        }
41        
42        template = try! repo.template(named: "file1")
43        rendering = try! template.render()
44        XCTAssertEqual(rendering, "é1.mustache\ndir/é1.mustache\ndir/dir/é1.mustache\ndir/dir/é2.mustache\n\n\ndir/é2.mustache\n\n\né2.mustache\n\n")
45        
46        template = try! repo.template(string: "{{>file1}}")
47        rendering = try! template.render()
48        XCTAssertEqual(rendering, "é1.mustache\ndir/é1.mustache\ndir/dir/é1.mustache\ndir/dir/é2.mustache\n\n\ndir/é2.mustache\n\n\né2.mustache\n\n")
49        
50        template = try! repo.template(string: "{{>dir/file1}}")
51        rendering = try! template.render()
52        XCTAssertEqual(rendering, "dir/é1.mustache\ndir/dir/é1.mustache\ndir/dir/é2.mustache\n\n\ndir/é2.mustache\n\n")
53        
54        template = try! repo.template(string: "{{>dir/dir/file1}}")
55        rendering = try! template.render()
56        XCTAssertEqual(rendering, "dir/dir/é1.mustache\ndir/dir/é2.mustache\n\n")
57    }
58    
59    func testTemplateRepositoryWithURLTemplateExtensionEncoding() {
60        let testBundle = Bundle(for: type(of: self))
61        var directoryPath: String
62        var repo: TemplateRepository
63        var template: Template
64        var rendering: String
65        
66        directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_UTF8", ofType: nil)!
67        repo = TemplateRepository(directoryPath: directoryPath, templateExtension: "mustache", encoding: String.Encoding.utf8)
68        template = try! repo.template(named: "file1")
69        rendering = try! template.render()
70        XCTAssertEqual(rendering, "é1.mustache\ndir/é1.mustache\ndir/dir/é1.mustache\ndir/dir/é2.mustache\n\n\ndir/é2.mustache\n\n\né2.mustache\n\n")
71        
72        directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_UTF8", ofType: nil)!
73        repo = TemplateRepository(directoryPath: directoryPath, templateExtension: "txt", encoding: String.Encoding.utf8)
74        template = try! repo.template(named: "file1")
75        rendering = try! template.render()
76        XCTAssertEqual(rendering, "é1.txt\ndir/é1.txt\ndir/dir/é1.txt\ndir/dir/é2.txt\n\n\ndir/é2.txt\n\n\né2.txt\n\n")
77        
78        directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_UTF8", ofType: nil)!
79        repo = TemplateRepository(directoryPath: directoryPath, templateExtension: "", encoding: String.Encoding.utf8)
80        template = try! repo.template(named: "file1")
81        rendering = try! template.render()
82        XCTAssertEqual(rendering, "é1\ndir/é1\ndir/dir/é1\ndir/dir/é2\n\n\ndir/é2\n\n\né2\n\n")
83        
84        directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_ISOLatin1", ofType: nil)!
85        repo = TemplateRepository(directoryPath: directoryPath, templateExtension: "mustache", encoding: String.Encoding.isoLatin1)
86        template = try! repo.template(named: "file1")
87        rendering = try! template.render()
88        XCTAssertEqual(rendering, "é1.mustache\ndir/é1.mustache\ndir/dir/é1.mustache\ndir/dir/é2.mustache\n\n\ndir/é2.mustache\n\n\né2.mustache\n\n")
89        
90        directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_ISOLatin1", ofType: nil)!
91        repo = TemplateRepository(directoryPath: directoryPath, templateExtension: "txt", encoding: String.Encoding.isoLatin1)
92        template = try! repo.template(named: "file1")
93        rendering = try! template.render()
94        XCTAssertEqual(rendering, "é1.txt\ndir/é1.txt\ndir/dir/é1.txt\ndir/dir/é2.txt\n\n\ndir/é2.txt\n\n\né2.txt\n\n")
95        
96        directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests_ISOLatin1", ofType: nil)!
97        repo = TemplateRepository(directoryPath: directoryPath, templateExtension: "", encoding: String.Encoding.isoLatin1)
98        template = try! repo.template(named: "file1")
99        rendering = try! template.render()
100        XCTAssertEqual(rendering, "é1\ndir/é1\ndir/dir/é1\ndir/dir/é2\n\n\ndir/é2\n\n\né2\n\n")
101    }
102    
103    func testAbsolutePartialName() {
104        let testBundle = Bundle(for: type(of: self))
105        let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests", ofType: nil)!
106        let repo = TemplateRepository(directoryPath: directoryPath)
107        let template = try! repo.template(named: "base")
108        let rendering = try! template.render()
109        XCTAssertEqual(rendering, "success")
110    }
111    
112    func testPartialNameCanNotEscapeTemplateRepositoryRootDirectory() {
113        let testBundle = Bundle(for: type(of: self))
114        let directoryPath = testBundle.path(forResource: "TemplateRepositoryFileSystemTests", ofType: nil)!
115        let repo = TemplateRepository(directoryPath: (directoryPath as NSString).appendingPathComponent("partials"))
116        
117        let template = try! repo.template(named: "partial2")
118        let rendering = try! template.render()
119        XCTAssertEqual(rendering, "success")
120        
121        do {
122            _ = try repo.template(named: "up")
123            XCTFail("Expected MustacheError")
124        } catch let error as MustacheError {
125            XCTAssertEqual(error.kind, MustacheError.Kind.templateNotFound)
126        } catch {
127            XCTFail("Expected MustacheError")
128        }
129    }
130}
131
Full Screen

PathTests.swift

Source: PathTests.swift Github

copy
1//
2//  PathTests.swift
3//  FileSmith
4//
5//  Created by Kåre Morstøl on 29/11/2016.
6//
7
8import XCTest
9import FileSmith
10import Foundation
11
12class PathTests: XCTestCase {
13
14	func testAddPaths() {
15		let absolutedir: DirectoryPath = "/tmp"
16		let relativedir: DirectoryPath = "relativedir"
17		let file: FilePath = "file"
18
19		XCTAssertEqual(String(describing: absolutedir + file), "/tmp/file")
20		XCTAssertEqual((relativedir + file).string, "relativedir/file")
21
22		let _: DirectoryPath = DirectoryPath.home + "something"
23		let _: FilePath = DirectoryPath.home + "something"
24		// let _ = DirectoryPath.home + "dir" // Will not and should not compile.
25	}
26
27	func testRelativeFilePath() {
28		let filepath = FilePath("folder1/file1.txt")
29
30		XCTAssertEqual(filepath.base?.absoluteString, FileManager.default.currentDirectoryPath)
31		XCTAssertEqual(filepath.base?.url, DirectoryPath.current.url)
32		XCTAssertEqual(filepath.relativeString, "folder1/file1.txt")
33		XCTAssertEqual(filepath.relativeURL, URL(fileURLWithPath: "folder1/file1.txt"))
34		XCTAssertEqual(filepath.string, "folder1/file1.txt")
35	}
36
37	func testRelativeDirectoryPath() {
38		var directorypath: DirectoryPath = "directory1/directory2"
39
40		XCTAssertEqual(directorypath.base?.absoluteString, FileManager.default.currentDirectoryPath)
41		XCTAssertEqual(directorypath.base?.url, DirectoryPath.current.url)
42		XCTAssertEqual(directorypath.relativeString, "directory1/directory2")
43		XCTAssertEqual(directorypath.relativeURL, URL(fileURLWithPath: "directory1/directory2"))
44		XCTAssertEqual(directorypath.string, "directory1/directory2")
45
46		directorypath = "."
47		XCTAssertEqual(directorypath.string, ".")
48		XCTAssertEqual(directorypath.relativeComponents!, [])
49		XCTAssertEqual(directorypath.base?.absoluteString, FileManager.default.currentDirectoryPath)
50	}
51
52	func testAbsoluteFilePath() {
53		let filepath = FilePath("/tmp/folder1/file1.txt")
54
55		XCTAssertNil(filepath.base)
56		XCTAssertNil(filepath.relativeString)
57		XCTAssertNil(filepath.relativeURL)
58		XCTAssertEqual(filepath.string, "/tmp/folder1/file1.txt")
59	}
60
61	func testAbsoluteDirectoryPath() {
62		let directorypath = DirectoryPath("/tmp/directory1/directory2/")
63
64		XCTAssertNil(directorypath.base)
65		XCTAssertNil(directorypath.relativeString)
66		XCTAssertNil(directorypath.relativeURL)
67		XCTAssertEqual(directorypath.string, "/tmp/directory1/directory2")
68	}
69
70	func testName() {
71		XCTAssertEqual(FilePath("/file.txt").name, "file.txt")
72		XCTAssertEqual(FilePath("file.txt").extension, "txt")
73		XCTAssertEqual(FilePath("dir/file.txt").nameWithoutExtension, "file")
74		XCTAssertEqual(FilePath(".file.txt").name, ".file.txt")
75		XCTAssertEqual(FilePath("/dir/.file.txt").extension, "txt")
76		XCTAssertEqual(FilePath(".file.txt").nameWithoutExtension, ".file")
77		XCTAssertEqual(FilePath(".file").name, ".file")
78		XCTAssertEqual(FilePath(".file").extension, nil)
79		XCTAssertEqual(FilePath(".file").nameWithoutExtension, ".file")
80		XCTAssertEqual(FilePath("file.txt.").name, "file.txt.")
81		XCTAssertEqual(FilePath("file.txt.").extension, nil)
82		XCTAssertEqual(FilePath("file.txt.").nameWithoutExtension, "file.txt")
83		XCTAssertEqual(FilePath("file").name, "file")
84		XCTAssertEqual(FilePath("file").extension, nil)
85		XCTAssertEqual(FilePath("file").nameWithoutExtension, "file")
86		XCTAssertEqual(DirectoryPath(".").name, DirectoryPath.current.name)
87		XCTAssertEqual(DirectoryPath("/").name, "/")
88	}
89
90	func testURL() {
91		XCTAssertEqual(
92			DirectoryPath(URL(fileURLWithPath:"/tmp/directory1/directory2/"))?.string,
93			"/tmp/directory1/directory2")
94		XCTAssertEqual(FilePath(URL(fileURLWithPath:"/tmp/directory1/file2"))?.string, "/tmp/directory1/file2")
95		XCTAssertEqual(
96			DirectoryPath(URL(fileURLWithPath:"/tmp/directory1/directory2/"))?.string,
97			"/tmp/directory1/directory2")
98
99		XCTAssertNil(FilePath(URL(string:"http://blog.nottoobadsoftware.com/tag/swift/")!))
100		XCTAssertNil(AnyPath(URL(string:"http://blog.nottoobadsoftware.com/tag/swift/")!))
101		XCTAssertNil(DirectoryPath(URL(string:"http://blog.nottoobadsoftware.com/tag/swift/")!))
102	}
103
104	func testPathTypeDetection() {
105		XCTAssertNil(path(detectTypeOf:"sdfsf/ljljlk"))
106		XCTAssert(path(detectTypeOf:"sdfsf/ljljlk/") is DirectoryPath)
107		XCTAssert(path(detectTypeOf:"/tmp") is DirectoryPath)
108		XCTAssert(path(detectTypeOf:#file) is FilePath)
109	}
110
111	func testDotDot() {
112		XCTAssertEqual(FilePath("/dir1/dir2/..").string, "/dir1")
113		XCTAssertEqual(FilePath("/../dir1/dir2/..").string, "/../dir1")
114		XCTAssertEqual(FilePath("/dir/dir/../../dir2").string, "/dir2")
115		XCTAssertEqual(FilePath("/dir/../dir/../../dir2").string, "/../dir2")
116		XCTAssertEqual(FilePath("/dir/dir/../../../dir2").string, "/../dir2")
117		XCTAssertEqual(FilePath("/../dir1/dir2/..").string, "/../dir1")
118		XCTAssertEqual(FilePath("/../dir1/../dir2/..").string, "/..")
119
120		let relative = FilePath(base: "/base1/../base2", relative: "rel1/..")
121		XCTAssertEqual(relative.relativeString, ".")
122		XCTAssertEqual(relative.base?.string, "/base2")
123		XCTAssertEqual(relative.absoluteString, "/base2")
124
125		var relativedir = DirectoryPath(base: "/base1/../", relative: "/rel1/../rel2")
126		XCTAssertEqual(relativedir.relativeString, "rel2")
127		XCTAssertEqual(relativedir.base?.string, "/")
128		XCTAssertEqual(relativedir.absoluteString, "/rel2")
129
130		relativedir = DirectoryPath(base: "/../base1", relative: "../rel1")
131		XCTAssertEqual(relativedir.relativeString, "../rel1")
132		XCTAssertEqual(relativedir.base?.string, "/../base1")
133		XCTAssertEqual(relativedir.absoluteString, "/../rel1")
134
135		XCTAssertEqual(relativedir.absolute.parent().string, "/..")
136		XCTAssertEqual(relativedir.parent().string, "..")
137		XCTAssertEqual(relativedir.parent(nr: 2).string, "/")
138		XCTAssertEqual(relativedir.parent().parent().string, "/")
139
140		XCTAssertEqual(relativedir.name, "rel1")
141		XCTAssertEqual(relativedir.parent().name, "..")
142		XCTAssertEqual(FilePath("/../dir1/dir2/..").name, "dir1")
143	}
144
145	func testCertainCharactersAtBeginningOfPath() {
146		XCTAssertEqual(DirectoryPath("~/").absoluteString, NSHomeDirectoryForUser(NSUserName())!)
147		XCTAssertEqual(DirectoryPath(".").absolute, DirectoryPath.current)
148		XCTAssertEqual(DirectoryPath("./"), DirectoryPath(""))
149	}
150
151	func testIsAParentOf() {
152		XCTAssertTrue(DirectoryPath("/a/b/").isAParentOf(AnyPath("/a/b/c")))
153		XCTAssertFalse(DirectoryPath("/a/b/").isAParentOf(AnyPath("/c/b/c")))
154		XCTAssertFalse(DirectoryPath("/a/b/").isAParentOf(AnyPath("/a/")))
155		XCTAssertFalse(DirectoryPath("/a/b/").isAParentOf(DirectoryPath("/a/b/")))
156	}
157
158	func testRelativeTo() {
159		let base = DirectoryPath("/a/b/c")
160		XCTAssertEqual(FilePath("/a/b/c/d").relativeTo(base), base.append(file: "d", relative: true))
161		XCTAssertEqual(FilePath("/a/b/c/d/e").relativeTo(base), base.append(file: "d/e/", relative: true))
162		XCTAssertEqual(DirectoryPath("/a/b/c").relativeTo(base), base.append(directory: ".", relative: true))
163		XCTAssertEqual(DirectoryPath("/a/b").relativeTo(base), base.append(directory: "..", relative: true))
164		XCTAssertEqual(DirectoryPath("/a").relativeTo(base), base.append(directory: "../../", relative: true))
165		XCTAssertEqual(DirectoryPath("/").relativeTo(base), base.append(directory: "../../../", relative: true))
166		XCTAssertEqual(DirectoryPath("/a/x/y/z/").relativeTo(base), base.append(directory: "../../x/y/z/", relative: true))
167		XCTAssertEqual(DirectoryPath("/x/y/z/").relativeTo(base), base.append(directory: "../../../x/y/z/", relative: true))
168	}
169}
170
171extension PathTests {
172	public static var allTests = [
173		("testAddPaths", testAddPaths),
174		("testRelativeFilePath", testRelativeFilePath),
175		("testRelativeDirectoryPath", testRelativeDirectoryPath),
176		("testAbsoluteFilePath", testAbsoluteFilePath),
177		("testAbsoluteDirectoryPath", testAbsoluteDirectoryPath),
178		("testName", testName),
179		("testURL", testURL),
180		("testPathTypeDetection", testPathTypeDetection),
181		("testDotDot", testDotDot),
182		("testCertainCharactersAtBeginningOfPath", testCertainCharactersAtBeginningOfPath),
183		("testIsAParentOf", testIsAParentOf),
184		]
185}
186
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in DirectoryPath

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)