forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspaceTests.swift
More file actions
214 lines (194 loc) · 9.45 KB
/
WorkspaceTests.swift
File metadata and controls
214 lines (194 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import XCTest
import Foundation
@testable import Workspace
class WorkspaceFileTests: XCTestCase {
func testMatchesPatterns() {
let url1 = URL(fileURLWithPath: "/path/to/file.swift")
let url2 = URL(fileURLWithPath: "/path/to/.git")
let patterns = [".git", ".svn"]
XCTAssertTrue(WorkspaceFile.matchesPatterns(url2, patterns: patterns))
XCTAssertFalse(WorkspaceFile.matchesPatterns(url1, patterns: patterns))
}
func testIsXCWorkspace() throws {
let tmpDir = try createTemporaryDirectory()
do {
let xcworkspaceURL = try createSubdirectory(in: tmpDir, withName: "myWorkspace.xcworkspace")
XCTAssertFalse(WorkspaceFile.isXCWorkspace(xcworkspaceURL))
let xcworkspaceDataURL = try createFile(in: xcworkspaceURL, withName: "contents.xcworkspacedata", contents: "")
XCTAssertTrue(WorkspaceFile.isXCWorkspace(xcworkspaceURL))
} catch {
deleteDirectoryIfExists(at: tmpDir)
throw error
}
deleteDirectoryIfExists(at: tmpDir)
}
func testIsXCProject() throws {
let tmpDir = try createTemporaryDirectory()
do {
let xcprojectURL = try createSubdirectory(in: tmpDir, withName: "myProject.xcodeproj")
XCTAssertFalse(WorkspaceFile.isXCProject(xcprojectURL))
let xcprojectDataURL = try createFile(in: xcprojectURL, withName: "project.pbxproj", contents: "")
XCTAssertTrue(WorkspaceFile.isXCProject(xcprojectURL))
} catch {
deleteDirectoryIfExists(at: tmpDir)
throw error
}
deleteDirectoryIfExists(at: tmpDir)
}
func testGetFilesInActiveProject() throws {
let tmpDir = try createTemporaryDirectory()
do {
let xcprojectURL = try createSubdirectory(in: tmpDir, withName: "myProject.xcodeproj")
_ = try createFile(in: xcprojectURL, withName: "project.pbxproj", contents: "")
_ = try createFile(in: tmpDir, withName: "file1.swift", contents: "")
_ = try createFile(in: tmpDir, withName: "file2.swift", contents: "")
_ = try createSubdirectory(in: tmpDir, withName: ".git")
let files = WorkspaceFile.getFilesInActiveWorkspace(workspaceURL: xcprojectURL, workspaceRootURL: tmpDir)
let fileNames = files.map { $0.url.lastPathComponent }
XCTAssertEqual(files.count, 2)
XCTAssertTrue(fileNames.contains("file1.swift"))
XCTAssertTrue(fileNames.contains("file2.swift"))
} catch {
deleteDirectoryIfExists(at: tmpDir)
throw error
}
deleteDirectoryIfExists(at: tmpDir)
}
func testGetFilesInActiveWorkspace() throws {
let tmpDir = try createTemporaryDirectory()
do {
let myWorkspaceRoot = try createSubdirectory(in: tmpDir, withName: "myWorkspace")
let xcWorkspaceURL = try createSubdirectory(in: myWorkspaceRoot, withName: "myWorkspace.xcworkspace")
let xcprojectURL = try createSubdirectory(in: myWorkspaceRoot, withName: "myProject.xcodeproj")
let myDependencyURL = try createSubdirectory(in: tmpDir, withName: "myDependency")
_ = try createFileFor_contents_dot_xcworkspacedata(directory: xcWorkspaceURL, fileRefs: [
"container:myProject.xcodeproj",
"group:../notExistedDir/notExistedProject.xcodeproj",
"group:../myDependency",])
_ = try createFile(in: xcprojectURL, withName: "project.pbxproj", contents: "")
// Files under workspace should be included
_ = try createFile(in: myWorkspaceRoot, withName: "file1.swift", contents: "")
// unsupported patterns and file extension should be excluded
_ = try createFile(in: myWorkspaceRoot, withName: "unsupportedFileExtension.xyz", contents: "")
_ = try createSubdirectory(in: myWorkspaceRoot, withName: ".git")
// Files under project metadata folder should be excluded
_ = try createFile(in: xcprojectURL, withName: "fileUnderProjectMetadata.swift", contents: "")
// Files under dependency should be included
_ = try createFile(in: myDependencyURL, withName: "depFile1.swift", contents: "")
// Should be excluded
_ = try createSubdirectory(in: myDependencyURL, withName: ".git")
// Files under unrelated directories should be excluded
_ = try createFile(in: tmpDir, withName: "unrelatedFile1.swift", contents: "")
let files = WorkspaceFile.getFilesInActiveWorkspace(workspaceURL: xcWorkspaceURL, workspaceRootURL: myWorkspaceRoot)
let fileNames = files.map { $0.url.lastPathComponent }
XCTAssertEqual(files.count, 2)
XCTAssertTrue(fileNames.contains("file1.swift"))
XCTAssertTrue(fileNames.contains("depFile1.swift"))
} catch {
deleteDirectoryIfExists(at: tmpDir)
throw error
}
deleteDirectoryIfExists(at: tmpDir)
}
func testGetSubprojectURLsFromXCWorkspace() throws {
let tmpDir = try createTemporaryDirectory()
do {
let xcworkspaceURL = try createSubdirectory(in: tmpDir, withName: "myWorkspace.xcworkspace")
_ = try createFileFor_contents_dot_xcworkspacedata(directory: xcworkspaceURL, fileRefs: [
"container:myProject.xcodeproj",
"group:myDependency"])
let subprojectURLs = WorkspaceFile.getSubprojectURLs(in: xcworkspaceURL)
XCTAssertEqual(subprojectURLs.count, 2)
XCTAssertEqual(subprojectURLs[0].path, tmpDir.path)
XCTAssertEqual(subprojectURLs[1].path, tmpDir.appendingPathComponent("myDependency").path)
} catch {
deleteDirectoryIfExists(at: tmpDir)
throw error
}
deleteDirectoryIfExists(at: tmpDir)
}
func testGetSubprojectURLs() {
let workspaceURL = URL(fileURLWithPath: "/path/to/workspace.xcworkspace")
let xcworkspaceData = """
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "container:tryapp/tryapp.xcodeproj">
</FileRef>
<FileRef
location = "group:Copilot for Xcode.xcodeproj">
</FileRef>
<FileRef
location = "group:Test1">
</FileRef>
<FileRef
location = "group:Test2/project2.xcodeproj">
</FileRef>
<FileRef
location = "absolute:/Test3/project3.xcodeproj">
</FileRef>
<FileRef
location = "group:../Test4/project4.xcodeproj">
</FileRef>
</Workspace>
""".data(using: .utf8)!
let subprojectURLs = WorkspaceFile.getSubprojectURLs(workspaceURL: workspaceURL, data: xcworkspaceData)
XCTAssertEqual(subprojectURLs.count, 5)
XCTAssertEqual(subprojectURLs[0].path, "/path/to/tryapp")
XCTAssertEqual(subprojectURLs[1].path, "/path/to")
XCTAssertEqual(subprojectURLs[2].path, "/path/to/Test1")
XCTAssertEqual(subprojectURLs[3].path, "/path/to/Test2")
XCTAssertEqual(subprojectURLs[4].path, "/path/to/../Test4")
}
func deleteDirectoryIfExists(at url: URL) {
if FileManager.default.fileExists(atPath: url.path) {
do {
try FileManager.default.removeItem(at: url)
} catch {
print("Failed to delete directory at \(url.path)")
}
}
}
func createTemporaryDirectory() throws -> URL {
let temporaryDirectoryURL = FileManager.default.temporaryDirectory
let directoryName = UUID().uuidString
let directoryURL = temporaryDirectoryURL.appendingPathComponent(directoryName)
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
#if DEBUG
print("Create temp directory \(directoryURL.path)")
#endif
return directoryURL
}
func createSubdirectory(in directory: URL, withName name: String) throws -> URL {
let subdirectoryURL = directory.appendingPathComponent(name)
try FileManager.default.createDirectory(at: subdirectoryURL, withIntermediateDirectories: true, attributes: nil)
return subdirectoryURL
}
func createFile(in directory: URL, withName name: String, contents: String) throws -> URL {
let fileURL = directory.appendingPathComponent(name)
let data = contents.data(using: .utf8)
FileManager.default.createFile(atPath: fileURL.path, contents: data, attributes: nil)
return fileURL
}
func createFileFor_contents_dot_xcworkspacedata(directory: URL, fileRefs: [String]) throws -> URL {
let contents = generateXCWorkspacedataContents(fileRefs: fileRefs)
return try createFile(in: directory, withName: "contents.xcworkspacedata", contents: contents)
}
func generateXCWorkspacedataContents(fileRefs: [String]) -> String {
var contents = """
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
"""
for fileRef in fileRefs {
contents += """
<FileRef
location = "\(fileRef)">
</FileRef>
"""
}
contents += "</Workspace>"
return contents
}
}