File: 11-stash.t

package info (click to toggle)
libgit-raw-perl 0.90%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,540 kB
  • sloc: perl: 5,432; ansic: 182; makefile: 6
file content (165 lines) | stat: -rw-r--r-- 4,236 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!perl

use Test::More;

use Git::Raw;
use File::Spec::Functions qw(catfile rel2abs);
use File::Slurp::Tiny qw(write_file);

is 0, Git::Raw::Stash::Progress->NONE;
is 1, Git::Raw::Stash::Progress->LOADING_STASH;
is 2, Git::Raw::Stash::Progress->ANALYZE_INDEX;
is 3, Git::Raw::Stash::Progress->ANALYZE_MODIFIED;
is 4, Git::Raw::Stash::Progress->ANALYZE_UNTRACKED;
is 5, Git::Raw::Stash::Progress->CHECKOUT_UNTRACKED;
is 6, Git::Raw::Stash::Progress->CHECKOUT_MODIFIED;
is 7, Git::Raw::Stash::Progress->DONE;

my $path = rel2abs(catfile('t', 'test_repo'));
my $repo = Git::Raw::Repository -> open($path);

my $file  = $repo -> workdir . 'test';
write_file($file, 'this is a test for stash');

my $index = $repo -> index;
$index -> add ('test');
write_file($file, 'this is a test for stash2');

my $config = $repo -> config;
my $name   = $config -> str('user.name');

my $email  = $config -> str('user.email');

my $me = Git::Raw::Signature -> now($name, $email);

is_deeply $repo -> status({}) -> {'test'}, {'flags' => ['index_modified', 'worktree_modified']};
ok ($repo -> stash($me, 'some stash'));
is_deeply $repo -> status({}), {};

eval { Git::Raw::Stash -> foreach($repo, sub { 1; }) };
ok (!$@);

Git::Raw::Stash -> foreach($repo, sub {
	my ($i, $msg, $commit) = @_;

	is $i, 0;
	is $msg, 'On main: some stash';
	isa_ok $commit, 'Git::Raw::Commit';

	0;
});

is $repo -> stash($me, 'some stash'), undef;

my $count = 0;
my $apply_opts = {
	callbacks => {
		apply_progress => sub {
			my ($progress) = @_;
			$count++;
		}
	},
	flags => {
		reinstate_index => 1
	}
};
Git::Raw::Stash -> apply($repo, 0, $apply_opts);

write_file($file, 'this is a test for stash3');

is $count, 5;
is_deeply $repo -> status({}) -> {'test'}, {'flags' => ['index_modified', 'worktree_modified']};
ok ($repo -> stash($me, 'some stash'));
is_deeply $repo -> status({}), {};

$count = 0;
Git::Raw::Stash -> pop($repo, 0, $apply_opts);

is $count, 5;
is_deeply $repo -> status({}) -> {'test'}, {'flags' => ['index_modified', 'worktree_modified']};
ok ($repo -> stash($me, 'some stash'));
is_deeply $repo -> status({}), {};

$count = 0;
Git::Raw::Stash -> foreach($repo, sub { $count++ });
is $count, 2;

Git::Raw::Stash -> drop($repo, 0);
Git::Raw::Stash -> drop($repo, 0);

Git::Raw::Stash -> foreach($repo, sub { die 'This should not be called' });

my $untracked_file  = $repo -> workdir . 'untracked';
write_file($untracked_file, 'this is an untracked file');

ok (!eval { $repo -> stash($me, 'stash bad', ['blah']) });

my $commit = $repo -> stash($me, 'stash untracked files', [undef, 'include_untracked']);
isa_ok $commit, 'Git::Raw::Commit';

my @stashes;
Git::Raw::Stash -> foreach($repo, sub {
	my ($index, $msg, $commit) = @_;
	push @stashes, { 'index' => $index, 'msg' => $msg, 'commit' => $commit };

	0;
});

is scalar(@stashes), 1;
is $stashes[0] -> {'msg'}, 'On main: stash untracked files';
ok (! -f $untracked_file);

write_file($untracked_file, 'this is an untracked file');
$index = $repo -> index;
$index -> add('untracked');
$index -> write;

$repo -> stash($me, 'dont stash indexed files', ['keep_index']);

@stashes = ();
Git::Raw::Stash -> foreach($repo, sub {
	my ($index, $msg, $commit) = @_;
	push @stashes, { 'index' => $index, 'msg' => $msg, 'commit' => $commit };

	0;
});

is scalar(@stashes), 2;
is $stashes[0] -> {'msg'}, 'On main: dont stash indexed files';
ok (-f $untracked_file);

$repo -> ignore("ignored\n");
my $ignored_file = $repo -> workdir . 'ignored';
write_file($ignored_file, 'this is an ignored file');

$repo -> stash($me, 'stash ignored files', ['include_ignored']);

@stashes = ();
Git::Raw::Stash -> foreach($repo, sub {
	my ($index, $msg, $id) = @_;
	push @stashes, { 'index' => $index, 'msg' => $msg, 'commit' => $commit };

	0;
});

is scalar(@stashes), 3;
is $stashes[0] -> {'msg'}, 'On main: stash ignored files';
ok (! -f $untracked_file);
ok (! -f $ignored_file);

@stashes = ();
Git::Raw::Stash -> foreach($repo, sub {
	my ($index, $msg, $id) = @_;
	push @stashes, { 'index' => $index, 'msg' => $msg, 'commit' => $commit };

	return 1 if (scalar(@stashes));
	0;
});

is scalar(@stashes), 1;

@stashes = ();
eval { Git::Raw::Stash -> foreach($repo, sub { die "Bad" }) };
ok ($@);

done_testing;