|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use v5.12.5; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +use Test::More; |
| 7 | + |
| 8 | +our $VERSION = '9999.99.99_99'; # VERSION |
| 9 | +use autodie; |
| 10 | + |
| 11 | +use File::Find; |
| 12 | +use File::Temp qw(tempdir); |
| 13 | +use Rex::Task; |
| 14 | +use Rex::Commands::Sync; |
| 15 | +use Test::Deep; |
| 16 | + |
| 17 | +subtest 'should sync_up' => sub { |
| 18 | + my $source = 't/sync'; |
| 19 | + my $target = prepare_directory(); |
| 20 | + |
| 21 | + run_task( |
| 22 | + sub { |
| 23 | + sync_up $source, $target; |
| 24 | + } |
| 25 | + ); |
| 26 | + |
| 27 | + compare_contents( $source, $target ); |
| 28 | +}; |
| 29 | + |
| 30 | +subtest 'should sync_up with excludes' => sub { |
| 31 | + my $source = 't/sync'; |
| 32 | + my $target = prepare_directory(); |
| 33 | + |
| 34 | + # NOTE: file4 should not be excluded, as it is nested |
| 35 | + run_task( |
| 36 | + sub { |
| 37 | + sync_up $source, $target, |
| 38 | + { exclude => [ 'dir/file2', 'file4', 'dir with spaces' ] }; |
| 39 | + } |
| 40 | + ); |
| 41 | + |
| 42 | + compare_contents( $source, $target, |
| 43 | + [ '/dir/file2', '/dir with spaces', '/dir with spaces/file3' ] ); |
| 44 | +}; |
| 45 | + |
| 46 | +subtest 'should sync_down' => sub { |
| 47 | + my $source = 't/sync'; |
| 48 | + my $target = prepare_directory(); |
| 49 | + |
| 50 | + run_task( |
| 51 | + sub { |
| 52 | + sync_down $source, $target; |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + compare_contents( $source, $target ); |
| 57 | +}; |
| 58 | + |
| 59 | +subtest 'should sync_down with excludes' => sub { |
| 60 | + my $source = 't/sync'; |
| 61 | + my $target = prepare_directory(); |
| 62 | + |
| 63 | + # NOTE: file4 should not be excluded, as it is nested |
| 64 | + run_task( |
| 65 | + sub { |
| 66 | + sync_down $source, $target, |
| 67 | + { exclude => [ 'dir/file2', 'file4', 'dir with spaces' ] }; |
| 68 | + } |
| 69 | + ); |
| 70 | + |
| 71 | + compare_contents( $source, $target, |
| 72 | + [ '/dir/file2', '/dir with spaces', '/dir with spaces/file3' ] ); |
| 73 | +}; |
| 74 | + |
| 75 | +sub prepare_directory { |
| 76 | + my $target = tempdir( CLEANUP => 1 ); |
| 77 | + die unless -d $target; |
| 78 | + |
| 79 | + return $target; |
| 80 | +} |
| 81 | + |
| 82 | +sub run_task { |
| 83 | + my ($func) = @_; |
| 84 | + |
| 85 | + my $task = Rex::Task->new( |
| 86 | + name => 'sync_test', |
| 87 | + func => $func, |
| 88 | + ); |
| 89 | + |
| 90 | + $task->run('<local>'); |
| 91 | +} |
| 92 | + |
| 93 | +sub compare_contents { |
| 94 | + my ( $source, $target, $excluded ) = @_; |
| 95 | + $excluded //= []; |
| 96 | + my %excluded_map = map { $_ => 1 } @{$excluded}; |
| 97 | + |
| 98 | + # test sync results |
| 99 | + my ( @expected, @result ); |
| 100 | + |
| 101 | + # expected results |
| 102 | + find( |
| 103 | + { |
| 104 | + wanted => sub { |
| 105 | + s/$source//; |
| 106 | + return unless length; |
| 107 | + return if $excluded_map{$_}; |
| 108 | + push @expected, $_; |
| 109 | + }, |
| 110 | + no_chdir => 1 |
| 111 | + }, |
| 112 | + $source |
| 113 | + ); |
| 114 | + |
| 115 | + # actual results |
| 116 | + find( |
| 117 | + { |
| 118 | + wanted => sub { |
| 119 | + s/$target//; |
| 120 | + return unless length; |
| 121 | + push @result, $_; |
| 122 | + }, |
| 123 | + no_chdir => 1 |
| 124 | + }, |
| 125 | + $target |
| 126 | + ); |
| 127 | + |
| 128 | + cmp_deeply( \@result, set(@expected), 'synced dir matches' ); |
| 129 | +} |
| 130 | + |
| 131 | +done_testing; |
| 132 | + |
0 commit comments