Skip to content
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

Add the ability for a trait to run code before and after a test #86

Merged
merged 1 commit into from
Feb 15, 2024

Conversation

dennisweissmann
Copy link
Contributor

@dennisweissmann dennisweissmann commented Oct 28, 2023

Add the ability for a trait to run code before and after a test.

Motivation:

As laid out in the vision document, we want traits to become extensible, and be able to invoke code before and after a test's body is invoked.

This PR provides the foundation that will not just give test authors a lot more power and flexibility in what they can achieve with traits, but will allow us (in a future PR) to re-implement e.g. the timeout behavior to be less special-cased.

Modifications:

I added a new protocol CustomExecutionTrait which inherits from Trait, that has a single additional requirement: an execute function that provides a function that eventually executes the test body in its argument so that trait authors can invoke it in whatever way they need, ie. have the ability to execute code before and after they invoke that function.

An example of such a trait is

struct DoSomethingBeforeAndAfterTrait: CustomExecutionTrait & TestTrait {

  func execute(_ function: () async throws -> Void, for test: Test) async throws {
    something()
    try await function()
    somethingElse()
  }
}

(or see CustomTraitTest).

Result:

Test authors now have a much more flexible system to write traits, allowing them to execute custom code before and after a test body is run.

@dennisweissmann
Copy link
Contributor Author

@swift-ci please test

@stmontgomery stmontgomery added the enhancement New feature or request label Oct 31, 2023
Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

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

Most of my comments are just about documentation and naming. I think the overall shape of the PR is good.

@dennisweissmann
Copy link
Contributor Author

@swift-ci please test

/// `test` represents a test function, this function is the body of that
/// test function (including all cases if it is parameterized.)
/// - test: The test under which `function` is being performed.
/// - testCase: The testCase under which `function` is being performed.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// - testCase: The testCase under which `function` is being performed.
/// - testCase: The test case under which `function` is being performed.

@FranzBusch
Copy link
Member

This seems really cool. I am wondering if we can also provide something that can run a test in a withXXX style method. We see those more and more in structured concurrency and they don't fit into the before + after pattern.

@stmontgomery
Copy link
Contributor

I am wondering if we can also provide something that can run a test in a withXXX style method. We see those more and more in structured concurrency and they don't fit into the before + after pattern.

This new API should support that use case, if I understand you correctly. Or if you don't think it will, can you elaborate?

@dennisweissmann
Copy link
Contributor Author

@swift-ci please test

@@ -81,6 +81,40 @@ public protocol SuiteTrait: Trait {
var isRecursive: Bool { get }
}

/// A protocol extending ``Trait`` that offers an additional customization point
Copy link
Contributor

Choose a reason for hiding this comment

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

I want to do an editorial pass on this type's documentation after we promote it to API, but it's fine at this point.

@dennisweissmann
Copy link
Contributor Author

@swift-ci please test

@dennisweissmann dennisweissmann merged commit a0cb8d5 into main Feb 15, 2024
2 checks passed
@dennisweissmann dennisweissmann deleted the dweissmann/114477003 branch February 15, 2024 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants