File: 14-tree-builder.t

package info (click to toggle)
libgit-raw-perl 0.79-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,448 kB
  • sloc: perl: 5,267; ansic: 182; makefile: 6
file content (119 lines) | stat: -rw-r--r-- 2,891 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
#!perl

use strict;
use warnings;

use Test::More;
use Git::Raw;
use File::Spec::Functions qw(catfile rel2abs);

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

my $empty_blob = $repo -> blob('');
my $builder    = Git::Raw::Tree::Builder -> new($repo);

is $builder -> entry_count, 0;

my $entry = $builder -> insert('one.txt', $empty_blob, 0100644);
$builder -> insert('two.txt', $empty_blob, 0100644);
$builder -> insert('three.txt', $empty_blob, 0100644);

isa_ok $entry, 'Git::Raw::Tree::Entry';
is $entry -> name, 'one.txt';

is $builder -> entry_count, 3;

$entry = $builder -> get('one.txt');
is $entry -> name, 'one.txt';
is $entry -> object -> id(), $empty_blob -> id ();

$entry = $builder -> get('two.txt');
is $entry -> name, 'two.txt';
is $entry -> object -> id(), $empty_blob -> id ();

$entry = $builder -> get('three.txt');
is $entry -> name, 'three.txt';
is $entry -> object -> id(), $empty_blob -> id ();

$entry = $builder -> get('four.txt');
ok !defined($entry);

my $tree = $builder -> write();

isa_ok $tree, 'Git::Raw::Tree';
is $tree -> id(), '228c738569c82d9906ea1801f698a7c2a70e56b1';
ok ('228c738569c82d9906ea1801f698a7c2a70e56b1' eq $tree);
ok ($tree eq $tree);
ok ($tree eq $tree -> id);
ok ($tree -> id eq $tree);
ok ($tree ne $builder);
is $tree cmp $tree->id, 0, "cmp";

my @entries = $tree -> entries();

is_deeply [ sort map { $_ -> name() } @entries ],
	[ qw/one.txt three.txt two.txt/ ];

is_deeply [ map { $_ -> object -> id() } @entries ],
	[ map { $empty_blob -> id() } (1..3) ];

$builder -> clear();

is $builder -> entry_count, 0;

$builder = Git::Raw::Tree::Builder -> new($repo, $tree);

is $builder -> entry_count, 3;

$builder -> remove('two.txt');

is $builder -> entry_count, 2;

$tree    = $builder -> write();
@entries = $tree -> entries();

is_deeply [ sort map { $_ -> name() } @entries ],
	[ qw/one.txt three.txt/ ];

is_deeply [ map { $_ -> object -> id() } @entries ],
	[ map { $empty_blob -> id() } (1..2) ];

eval {
	$builder -> insert('four.txt', $empty_blob, 0100754);
	fail q{Should've raised an error!};
} or do {
	like $@, qr/failed to insert entry/;
};

eval {
	$builder -> insert('four/five.txt', $empty_blob, 0100755);
	fail q{Should've raised an error!};
} or do {
	like $@, qr/failed to insert entry/;
};

eval {
	$builder -> remove('two.txt');
	fail q{Should've raised an error!};
} or do {
	like $@, qr/failed to remove entry/;
};

my $subtree = $tree;

$builder -> insert('subdir', $subtree, 0040000);
is $builder -> entry_count, 3;

$tree    = $builder -> write();
@entries = $tree -> entries();

@entries = sort { $a -> name() cmp $b -> name() } @entries;

is_deeply [ map { $_ -> name() } @entries ],
	[ qw/one.txt subdir three.txt/ ];

is_deeply [ map { $_ -> object -> id() } @entries ],
	[ $empty_blob -> id(), $subtree -> id(), $empty_blob -> id() ];

done_testing;