Skip to content

Commit faf4d7e

Browse files
committed
Make tests use the psueod fs and call detectHostLinuxDistribution directly.
Tests were reimplementing the parsing, which is not of much value. Call the detectHostLinuxDistribution() function directly to exercise the parsing logic. Add a way to pass in a filesystem so we can pass the mocked filesystem.
1 parent e1e59db commit faf4d7e

File tree

3 files changed

+100
-431
lines changed

3 files changed

+100
-431
lines changed

Sources/SWBGenericUnixPlatform/Plugin.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,17 @@ struct GenericUnixSDKRegistryExtension: SDKRegistryExtension {
129129
let shouldUseLLD = {
130130
switch operatingSystem {
131131
case .freebsd:
132-
return true // FreeBSD is always LLVM-based.
132+
// FreeBSD is always LLVM-based.
133+
return true
133134
case .linux:
134135
// Amazon Linux 2 has a gold linker bug see: https://sourceware.org/bugzilla/show_bug.cgi?id=23016.
135136
guard let distribution = operatingSystem.distribution else {
136137
return false
137138
}
138139
return distribution.kind == .amazon && distribution.version == "2"
139140
default:
140-
return operatingSystem != context.hostOperatingSystem // Cross-compiling.
141+
// Cross-compiling.
142+
return operatingSystem != context.hostOperatingSystem
141143
}
142144
}()
143145

Sources/SWBUtil/ProcessInfo.swift

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,35 +228,44 @@ public enum OperatingSystem: Hashable, Sendable {
228228
}
229229
}
230230

231-
/// Detects the Linux distribution by examining system files
231+
private func detectHostLinuxDistribution() -> LinuxDistribution? {
232+
return detectHostLinuxDistribution(fs: localFS)
233+
}
234+
235+
/// Detects the Linux distribution by examining system files with an injected filesystem
232236
/// Start with the "generic" /etc/os-release then fallback
233237
/// to various distribution named files.
234-
private func detectHostLinuxDistribution() -> LinuxDistribution? {
235-
#if os(Linux)
236-
// Try /etc/os-release first (standard)
237-
if let osRelease = try? String(contentsOfFile: "/etc/os-release") {
238+
public func detectHostLinuxDistribution(fs: any FSProxy) -> LinuxDistribution? {
239+
// Try /etc/os-release first (standard)
240+
let osReleasePath = Path("/etc/os-release")
241+
if fs.exists(osReleasePath) {
242+
if let osReleaseData = try? fs.read(osReleasePath),
243+
let osRelease = String(data: Data(osReleaseData.bytes), encoding: .utf8) {
238244
if let distribution = parseOSRelease(osRelease) {
239245
return distribution
240246
}
241247
}
242-
// Fallback to distribution-specific files
243-
let distributionFiles: [(String, LinuxDistribution.Kind)] = [
244-
("/etc/ubuntu-release", .ubuntu),
245-
("/etc/debian_version", .debian),
246-
("/etc/amazon-release", .amazon),
247-
("/etc/centos-release", .centos),
248-
("/etc/redhat-release", .rhel),
249-
("/etc/fedora-release", .fedora),
250-
("/etc/SuSE-release", .suse),
251-
("/etc/alpine-release", .alpine),
252-
("/etc/arch-release", .arch),
253-
]
254-
for (file, kind) in distributionFiles {
255-
if FileManager.default.fileExists(atPath: file) {
256-
return LinuxDistribution(kind: kind)
257-
}
248+
}
249+
250+
// Fallback to distribution-specific files
251+
let distributionFiles: [(String, LinuxDistribution.Kind)] = [
252+
("/etc/ubuntu-release", .ubuntu),
253+
("/etc/debian_version", .debian),
254+
("/etc/amazon-release", .amazon),
255+
("/etc/centos-release", .centos),
256+
("/etc/redhat-release", .rhel),
257+
("/etc/fedora-release", .fedora),
258+
("/etc/SuSE-release", .suse),
259+
("/etc/alpine-release", .alpine),
260+
("/etc/arch-release", .arch),
261+
]
262+
263+
for (file, kind) in distributionFiles {
264+
if fs.exists(Path(file)) {
265+
return LinuxDistribution(kind: kind)
258266
}
259-
#endif
267+
}
268+
260269
return nil
261270
}
262271

0 commit comments

Comments
 (0)