Skip to content

Commit d775615

Browse files
author
David Ungar
authored
Merge pull request #679 from davidungar/Clean-up-incremental-tests-file
[NFC] Reorganize the tests in IncrementalCompilationTests.swift
2 parents 30af8c4 + 409277c commit d775615

File tree

3 files changed

+639
-589
lines changed

3 files changed

+639
-589
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
//===---------- CrossModuleIncrementalBuildTests.swift - Swift Testing ----===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
import XCTest
13+
import TSCBasic
14+
15+
@_spi(Testing) import SwiftDriver
16+
import SwiftOptions
17+
import TestUtilities
18+
19+
class CrossModuleIncrementalBuildTests: XCTestCase {
20+
func makeOutputFileMap(
21+
in workingDirectory: AbsolutePath,
22+
for files: [AbsolutePath],
23+
outputTransform transform: (String) -> String = { $0 }
24+
) -> String {
25+
"""
26+
{
27+
"": {
28+
"swift-dependencies": "\(workingDirectory.appending(component: "module.swiftdeps"))"
29+
}
30+
""".appending(files.map { file in
31+
"""
32+
,
33+
"\(file)": {
34+
"dependencies": "\(transform(file.basenameWithoutExt) + ".d")",
35+
"object": "\(transform(file.pathString) + ".o")",
36+
"swiftmodule": "\(transform(file.basenameWithoutExt) + "~partial.swiftmodule")",
37+
"swift-dependencies": "\(transform(file.basenameWithoutExt) + ".swiftdeps")"
38+
}
39+
"""
40+
}.joined(separator: "\n").appending("\n}"))
41+
}
42+
43+
func testChangingOutputFileMap() throws {
44+
try withTemporaryDirectory { path in
45+
try localFileSystem.changeCurrentWorkingDirectory(to: path)
46+
let magic = path.appending(component: "magic.swift")
47+
try localFileSystem.writeFileContents(magic) {
48+
$0 <<< "public func castASpell() {}"
49+
}
50+
51+
let ofm = path.appending(component: "ofm.json")
52+
try localFileSystem.writeFileContents(ofm) {
53+
$0 <<< self.makeOutputFileMap(in: path, for: [ magic ]) {
54+
$0 + "-some_suffix"
55+
}
56+
}
57+
58+
do {
59+
var driver = try Driver(args: [
60+
"swiftc",
61+
"-incremental",
62+
"-emit-module",
63+
"-output-file-map", ofm.pathString,
64+
"-module-name", "MagicKit",
65+
"-working-directory", path.pathString,
66+
"-c",
67+
magic.pathString,
68+
])
69+
let jobs = try driver.planBuild()
70+
try driver.run(jobs: jobs)
71+
}
72+
73+
try localFileSystem.writeFileContents(ofm) {
74+
$0 <<< self.makeOutputFileMap(in: path, for: [ magic ]) {
75+
$0 + "-some_other_suffix"
76+
}
77+
}
78+
79+
do {
80+
var driver = try Driver(args: [
81+
"swiftc",
82+
"-incremental",
83+
"-emit-module",
84+
"-output-file-map", ofm.pathString,
85+
"-module-name", "MagicKit",
86+
"-working-directory", path.pathString,
87+
"-c",
88+
magic.pathString,
89+
])
90+
let jobs = try driver.planBuild()
91+
try driver.run(jobs: jobs)
92+
}
93+
}
94+
}
95+
96+
func testEmbeddedModuleDependencies() throws {
97+
try withTemporaryDirectory { path in
98+
try localFileSystem.changeCurrentWorkingDirectory(to: path)
99+
do {
100+
let magic = path.appending(component: "magic.swift")
101+
try localFileSystem.writeFileContents(magic) {
102+
$0 <<< "public func castASpell() {}"
103+
}
104+
105+
let ofm = path.appending(component: "ofm.json")
106+
try localFileSystem.writeFileContents(ofm) {
107+
$0 <<< self.makeOutputFileMap(in: path, for: [ magic ])
108+
}
109+
110+
var driver = try Driver(args: [
111+
"swiftc",
112+
"-incremental",
113+
"-emit-module",
114+
"-output-file-map", ofm.pathString,
115+
"-module-name", "MagicKit",
116+
"-working-directory", path.pathString,
117+
"-c",
118+
magic.pathString,
119+
])
120+
let jobs = try driver.planBuild()
121+
try driver.run(jobs: jobs)
122+
}
123+
124+
let main = path.appending(component: "main.swift")
125+
try localFileSystem.writeFileContents(main) {
126+
$0 <<< "import MagicKit\n"
127+
$0 <<< "castASpell()"
128+
}
129+
130+
let ofm = path.appending(component: "ofm2.json")
131+
try localFileSystem.writeFileContents(ofm) {
132+
$0 <<< self.makeOutputFileMap(in: path, for: [ main ])
133+
}
134+
135+
var driver = try Driver(args: [
136+
"swiftc",
137+
"-incremental",
138+
"-emit-module",
139+
"-output-file-map", ofm.pathString,
140+
"-module-name", "theModule",
141+
"-I", path.pathString,
142+
"-working-directory", path.pathString,
143+
"-c",
144+
main.pathString,
145+
])
146+
147+
let jobs = try driver.planBuild()
148+
try driver.run(jobs: jobs)
149+
150+
let sourcePath = path.appending(component: "main.swiftdeps")
151+
let data = try localFileSystem.readFileContents(sourcePath)
152+
let graph = try XCTUnwrap(SourceFileDependencyGraph(data: data,
153+
from: DependencySource(VirtualPath.absolute(sourcePath).intern())!,
154+
fromSwiftModule: false))
155+
XCTAssertEqual(graph.majorVersion, 1)
156+
XCTAssertEqual(graph.minorVersion, 0)
157+
graph.verify()
158+
159+
var foundNode = false
160+
let swiftmodulePath = ExternalDependency(fileName: path.appending(component: "MagicKit.swiftmodule").pathString)
161+
graph.forEachNode { node in
162+
if case .externalDepend(swiftmodulePath) = node.key.designator {
163+
XCTAssertFalse(foundNode)
164+
foundNode = true
165+
XCTAssertEqual(node.key.aspect, .interface)
166+
XCTAssertTrue(node.defsIDependUpon.isEmpty)
167+
XCTAssertFalse(node.isProvides)
168+
}
169+
}
170+
XCTAssertTrue(foundNode)
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)