Skip to content

Commit

Permalink
Merge pull request #49 from glasswalk3r/refactor/ubuntu-without-exec
Browse files Browse the repository at this point in the history
refactor: replaced system exec
  • Loading branch information
DrHyde authored May 13, 2024
2 parents 1bc6369 + 4c219a5 commit 1d276a1
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/Devel/AssertOS/Linux/Ubuntu.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package Devel::AssertOS::Linux::Ubuntu;
use Devel::CheckOS;
use strict;
use warnings;

use Devel::AssertOS::OSFeatures::Release '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
Expand Down
83 changes: 83 additions & 0 deletions lib/Devel/AssertOS/OSFeatures/Release.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package Devel::AssertOS::OSFeatures::Release;

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::OSFeatures::Release - functions to manipulate os-release file
=head1 SYNOPSIS
use Devel::AssertOS::OSFeatures::Release 'distributor_id';
my $id = distributor_id;
=head1 DESCRIPTION
This module exports functions to handle text files related to Debian-like
distributions.
=head1 EXPORTED
The following subs are exported.
=head2 distributor_id
Retrieves and returns the distributor ID from the F</etc/os-release> file.
It is expected that the file exists, it is readable and have the following
(minimum) content format:
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/"
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<ID> or C<undef>, if the conditions are not those
specified above.
=cut

sub distributor_id {
my $filename = 'os-release';
my $file_path = File::Spec->catfile( ( '', 'etc' ), $filename );
my $regex = qr/^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 = ucfirst(lc $1) if (defined($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;
17 changes: 17 additions & 0 deletions t/os-release.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use warnings;
use strict;
use Test::More;
use Devel::AssertOS::OSFeatures::Release 'distributor_id';

my $total_tests = 3;
plan tests => $total_tests;

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' );
}

0 comments on commit 1d276a1

Please sign in to comment.