Skip to content

Implement Codable for ProcessEnvironmentKey #469

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 1 commit into from
Feb 23, 2024
Merged
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
14 changes: 14 additions & 0 deletions Sources/TSCBasic/Process/ProcessEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ public struct ProcessEnvironmentKey {
}
}

extension ProcessEnvironmentKey: Encodable {
public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.value)
}
}

extension ProcessEnvironmentKey: Decodable {
public init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
self.value = try container.decode(String.self)
}
}

extension ProcessEnvironmentKey: Equatable {
public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
#if os(Windows)
Expand Down
12 changes: 11 additions & 1 deletion Tests/TSCBasicTests/ProcessEnvTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Foundation
import XCTest

import TSCBasic
import TSCTestSupport

class ProcessEnvTests: XCTestCase {

func testEnvVars() throws {
let key = "SWIFTPM_TEST_FOO"
XCTAssertEqual(ProcessEnv.vars[key], nil)
Expand Down Expand Up @@ -64,4 +64,14 @@ class ProcessEnvTests: XCTestCase {
XCTAssertNotEqual(ProcessEnvironmentKey("Key"), "KEY")
#endif
}

func testEnvironmentKeysCodable() throws {
let encoder = JSONEncoder()
let json = try encoder.encode(ProcessEnvironmentKey("foo"))
XCTAssertEqual(String(decoding: json, as: UTF8.self), #""foo""#)

let decoder = JSONDecoder()
let result = try decoder.decode(ProcessEnvironmentKey.self, from: json)
XCTAssertEqual(result, ProcessEnvironmentKey("foo"))
}
}