(no commit message)
[ikiwiki] / t / git.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 my $dir;
6 BEGIN {
7         $dir="/tmp/ikiwiki-test-git.$$";
8         my $git=`which git`;
9         chomp $git;
10         if (! -x $git) {
11                 eval q{
12                         use Test::More skip_all => "git not available"
13                 }
14         }
15         if (! mkdir($dir)) {
16                 die $@;
17         }
18 }
19 use Test::More tests => 26;
20
21 BEGIN { use_ok("IkiWiki"); }
22
23 %config=IkiWiki::defaultconfig();
24 $config{rcs} = "git";
25 $config{srcdir} = "$dir/src";
26 $config{diffurl} = '/nonexistent/cgit/plain/[[file]]';
27 IkiWiki::loadplugins();
28 IkiWiki::checkconfig();
29
30 my $makerepo;
31 if ($ENV{INSTALLED_TESTS}) {
32         $makerepo = "ikiwiki-makerepo";
33 }
34 else {
35         $makerepo = "./ikiwiki-makerepo";
36 }
37
38 ok (mkdir($config{srcdir}));
39 is (system("$makerepo git $config{srcdir} $dir/repo"), 0);
40
41 my @changes;
42 @changes = IkiWiki::rcs_recentchanges(3);
43
44 is($#changes, 0); # counts for dummy commit during repo creation
45 # ikiwiki-makerepo's first commit is setting up the .gitignore
46 is($changes[0]{message}[0]{"line"}, "initial commit");
47 is($changes[0]{pages}[0]{"page"}, ".gitignore");
48
49 # Web commit
50 my $test1 = readfile("t/test1.mdwn");
51 writefile('test1.mdwn', $config{srcdir}, $test1);
52 IkiWiki::rcs_add("test1.mdwn");
53 IkiWiki::rcs_commit(
54         file => "test1.mdwn",
55         message => "Added the first page",
56         token => "moo",
57 );
58
59 @changes = IkiWiki::rcs_recentchanges(3);
60
61 is($#changes, 1);
62 is($changes[0]{message}[0]{"line"}, "Added the first page");
63 is($changes[0]{pages}[0]{"page"}, "test1");
64         
65 # Manual commit
66 my $message = "Added the second page";
67
68 my $test2 = readfile("t/test2.mdwn");
69 writefile('test2.mdwn', $config{srcdir}, $test2);
70 system "cd $config{srcdir}; git add test2.mdwn >/dev/null 2>&1";
71 system "cd $config{srcdir}; git commit -m \"$message\" test2.mdwn >/dev/null 2>&1";
72 system "cd $config{srcdir}; git push origin >/dev/null 2>&1";
73
74 @changes = IkiWiki::rcs_recentchanges(3);
75
76 is($#changes, 2);
77 is($changes[0]{message}[0]{"line"}, $message);
78 is($changes[0]{pages}[0]{"page"}, "test2");
79
80 is($changes[1]{pages}[0]{"page"}, "test1");
81
82 # Renaming
83
84 writefile('test3.mdwn', $config{srcdir}, $test1);
85 IkiWiki::rcs_add("test3.mdwn");
86 IkiWiki::rcs_rename("test3.mdwn", "test4.mdwn");
87 IkiWiki::rcs_commit_staged(message => "Added the 4th page");
88
89 @changes = IkiWiki::rcs_recentchanges(4);
90
91 is($#changes, 3);
92 is($changes[0]{pages}[0]{"page"}, "test4");
93
94 ok(mkdir($config{srcdir}."/newdir"));
95 IkiWiki::rcs_rename("test4.mdwn", "newdir/test5.mdwn");
96 IkiWiki::rcs_commit_staged(message => "Added the 5th page");
97
98 @changes = IkiWiki::rcs_recentchanges(4);
99
100 is($#changes, 3);
101 is($changes[0]{pages}[0]{"page"}, "newdir/test5");
102
103 IkiWiki::rcs_remove("newdir/test5.mdwn");
104 IkiWiki::rcs_commit_staged(message => "Remove the 5th page");
105
106 # diffurl escaping
107 ok(mkdir($config{srcdir}."/diffurl_dir"));
108 my $test3 = readfile("t/test1.mdwn");
109 writefile('test3.mdwn', $config{srcdir}."/diffurl_dir", $test3);
110 IkiWiki::rcs_add("diffurl_dir/test3.mdwn");
111 IkiWiki::rcs_commit(
112         file => "diffurl_dir/test3.mdwn",
113         message => "Added a page in diffurl_dir",
114         token => "moo",
115 );
116
117 @changes = IkiWiki::rcs_recentchanges(5);
118
119 is($#changes, 4);
120 is($changes[0]{pages}[0]{"page"}, "diffurl_dir/test3");
121
122 unlike(
123         $changes[0]{pages}[0]{"diffurl"},
124         qr{%2F}m,
125         q{path separators are preserved when UTF-8scaping filename}
126 );
127
128 # do a clean checkout to verify that "empty ident not allowed" is avoided
129 ok(! system("rm", "-rf", $config{srcdir}));
130 ok(! system("git", "clone", "$dir/repo", $config{srcdir}));
131
132 writefile('unconfigured_author.mdwn', $config{srcdir}, 'I am an unconfigured git author');
133 IkiWiki::rcs_add("unconfigured_author.mdwn");
134 IkiWiki::rcs_commit(
135         file => "unconfigured_author.mdwn",
136         message => "hello, world",
137         token => "moo",
138 );
139
140 @changes = IkiWiki::rcs_recentchanges(6);
141
142 is($#changes, 5);
143 is($changes[0]{pages}[0]{"page"}, "unconfigured_author");
144
145 system "rm -rf $dir";