-
Notifications
You must be signed in to change notification settings - Fork 90
Add an Environment type to represent environment variables #298
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2025 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 Foundation | ||
|
||
@TaskLocal fileprivate var processEnvironment = ProcessInfo.processInfo.environment | ||
|
||
/// Binds the internal defaults to the specified `environment` for the duration of the synchronous `operation`. | ||
/// - parameter clean: `true` to start with a clean environment, `false` to merge the input environment over the existing process environment. | ||
/// - note: This is implemented via task-local values. | ||
@_spi(Testing) public func withEnvironment<R>(_ environment: [String: String], clean: Bool = false, operation: () throws -> R) rethrows -> R { | ||
try $processEnvironment.withValue(clean ? environment : processEnvironment.addingContents(of: environment), operation: operation) | ||
} | ||
|
||
/// Binds the internal defaults to the specified `environment` for the duration of the asynchronous `operation`. | ||
/// - parameter clean: `true` to start with a clean environment, `false` to merge the input environment over the existing process environment. | ||
/// - note: This is implemented via task-local values. | ||
@_spi(Testing) public func withEnvironment<R>(_ environment: [String: String], clean: Bool = false, operation: () async throws -> R) async rethrows -> R { | ||
try await $processEnvironment.withValue(clean ? environment : processEnvironment.addingContents(of: environment), operation: operation) | ||
} | ||
|
||
/// Gets the value of the named variable from the process' environment. | ||
/// - parameter name: The name of the environment variable. | ||
/// - returns: The value of the variable as a `String`, or `nil` if it is not defined in the environment. | ||
public func getEnvironmentVariable(_ name: String) -> String? { | ||
processEnvironment[name] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2025 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A key used to access values in an ``Environment``. | ||
/// | ||
/// This type respects the compiled platform's case sensitivity requirements. | ||
public struct EnvironmentKey { | ||
public var rawValue: String | ||
|
||
package init(_ rawValue: String) { | ||
self.rawValue = rawValue | ||
} | ||
} | ||
|
||
extension EnvironmentKey { | ||
package static let path: Self = "PATH" | ||
} | ||
|
||
extension EnvironmentKey: CodingKeyRepresentable {} | ||
|
||
extension EnvironmentKey: Comparable { | ||
public static func < (lhs: Self, rhs: Self) -> Bool { | ||
// Even on windows use a stable sort order. | ||
lhs.rawValue < rhs.rawValue | ||
} | ||
} | ||
|
||
extension EnvironmentKey: CustomStringConvertible { | ||
public var description: String { self.rawValue } | ||
} | ||
|
||
extension EnvironmentKey: Encodable { | ||
public func encode(to encoder: any Swift.Encoder) throws { | ||
try self.rawValue.encode(to: encoder) | ||
} | ||
} | ||
|
||
extension EnvironmentKey: Equatable { | ||
public static func == (_ lhs: Self, _ rhs: Self) -> Bool { | ||
#if os(Windows) | ||
lhs.rawValue.lowercased() == rhs.rawValue.lowercased() | ||
#else | ||
lhs.rawValue == rhs.rawValue | ||
#endif | ||
} | ||
} | ||
|
||
extension EnvironmentKey: ExpressibleByStringLiteral { | ||
public init(stringLiteral rawValue: String) { | ||
self.init(rawValue) | ||
} | ||
} | ||
|
||
extension EnvironmentKey: Decodable { | ||
public init(from decoder: any Swift.Decoder) throws { | ||
self.rawValue = try String(from: decoder) | ||
} | ||
} | ||
|
||
extension EnvironmentKey: Hashable { | ||
public func hash(into hasher: inout Hasher) { | ||
#if os(Windows) | ||
self.rawValue.lowercased().hash(into: &hasher) | ||
#else | ||
self.rawValue.hash(into: &hasher) | ||
#endif | ||
} | ||
} | ||
|
||
extension EnvironmentKey: RawRepresentable { | ||
public init?(rawValue: String) { | ||
self.rawValue = rawValue | ||
} | ||
} | ||
|
||
extension EnvironmentKey: Sendable {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2025 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 SWBUtil | ||
import Foundation | ||
import Testing | ||
|
||
@Suite fileprivate struct EnvironmentKeyTests { | ||
let isCaseInsensitive: Bool | ||
|
||
init() throws { | ||
isCaseInsensitive = try ProcessInfo.processInfo.hostOperatingSystem() == .windows | ||
} | ||
|
||
@Test func comparable() { | ||
let key0 = EnvironmentKey("Test") | ||
let key1 = EnvironmentKey("Test1") | ||
#expect(key0 < key1) | ||
|
||
let key2 = EnvironmentKey("test") | ||
#expect(key0 < key2) | ||
} | ||
|
||
@Test func customStringConvertible() { | ||
let key = EnvironmentKey("Test") | ||
#expect(key.description == "Test") | ||
} | ||
|
||
@Test func encodable() throws { | ||
let key = EnvironmentKey("Test") | ||
let data = try JSONEncoder().encode(key) | ||
let string = String(data: data, encoding: .utf8) | ||
#expect(string == #""Test""#) | ||
} | ||
|
||
@Test func equatable() { | ||
let key0 = EnvironmentKey("Test") | ||
let key1 = EnvironmentKey("Test") | ||
#expect(key0 == key1) | ||
|
||
let key2 = EnvironmentKey("Test2") | ||
#expect(key0 != key2) | ||
|
||
if isCaseInsensitive { | ||
// Test case insensitivity on windows | ||
let key3 = EnvironmentKey("teSt") | ||
#expect(key0 == key3) | ||
} | ||
} | ||
|
||
@Test func expressibleByStringLiteral() { | ||
let key0 = EnvironmentKey("Test") | ||
#expect(key0 == "Test") | ||
} | ||
|
||
@Test func decodable() throws { | ||
let jsonString = #""Test""# | ||
let data = jsonString.data(using: .utf8)! | ||
let key = try JSONDecoder().decode(EnvironmentKey.self, from: data) | ||
#expect(key.rawValue == "Test") | ||
} | ||
|
||
@Test func hashable() { | ||
var set = Set<EnvironmentKey>() | ||
let key0 = EnvironmentKey("Test") | ||
#expect(set.insert(key0).inserted) | ||
|
||
let key1 = EnvironmentKey("Test") | ||
#expect(set.contains(key1)) | ||
#expect(!set.insert(key1).inserted) | ||
|
||
let key2 = EnvironmentKey("Test2") | ||
#expect(!set.contains(key2)) | ||
#expect(set.insert(key2).inserted) | ||
|
||
if isCaseInsensitive { | ||
// Test case insensitivity on windows | ||
let key3 = EnvironmentKey("teSt") | ||
#expect(set.contains(key3)) | ||
#expect(!set.insert(key3).inserted) | ||
} | ||
|
||
#expect(set == ["Test", "Test2"]) | ||
} | ||
|
||
@Test func rawRepresentable() { | ||
let key = EnvironmentKey(rawValue: "Test") | ||
#expect(key?.rawValue == "Test") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ignore the "diffs" in this file. This PR is entirely additions; the original Environment.swift was renamed to EnvironmentHelpers.swift.