App-FileSync

 view release on metacpan or  search on metacpan

t/snapshot.t  view on Meta::CPAN

	print {$fh} $content;
	close $fh;

	utime($mtime, $mtime, $path) if defined $mtime;

	return $path;
}

# Replace a file the way the syncer does (temp + rename), which breaks any
# hardlink rather than writing through it.
sub replace_file {
	my ($path, $content, $mtime) = @_;

	my $temp = "$path.tmp-$$";
	open my $fh, '>', $temp or die "Cannot write $temp: $!";
	binmode $fh;
	print {$fh} $content;
	close $fh;

	utime($mtime, $mtime, $temp) if defined $mtime;
	rename($temp, $path) or die "Cannot rename $temp to $path: $!";

	return $path;
}

sub read_file {
	my ($path) = @_;
	open my $fh, '<', $path or die "Cannot read $path: $!";
	binmode $fh;
	local $/;
	my $content = <$fh>;
	close $fh;
	return $content;
}

# ------------------------------------------------------- the snapshot module
{
	my $root = tempdir(CLEANUP => 1);

	write_file("$root/a.txt", "alpha\n", $T0);
	write_file("$root/sub/b.txt", "bravo\n", $T0 + 1);
	write_file("$root/.filesync-root", "marker\n", $T0);
	make_path("$root/empty");

	# Hardlinks are the Unix default; Windows uses copies (SPEC 6.3).
	my $mode  = App::FileSync::Snapshot::default_mode();
	my $first = App::FileSync::Snapshot::create(
		root => $root, date => '2026-10-20', mode => $mode,
	);

	is($first->{created}, 1, 'a snapshot is created');
	is($first->{files}, 2, 'both files are in the snapshot');
	is($first->{linked}, $mode eq 'hardlink' ? 2 : 0,
		'files are hardlinked, not copied, when that is the platform default');
	is($first->{copied}, $mode eq 'copy' ? 2 : 0,
		'files are copied when copying is the platform default');
	SKIP: {
		skip 'inode numbers are not meaningful here', 1
			unless $mode eq 'hardlink' && $^O ne 'MSWin32';

		is((stat("$root/a.txt"))[1], (stat("$first->{path}/a.txt"))[1],
			'the snapshot file shares the inode with the mirror');
	}
	ok(-d "$first->{path}/empty", 'empty directories are captured');
	ok(!-e "$first->{path}/.filesync-root", 'the marker is not part of the snapshot');
	ok(!-e "$first->{path}/.filesync-snapshots", 'the snapshot directory does not contain itself');
	is(read_file("$first->{path}/sub/b.txt"), "bravo\n", 'content is captured');

	my $same = App::FileSync::Snapshot::create(
		root => $root, date => '2026-10-20', mode => $mode,
	);
	is($same->{created}, 0, 'an existing day is never overwritten');
	ok(-e "$first->{path}/a.txt", 'the first snapshot of the day survives');

	App::FileSync::Snapshot::create(root => $root, date => '2026-10-18');
	App::FileSync::Snapshot::create(root => $root, date => '2026-10-19');

	my $list = App::FileSync::Snapshot::list(root => $root);
	is_deeply(
		[ map { $_->{date} } @{$list} ],
		[ '2026-10-20', '2026-10-19', '2026-10-18' ],
		'list is newest first',
	);
	is($list->[0]{files}, 2, 'the listing counts files');

	my $pruned = App::FileSync::Snapshot::prune(root => $root, keep => 2);
	is_deeply($pruned->{removed}, ['2026-10-18'], 'the oldest snapshot is pruned');
	ok(!-d "$root/.filesync-snapshots/2026-10-18", 'it is gone from disk');
	is(scalar @{ App::FileSync::Snapshot::list(root => $root) }, 2, 'two snapshots are kept');

	# Restoring a single path replaces what is there now.
	replace_file("$root/a.txt", "corrupted\n", $T0 + 500);
	my $restored = App::FileSync::Snapshot::restore(
		root => $root, date => '2026-10-20', paths => ['a.txt'], to => $root,
	);
	is($restored->{files}, 1, 'one file restored');
	is(read_file("$root/a.txt"), "alpha\n", 'the old content is back');
	is((stat("$root/a.txt"))[9], $T0, 'mtime is preserved by the restore');

	# A copy-mode snapshot does not share inodes.
	my $copy = App::FileSync::Snapshot::create(
		root => $root, date => '2026-10-21', mode => 'copy',
	);
	is($copy->{copied}, 2, 'copy mode copies');
	SKIP: {
		skip 'inode numbers are not meaningful here', 1 if $^O eq 'MSWin32';

		isnt((stat("$root/a.txt"))[1], (stat("$copy->{path}/a.txt"))[1],
			'copy mode does not share inodes');
	}
	is(read_file("$copy->{path}/a.txt"), "alpha\n", 'copy mode captured the content');

	# A whole snapshot can be exported somewhere else.
	my $export = tempdir(CLEANUP => 1);
	my $whole = App::FileSync::Snapshot::restore(
		root => $root, date => '2026-10-20', to => $export,
	);
	is($whole->{files}, 2, 'the whole snapshot was restored');
	is(read_file("$export/sub/b.txt"), "bravo\n", 'nested content exported');
	ok(-d "$export/empty", 'empty directories are restored');

	# A hardlinked snapshot shares the inode, so an in-place edit of the mirror
	# is visible in it; copy mode is independent.  The syncer itself always
	# replaces files, so this only bites when something else edits the mirror.
	SKIP: {
		skip 'hardlinks are not the default here', 2
			unless App::FileSync::Snapshot::default_mode() eq 'hardlink';

		my $linked = App::FileSync::Snapshot::create(
			root => $root, date => '2026-10-22', mode => 'hardlink',
		);
		write_file("$root/a.txt", "edited in place\n", $T0 + 600);
		is(read_file("$linked->{path}/a.txt"), "edited in place\n",
			'in-place edits show through a hardlinked snapshot (documented caveat)');
		is(read_file("$copy->{path}/a.txt"), "alpha\n",
			'a copy-mode snapshot is unaffected by in-place edits');
	}
}

# ------------------------------------------------- a failed build rolls back
# create() builds in .filesync-snapshots/.tmp-PID-N and renames it to the date
# only when the whole tree is in; a failure part way must leave neither the
# temporary directory nor the dated snapshot.
{
	my $leftovers = sub {
		my ($root) = @_;
		my $dir = App::FileSync::Snapshot::snapshot_root($root);
		opendir my $dh, $dir or return [];
		my @names = sort grep { !/\A\.\.?\z/ } readdir $dh;
		closedir $dh;
		return \@names;
	};

	# An error that is already catalogued (a directory that cannot be read)
	# passes through unchanged.  Test::Permissions finds out whether chmod
	# can really take the access away here (not as root, not on Windows or a
	# filesystem that ignores modes), and puts the mode back afterwards.
	my $root = tempdir(CLEANUP => 1);
	SKIP: {
		skip_unless_can_revoke('search', 5, $root);

		write_file("$root/a.txt", "alpha\n", $T0);
		write_file("$root/b.txt", "bravo\n", $T0);
		write_file("$root/locked/c.txt", "charlie\n", $T0);

		with_revoked(search => "$root/locked", sub {
			throws_ok { App::FileSync::Snapshot::create(root => $root, date => '2026-10-24', mode => 'copy') }
				qr/\ASnapshot: cannot read \Q$root\E\/locked: /, 'an unreadable directory stops the build';



( run in 1.830 second using v1.01-cache-2.11-cpan-eb134014566 )