Debian Codegraph @

t/snapshot-test2.t

raw
1 use Test2::V0;
2 use Test::Snapshot;
3 use File::Temp qw/ tempfile tempdir /;
4 use Capture::Tiny qw(capture);
5 use App::Prove;
6
7 sub tempcopy {
8 my ($text, $dir) = @_;
9 my ($tfh, $filename) = tempfile( DIR => $dir );
10 print $tfh $text;
11 close $tfh;
12 $filename;
13 }
14
15 $ENV{TEST_SNAPSHOT_UPDATE} = 0; # override to ensure known value
16
17 my $dir = tempdir( CLEANUP => 1 );
18 my $filename = tempcopy(<<'EOF', $dir);
19 use Test2::V0;
20 use Test::Snapshot;
21
22 subtest 'subtestname' => sub {
23 is_deeply_snapshot 'just some text', 'subtest desc';
24 };
25
26 is_deeply_snapshot { message => 'output' }, 'desc';
27
28 done_testing;
29 EOF
30
31 sub do_test {
32 my ($filename, $update, $expect, $description) = @_;
33 my ($out, $err, $exit) = capture {
34 local $ENV{TEST_SNAPSHOT_UPDATE} = $update;
35 my $app = App::Prove->new;
36 $app->process_args(qw(-b), $filename);
37 $app->run ? 0 : 1;
38 };
39 is $exit, $expect, $description
40 or diag 'Output was: ', $out, 'Error was: ', $err;
41 }
42
43 do_test($filename, '', 1, 'fails first time');
44 do_test($filename, 1, 1, 'fails second time, snapshots were not created');
45 do_test($filename, 1, 0, 'works third time, snapshots were created');
46
47 done_testing;