add an empty .gitignore to a newly created snapshot
[scpubgit/File-Tree-Snapshot.git] / t / basic.t
1 use strictures 1;
2 use Test::More;
3
4 use File::Tree::Snapshot;
5 use File::Path qw( rmtree );
6 use FindBin;
7
8 my $tree_path = "$FindBin::Bin/test-tree";
9
10 my $tree = File::Tree::Snapshot->new(
11     storage_path    => $tree_path,
12 );
13
14 ok not($tree->exists), 'tree doesnt exist yet';
15 ok $tree->create, 'tree creation successful';
16 ok $tree->exists, 'tree does now exit';
17 ok(-e "$tree_path/.gitignore", 'created .gitignore');
18
19 do {
20   ok(my $fh = $tree->open('>', 'foo/bar.txt', mkpath => 1), 'open file');
21   print $fh "baz";
22   close $fh;
23 };
24
25 my ($file) = $tree->find_files('txt', 'foo');
26 ok -e $file, 'written file exists';
27
28 ok $tree->commit, 'commit';
29 ok $tree->reset, 'reset';
30 ok -e $file, 'file still exists';
31
32 do {
33   ok(my $fh = $tree->open('>', 'foo/bar.txt', mkpath => 1), 'open file again');
34   print $fh "qux";
35   close $fh;
36 };
37
38 do {
39   ok(my $fh = $tree->open('>', 'foo/baz.txt', mkpath => 1), 'open other file');
40   print $fh "qux";
41   close $fh;
42 };
43
44 ok $tree->reset, 'reset before commit';
45 ok -e $tree->file('foo/bar.txt'), 'original file still exists';
46 ok not(-e $tree->file('foo/baz.txt')), 'new file no longer exists';
47
48 do {
49   my $fh = $tree->open('<', 'foo/bar.txt');
50   my $body = do { local $/; <$fh> };
51   is $body, 'baz', 'reset to original content';
52 };
53
54 rmtree $tree_path;
55
56 done_testing;