Skip to content

New constructor for envvar-based condition trait creation #1265

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions Sources/Testing/Traits/ConditionTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ extension Trait where Self == ConditionTrait {
Self(kind: .conditional(condition), comments: Array(comment), sourceLocation: sourceLocation)
}

/// Constructs a condition trait that disables a test if an environment
/// variable is not present.
///
/// - Parameters:
/// - comment: An optional comment that describes this trait.
/// - sourceLocation: The source location of the trait.
/// - environmentName: The name of an environment variable. If it is found
/// the current environment, the trait allows the test to run.
/// Otherwise, the testing library skips the test. The value of
/// the environment variable is not inspected.
///
/// - Returns: An instance of ``ConditionTrait`` that checks for the provided
/// environment variable.
@_spi(Experimental)
public static func enabled(
ifEnvironmentPresent environmentName: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

A single environment variable is not "an environment" so we need to bikeshed this parameter label and name.

_ comment: Comment? = nil,
sourceLocation: SourceLocation = #_sourceLocation
) -> Self {
Self(
kind: .conditional {
Environment.variable(named: environmentName) != nil
}, comments: Array(comment), sourceLocation: sourceLocation
)
}

/// Constructs a condition trait that disables a test unconditionally.
///
/// - Parameters:
Expand Down Expand Up @@ -207,4 +233,30 @@ extension Trait where Self == ConditionTrait {
) -> Self {
Self(kind: .conditional { !(try await condition()) }, comments: Array(comment), sourceLocation: sourceLocation)
}

/// Constructs a condition trait that disables a test if an environment
/// variable is present.
///
/// - Parameters:
/// - comment: An optional comment that describes this trait.
/// - sourceLocation: The source location of the trait.
/// - environmentName: The name of an environment variable. If it is not
/// found the current environment, the trait allows the test to run.
/// Otherwise, the testing library skips the test. The value of
/// the environment variable is not inspected.
///
/// - Returns: An instance of ``ConditionTrait`` that checks for the provided
/// environment variable.
@_spi(Experimental)
public static func disabled(
ifEnvironmentPresent environmentName: String,
_ comment: Comment? = nil,
sourceLocation: SourceLocation = #_sourceLocation
) -> Self {
Self(
kind: .conditional {
Environment.variable(named: environmentName) == nil
}, comments: Array(comment), sourceLocation: sourceLocation
)
}
}
45 changes: 42 additions & 3 deletions Tests/TestingTests/Traits/ConditionTraitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

@testable import Testing
@_spi(Experimental) @testable import Testing

@Suite("Condition Trait Tests", .tags(.traitRelated))
struct ConditionTraitTests {
Expand Down Expand Up @@ -37,15 +37,27 @@ struct ConditionTraitTests {
.disabled(if: false)
)
func disabledTraitIf() throws {}


@Test(
Copy link
Contributor

Choose a reason for hiding this comment

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

These should probably be marked .hidden. We have infrastructure for testing fixture tests and verifying they are (or are not) run.

".enabled if a certain env var exists",
.enabled(ifEnvironmentPresent: "TEST_ENV_VAR")
)
func enabledEnvironmentPresentIf() throws {}

@Test(
".disabled if a certain env var exists",
.disabled(ifEnvironmentPresent: "TEST_ENV_VAR")
)
func disabledEnvironmentPresentIf() throws {}

@Test
func evaluateCondition() async throws {
let trueUnconditional = ConditionTrait(kind: .unconditional(true), comments: [], sourceLocation: #_sourceLocation)
let falseUnconditional = ConditionTrait.disabled()
let enabledTrue = ConditionTrait.enabled(if: true)
let enabledFalse = ConditionTrait.enabled(if: false)
var result: Bool

result = try await trueUnconditional.evaluate()
#expect(result)
result = try await falseUnconditional.evaluate()
Expand All @@ -55,4 +67,31 @@ struct ConditionTraitTests {
result = try await enabledFalse.evaluate()
#expect(!result)
}

// TODO: What do we wanna do about envvar thread safety? This won't be safe to
Copy link
Contributor

Choose a reason for hiding this comment

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

See our existing tests that modify environment variables.

// run in parallel alongside any other env var based tests because it ends up
// calling setenv.
@Test
func evaluateConditionEnvironmentVariable() async throws {
let enabledEnvironment = ConditionTrait.enabled(ifEnvironmentPresent: "TEST_ENV_VAR")
let disabledEnvironment = ConditionTrait.disabled(ifEnvironmentPresent: "TEST_ENV_VAR")
var result: Bool

result = try await enabledEnvironment.evaluate()
#expect(!result)
result = try await disabledEnvironment.evaluate()
#expect(result)

try #require(Environment.setVariable("1", named: "TEST_ENV_VAR"))
result = try await enabledEnvironment.evaluate()
#expect(result)
result = try await disabledEnvironment.evaluate()
#expect(!result)

try #require(Environment.setVariable("0", named: "TEST_ENV_VAR"))
result = try await enabledEnvironment.evaluate()
#expect(result, "Actual value of the environment variable shouldn't matter")
result = try await disabledEnvironment.evaluate()
#expect(!result, "Actual value of the environment variable shouldn't matter")
}
}