summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2020-02-01 15:36:10 -0700
committerSean Whitton <spwhitton@spwhitton.name>2020-02-01 15:36:10 -0700
commit961bc7ee398aff3ecb2e29a5c2d44e2283380531 (patch)
tree4bd978b481d9becf3b95f21975e4380bc2051741 /t
parentc1aabf33bd7f70298ea977b0e20dc262a92bde2b (diff)
downloadp5-Git-Annex-961bc7ee398aff3ecb2e29a5c2d44e2283380531.tar.gz
add 'unused' method to Git::Annex
Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 't')
-rwxr-xr-xt/12_unused.t129
-rw-r--r--t/lib/t/Setup.pm2
-rw-r--r--t/lib/t/Util.pm23
3 files changed, 153 insertions, 1 deletions
diff --git a/t/12_unused.t b/t/12_unused.t
new file mode 100755
index 0000000..55b6a9d
--- /dev/null
+++ b/t/12_unused.t
@@ -0,0 +1,129 @@
+#!/usr/bin/perl
+
+use 5.028;
+use strict;
+use warnings;
+use lib 't/lib';
+
+use Try::Tiny;
+use Test::More;
+use Git::Annex;
+use t::Setup;
+use t::Util;
+use Storable;
+use Data::Compare;
+use Time::HiRes qw(time);
+use File::Slurp;
+use File::Spec::Functions qw(catfile);
+use Scalar::Util qw(looks_like_number);
+
+with_temp_annexes {
+ my (undef, $source1) = @_;
+ $source1->rm("foo/foo2/baz");
+ $source1->commit({ message => "rm" });
+
+ my $annex = Git::Annex->new($source1->dir);
+
+ $annex->git->config(qw(annex.used-refspec +refs/heads/*));
+ my @unused = @{ $annex->unused };
+ ok $annex->{_unused}{unused_args}{used_refspec} eq "+refs/heads/*",
+ "uses configured annex.used-refspec";
+ $annex->git->config(qw(--unset annex.used-refspec));
+ @unused = @{ $annex->unused(used_refspec => "+refs/heads/*") };
+ ok $annex->{_unused}{unused_args}{used_refspec} eq "+refs/heads/*",
+ "uses passed used_refspec";
+ @unused = @{ $annex->unused };
+ ok $annex->{_unused}{unused_args}{used_refspec} eq
+ "+refs/heads/*:-refs/heads/synced/*",
+ "uses default --used-refspec";
+
+ ok @unused == 1, "there is one unused file";
+ my %unused_file = %{ $unused[0] };
+ ok !$unused_file{tmp}, "unused file is not a tmp file";
+ ok !$unused_file{bad}, "unused file is not a bad file";
+ ok looks_like_number $unused_file{number}, "unused file has a number";
+ ok $unused_file{key} =~ /^SHA256E-/, "unused file has a key";
+
+ my ($_unused, $cache_timestamp) = read_cache($annex);
+ ok Compare(\@unused, $_unused->{unused}), "it caches its results";
+ sleep 1;
+ my $cmd_time = time;
+ @unused = @{ $annex->unused };
+ ($_unused, $cache_timestamp) = read_cache($annex);
+ ok $_unused->{timestamp} < $cmd_time
+ && Compare(\@unused, $_unused->{unused}),
+ "it returns results from the cache";
+
+ @unused = @{ $annex->unused(used_refspec => "+refs/heads/*") };
+ ok $cache_timestamp < $annex->{_unused}{timestamp},
+ "changing the used_refspec invalidates the cache";
+ sleep 1;
+ ($_unused, $cache_timestamp) = read_cache($annex);
+ write_file catfile(qw(source1 extra)), "extra\n";
+ $source1->add("extra");
+ $source1->commit({ message => "add" });
+ @unused = @{ $annex->unused(used_refspec => "+refs/heads/*") };
+ ok $cache_timestamp < $annex->{_unused}{timestamp},
+ "committing to a branch invalidates the cache";
+ sleep 1;
+ ($_unused, $cache_timestamp) = read_cache($annex);
+ $source1->checkout("git-annex");
+ write_file catfile(qw(source1 extra)), "extra\n";
+ $source1->add("extra");
+ $source1->commit({ message => "add" });
+ $source1->checkout("master");
+ @unused = @{ $annex->unused(used_refspec => "+refs/heads/*") };
+ ok $cache_timestamp == $annex->{_unused}{timestamp},
+ "committing to git-annex branch does not invalidate the cache";
+ ($_unused, $cache_timestamp) = read_cache($annex);
+ $source1->annex("unused");
+ sleep 1;
+ @unused = @{ $annex->unused(used_refspec => "+refs/heads/*") };
+ ok $cache_timestamp < $annex->{_unused}{timestamp},
+ "running `git annex unused` invalidates the cache";
+
+ ($_unused, $cache_timestamp) = read_cache($annex);
+ ok !exists $unused_file{log_lines}, "unused file was not logged";
+ sleep 1;
+ @unused = @{ $annex->unused(used_refspec => "+refs/heads/*", log => 1) };
+ %unused_file = %{ $unused[0] };
+ my $annex_unused = $annex->_git_path(qw(annex unused));
+ my $annex_unused_timestamp = (stat $annex_unused)[9];
+ ok $annex_unused_timestamp <= $cache_timestamp,
+ "asking for logs does not invalidate (most of) the cache";
+ ok grep(/add/, @{ $unused_file{log_lines} }),
+ "log lines contain correct commit message";
+ ($_unused, $cache_timestamp) = read_cache($annex);
+ @unused = @{ $annex->unused(used_refspec => "+refs/heads/*") };
+ ok $cache_timestamp == $annex->{_unused}{timestamp},
+ "turning logs off again does not invalidate the cache";
+ %unused_file = %{ $unused[0] };
+ ok grep(/add/, @{ $unused_file{log_lines} }),
+ "log lines still contain correct commit message";
+};
+
+with_temp_annexes {
+ my (undef, $source1) = @_;
+ corrupt_annexed_file $source1, "foo/foo2/baz";
+ try { $source1->annex("fsck") };
+ my $annex = Git::Annex->new($source1->dir);
+ my @unused = @{ $annex->unused };
+ ok @unused == 1, "there is one unused file";
+ my %unused_file = %{ $unused[0] };
+ ok !$unused_file{tmp}, "unused file is not a tmp file";
+ ok $unused_file{bad}, "unused file is a bad file";
+ ok looks_like_number $unused_file{number}, "unused file has a number";
+ ok $unused_file{key} =~ /^SHA256E-/, "unused file has a key";
+};
+
+# TODO somehow generate some tmp files (.git/annex/tmp) and check
+# those are identified with the 'tmp' hash key
+
+sub read_cache {
+ my $annex = shift;
+ my $cache = retrieve $annex->_unused_cache;
+ my $cache_timestamp = $cache->{timestamp};
+ return ($cache, $cache_timestamp);
+}
+
+done_testing;
diff --git a/t/lib/t/Setup.pm b/t/lib/t/Setup.pm
index 15dd49b..454fa22 100644
--- a/t/lib/t/Setup.pm
+++ b/t/lib/t/Setup.pm
@@ -41,7 +41,7 @@ sub with_temp_annexes (&) {
$source2->RUN(qw(-c annex.addunlocked=false annex add other));
$source2->commit({ message => "add" });
- &{ $_[0] }($temp);
+ &{ $_[0] }($temp, $source1, $source2, $dest);
}
rmtree $temp;
}
diff --git a/t/lib/t/Util.pm b/t/lib/t/Util.pm
new file mode 100644
index 0000000..a69ac4c
--- /dev/null
+++ b/t/lib/t/Util.pm
@@ -0,0 +1,23 @@
+package t::Util;
+
+use 5.028;
+use strict;
+use warnings;
+use parent 'Exporter';
+use File::Slurp;
+use File::Spec::Functions qw(rel2abs);
+
+our @EXPORT = qw( corrupt_annexed_file );
+
+sub corrupt_annexed_file {
+ my ($git, $file) = @_;
+
+ my ($key) = $git->annex("lookupkey", $file);
+ my ($loc) = $git->annex("contentlocation", $key);
+ $loc = rel2abs $loc, $git->dir;
+
+ chmod 0777, $loc;
+ append_file $loc, "bazbaz\n";
+}
+
+1;