basic tests for snapshot handling
[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
18 do {
19   ok(my $fh = $tree->open('>', 'foo/bar.txt', mkpath => 1), 'open file');
20   print $fh "baz";
21   close $fh;
22 };
23
24 my ($file) = $tree->find_files('txt', 'foo');
25 ok -e $file, 'written file exists';
26
27 ok $tree->commit, 'commit';
28 ok $tree->reset, 'reset';
29 ok -e $file, 'file still exists';
30
31 do {
32   ok(my $fh = $tree->open('>', 'foo/bar.txt', mkpath => 1), 'open file again');
33   print $fh "qux";
34   close $fh;
35 };
36
37 do {
38   ok(my $fh = $tree->open('>', 'foo/baz.txt', mkpath => 1), 'open other file');
39   print $fh "qux";
40   close $fh;
41 };
42
43 ok $tree->reset, 'reset before commit';
44 ok -e $tree->file('foo/bar.txt'), 'original file still exists';
45 ok not(-e $tree->file('foo/baz.txt')), 'new file no longer exists';
46
47 do {
48   my $fh = $tree->open('<', 'foo/bar.txt');
49   my $body = do { local $/; <$fh> };
50   is $body, 'baz', 'reset to original content';
51 };
52
53 rmtree $tree_path;
54
55 done_testing;