Skip to content

Commit

Permalink
Test: Add platform helpers
Browse files Browse the repository at this point in the history
Add helpers related to the current platform to assist with conditionally
running test cases.
  • Loading branch information
bkhouri committed Nov 21, 2024
1 parent cb829d8 commit 8b71a84
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Sources/_InternalTestSupport/PlatformHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@


//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics

import Testing

public func isWindows() -> Bool {
#if os(Windows)
return true
#else
return false
#endif
}

public func isLinux() -> Bool {
#if os(Linux)
return true
#else
return false
#endif
}

public func isMacOS() -> Bool {
#if os(macOS)
return true
#else
return false
#endif
}

public func isRealSigningIdentityTestEnabled() -> Bool {
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
return true
#else
return false
#endif
}

public func isEnvironmentVariableSet(_ variableName: EnvironmentKey) -> Bool {
guard let value = Environment.current[variableName] else { return false }
return !value.isEmpty
}
35 changes: 35 additions & 0 deletions Tests/_InternalTestSupportTests/PlatformHelpersTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import _InternalTestSupport
import Basics
import Testing

struct testisEnvironmentVariableSet {
@Test(
arguments: [
(name: "", expected: false),
(name: "DOES_NOT_EXIST", expected: false),
(name: "HOME", expected: true)
]
)
func testisEnvironmentVariableSetReturnsExpectedValue(name: String, expected: Bool) {
// GIVEN we have an environment variable name
let variableName = EnvironmentKey(name)

// WHEN we call isEnvironmentVariableSet(varaiblename)
let actual = isEnvironmentVariableSet(variableName)

// THEN we expect to return true
#expect(actual == expected, "Actual is not as expected")
}
}

0 comments on commit 8b71a84

Please sign in to comment.