App-FileSync

 view release on metacpan or  search on metacpan

t/settings.t  view on Meta::CPAN

	unlike($after, qr/quiet warning/, '--quiet suppresses warnings');
	like($after, qr/^ERROR: quiet error$/m, '--quiet keeps errors');
}

# ------------------------------------------------ the client uses the logger
{
	my $env = TestSync->new_env();
	$env->write_file('client', 'a.txt', "alpha\n", 1_700_000_000);

	my @captured;
	my $logger = Log::Abstraction->new(level => 'debug', logger => \@captured);

	my $config = App::FileSync::Config::load_client($env->{config_path});
	my $result = App::FileSync::Client->new(
		config => $config,
		name   => 'docs',
		logger => $logger,
	)->sync();

	is($result->{summary}{pushed}, 1, 'the sync still works with an injected logger');

	my @messages = map { $_->{message} } @captured;
	ok((grep { $_ eq 'plan received' } @messages), 'the plan is logged');
	ok((grep { $_ eq 'sync finished' } @messages), 'the completed run is logged');

	my ($finished) = grep { $_->{message} eq 'sync finished' } @captured;
	is($finished->{level}, 'info', 'a completed run is logged at info');
	is($finished->{fields}{repo}, 'docs', 'the log entry names the repo');
	is($finished->{fields}{pushed}, 1, 'the log entry carries the counts');

	my ($plan) = grep { $_->{message} eq 'plan received' } @captured;
	is($plan->{level}, 'debug', 'the plan is logged at debug');
	is($plan->{fields}{actions}, 1, 'the plan entry counts the actions');
}

# ----------------------------------- the settings command
{
	my $dir = tempdir(CLEANUP => 1);
	my $settings = write_json(
		File::Spec->catfile($dir, 'app-filesync.json'),
		qq({"App__FileSync":{"ssh_identity":"/home/me/.ssh/id_filesync"}}\n),
	);

	local $ENV{FILESYNC_SETTINGS} = $settings;

	# The repos come from the client config: one overrides the identity, one
	# inherits everything, one uses the local transport.
	my $client_config = write_json(
		File::Spec->catfile($dir, 'config.json'),
		qq({"repos":{"docs":{"transport":"ssh","server":"srv","identity":"/home/me/.ssh/docs_key"},"mirror":{"transport":"ssh","server":"srv","port":2222,"host_key_checking":"accept-new"},"localdocs":{"transport":"local","server_root":"/srv/filesync"}}}\n)...
	);
	local $ENV{FILESYNC_CONFIG} = $client_config;

	my $output = `$^X $filesync settings 2>&1`;
	is($?, 0, 'filesync settings exits 0');
	like($output, qr/^settings file: \Q$settings\E$/m, 'it names the settings file');
	like($output, qr/^\s+ssh_identity\s+\/home\/me\/\.ssh\/id_filesync\s+file \Q$settings\E \[App__FileSync\]$/m,
		'it shows an SSH default and where it came from');
	like($output, qr/^\s+server_command\s+filesync-server\s+built-in default$/m,
		'it shows the transport default for the remote program');
	like($output, qr/^\s+log_file\s+\(stderr\)/m, 'an empty log_file is shown as stderr');

	like($output, qr/^repos:$/m, 'the repos are listed');
	like($output, qr/^  docs \(ssh, srv\)$/m, 'a repo names its transport and server');
	like($output, qr/^    ssh_identity\s+\/home\/me\/\.ssh\/docs_key\s+repo \(\Q$client_config\E \[docs\]\)$/m,
		'a repo value says the repo supplied it');
	like($output, qr/^    ssh_known_hosts\s+\(ssh default\)\s+ssh default$/m,
		'an unset value says ssh supplied it');
	like($output, qr/^    server_command\s+filesync-server\s+process \(built-in default\)$/m,
		'an inherited value says the process settings supplied it');
	like($output, qr/^    port\s+22\s+ssh default \[repo only\]$/m,
		'an unset port still says the key is repo only');
	like($output, qr/^  mirror \(ssh, srv\)$/m, 'the second SSH repo is listed');
	like($output, qr/^    port\s+2222\s+repo \(\Q$client_config\E \[mirror\]\) \[repo only\]$/m,
		'a repo port is marked repo only');
	like($output, qr/^    host_key_checking\s+accept-new\s+repo \(\Q$client_config\E \[mirror\]\) \[repo only\]$/m,
		'a repo host-key policy is marked repo only');
	like($output, qr/local transport does not use the SSH settings/,
		'a local repo has no SSH rows');

	$output = `$^X $filesync settings --json 2>&1`;
	is($?, 0, 'filesync settings --json exits 0');
	my $decoded = eval { App::FileSync::json_decode($output) };
	ok(ref($decoded) eq 'HASH', 'the JSON parses');
	is($decoded->{settings_file}, $settings, 'the JSON names the settings file');
	is(scalar @{ $decoded->{settings} }, 6, 'the JSON has a row per setting');
	my ($identity_row) = grep { $_->{key} eq 'ssh_identity' } @{ $decoded->{settings} };
	is($identity_row->{value}, '/home/me/.ssh/id_filesync', 'the JSON carries the value');
	is($identity_row->{source}, 'file', 'the JSON carries the source kind');

	is($decoded->{config_file}, $client_config, 'the JSON names the client config');
	is(scalar @{ $decoded->{repos} }, 3, 'the JSON has a row per repo');
	my ($docs) = grep { $_->{repo} eq 'docs' } @{ $decoded->{repos} };
	my %repo_ssh = map { $_->{key} => $_ } @{ $docs->{ssh} };
	is($repo_ssh{ssh_identity}{value}, '/home/me/.ssh/docs_key',
		'the JSON carries the repo override');
	is($repo_ssh{ssh_identity}{source}, 'repo', 'with its source');
	is($repo_ssh{ssh_identity}{label}, "$client_config [docs]", 'and label');
	is($repo_ssh{ssh_known_hosts}{source}, 'default',
		'an unset key is a transport default in the JSON');
	is($repo_ssh{server_command}{value}, 'filesync-server',
		'the inherited remote program is effective');
	is($repo_ssh{server_command}{source}, 'process', 'and attributed to the settings');
	is($repo_ssh{port}{value}, 22, 'an unset port is effective in the JSON');
	ok($repo_ssh{port}{repo_only}, 'and still marks the key repo only');

	my ($mirror) = grep { $_->{repo} eq 'mirror' } @{ $decoded->{repos} };
	my %mirror_ssh = map { $_->{key} => $_ } @{ $mirror->{ssh} };
	is($mirror_ssh{port}{value}, 2222, 'the JSON carries the repo port');
	ok($mirror_ssh{port}{repo_only}, 'marked repo only');
	is($mirror_ssh{host_key_checking}{value}, 'accept-new',
		'the JSON carries the repo host-key policy');
	ok($mirror_ssh{host_key_checking}{repo_only}, 'marked repo only');

	my ($local) = grep { $_->{repo} eq 'localdocs' } @{ $decoded->{repos} };
	is($local->{ssh}, undef, 'the local repo has no SSH rows in the JSON');

	$output = `$^X $filesync settings extra 2>&1`;
	isnt($?, 0, 'a stray argument is an error');
	like($output, qr/settings: unexpected arguments: extra/, 'and names it');
}



( run in 1.299 second using v1.01-cache-2.11-cpan-60884a8df76 )