| 1 | package App::ModuleVersionDiffUtils; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | our $DATE = '2021-05-25'; # DATE | |||||||||||||
| 4 | 1 | our $VERSION = '0.003'; # VERSION | |||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use strict 'subs', 'vars'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Log::ger; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | our %SPEC; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 0 | ▶ | sub _load_module { | ||||||||||||
|
Subroutine
_load_module: not called CC=4 SCAR=30.0
| |||||||||||||||
| 14 | 0 | require Module::Path::More; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 0 | my ($args, $which) = @_; | |||||||||||||
| 17 | |||||||||||||||
| 18 | 0 | my $paths; | |||||||||||||
| 19 | { | ||||||||||||||
| 20 | 0 | ▶ | local @INC = (@{ $args->{include_dir} // []}, @INC); | ||||||||||||
|
Condition0%0/2
$args->{"include_dir"} // []
MC/DC0%0/1
$args->{"include_dir"} // []
$args->{"include_dir"}
Truth table0%0/2
A
A= $args->{"include_dir"}
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | $paths = Module::Path::More::module_path( | ||||||||||||||
| 23 | module => $args->{module}, | ||||||||||||||
| 24 | 0 | all => 1, | |||||||||||||
| 25 | ); | ||||||||||||||
| 26 | } | ||||||||||||||
| 27 | 0 | log_trace "Found module %s in %s", $args->{module}, $paths; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 0 | ▶ | if (@$paths < 2) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 0 | die "Found less than two versions of $args->{module}, need at least two"; | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | (my $mod_pm = "$args->{module}.pm") =~ s!::!/!g; | |||||||||||||
| 34 | 0 | ▶ | if ($which == 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 0 | do $paths->[0]; | |||||||||||||
| 36 | 0 | $INC{$mod_pm} = $paths->[0]; | |||||||||||||
| 37 | } else { | ||||||||||||||
| 38 | 0 | do $paths->[1]; | |||||||||||||
| 39 | 0 | $INC{$mod_pm} = $paths->[1]; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | sub _load_first_module { | ||||||||||||
|
Subroutine
_load_first_module: not called CC=1 SCAR=6.9
| |||||||||||||||
| 44 | 0 | my $args = shift; | |||||||||||||
| 45 | 0 | _load_module($args, 1); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | # XXX how to turn off redefine warnings? | ||||||||||||||
| 49 | 0 | ▶ | sub _load_second_module { | ||||||||||||
|
Subroutine
_load_second_module: not called CC=1 SCAR=6.9
| |||||||||||||||
| 50 | 0 | my $args = shift; | |||||||||||||
| 51 | 0 | _load_module($args, 2); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 1 | our %args_module_spec = ( | |||||||||||||
| 55 | include_dir => { | ||||||||||||||
| 56 | schema => ['array*', of=>'dirname*'], | ||||||||||||||
| 57 | cmdline_aliases => {I=>{}}, | ||||||||||||||
| 58 | }, | ||||||||||||||
| 59 | module => { | ||||||||||||||
| 60 | schema => 'perl::modname*', | ||||||||||||||
| 61 | description => <<'_', | ||||||||||||||
| 62 | |||||||||||||||
| 63 | Module will be searched in the `@INC` (you can specify `-I` to add more | ||||||||||||||
| 64 | directories to search). There needs to be at least two locations of the module. | ||||||||||||||
| 65 | Otherwise, the application will croak. | ||||||||||||||
| 66 | |||||||||||||||
| 67 | _ | ||||||||||||||
| 68 | req => 1, | ||||||||||||||
| 69 | pos => 0, | ||||||||||||||
| 70 | }, | ||||||||||||||
| 71 | ); | ||||||||||||||
| 72 | |||||||||||||||
| 73 | $SPEC{diff_two_module_version_hash} = { | ||||||||||||||
| 74 | 1 | v => 1.1, | |||||||||||||
| 75 | args => { | ||||||||||||||
| 76 | %args_module_spec, | ||||||||||||||
| 77 | hash_name => { | ||||||||||||||
| 78 | summary => 'Hash name to be found in module namespace, with sigil', | ||||||||||||||
| 79 | schema => ['str*', match=>qr/\A[%\$]\w+\z/], | ||||||||||||||
| 80 | req => 1, | ||||||||||||||
| 81 | pos => 1, | ||||||||||||||
| 82 | }, | ||||||||||||||
| 83 | }, | ||||||||||||||
| 84 | 'cmdline.skip_format' => 1, | ||||||||||||||
| 85 | examples => [ | ||||||||||||||
| 86 | { | ||||||||||||||
| 87 | argv => ['Foo::Bar', '%hash'], | ||||||||||||||
| 88 | summary => 'Diff %hash between two versions of Foo::Bar', | ||||||||||||||
| 89 | test => 0, | ||||||||||||||
| 90 | 'x.doc.show_result' => 0, | ||||||||||||||
| 91 | }, | ||||||||||||||
| 92 | { | ||||||||||||||
| 93 | argv => ['Foo::Bar', '$hashref'], | ||||||||||||||
| 94 | summary => 'Diff $hashref between two versions of Foo::Bar', | ||||||||||||||
| 95 | test => 0, | ||||||||||||||
| 96 | 'x.doc.show_result' => 0, | ||||||||||||||
| 97 | }, | ||||||||||||||
| 98 | ], | ||||||||||||||
| 99 | }; | ||||||||||||||
| 100 | 0 | ▶ | sub diff_two_module_version_hash { | ||||||||||||
|
Subroutine
diff_two_module_version_hash: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||
| 101 | 0 | my %args = @_; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | my $mod = $args{module}; | |||||||||||||
| 104 | 0 | _load_first_module(\%args); | |||||||||||||
| 105 | 0 | ▶ | my $version1 = ${"$mod\::VERSION"} // 'dev'; | ||||||||||||
|
Condition0%0/2
${"${mod}::VERSION";} // "dev"
MC/DC0%0/1
${"${mod}::VERSION";} // "dev"
${"${mod}::VERSION";}
Truth table0%0/2
A
A= ${"${mod}::VERSION";}
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | 0 | my $hash1; | |||||||||||||
| 108 | 0 | ▶ | if ($args{hash_name} =~ /\A%(.+)/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 109 | 0 | $hash1 = { %{"$mod\::$1"} }; | |||||||||||||
| 110 | 0 | %{"$mod\::$1"} = (); | |||||||||||||
| 111 | } elsif ($args{hash_name} =~ /\A\$(.+)/) { | ||||||||||||||
| 112 | 0 | $hash1 = ${"$mod\::$1"}; | |||||||||||||
| 113 | 0 | ▶ | die "\$$mod\::$1 is not a hashref" unless ref $hash1 eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | 0 | $hash1 = {%$hash1}; | |||||||||||||
| 115 | 0 | %{ ${"$mod\::$1"} } = (); | |||||||||||||
| 116 | } else { | ||||||||||||||
| 117 | 0 | die "Invalid hash name $args{hash_name}, must be '\%foo' or '\$foo'"; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 0 | undef ${"$mod\::VERSION"}; | |||||||||||||
| 121 | 0 | _load_second_module(\%args); | |||||||||||||
| 122 | 0 | ▶ | my $version2 = ${"$mod\::VERSION"} // 'dev'; | ||||||||||||
|
Condition0%0/2
${"${mod}::VERSION";} // "dev"
MC/DC0%0/1
${"${mod}::VERSION";} // "dev"
${"${mod}::VERSION";}
Truth table0%0/2
A
A= ${"${mod}::VERSION";}
| |||||||||||||||
| 123 | |||||||||||||||
| 124 | 0 | my $hash2; | |||||||||||||
| 125 | 0 | ▶ | if ($args{hash_name} =~ /\A%(.+)/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 126 | 0 | $hash2 = { %{"$mod\::$1"} }; | |||||||||||||
| 127 | 0 | %{"$mod\::$1"} = (); | |||||||||||||
| 128 | } elsif ($args{hash_name} =~ /\A\$(.+)/) { | ||||||||||||||
| 129 | 0 | $hash2 = ${"$mod\::$1"}; | |||||||||||||
| 130 | 0 | ▶ | die "\$$mod\::$1 is not a hashref" unless ref $hash2 eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | $hash2 = {%$hash2}; | |||||||||||||
| 132 | 0 | %{ ${"$mod\::$1"} } = (); | |||||||||||||
| 133 | } else { | ||||||||||||||
| 134 | 0 | die "Invalid hash name $args{hash_name}, must be '\%foo' or '\$foo'"; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 0 | my ($label1, $label2); | |||||||||||||
| 138 | 0 | ▶ | if ($version1 ne $version2) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 0 | $label1 = "$mod version $version1"; | |||||||||||||
| 140 | 0 | $label2 = "$mod version $version2"; | |||||||||||||
| 141 | } else { | ||||||||||||||
| 142 | 0 | $label1 = "first version of $mod"; | |||||||||||||
| 143 | 0 | $label2 = "second version of $mod"; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 0 | my @res; | |||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | push @res, "Keys only in ${label1}'s hash:\n"; | |||||||||||||
| 149 | 0 | for my $k (sort keys %$hash1) { | |||||||||||||
| 150 | 0 | ▶ | next if exists $hash2->{$k}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | 0 | push @res, " $k\n"; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | 0 | push @res, "\n"; | |||||||||||||
| 154 | |||||||||||||||
| 155 | 0 | push @res, "Keys only in ${label2}'s hash:\n"; | |||||||||||||
| 156 | 0 | for my $k (sort keys %$hash2) { | |||||||||||||
| 157 | 0 | ▶ | next if exists $hash1->{$k}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 0 | push @res, " $k\n"; | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | 0 | push @res, "\n"; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | push @res, "Keys where the values change:\n"; | |||||||||||||
| 163 | 0 | for my $k (sort keys %$hash2) { | |||||||||||||
| 164 | 0 | ▶ | next unless exists $hash1->{$k}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | 0 | ▶ | my $v1 = $hash1->{$k} // ''; | ||||||||||||
|
Condition0%0/2
$hash1->{$k} // ""
MC/DC0%0/1
$hash1->{$k} // ""
$hash1->{$k}
Truth table0%0/2
A
A= $hash1->{$k}
| |||||||||||||||
| 166 | 0 | ▶ | my $v2 = $hash2->{$k} // ''; | ||||||||||||
|
Condition0%0/2
$hash2->{$k} // ""
MC/DC0%0/1
$hash2->{$k} // ""
$hash2->{$k}
Truth table0%0/2
A
A= $hash2->{$k}
| |||||||||||||||
| 167 | 0 | ▶ | next if $v1 eq $v2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | 0 | push @res, " $k ($v1 -> $v2)\n"; | |||||||||||||
| 169 | } | ||||||||||||||
| 170 | |||||||||||||||
| 171 | 0 | [200, "OK", join("", @res), {'cmdline.skip_format'=>1}]; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | |||||||||||||||
| 174 | 1 | 1; | |||||||||||||
| 175 | # ABSTRACT: Utilities to diff stuffs from two different versions of a module | ||||||||||||||
| 176 | |||||||||||||||
| 177 | __END__ | ||||||||||||||