-
Notifications
You must be signed in to change notification settings - Fork 120
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -37,15 +37,27 @@ struct ConditionTraitTests { | |
.disabled(if: false) | ||
) | ||
func disabledTraitIf() throws {} | ||
|
||
|
||
@Test( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should probably be marked |
||
".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() | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
} | ||
} |
There was a problem hiding this comment.
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.