-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathWorkspaceFileTests.swift
More file actions
460 lines (394 loc) · 19.9 KB
/
WorkspaceFileTests.swift
File metadata and controls
460 lines (394 loc) · 19.9 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
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 createXCProjectFolder(in: tmpDir, withName: "myProject.xcodeproj")
_ = 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 createXCWorkspaceFolder(in: myWorkspaceRoot, withName: "myWorkspace.xcworkspace", fileRefs: [
"container:myProject.xcodeproj",
"group:../notExistedDir/notExistedProject.xcodeproj",
"group:../myDependency",])
let xcprojectURL = try createXCProjectFolder(in: myWorkspaceRoot, withName: "myProject.xcodeproj")
let myDependencyURL = try createSubdirectory(in: tmpDir, withName: "myDependency")
// 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)
}
let workspaceDir = try createSubdirectory(in: tmpDir, withName: "workspace")
// Create tryapp directory and project
let tryappDir = try createSubdirectory(in: tmpDir, withName: "tryapp")
_ = try createXCProjectFolder(in: tryappDir, withName: "tryapp.xcodeproj")
// Create Copilot for Xcode project
_ = try createXCProjectFolder(in: workspaceDir, withName: "Copilot for Xcode.xcodeproj")
// Create Test1 directory
let test1Dir = try createSubdirectory(in: tmpDir, withName: "Test1")
// Create Test2 directory and project
let test2Dir = try createSubdirectory(in: tmpDir, withName: "Test2")
_ = try createXCProjectFolder(in: test2Dir, withName: "project2.xcodeproj")
// Create the workspace data file with our references
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">
</FileRef>
</Workspace>
"""
let workspaceURL = try createXCWorkspaceFolder(in: workspaceDir, withName: "workspace.xcworkspace", xcworkspacedata: xcworkspaceData)
let subprojectURLs = WorkspaceFile.getSubprojectURLs(in: workspaceURL)
XCTAssertEqual(subprojectURLs.count, 4)
let resolvedPaths = subprojectURLs.map { $0.path }
let expectedPaths = [
tryappDir.path,
workspaceDir.path, // For Copilot for Xcode.xcodeproj
test1Dir.path,
test2Dir.path
]
XCTAssertEqual(resolvedPaths, expectedPaths)
}
func testGetSubprojectURLsFromEmbeddedXCWorkspace() throws {
let tmpDir = try createTemporaryDirectory()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
// Create the workspace data file with a self reference
let xcworkspaceData = """
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>
"""
// Create the MyApp directory structure
let myAppDir = try createSubdirectory(in: tmpDir, withName: "MyApp")
let xcodeProjectDir = try createXCProjectFolder(in: myAppDir, withName: "MyApp.xcodeproj")
let embeddedWorkspaceDir = try createXCWorkspaceFolder(in: xcodeProjectDir, withName: "MyApp.xcworkspace", xcworkspacedata: xcworkspaceData)
let subprojectURLs = WorkspaceFile.getSubprojectURLs(in: embeddedWorkspaceDir)
XCTAssertEqual(subprojectURLs.count, 1)
XCTAssertEqual(subprojectURLs[0].lastPathComponent, "MyApp")
XCTAssertEqual(subprojectURLs[0].path, myAppDir.path)
}
func testGetSubprojectURLsFromXCWorkspaceOrganizedByGroup() throws {
let tmpDir = try createTemporaryDirectory()
defer {
deleteDirectoryIfExists(at: tmpDir)
}
// Create directories for the projects and groups
let tryappDir = try createSubdirectory(in: tmpDir, withName: "tryapp")
_ = try createXCProjectFolder(in: tryappDir, withName: "tryapp.xcodeproj")
let webLibraryDir = try createSubdirectory(in: tmpDir, withName: "WebLibrary")
// Create the group directories
let group1Dir = try createSubdirectory(in: tmpDir, withName: "group1")
let group2Dir = try createSubdirectory(in: group1Dir, withName: "group2")
_ = try createSubdirectory(in: group2Dir, withName: "group3")
_ = try createSubdirectory(in: group1Dir, withName: "group4")
// Create the MyProjects directory
let myProjectsDir = try createSubdirectory(in: tmpDir, withName: "MyProjects")
// Create the copilot-xcode directory and project
let copilotXcodeDir = try createSubdirectory(in: myProjectsDir, withName: "copilot-xcode")
_ = try createXCProjectFolder(in: copilotXcodeDir, withName: "Copilot for Xcode.xcodeproj")
// Create the SwiftLanguageWeather directory and project
let swiftWeatherDir = try createSubdirectory(in: myProjectsDir, withName: "SwiftLanguageWeather")
_ = try createXCProjectFolder(in: swiftWeatherDir, withName: "SwiftWeather.xcodeproj")
// Create the workspace data file with a complex group structure
let xcworkspaceData = """
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "container:tryapp/tryapp.xcodeproj">
</FileRef>
<FileRef
location = "group:WebLibrary">
</FileRef>
<Group
location = "group:group1"
name = "group1">
<Group
location = "group:group2"
name = "group2">
<Group
location = "group:group3"
name = "group3">
<FileRef
location = "group:../../../MyProjects/copilot-xcode/Copilot for Xcode.xcodeproj">
</FileRef>
</Group>
</Group>
<Group
location = "group:group4"
name = "group4">
</Group>
<FileRef
location = "group:../MyProjects/SwiftLanguageWeather/SwiftWeather.xcodeproj">
</FileRef>
</Group>
</Workspace>
"""
// Create a test workspace structure
let workspaceURL = try createXCWorkspaceFolder(in: tmpDir, withName: "workspace.xcworkspace", xcworkspacedata: xcworkspaceData)
let subprojectURLs = WorkspaceFile.getSubprojectURLs(in: workspaceURL)
XCTAssertEqual(subprojectURLs.count, 4)
let expectedPaths = [
tryappDir.path,
webLibraryDir.path,
copilotXcodeDir.path,
swiftWeatherDir.path
]
for expectedPath in expectedPaths {
XCTAssertTrue(subprojectURLs.contains { $0.path == expectedPath }, "Expected path not found: \(expectedPath)")
}
}
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 createXCProjectFolder(in baseDirectory: URL, withName projectName: String) throws -> URL {
let projectURL = try createSubdirectory(in: baseDirectory, withName: projectName)
if projectName.hasSuffix(".xcodeproj") {
_ = try createFile(in: projectURL, withName: "project.pbxproj", contents: "// Project file contents")
}
return projectURL
}
func createXCWorkspaceFolder(in baseDirectory: URL, withName workspaceName: String, fileRefs: [String]?) throws -> URL {
let xcworkspaceURL = try createSubdirectory(in: baseDirectory, withName: workspaceName)
if let fileRefs {
_ = try createXCworkspacedataFile(directory: xcworkspaceURL, fileRefs: fileRefs)
}
return xcworkspaceURL
}
func createXCWorkspaceFolder(in baseDirectory: URL, withName workspaceName: String, xcworkspacedata: String) throws -> URL {
let xcworkspaceURL = try createSubdirectory(in: baseDirectory, withName: workspaceName)
_ = try createFile(in: xcworkspaceURL, withName: "contents.xcworkspacedata", contents: xcworkspacedata)
return xcworkspaceURL
}
func createXCworkspacedataFile(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
}
}
}