File: 26-index.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 (123 lines) | stat: -rw-r--r-- 3,495 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
#!perl

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

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

# in memory
my $index = Git::Raw::Index -> new;
isa_ok $index, 'Git::Raw::Index';

# no repository associated yet
ok(!eval {$index -> write_tree});

my $r = $index -> owner;
is $r, undef;

$r = $repo -> index(undef);
ok(!defined($r));

$repo -> index($index);

$r = $index -> owner;
isa_ok $r, 'Git::Raw::Repository';
is $index -> path, undef;

$index -> clear;
is $index -> entry_count, 0;

ok(!eval {$index -> add_frombuffer('blah', undef)});
is $index -> entry_count, 0;

ok(!eval {$index -> add_frombuffer('invalid-type', 'contents', 'mode-string')});
is $index -> entry_count, 0;
ok(!eval {$index -> add_frombuffer('invalid-mode', 'contents', 1)});
is $index -> entry_count, 0;
ok(!eval {$index -> add_frombuffer('tree-mode', 'contents', 040000)});
is $index -> entry_count, 0;

$index -> add_frombuffer ('d.txt', 'content4');
is $index -> entry_count, 1;
$index -> add_frombuffer ('a/b.txt', 'content2');
is $index -> entry_count, 2;
$index -> add_frombuffer ('a.txt', 'content1');
is $index -> entry_count, 3;
$index -> add_frombuffer ('mode/plain', 'file content', 0100644);
is $index -> entry_count, 4;
$index -> add_frombuffer ('mode/exec', 'echo Example', 0100755);
is $index -> entry_count, 5;
$index -> add_frombuffer ('mode/link', '../a.txt', 0120000);
is $index -> entry_count, 6;

my $content3 = 'content3';
my $blob_id = 'a2b32293aab475bf50798c7642f0fe0593c167f6';
my $entry = $index -> add_frombuffer ('a/b/c.txt', \$content3);
isa_ok $entry, 'Git::Raw::Index::Entry';
is $entry -> id, $blob_id;
is $entry -> path, 'a/b/c.txt';
is $entry -> size, length($content3);
is $entry -> mode, 0100644;
is $entry -> stage, 0;
is $entry -> blob, $blob_id;
is $entry -> blob -> content, $content3;

$index -> add($entry);

$index -> write_tree;

my $tree = $index -> write_tree($repo);

my @entries = $tree -> entries;
is scalar(@entries), 4;

is $entries[0] -> name, 'a.txt';
is $entries[1] -> name, 'a';
is $entries[2] -> name, 'd.txt';
is $entries[3] -> name, 'mode';

is $entries[0] -> file_mode, 0100644;
is $entries[2] -> file_mode, 0100644;

isa_ok $entries[0] -> object, 'Git::Raw::Blob';
isa_ok $entries[1] -> object, 'Git::Raw::Tree';
isa_ok $entries[2] -> object, 'Git::Raw::Blob';
isa_ok $entries[3] -> object, 'Git::Raw::Tree';
is $entries[0] -> object -> content, 'content1';
is $entries[2] -> object -> content, 'content4';

my @entries2 = $entries[1] -> object -> entries;
is scalar(@entries2), 2;

is $entries2[0] -> name, 'b.txt';
is $entries2[1] -> name, 'b';
is $entries2[0] -> object -> content, 'content2';

my @entries3 = $entries2[1] -> object -> entries;
is scalar(@entries3), 1;

is $entries3[0] -> name, 'c.txt';
is $entries3[0] -> object -> content, 'content3';

my @entries4 = $entries[3] -> object -> entries;
is scalar(@entries4), 3;

is $entries4[0] -> name, 'exec';
is $entries4[1] -> name, 'link';
is $entries4[2] -> name, 'plain';

is $entries4[0] -> file_mode, 0100755;
is $entries4[1] -> file_mode, 0120000;
is $entries4[2] -> file_mode, 0100644;

isa_ok $entries4[0] -> object, 'Git::Raw::Blob';
isa_ok $entries4[1] -> object, 'Git::Raw::Blob';
isa_ok $entries4[2] -> object, 'Git::Raw::Blob';

is $entries4[0] -> object -> content, 'echo Example';
is $entries4[1] -> object -> content, '../a.txt';
is $entries4[2] -> object -> content, 'file content';

done_testing;