Skip to content

Merge tests defined in extensions #1227

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 6 commits into from
May 10, 2024

Conversation

plemarquand
Copy link
Contributor

Merge the XCTests and swift-testing tests defined in extensions into their parent TestItems.

This is done as another pass after the TestScanner visitors have walked the tree.

Fixes #1218

@plemarquand
Copy link
Contributor Author

@ahoppen this is a pretty naive first pass at a solution. It does outline the expected behaviour with some new tests, which should be helpful going forward.

@plemarquand
Copy link
Contributor Author

@swift-ci Please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Hmm, I’m not sure if we can or want to do this in general. The underlying problem is if we have the following

FileA.swift

struct MyTests {
  @Test
  func runTest() {}
}

FileB.swift

private struct MyTests {
  @Test
  func runTest() {}
}

FileC.swift

extension MyTests {
  @Test
  func anotherTest() {}
}

Then MyTests from FileA.swift and FileB.swift are distinct test suites (yes we don’t handle that currently, but we should be able to when we add file names to the test IDs), so we shouldn’t merge them. Figuring out that extension MyTests should be merged with MyTests from FileA.swift requires module-wide name lookup which can’t be done syntactically.

That’s why I decided to consider each extension as a separate node within the tree of test items. That also has the advantage that there is a test item that covers extension MyTests, so that in VS Code you get an indicator in the side gutter to run the MyTests tests. I think after merging them, you won’t get that.

What do you think? Does my line of thinking make sense? How bad is having a separate item for an extension?

@plemarquand
Copy link
Contributor Author

@ahoppen The example you outline is a tricky one for sure. I knew there would be dragons in the details on this one, which is why I called this out as being a naive implementation.

I think from a user experience perspective breaking out extensions in to their own "suites" doesn't match Swift's conceptual model of extensions. If extensions are additions to existing types, then in a test context I'd expect extensions to be an addition to an existing suite.

If it makes sense to a user to run all the tests in an extension as a group then I'd argue that group should be its own well defined suite.

Is wrangling extensions across the workspace a common problem in sourcekit? Extensions kind of break syntactic analysis at a file level.

@plemarquand plemarquand requested a review from ahoppen May 7, 2024 13:27
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

The merging problem truly is harder than it seems at first glance. I left a few ideas for more tests that I think we should cover. It probably means adding more information to the syntactic test scanner about whether a test item is an extension or not, which we probably don’t want to expose as part of the textDocument/tests and workspaces/tests requests.

children: [],
tags: []
)
],
tags: []
),
)
]
Copy link
Member

Choose a reason for hiding this comment

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

And another test case that I think would be worth adding. In this case we should pick the struct as the primary test suite location but I think right now we pick the extension MyTests.

FileA.swift

extension MyTests {
  @Test func twoIsThree() {}
}

FileB.swift

@Suite struct MyTests {
  @Test func oneIsTwo {}
}

I think this means we need to add some information about whether a test is an extension to the syntactic test scanner because otherwise we can’t distinguish the extension from the struct definition during the merge operation. Once we have that, we should really only merge extensions into their defining type. I think that has a few other advantages as well:

  • We will never accidentally merge two private suites from different files.
  • If we have an extension from a type that is syntactically ambiguous (eg. extension of a private suite whose name is used in two files), we could decide to not merge it for now.
  • It can be more performant because we only need to try merging extensions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a good test case to add for sure. I've refactored so now the tests in struct MyTests will appear in the list before those defined in extensions.

TestItem now has an isExtension flag which, while not only necessary to sort/merge things correctly, may also be interesting to tools developers. Thoughts?

@plemarquand
Copy link
Contributor Author

@swift-ci Please test

@plemarquand plemarquand requested a review from ahoppen May 7, 2024 17:39
Merge the XCTests and swift-testing tests defined in extensions into
their parent TestItems.

This is done as another pass after the TestScanner visitors have walked
the tree.

Fixes swiftlang#1218
Sort the list of test items prioritizing those defined in the
originating type definition over those in extensions.
@plemarquand plemarquand force-pushed the merge-extension-tests branch from c4089fb to d8d6cd1 Compare May 7, 2024 17:56
@plemarquand plemarquand requested a review from ahoppen May 8, 2024 18:31
@plemarquand
Copy link
Contributor Author

@swift-ci Please test

@plemarquand
Copy link
Contributor Author

@swift-ci Please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Nice. Let’s 🚢 it

@ahoppen
Copy link
Member

ahoppen commented May 9, 2024

@swift-ci Please test Windows

1 similar comment
@plemarquand
Copy link
Contributor Author

@swift-ci Please test Windows

@plemarquand plemarquand merged commit 6a791fd into swiftlang:main May 10, 2024
3 checks passed
@plemarquand plemarquand deleted the merge-extension-tests branch May 10, 2024 00:51
ahoppen added a commit to ahoppen/sourcekit-lsp that referenced this pull request May 15, 2024
…nsion-tests"

This reverts commit 6a791fd, reversing
changes made to 73cec82.
@ahoppen ahoppen mentioned this pull request May 15, 2024
ahoppen added a commit that referenced this pull request May 16, 2024
ahoppen added a commit to ahoppen/sourcekit-lsp that referenced this pull request Jun 1, 2024
We reverted swiftlang#1227 because it was hitting issues in debug info generation (rdar://128155050), which have been fixed. Re-apply the PR.

This reverts commit ff5db5f.

rdar://128159962
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.

Tests inside extensions are not nested correctly in documentText/tests and workspace/tests responses
2 participants