File: PrepareForIndexTests.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (221 lines) | stat: -rw-r--r-- 9,763 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2015-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Build
import Foundation
@testable import Commands
@testable import CoreCommands
import LLBuildManifest
import _InternalTestSupport
import TSCBasic
import XCTest
import class Basics.ObservabilitySystem
@_spi(DontAdoptOutsideOfSwiftPMExposedForBenchmarksAndTestsOnly)
import func PackageGraph.loadModulesGraph
import class PackageModel.Manifest
import struct PackageModel.TargetDescription

class PrepareForIndexTests: XCTestCase {
    func testPrepare() async throws {
        let (graph, fs, scope) = try macrosPackageGraph()

        let plan = try await BuildPlan(
            destinationBuildParameters: mockBuildParameters(destination: .target, prepareForIndexing: .on),
            toolsBuildParameters: mockBuildParameters(destination: .host, prepareForIndexing: .off),
            graph: graph,
            fileSystem: fs,
            observabilityScope: scope
        )

        let builder = LLBuildManifestBuilder(plan, fileSystem: fs, observabilityScope: scope)
        let manifest = try builder.generateManifest(at: "/manifest")

        // Make sure we're building the swift modules
        let outputs = manifest.commands.flatMap(\.value.tool.outputs).map(\.name)
        XCTAssertTrue(outputs.contains(where: { $0.hasSuffix(".swiftmodule") }))

        // Ensure swiftmodules built with correct arguments
        let coreCommands = manifest.commands.values.filter {
            $0.tool.outputs.contains(where: {
                $0.name.hasSuffix("debug/Core.build/Core.swiftmodule")
            })
        }
        XCTAssertEqual(coreCommands.count, 1)
        let coreSwiftc = try XCTUnwrap(coreCommands.first?.tool as? SwiftCompilerTool)
        XCTAssertTrue(coreSwiftc.otherArguments.contains("-experimental-skip-all-function-bodies"))

        // Ensure tools are built normally
        let toolCommands = manifest.commands.values.filter {
            $0.tool.outputs.contains(where: {
                $0.name.hasSuffix("debug/Modules-tool/SwiftSyntax.swiftmodule")
            })
        }
        XCTAssertEqual(toolCommands.count, 1)
        let toolSwiftc = try XCTUnwrap(toolCommands.first?.tool as? SwiftCompilerTool)
        XCTAssertFalse(toolSwiftc.otherArguments.contains("-experimental-skip-all-function-bodies"))
        XCTAssertTrue(toolSwiftc.outputs.contains(where: {
            $0.name.hasSuffix(".swift.o")
        }))

        // Make sure only object files for tools are built
        XCTAssertTrue(
            outputs.filter { $0.hasSuffix(".o") }.allSatisfy { $0.contains("-tool.build/") },
            "outputs:\n\t\(outputs.filter { $0.hasSuffix(".o") }.joined(separator: "\n\t"))"
        )
    }

    func testCModuleTarget() async throws {
        let (graph, fs, scope) = try trivialPackageGraph()

        let plan = try await BuildPlan(
            destinationBuildParameters: mockBuildParameters(destination: .target, prepareForIndexing: .on),
            toolsBuildParameters: mockBuildParameters(destination: .host, prepareForIndexing: .off),
            graph: graph,
            fileSystem: fs,
            observabilityScope: scope
        )
        let builder = LLBuildManifestBuilder(plan, fileSystem: fs, observabilityScope: scope)
        let manifest = try builder.generateManifest(at: "/manifest")

        // Ensure our C module is here.
        let lib = try XCTUnwrap(graph.module(for: "lib"))
        let name = lib.getLLBuildTargetName(buildParameters: plan.destinationBuildParameters)
        XCTAssertTrue(manifest.targets.keys.contains(name))
    }

    // enable-testing requires the non-exportable-decls, make sure they aren't skipped.
    func testEnableTesting() async throws {
        let fs = InMemoryFileSystem(
            emptyFiles:
            "/Pkg/Sources/lib/lib.swift",
            "/Pkg/Tests/test/TestCase.swift"
        )

        let observability = ObservabilitySystem.makeForTesting()
        let scope = observability.topScope

        let graph = try loadModulesGraph(
            fileSystem: fs,
            manifests: [
                Manifest.createRootManifest(
                    displayName: "Pkg",
                    path: "/Pkg",
                    targets: [
                        TargetDescription(name: "lib", dependencies: []),
                        TargetDescription(name: "test", dependencies: ["lib"], type: .test),
                    ]
                ),
            ],
            observabilityScope: scope
        )
        XCTAssertNoDiagnostics(observability.diagnostics)

        // Under debug, enable-testing is turned on by default. Make sure the flag is not added.
        let debugPlan = try await BuildPlan(
            destinationBuildParameters: mockBuildParameters(destination: .target, config: .debug, prepareForIndexing: .on),
            toolsBuildParameters: mockBuildParameters(destination: .host, prepareForIndexing: .off),
            graph: graph,
            fileSystem: fs,
            observabilityScope: observability.topScope
        )
        let debugBuilder = LLBuildManifestBuilder(debugPlan, fileSystem: fs, observabilityScope: scope)
        let debugManifest = try debugBuilder.generateManifest(at: "/manifest")

        XCTAssertNil(debugManifest.commands.values.first(where: {
            guard let swiftCommand = $0.tool as? SwiftCompilerTool,
                swiftCommand.outputs.contains(where: { $0.name.hasSuffix("/lib.swiftmodule")})
            else {
                return false
            }
            return swiftCommand.otherArguments.contains("-experimental-skip-non-exportable-decls")
                && !swiftCommand.otherArguments.contains("-enable-testing")
        }))

        // Under release, enable-testing is turned off by default so we should see our flag
        let releasePlan = try await BuildPlan(
            destinationBuildParameters: mockBuildParameters(destination: .target, config: .release, prepareForIndexing: .on),
            toolsBuildParameters: mockBuildParameters(destination: .host, prepareForIndexing: .off),
            graph: graph,
            fileSystem: fs,
            observabilityScope: observability.topScope
        )
        let releaseBuilder = LLBuildManifestBuilder(releasePlan, fileSystem: fs, observabilityScope: scope)
        let releaseManifest = try releaseBuilder.generateManifest(at: "/manifest")

        XCTAssertEqual(releaseManifest.commands.values.filter({
            guard let swiftCommand = $0.tool as? SwiftCompilerTool,
                swiftCommand.outputs.contains(where: { $0.name.hasSuffix("/lib.swiftmodule")})
            else {
                return false
            }
            return swiftCommand.otherArguments.contains("-experimental-skip-non-exportable-decls")
                && !swiftCommand.otherArguments.contains("-enable-testing")
        }).count, 1)
    }

    func testPrepareNoLazy() async throws {
        let (graph, fs, scope) = try macrosPackageGraph()

        let plan = try await BuildPlan(
            destinationBuildParameters: mockBuildParameters(destination: .target, prepareForIndexing: .noLazy),
            toolsBuildParameters: mockBuildParameters(destination: .host, prepareForIndexing: .off),
            graph: graph,
            fileSystem: fs,
            observabilityScope: scope
        )

        let builder = LLBuildManifestBuilder(plan, fileSystem: fs, observabilityScope: scope)
        let manifest = try builder.generateManifest(at: "/manifest")

        // Ensure swiftmodules built with correct arguments
        let coreCommands = manifest.commands.values.filter {
            $0.tool.outputs.contains(where: {
                $0.name.hasSuffix("debug/Core.build/Core.swiftmodule")
            })
        }
        XCTAssertEqual(coreCommands.count, 1)
        let coreSwiftc = try XCTUnwrap(coreCommands.first?.tool as? SwiftCompilerTool)
        XCTAssertFalse(coreSwiftc.otherArguments.contains("-experimental-lazy-typecheck"))
        XCTAssertTrue(coreSwiftc.otherArguments.contains("-experimental-allow-module-with-compiler-errors"))
    }

    func testToolsDontPrepare() throws {
        let options = try GlobalOptions.parse(["--experimental-prepare-for-indexing"])
        let state = try SwiftCommandState(
            outputStream: stderrStream,
            options: options,
            toolWorkspaceConfiguration: .init(shouldInstallSignalHandlers: false),
            workspaceDelegateProvider: {
                CommandWorkspaceDelegate(
                    observabilityScope: $0,
                    outputHandler: $1,
                    progressHandler: $2,
                    inputHandler: $3
                )
            },
            workspaceLoaderProvider: {
                XcodeWorkspaceLoader(
                    fileSystem: $0,
                    observabilityScope: $1
                )
            },
            hostTriple: .arm64Linux,
            fileSystem: localFileSystem,
            environment: .current
        )

        XCTAssertEqual(try state.productsBuildParameters.prepareForIndexing, .on)
        // Tools builds should never do prepare for indexing since they're needed
        // for the prepare builds.
        XCTAssertEqual(try state.toolsBuildParameters.prepareForIndexing, .off)
    }
}