From 152fddd3878df5860c65a0e8bbc60044f3f27082 Mon Sep 17 00:00:00 2001 From: Alceu Rodrigues de Freitas Junior Date: Tue, 7 May 2024 21:42:44 -0300 Subject: [PATCH 1/6] refactor: replaced system exec lsb-release just reads /etc/lsb-release file, which is easy to parse and return the expected value. --- lib/Devel/AssertOS/Linux/Ubuntu.pm | 11 +++-- lib/Devel/AssertOS/Ubuntu/LSB.pm | 76 ++++++++++++++++++++++++++++++ t/ubuntu.t | 11 +++++ 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 lib/Devel/AssertOS/Ubuntu/LSB.pm create mode 100644 t/ubuntu.t diff --git a/lib/Devel/AssertOS/Linux/Ubuntu.pm b/lib/Devel/AssertOS/Linux/Ubuntu.pm index 036fc26..96fabbe 100644 --- a/lib/Devel/AssertOS/Linux/Ubuntu.pm +++ b/lib/Devel/AssertOS/Linux/Ubuntu.pm @@ -3,18 +3,21 @@ package Devel::AssertOS::Linux::Ubuntu; use Devel::CheckOS; use strict; use warnings; + +use Devel::AssertOS::Ubuntu::LSB 'distributor_id'; + no warnings 'redefine'; -our $VERSION = '1.0'; +our $VERSION = '1.1'; sub os_is { - Devel::CheckOS::os_is('Linux') && - `lsb_release -i 2>/dev/null` =~ /Ubuntu/ + my $id = distributor_id; + Devel::CheckOS::os_is('Linux') && defined($id) && $id =~ /Ubuntu/; } sub expn { "The operating system is some version of Ubuntu" } -Devel::CheckOS::die_unsupported() unless(os_is()); +Devel::CheckOS::die_unsupported() unless ( os_is() ); =head1 COPYRIGHT and LICENCE diff --git a/lib/Devel/AssertOS/Ubuntu/LSB.pm b/lib/Devel/AssertOS/Ubuntu/LSB.pm new file mode 100644 index 0000000..362e2c8 --- /dev/null +++ b/lib/Devel/AssertOS/Ubuntu/LSB.pm @@ -0,0 +1,76 @@ +package Devel::AssertOS::Ubuntu::LSB; + +use strict; +use warnings; +use File::Spec; +use parent 'Exporter'; + +our $VERSION = '1.0'; +our @EXPORT_OK = qw(distributor_id); + +=pod + +=head1 NAME + +Devel::AssertOS::Ubuntu::LSB - functions to manipulate LSB files + +=head1 SYNOPSIS + + use Devel::AssertOS::Ubuntu::LSB 'distributor_id'; + my $id = distributor_id; + +=head1 DESCRIPTION + +This module exports functions to handle text files related to Ubuntu LSB. + +=head1 EXPORTED + +The following subs are exported. + +=head2 distributor_id + +Retrieves and returns the distributor ID from the F file. + +It is expected that the file exists, it is readable and have the following +content format: + + DISTRIB_ID=Ubuntu + DISTRIB_RELEASE=22.04 + DISTRIB_CODENAME=jammy + DISTRIB_DESCRIPTION="Ubuntu 22.04.4 LTS" + +It returns the value of C of C, if the conditions are not +those specified above. + +=cut + +sub distributor_id { + my $filename = 'lsb-release'; + my $file_path = File::Spec->catfile( ( '', 'etc' ), $filename ); + my $regex = qr/^DISTRIB_ID\=(\w+)/; + my $dist_id = undef; + + if ( -r $file_path ) { + open my $in, '<', $file_path or die "Cannot read $file_path: $!"; + while (<$in>) { + chomp; + if ( $_ =~ $regex ) { + $dist_id = $1; + last; + } + } + close($in) or die "Cannot close $file_path: $!"; + } + + return $dist_id; +} + +=head1 COPYRIGHT and LICENCE + +Copyright 2024 David Cantrell + +This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. + +=cut + +1; diff --git a/t/ubuntu.t b/t/ubuntu.t new file mode 100644 index 0000000..1b84624 --- /dev/null +++ b/t/ubuntu.t @@ -0,0 +1,11 @@ +use warnings; +use strict; +use Test::More; +use Devel::AssertOS::Ubuntu::LSB 'distributor_id'; + +SKIP: { + skip 'Not running on Ubuntu-like', 1 unless ( -r '/etc/lsb-release' ); + is( distributor_id(), 'Ubuntu', 'can fetch the correct distributor ID' ); +} + +done_testing; From 8d80bfa877dd39a3b96b395289fd96c817f4f7c8 Mon Sep 17 00:00:00 2001 From: Alceu Rodrigues de Freitas Junior Date: Fri, 10 May 2024 22:32:18 -0300 Subject: [PATCH 2/6] refactor: not restricted to Ubuntu --- lib/Devel/AssertOS/{Ubuntu/LSB.pm => OSFeatures/Release.pm} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/Devel/AssertOS/{Ubuntu/LSB.pm => OSFeatures/Release.pm} (100%) diff --git a/lib/Devel/AssertOS/Ubuntu/LSB.pm b/lib/Devel/AssertOS/OSFeatures/Release.pm similarity index 100% rename from lib/Devel/AssertOS/Ubuntu/LSB.pm rename to lib/Devel/AssertOS/OSFeatures/Release.pm From ae04273d6fa11c74f9f1c805d15553de1aa8aa1a Mon Sep 17 00:00:00 2001 From: Alceu Rodrigues de Freitas Junior Date: Fri, 10 May 2024 22:57:52 -0300 Subject: [PATCH 3/6] refactor: renamed module --- lib/Devel/AssertOS/Linux/Ubuntu.pm | 2 +- t/ubuntu.t | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Devel/AssertOS/Linux/Ubuntu.pm b/lib/Devel/AssertOS/Linux/Ubuntu.pm index 96fabbe..428a6d3 100644 --- a/lib/Devel/AssertOS/Linux/Ubuntu.pm +++ b/lib/Devel/AssertOS/Linux/Ubuntu.pm @@ -4,7 +4,7 @@ use Devel::CheckOS; use strict; use warnings; -use Devel::AssertOS::Ubuntu::LSB 'distributor_id'; +use Devel::AssertOS::OSFeatures::Release 'distributor_id'; no warnings 'redefine'; diff --git a/t/ubuntu.t b/t/ubuntu.t index 1b84624..b771b54 100644 --- a/t/ubuntu.t +++ b/t/ubuntu.t @@ -1,7 +1,7 @@ use warnings; use strict; use Test::More; -use Devel::AssertOS::Ubuntu::LSB 'distributor_id'; +use Devel::AssertOS::OSFeatures::Release 'distributor_id'; SKIP: { skip 'Not running on Ubuntu-like', 1 unless ( -r '/etc/lsb-release' ); From 0929ca25bc8fa4868635ff347f476c65d7624a5d Mon Sep 17 00:00:00 2001 From: Alceu Rodrigues de Freitas Junior Date: Fri, 10 May 2024 23:02:27 -0300 Subject: [PATCH 4/6] refactor: reading from os-release instead --- lib/Devel/AssertOS/OSFeatures/Release.pm | 37 ++++++++++++++---------- t/ubuntu.t | 5 +--- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/Devel/AssertOS/OSFeatures/Release.pm b/lib/Devel/AssertOS/OSFeatures/Release.pm index 362e2c8..bfdb96d 100644 --- a/lib/Devel/AssertOS/OSFeatures/Release.pm +++ b/lib/Devel/AssertOS/OSFeatures/Release.pm @@ -1,4 +1,4 @@ -package Devel::AssertOS::Ubuntu::LSB; +package Devel::AssertOS::OSFeatures::Release; use strict; use warnings; @@ -12,16 +12,17 @@ our @EXPORT_OK = qw(distributor_id); =head1 NAME -Devel::AssertOS::Ubuntu::LSB - functions to manipulate LSB files +Devel::AssertOS::OSFeatures::Release - functions to manipulate os-release file =head1 SYNOPSIS - use Devel::AssertOS::Ubuntu::LSB 'distributor_id'; + use Devel::AssertOS::OSFeatures::Release 'distributor_id'; my $id = distributor_id; =head1 DESCRIPTION -This module exports functions to handle text files related to Ubuntu LSB. +This module exports functions to handle text files related to Debian-like +distributions. =head1 EXPORTED @@ -29,25 +30,31 @@ The following subs are exported. =head2 distributor_id -Retrieves and returns the distributor ID from the F file. +Retrieves and returns the distributor ID from the F file. It is expected that the file exists, it is readable and have the following -content format: +(minimum) content format: - DISTRIB_ID=Ubuntu - DISTRIB_RELEASE=22.04 - DISTRIB_CODENAME=jammy - DISTRIB_DESCRIPTION="Ubuntu 22.04.4 LTS" + NAME="Ubuntu" + VERSION_ID="22.04" + VERSION="22.04.4 LTS (Jammy Jellyfish)" + VERSION_CODENAME=jammy + ID=ubuntu + ID_LIKE=debian + HOME_URL="https://www.ubuntu.com/" -It returns the value of C of C, if the conditions are not -those specified above. +This excerpt is from Ubuntu 22.04, but other distributions might have fewer, +more or different fields and values. + +It returns the value of C or C, if the conditions are not those +specified above. =cut sub distributor_id { - my $filename = 'lsb-release'; + my $filename = 'os-release'; my $file_path = File::Spec->catfile( ( '', 'etc' ), $filename ); - my $regex = qr/^DISTRIB_ID\=(\w+)/; + my $regex = qr/^ID\=(\w+)/; my $dist_id = undef; if ( -r $file_path ) { @@ -55,7 +62,7 @@ sub distributor_id { while (<$in>) { chomp; if ( $_ =~ $regex ) { - $dist_id = $1; + $dist_id = ucfirst(lc $1) if (defined($1)); last; } } diff --git a/t/ubuntu.t b/t/ubuntu.t index b771b54..617eaa9 100644 --- a/t/ubuntu.t +++ b/t/ubuntu.t @@ -3,9 +3,6 @@ use strict; use Test::More; use Devel::AssertOS::OSFeatures::Release 'distributor_id'; -SKIP: { - skip 'Not running on Ubuntu-like', 1 unless ( -r '/etc/lsb-release' ); - is( distributor_id(), 'Ubuntu', 'can fetch the correct distributor ID' ); -} +is( distributor_id(), 'Ubuntu', 'can fetch the correct distribution ID' ); done_testing; From 8838056fe61b7762b0802bd5feb29975478d12f0 Mon Sep 17 00:00:00 2001 From: Alceu Rodrigues de Freitas Junior Date: Sat, 11 May 2024 23:00:25 -0300 Subject: [PATCH 5/6] fix: tests only on Linux --- t/ubuntu.t | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/t/ubuntu.t b/t/ubuntu.t index 617eaa9..d9968ce 100644 --- a/t/ubuntu.t +++ b/t/ubuntu.t @@ -3,6 +3,15 @@ use strict; use Test::More; use Devel::AssertOS::OSFeatures::Release 'distributor_id'; -is( distributor_id(), 'Ubuntu', 'can fetch the correct distribution ID' ); +my $total_tests = 3; +plan tests => $total_tests; -done_testing; +SKIP: { + skip 'Not a Linux distribution', $total_tests unless ( $^O eq 'linux' ); + my $id = distributor_id(); + ok( $id, 'can fetch the distribution ID' ) + or BAIL_OUT('No use to keep testing with ID = undef'); + like $id, qr/^\w+$/, 'ID looks like a string'; + my $copy = ucfirst( lc $id ); + is( $id, $copy, 'ID is returned with first character in uppercase' ); +} From 4c219a5a464e483cc736df18da3f5ec3aee21b5a Mon Sep 17 00:00:00 2001 From: Alceu Rodrigues de Freitas Junior Date: Sat, 11 May 2024 23:01:08 -0300 Subject: [PATCH 6/6] refactor: proper filename --- t/{ubuntu.t => os-release.t} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename t/{ubuntu.t => os-release.t} (100%) diff --git a/t/ubuntu.t b/t/os-release.t similarity index 100% rename from t/ubuntu.t rename to t/os-release.t