-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathWorkspaceTests.swift
More file actions
328 lines (285 loc) · 14.6 KB
/
Copy pathWorkspaceTests.swift
File metadata and controls
328 lines (285 loc) · 14.6 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
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 {
throw error
}
}
func testIsXCProject() throws {
let tmpDir = try createTemporaryDirectory()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
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 {
throw error
}
}
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()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
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 {
throw error
}
}
func testGetSubprojectURLsFromXCWorkspace() throws {
let tmpDir = try createTemporaryDirectory()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
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 {
throw error
}
}
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
}
func testIsValidFile() throws {
let tmpDir = try createTemporaryDirectory()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
do {
// Test valid Swift file
let swiftFileURL = try createFile(in: tmpDir, withName: "ValidFile.swift", contents: "// Swift code")
XCTAssertTrue(try WorkspaceFile.isValidFile(swiftFileURL))
// Test valid files with different supported extensions
let jsFileURL = try createFile(in: tmpDir, withName: "script.js", contents: "// JavaScript")
XCTAssertTrue(try WorkspaceFile.isValidFile(jsFileURL))
let mdFileURL = try createFile(in: tmpDir, withName: "README.md", contents: "# Markdown")
XCTAssertTrue(try WorkspaceFile.isValidFile(mdFileURL))
let jsonFileURL = try createFile(in: tmpDir, withName: "config.json", contents: "{}")
XCTAssertTrue(try WorkspaceFile.isValidFile(jsonFileURL))
// Test case insensitive extension matching
let swiftUpperURL = try createFile(in: tmpDir, withName: "File.SWIFT", contents: "// Swift")
XCTAssertTrue(try WorkspaceFile.isValidFile(swiftUpperURL))
// Test unsupported file extension
let unsupportedFileURL = try createFile(in: tmpDir, withName: "file.xyz", contents: "unsupported")
XCTAssertFalse(try WorkspaceFile.isValidFile(unsupportedFileURL))
// Test files matching skip patterns
let gitFileURL = try createFile(in: tmpDir, withName: ".git", contents: "")
XCTAssertFalse(try WorkspaceFile.isValidFile(gitFileURL))
let dsStoreURL = try createFile(in: tmpDir, withName: ".DS_Store", contents: "")
XCTAssertFalse(try WorkspaceFile.isValidFile(dsStoreURL))
let nodeModulesURL = try createFile(in: tmpDir, withName: "node_modules", contents: "")
XCTAssertFalse(try WorkspaceFile.isValidFile(nodeModulesURL))
// Test directory (should return false)
let subdirURL = try createSubdirectory(in: tmpDir, withName: "subdir")
XCTAssertFalse(try WorkspaceFile.isValidFile(subdirURL))
// Test Xcode workspace (should return false)
let xcworkspaceURL = try createSubdirectory(in: tmpDir, withName: "test.xcworkspace")
_ = try createFile(in: xcworkspaceURL, withName: "contents.xcworkspacedata", contents: "")
XCTAssertFalse(try WorkspaceFile.isValidFile(xcworkspaceURL))
// Test Xcode project (should return false)
let xcprojectURL = try createSubdirectory(in: tmpDir, withName: "test.xcodeproj")
_ = try createFile(in: xcprojectURL, withName: "project.pbxproj", contents: "")
XCTAssertFalse(try WorkspaceFile.isValidFile(xcprojectURL))
} catch {
throw error
}
}
func testIsValidFileWithCustomExclusionFilter() throws {
let tmpDir = try createTemporaryDirectory()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
do {
let swiftFileURL = try createFile(in: tmpDir, withName: "TestFile.swift", contents: "// Swift code")
let jsFileURL = try createFile(in: tmpDir, withName: "script.js", contents: "// JavaScript")
// Test without custom exclusion filter
XCTAssertTrue(try WorkspaceFile.isValidFile(swiftFileURL))
XCTAssertTrue(try WorkspaceFile.isValidFile(jsFileURL))
// Test with custom exclusion filter that excludes Swift files
let excludeSwiftFilter: (URL) -> Bool = { url in
return url.pathExtension.lowercased() == "swift"
}
XCTAssertFalse(try WorkspaceFile.isValidFile(swiftFileURL, shouldExcludeFile: excludeSwiftFilter))
XCTAssertTrue(try WorkspaceFile.isValidFile(jsFileURL, shouldExcludeFile: excludeSwiftFilter))
// Test with custom exclusion filter that excludes files with "Test" in name
let excludeTestFilter: (URL) -> Bool = { url in
return url.lastPathComponent.contains("Test")
}
XCTAssertFalse(try WorkspaceFile.isValidFile(swiftFileURL, shouldExcludeFile: excludeTestFilter))
XCTAssertTrue(try WorkspaceFile.isValidFile(jsFileURL, shouldExcludeFile: excludeTestFilter))
} catch {
throw error
}
}
func testIsValidFileWithAllSupportedExtensions() throws {
let tmpDir = try createTemporaryDirectory()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
do {
let supportedExtensions = supportedFileExtensions
for (index, ext) in supportedExtensions.enumerated() {
let fileName = "testfile\(index).\(ext)"
let fileURL = try createFile(in: tmpDir, withName: fileName, contents: "test content")
XCTAssertTrue(try WorkspaceFile.isValidFile(fileURL), "File with extension .\(ext) should be valid")
}
} catch {
throw error
}
}
}