Skip to content

Commit b0b491c

Browse files
committed
Add tests for GenerateXcodeproj
Add test cases for the inclusion of files in the root directory, for the inclusion of non-source files in source directories and for the exclusion of files by git.
1 parent fe35809 commit b0b491c

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Tests/XcodeprojTests/GenerateXcodeprojTests.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import TestSupport
1313
import PackageDescription
1414
import PackageGraph
1515
import PackageModel
16+
import SourceControl
1617
@testable import Xcodeproj
1718
import Utility
1819
import XCTest
@@ -101,6 +102,83 @@ class GenerateXcodeprojTests: XCTestCase {
101102
XCTAssertTrue(warnings.contains("warning: Target '\(moduleName)' conflicts with required framework filenames, rename this target to avoid conflicts."))
102103
}
103104

105+
func testGenerateXcodeprojWithRootFiles() {
106+
mktmpdir { dstdir in
107+
108+
let packagePath = dstdir.appending(component: "Foo")
109+
let modulePath = packagePath.appending(components: "Sources", "DummyModule")
110+
try makeDirectories(modulePath)
111+
try localFileSystem.writeFileContents(modulePath.appending(component: "dummy.swift"), bytes: "dummy_data")
112+
try localFileSystem.writeFileContents(packagePath.appending(component: "a.txt"), bytes: "dummy_data")
113+
114+
let diagnostics = DiagnosticsEngine()
115+
let graph = loadMockPackageGraph([packagePath.asString: Package(name: "Foo")], root: packagePath.asString, diagnostics: diagnostics, in: localFileSystem)
116+
XCTAssertFalse(diagnostics.hasErrors)
117+
118+
let projectName = "DummyProjectName"
119+
let outpath = Xcodeproj.buildXcodeprojPath(outputDir: dstdir, projectName: projectName)
120+
let project = try Xcodeproj.generate(projectName: projectName, xcodeprojPath: outpath, graph: graph, options: XcodeprojOptions(), diagnostics: diagnostics)
121+
122+
XCTAssertTrue(project.mainGroup.subitems.contains { $0.path == "a.txt" })
123+
}
124+
}
125+
126+
func testGenerateXcodeprojWithNonSourceFilesInSourceDirectories() {
127+
mktmpdir { dstdir in
128+
129+
let packagePath = dstdir.appending(component: "Foo")
130+
let modulePath = packagePath.appending(components: "Sources", "DummyModule")
131+
try makeDirectories(modulePath)
132+
try localFileSystem.writeFileContents(modulePath.appending(component: "dummy.swift"), bytes: "dummy_data")
133+
try localFileSystem.writeFileContents(modulePath.appending(component: "a.txt"), bytes: "dummy_data")
134+
135+
let diagnostics = DiagnosticsEngine()
136+
let graph = loadMockPackageGraph([packagePath.asString: Package(name: "Foo")], root: packagePath.asString, diagnostics: diagnostics, in: localFileSystem)
137+
XCTAssertFalse(diagnostics.hasErrors)
138+
139+
let projectName = "DummyProjectName"
140+
let outpath = Xcodeproj.buildXcodeprojPath(outputDir: dstdir, projectName: projectName)
141+
let project = try Xcodeproj.generate(projectName: projectName, xcodeprojPath: outpath, graph: graph, options: XcodeprojOptions(), diagnostics: diagnostics)
142+
143+
let sources = project.mainGroup.subitems[1] as? Xcode.Group
144+
let dummyModule = sources?.subitems[0] as? Xcode.Group
145+
let aTxt = dummyModule?.subitems[0]
146+
147+
XCTAssertEqual(aTxt?.path, "a.txt")
148+
}
149+
}
150+
151+
func testGenerateXcodeprojWithFilesIgnoredByGit() {
152+
mktmpdir { dstdir in
153+
154+
let packagePath = dstdir.appending(component: "Foo")
155+
let modulePath = packagePath.appending(components: "Sources", "DummyModule")
156+
157+
try makeDirectories(modulePath)
158+
try localFileSystem.writeFileContents(modulePath.appending(component: "dummy.swift"), bytes: "dummy_data")
159+
160+
initGitRepo(packagePath, addFile: false)
161+
// Add a .gitignore
162+
try localFileSystem.writeFileContents(packagePath.appending(component: ".gitignore"), bytes: "ignored_file")
163+
try localFileSystem.writeFileContents(modulePath.appending(component: "ignored_file"), bytes: "dummy_data")
164+
try localFileSystem.writeFileContents(packagePath.appending(component: "ignored_file"), bytes: "dummy_data")
165+
166+
let diagnostics = DiagnosticsEngine()
167+
let graph = loadMockPackageGraph([packagePath.asString: Package(name: "Foo")], root: packagePath.asString, diagnostics: diagnostics, in: localFileSystem)
168+
XCTAssertFalse(diagnostics.hasErrors)
169+
170+
let projectName = "DummyProjectName"
171+
let outpath = Xcodeproj.buildXcodeprojPath(outputDir: dstdir, projectName: projectName)
172+
let project = try Xcodeproj.generate(projectName: projectName, xcodeprojPath: outpath, graph: graph, options: XcodeprojOptions(), diagnostics: diagnostics)
173+
174+
let sources = project.mainGroup.subitems[1] as? Xcode.Group
175+
let dummyModule = sources?.subitems[0] as? Xcode.Group
176+
177+
XCTAssertEqual(dummyModule?.subitems.count, 1)
178+
XCTAssertFalse(project.mainGroup.subitems.contains { $0.path == "ignored_file" })
179+
}
180+
}
181+
104182
static var allTests = [
105183
("testXcodebuildCanParseIt", testXcodebuildCanParseIt),
106184
("testXcconfigOverrideValidatesPath", testXcconfigOverrideValidatesPath),

0 commit comments

Comments
 (0)