Skip to content

Prepend module name to TestItem IDs #1530

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 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sources/SKCore/BuildSystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ extension BuildSystemManager {
.first
}

/// Returns the target's module name as parsed from the `ConfiguredTarget`'s compiler arguments.
public func moduleName(for document: DocumentURI, in target: ConfiguredTarget) async -> String? {
guard let buildSettings = await buildSettings(for: document, in: target, language: .swift),
let moduleNameFlagIndex = buildSettings.compilerArguments.firstIndex(of: "-module-name")
else {
return nil
}

let moduleNameIndex = buildSettings.compilerArguments.index(after: moduleNameFlagIndex)
return buildSettings.compilerArguments[moduleNameIndex]
}

/// Returns the build settings for `document` from `buildSystem`.
///
/// Implementation detail of `buildSettings(for:language:)`.
Expand Down
39 changes: 38 additions & 1 deletion Sources/SourceKitLSP/TestDiscovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ extension SourceKitLSPServer {

func workspaceTests(_ req: WorkspaceTestsRequest) async throws -> [TestItem] {
return await self.workspaces
.concurrentMap { await self.tests(in: $0) }
.concurrentMap { await self.tests(in: $0).prefixTestsWithModuleName(workspace: $0) }
.flatMap { $0 }
.sorted { $0.testItem.location < $1.testItem.location }
.mergingTestsInExtensions()
Expand All @@ -272,6 +272,7 @@ extension SourceKitLSPServer {
languageService: LanguageService
) async throws -> [TestItem] {
return try await documentTestsWithoutMergingExtensions(req, workspace: workspace, languageService: languageService)
.prefixTestsWithModuleName(workspace: workspace)
.mergingTestsInExtensions()
}

Expand Down Expand Up @@ -476,6 +477,42 @@ fileprivate extension Array<AnnotatedTestItem> {
}
return result
}

func prefixTestsWithModuleName(workspace: Workspace) async -> Self {
return await self.asyncMap({
// If the module name can't be determined we return the test item without a prefixed id.
guard let moduleName = await self.moduleName(from: workspace, for: $0.testItem.location.uri) else {
return $0
}
var newTest = $0.testItem
newTest.id = "\(moduleName).\(newTest.id)"
newTest.children = await prefixTestsWithModuleName(workspace: workspace, newTest.children)
return AnnotatedTestItem(testItem: newTest, isExtension: $0.isExtension)
})
}

private func prefixTestsWithModuleName(workspace: Workspace, _ tests: [TestItem]) async -> [TestItem] {
return await tests.asyncMap({
guard let moduleName = await self.moduleName(from: workspace, for: $0.location.uri) else {
return $0
}

var newTest = $0
newTest.id = "\(moduleName).\(newTest.id)"
newTest.children = await prefixTestsWithModuleName(workspace: workspace, newTest.children)
return newTest
})
}

private func moduleName(from workspace: Workspace, for uri: DocumentURI) async -> String? {
guard let configuredTarget = await workspace.buildSystemManager.canonicalConfiguredTarget(for: uri) else {
return nil
}
// If for whatever reason we can't get a module name from the build system, fall back
// to using the targetID as this would be used when there is no command line arguments
// to define the module name as something other than the targetID.
return await workspace.buildSystemManager.moduleName(for: uri, in: configuredTarget) ?? configuredTarget.targetID
}
}

extension SwiftLanguageService {
Expand Down
Loading