[Bps-public-commit] GnuPG-Interface branch, work-with-both-gnupgs, updated. 0.52-26-g85acd91

Brian Duggan brian at bestpractical.com
Fri Mar 16 16:28:57 EDT 2018


The branch, work-with-both-gnupgs has been updated
       via  85acd916bbd6dfae0b630ef9072ff393aac75b50 (commit)
      from  2115aaec07ffdbc17469118d6bf60a53868fb2c9 (commit)

Summary of changes:
 lib/GnuPG/Interface.pm | 55 +++++++++++++++++++++++++-------------------------
 t/000_setup.t          |  4 ++--
 t/decrypt.t            |  4 ++--
 t/get_secret_keys.t    |  6 +++---
 t/list_secret_keys.t   |  5 +++--
 t/zzz_cleanup.t        |  2 +-
 6 files changed, 38 insertions(+), 38 deletions(-)

- Log -----------------------------------------------------------------
commit 85acd916bbd6dfae0b630ef9072ff393aac75b50
Author: Brian C. Duggan <brian at bestpractical.com>
Date:   Fri Mar 16 03:33:04 2018 -0400

    Switch to version comparison from branch comparison
    
    Remove the is_modern and friends branch checking functions. Replace with
    cmp_version for comparing version numbers.

diff --git a/lib/GnuPG/Interface.pm b/lib/GnuPG/Interface.pm
index 12376ce..afd68c8 100644
--- a/lib/GnuPG/Interface.pm
+++ b/lib/GnuPG/Interface.pm
@@ -36,6 +36,20 @@ has $_ => (
     clearer => 'clear_' . $_,
 ) for qw(call passphrase);
 
+# NB: GnuPG versions
+#
+# There are three primary "branches" of GnuPG: classic, stable, and
+# modern. They each behave slightly differently. Each branch
+# corresponds to contiguous versions of GnuPG.
+#
+# When using features specific to branches, check that the system's
+# version of gpg corresponds to the branch.
+#
+# classic: < 2.0
+# stable:  >= 2.0 and < 2.1
+# modern:  >= 2.1
+#
+# You can find examples of version comparison in the tests.
 has version => (
     isa      => 'Str',
     is       => 'ro',
@@ -43,13 +57,6 @@ has version => (
     writer   => '_set_version',
 );
 
-has branch => (
-    isa      => 'Str',
-    is       => 'ro',
-    reader   => 'branch',
-    writer   => '_set_branch',
-);
-
 has options => (
     isa        => 'GnuPG::Options',
     is         => 'rw',
@@ -67,12 +74,6 @@ sub BUILD {
     $self->hash_init( call => 'gpg' );
     $self->hash_init(%$args);
     $self->_set_version($self->_version());
-
-    my @version = split('\.', $self->version());
-
-    $self->_set_branch('classic') if $version[0] == 1;
-    $self->_set_branch('stable') if $version[0] == 2 && $version[1] == 0;
-    $self->_set_branch('modern') if $version [0] > 2 || $version[0] == 2 && $version[2] >= 1;
 }
 
 struct(
@@ -131,12 +132,12 @@ sub fork_attach_exec( $% ) {
 
     # Don't use loopback pintentry for non-modern GPG
     #
-    # Check that $version is populated before running is_modern(). If
+    # Check that $version is populated before running cmp_version. If
     # we are invoked as part of BUILD to populate $version, then any
     # methods that depend on $version will fail. We don't care about
     # loopback when we're called just to check gpg version.
     $use_loopback_pinentry = 1
-      if ($handles->passphrase() && $self->version && $self->is_modern );
+      if ($handles->passphrase() && $self->version && $self->cmp_version($self->version, '2.1') > 0 );
 
     # deprecation support
     $args{commands} ||= $args{gnupg_commands};
@@ -808,19 +809,17 @@ sub _version {
     return $1;
 }
 
-sub is_classic {
-    my ( $self ) = @_;
-    return $self->branch eq 'classic';
-}
-
-sub is_stable {
-    my ( $self ) = @_;
-    return $self->branch eq 'stable';
-}
-
-sub is_modern {
-    my ( $self ) = @_;
-    return $self->branch eq 'modern';
+sub cmp_version($$) {
+    my ( $self, $a, $b ) = (@_);
+    my @a = split '\.', $a;
+    my @b = split '\.', $b;
+    @a > @b
+        ? push @b, (0) x (@a- at b)
+        : push @a, (0) x (@b- at a);
+    for ( my $i = 0; $i < @a; $i++ ) {
+        return $a[$i] <=> $b[$i] if $a[$i] <=> $b[$i];
+    }
+    return 0;
 }
 
 sub test_default_key_passphrase() {
diff --git a/t/000_setup.t b/t/000_setup.t
index 2677245..e133ec5 100644
--- a/t/000_setup.t
+++ b/t/000_setup.t
@@ -16,13 +16,13 @@ TEST
     make_path($homedir, { mode => 0700 });
     my $agentconf = IO::File->new( "> " . $homedir . "/gpg-agent.conf" );
     # Classic gpg can't use loopback pinentry programs like fake-pinentry.pl.
-    $agentconf->write("pinentry-program " . getcwd() . "/test/fake-pinentry.pl\n") if $gnupg->is_modern;
+    $agentconf->write("pinentry-program " . getcwd() . "/test/fake-pinentry.pl\n") if $gnupg->cmp_version($gnupg->version, '2.1') >= 0;
     $agentconf->close();
     copy('test/gpg.conf', $homedir . '/gpg.conf');
     # In classic gpg, gpgconf cannot kill gpg-agent. But these tests
     # will not start an agent when using classic gpg. For modern gpg,
     # reset the state of any long-lived gpg-agent, ignoring errors:
-    system('gpgconf', '--homedir', $homedir, '--quiet', '--kill', 'gpg-agent') if $gnupg->is_modern;
+    system('gpgconf', '--homedir', $homedir, '--quiet', '--kill', 'gpg-agent') if $gnupg->cmp_version($gnupg->version, '2.1') >= 0;
 
     reset_handles();
 
diff --git a/t/decrypt.t b/t/decrypt.t
index 70aeaf6..b72b782 100644
--- a/t/decrypt.t
+++ b/t/decrypt.t
@@ -63,7 +63,7 @@ TEST
 # test without default_passphrase (that is, by using the agent)
 TEST
 {
-    return 1 unless $gnupg->is_modern();
+    return 1 unless $gnupg->cmp_version($gnupg->version, '2.1') >= 0;
 
     reset_handles();
 
@@ -85,7 +85,7 @@ TEST
 
 TEST
 {
-    return 1 unless $gnupg->is_modern();
+    return 1 unless $gnupg->cmp_version($gnupg->version, '2.1') >= 0;
 
     return compare( $texts{alt_plain}->fn(), $texts{temp}->fn() ) == 0;
 };
diff --git a/t/get_secret_keys.t b/t/get_secret_keys.t
index b370e57..5b4f97e 100644
--- a/t/get_secret_keys.t
+++ b/t/get_secret_keys.t
@@ -45,7 +45,7 @@ TEST
         usage_flags            => 'scaESCA',
         pubkey_data            => $pubkey_data,
     };
-    if (!$gnupg->is_modern) {
+    if ($gnupg->cmp_version($gnupg->version, '2.1') < 0) {
       # older versions don't report ownertrust or pubkey_data for secret keys:
       delete $args->{pubkey_data};
       $args->{owner_trust} = '';
@@ -91,7 +91,7 @@ TEST
         pubkey_data              => $subkey_pub_data,
       };
 
-    if (!$gnupg->is_modern) {
+    if ($gnupg->cmp_version($gnupg->version, '2.1') < 0) {
       # older versions do not report pubkey data for secret keys
       delete $sub_args->{pubkey_data};
     }
@@ -105,7 +105,7 @@ TEST
 
     $handmade_key->push_subkeys( $subkey );
     # older versions do not report designated revokers for secret keys
-    $handmade_key->push_revokers( $revoker ) if ($gnupg->is_modern);
+    $handmade_key->push_revokers( $revoker ) if ($gnupg->cmp_version($gnupg->version, '2.1') >= 0);
 
     $handmade_key->compare( $given_key );
 };
diff --git a/t/list_secret_keys.t b/t/list_secret_keys.t
index 0c141d6..52f698f 100644
--- a/t/list_secret_keys.t
+++ b/t/list_secret_keys.t
@@ -23,7 +23,7 @@ TEST
     $outfile = 'test/secret-keys/1.out';
     my $out = IO::File->new( "> $outfile" )
       or die "cannot open $outfile for writing: $ERRNO";
-    my $seckey_file = $gnupg->is_modern ? 'pubring.kbx' : 'secring.gpg';
+    my $seckey_file = $gnupg->cmp_version($gnupg->version, '2.1') >= 0 ? 'pubring.kbx' : 'secring.gpg';
     my $pubring_line = $gnupg->options->homedir() . '/' . $seckey_file . "\n";
     while (<$stdout>) {
       if ($_ eq $pubring_line) {
@@ -44,7 +44,8 @@ TEST
 
 TEST
 {
-    my $branch = $gnupg->is_modern ? 'modern' : '0';
+    my $branch = $gnupg->cmp_version($gnupg->version, '2.1') >= 0 ? 'modern' : '0';
+    print $branch."\n";
     my @files_to_test = ( 'test/secret-keys/1.'.$branch.'.test' );
 
     return file_match( $outfile, @files_to_test );
diff --git a/t/zzz_cleanup.t b/t/zzz_cleanup.t
index 8ed60e6..6eba3f4 100644
--- a/t/zzz_cleanup.t
+++ b/t/zzz_cleanup.t
@@ -16,7 +16,7 @@ TEST
     # In classic gpg, gpgconf cannot kill gpg-agent. But these tests
     # will not start an agent when using classic gpg. For modern gpg,
     # kill off any long-lived gpg-agent, ignoring errors:
-    system('gpgconf', '--homedir', $homedir, '--quiet', '--kill', 'gpg-agent') if $gnupg->is_modern();
+    system('gpgconf', '--homedir', $homedir, '--quiet', '--kill', 'gpg-agent') if $gnupg->cmp_version($gnupg->version, '2.1') >=0;
     remove_tree($homedir, {error => \$err});
     unlink('test/gnupghome');
     return ! @$err;

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list