From 2a1c7ef7c020dd3ed47ed18f28f3ce8f9dad9c79 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 8 Feb 2020 00:14:10 -0700 Subject: option to populate @ARGV using an anonymous array Now Perl code can call our App:: code and simulate command line arguments by passing an arrayref. Note that we can't just say local @ARGV = @_ if @_; because then we can't distinguish between the intent to simulate passing zero command line arguments and a request to look to @ARGV. Signed-off-by: Sean Whitton --- lib/App/annex_review_unused.pm | 3 +++ lib/App/annex_to_annex.pm | 3 +++ lib/App/annex_to_annex_dropunused.pm | 3 +++ lib/App/annex_to_annex_reinject.pm | 3 +++ t/21_annex-to-annex.t | 5 ++++- 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/App/annex_review_unused.pm b/lib/App/annex_review_unused.pm index d170601..d92b6d6 100644 --- a/lib/App/annex_review_unused.pm +++ b/lib/App/annex_review_unused.pm @@ -39,6 +39,9 @@ for that command. =cut sub main { + shift if $_[0] and ref $_[0] eq ""; # in case main called as a class method + local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; + my $annex = Git::Annex->new; my $just_print = 0; diff --git a/lib/App/annex_to_annex.pm b/lib/App/annex_to_annex.pm index 29cd3f7..56ea2d8 100644 --- a/lib/App/annex_to_annex.pm +++ b/lib/App/annex_to_annex.pm @@ -42,6 +42,9 @@ that command. =cut sub main { + shift if $_[0] and ref $_[0] eq ""; # in case main called as a class method + local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; + # only support v7 because supporting v5 too would make things quite # complex. require git-annex >=7.20191009 because it will refuse to # work in v5 repos, and because it supports `git annex find --unlocked` diff --git a/lib/App/annex_to_annex_dropunused.pm b/lib/App/annex_to_annex_dropunused.pm index 89b16b5..23c8851 100644 --- a/lib/App/annex_to_annex_dropunused.pm +++ b/lib/App/annex_to_annex_dropunused.pm @@ -47,6 +47,9 @@ documentation for that command. =cut sub main { + shift if $_[0] and ref $_[0] eq ""; # in case main called as a class method + local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; + my $annex = Git::Annex->new; my @to_drop; diff --git a/lib/App/annex_to_annex_reinject.pm b/lib/App/annex_to_annex_reinject.pm index c208d05..a857194 100644 --- a/lib/App/annex_to_annex_reinject.pm +++ b/lib/App/annex_to_annex_reinject.pm @@ -40,6 +40,9 @@ documentation for that command. =cut sub main { + shift if $_[0] and ref $_[0] eq ""; # in case main called as a class method + local @ARGV = @{ $_[0] } if $_[0] and ref $_[0] ne ""; + die "usage: annex-to-annex-reinject SOURCEANNEX DESTANNEX\n" unless @ARGV == 2; diff --git a/t/21_annex-to-annex.t b/t/21_annex-to-annex.t index 76ddd27..9589b59 100755 --- a/t/21_annex-to-annex.t +++ b/t/21_annex-to-annex.t @@ -56,7 +56,10 @@ with_temp_annexes { with_temp_annexes { my (undef, $source1, $source2, $dest) = @_; - run_bin qw(annex-to-annex --commit source1/foo source2/other dest); + # also implicitly test, here, that we can invoke the program by + # passing a subroutine reference when invoking main as a class + # method + App::annex_to_annex->main([qw(--commit source1/foo source2/other dest)]); @output = $source1->RUN(qw(log -1 --oneline --name-status)); like $output[0], qr/migrated by annex-to-annex/, -- cgit v1.2.3