@@ -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