Skip to content

Commit f8ac291

Browse files
committed
Tests: Update isAmazonLinux2() logic
The ProcessInfo extension to determine if a host is running on Amazon Linux 2 was slightly flawed as it did not take into account the double-quote (") surrounding the values. Update the logic to look at "PRETTY_NAME" since the ID can be used in multiple Amazon Linux OS'es, and use a regular expression in the logic.
1 parent 69f851d commit f8ac291

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

Sources/_InternalTestSupport/ProcessInfo+hostutils.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ extension ProcessInfo {
2222
return false
2323
}
2424
}
25-
let lines = contentString.components(separatedBy: .newlines)
26-
for line in lines {
27-
if line.starts(with: "ID=") {
28-
let id = line.replacingOccurrences(of: "ID=", with: "").trimmingCharacters(in: .whitespacesAndNewlines)
29-
if id == "amzn" { // ID for Amazon Linux is "amzn"
25+
do {
26+
let nameRegex = try Regex("PRETTY_NAME=\"?Amazon Linux 2.*\"?")
27+
let lines = contentString.components(separatedBy: .newlines)
28+
for line in lines {
29+
if line.contains(nameRegex) {
3030
return true
3131
}
3232
}
33+
} catch {
34+
print("Unable to determine if host is Amazon Linux 2")
3335
}
3436
return false
3537
}

Tests/BasicsTests/ProcessInfoTests.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,27 @@ struct ProcessInfoExtensionTests {
5050
@Test(
5151
arguments: [
5252
(contentUT: "", expected: false),
53-
(contentUT: "ID=", expected: false),
54-
(contentUT: "ID=foo", expected: false),
55-
(contentUT: "ID=amzn", expected: true),
56-
(contentUT: " ID=amzn", expected: false),
53+
(contentUT: "PRETTY_NAME=", expected: false),
54+
(contentUT: "PRETTY_NAME=foo", expected: false),
55+
(contentUT: "PRETTY_NAME=amzn", expected: false),
56+
(contentUT: "PRETTY_NAME=Amazon Linux 2", expected: true),
57+
(contentUT: "PRETTY_NAME=\"Amazon Linux 2\"", expected: true),
58+
(contentUT: " PRETTY_NAME=amzn", expected: false),
59+
(
60+
contentUT: """
61+
NAME="Amazon Linux"
62+
VERSION="2"
63+
ID="amzn"
64+
ID_LIKE="centos rhel fedora"
65+
VERSION_ID="2"
66+
PRETTY_NAME="Amazon Linux 2"
67+
ANSI_COLOR="0;33"
68+
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
69+
HOME_URL="https://amazonlinux.com/"
70+
SUPPORT_END="2026-06-30"
71+
""",
72+
expected: true
73+
)
5774
], prefixAndSuffixData,
5875
)
5976
fileprivate func isAmazonLinux2ReturnsExpectedValue(

0 commit comments

Comments
 (0)