1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
@testable @_spi(Experimental) import Testing
private import _TestingInternals
@Suite("Environment Tests", .serialized)
struct EnvironmentTests {
var name = "SWT_ENVIRONMENT_VARIABLE_FOR_TESTING"
@Test("Get whole environment block")
func getWholeEnvironment() throws {
let value = "\(UInt64.random(in: 0 ... .max))"
try #require(Environment.setVariable(value, named: name))
defer {
Environment.setVariable(nil, named: name)
}
let env = Environment.get()
#expect(env[name] == value)
}
@Test("Read environment variable")
func readEnvironmentVariable() throws {
let value = "\(UInt64.random(in: 0 ... .max))"
try #require(nil == Environment.variable(named: name))
defer {
Environment.setVariable(nil, named: name)
}
try #require(Environment.setVariable(value, named: name))
#expect(Environment.variable(named: name) == value)
}
@Test("Read true environment flags",
arguments: [
"1", String(describing: UInt64.max), String(describing: Int64.min),
String(describing: UInt64.random(in: 1 ... .max)), String(describing: Int64.random(in: .min ..< 0)),
"YES", "yes", "yEs", "TRUE", "true", "tRuE",
]
)
func readTrueFlag(value: String) throws {
try #require(nil == Environment.variable(named: name))
defer {
Environment.setVariable(nil, named: name)
}
try #require(Environment.setVariable(value, named: name))
#expect(Environment.variable(named: name) == value)
#expect(Environment.flag(named: name) == true)
}
@Test("Read false environment flags",
arguments: [
"0", "", " ", "\t",
"NO", "no", "nO", "FALSE", "false", "fAlSe",
"alphabetical", "ümlaut", "😀",
]
)
func readFalseFlag(value: String) throws {
try #require(nil == Environment.variable(named: name))
defer {
Environment.setVariable(nil, named: name)
}
try #require(Environment.setVariable(value, named: name))
#expect(Environment.variable(named: name) == value)
#expect(Environment.flag(named: name) == false)
}
}
// MARK: - Fixtures
extension Environment {
/// Set the environment variable with the specified name.
///
/// - Parameters:
/// - value: The new value for the specified environment variable. Pass
/// `nil` to remove the variable from the current process' environment.
/// - name: The name of the environment variable.
///
/// - Returns: Whether or not the environment variable was successfully set.
@discardableResult
static func setVariable(_ value: String?, named name: String) -> Bool {
#if SWT_NO_ENVIRONMENT_VARIABLES
simulatedEnvironment.withLock { environment in
environment[name] = value
}
return true
#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
if let value {
return 0 == setenv(name, value, 1)
}
return 0 == unsetenv(name)
#elseif os(Windows)
name.withCString(encodedAs: UTF16.self) { name in
if let value {
return value.withCString(encodedAs: UTF16.self) { value in
SetEnvironmentVariableW(name, value)
}
}
return SetEnvironmentVariableW(name, nil)
}
#else
#warning("Platform-specific implementation missing: environment variables unavailable")
return false
#endif
}
}
|