Skip to content

Add a new test project type that uses a custom build server #2035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Mar 5, 2025

This allows us to more easily test behavior for build servers that have different behavior than SwiftPM and compile commands without having to implement the build server in Python.

@ahoppen ahoppen requested review from bnbarham and hamishknight March 5, 2025 00:32
@ahoppen
Copy link
Member Author

ahoppen commented Mar 5, 2025

Not to self: If this gets merged before #2032, then we can update that PR to use the following test case, using the new build server test infrastructure.

Updated test

func testUseResponseFileIfTooManyArguments() async throws {
  // The build system returns too many arguments to fit them into a command line invocation, so we need to use a
  // response file to invoke the indexer.

  final class BuildServer: CustomBuildServer {
    private let projectRoot: URL
    private var testFileURL: URL { projectRoot.appendingPathComponent("Test File.swift") }

    init(projectRoot: URL, connectionToSourceKitLSP: any LanguageServerProtocol.Connection) {
      self.projectRoot = projectRoot
    }

    func initializeBuildRequest(_ request: InitializeBuildRequest) async throws -> InitializeBuildResponse {
      return initializationResponse(
        initializeData: SourceKitInitializeBuildResponseData(
          indexDatabasePath: try projectRoot.appendingPathComponent("index-db").filePath,
          indexStorePath: try projectRoot.appendingPathComponent("index-store").filePath,
          prepareProvider: true,
          sourceKitOptionsProvider: true
        )
      )
    }

    func buildTargetSourcesRequest(_ request: BuildTargetSourcesRequest) async throws -> BuildTargetSourcesResponse {
      return BuildTargetSourcesResponse(items: [
        SourcesItem(
          target: .dummy,
          sources: [
            SourceItem(uri: URI(testFileURL), kind: .file, generated: false)
          ]
        )
      ])
    }

    func textDocumentSourceKitOptionsRequest(
      _ request: TextDocumentSourceKitOptionsRequest
    ) async throws -> TextDocumentSourceKitOptionsResponse? {
      var arguments =
        [try testFileURL.filePath] + (0..<50_000).map { "-DTHIS_IS_AN_OPTION_THAT_CONTAINS_MANY_BYTES_\($0)" }
      if let defaultSDKPath {
        arguments += ["-sdk", defaultSDKPath]
      }
      return TextDocumentSourceKitOptionsResponse(
        compilerArguments: arguments
      )
    }

  }

  let project = try await CustomBuildServerTestProject(
    files: [
      // File name contains a space to ensure we escape it in the response file.
      "Test File.swift": """
      func 1️⃣myTestFunc() {}
      """
    ],
    buildServer: BuildServer.self,
    enableBackgroundIndexing: true
  )
  try await project.testClient.send(PollIndexRequest())

  let symbols = try await project.testClient.send(WorkspaceSymbolsRequest(query: "myTestFunc"))
  XCTAssertEqual(
    symbols,
    [
      .symbolInformation(
        SymbolInformation(
          name: "myTestFunc()",
          kind: .function,
          location: try project.location(from: "1️⃣", to: "1️⃣", in: "Test File.swift")
        )
      )
    ]
  )
}

@ahoppen
Copy link
Member Author

ahoppen commented Mar 5, 2025

@swift-ci Please test

This allows us to more easily test behavior for build servers that have different behavior than SwiftPM and compile commands without having to implement the build server in Python.
@ahoppen ahoppen force-pushed the build-server-tests branch from e2e82f1 to 5c60d1d Compare March 6, 2025 07:54
@ahoppen
Copy link
Member Author

ahoppen commented Mar 6, 2025

@swift-ci Please test

@@ -2,53 +2,61 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2025, right? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah… Probably from some copy-pasting

@ahoppen
Copy link
Member Author

ahoppen commented Mar 6, 2025

@swift-ci Please test Windows

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented Mar 6, 2025

@swift-ci Please test Windows

@ahoppen ahoppen merged commit a7b7f21 into swiftlang:main Mar 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants