5 use DX::Observer::FromCode;
6 use DX::Action::FromCode;
11 package My::PathStatus;
15 with 'DX::Role::Fact';
17 has path => (is => 'ro', required => 1);
18 has info => (is => 'ro', predicate => 1);
20 package My::PathStatusInfo;
24 with 'DX::Role::Fact';
26 has is_directory => (is => 'ro', default => 0);
27 has is_file => (is => 'ro', default => 0);
28 has mode => (is => 'ro', required => 1);
31 @INC{qw(My/PathStatus.pm My/PathStatusInfo.pm)} = (__FILE__,__FILE__);
34 '.ssh' => My::PathStatus->new(
36 info => My::PathStatusInfo->new(is_directory => 1, mode => '0755')
38 '.ssh/authorized_keys' => My::PathStatus->new(
39 path => '.ssh/authorized_keys',
40 info => My::PathStatusInfo->new(is_file => 1, mode => '0644')
45 '.ssh' => My::PathStatus->new(
48 '.ssh/authorized_keys' => My::PathStatus->new(
49 path => '.ssh/authorized_keys'
55 my $solver = DX::Solver->new(
56 facts => { path_status => DX::SetOver->new(
58 values => \%path_status,
62 $solver->add_rule(@$_) for (
63 [ path_status => [ qw(PS) ],
64 [ member_of => 'PS', \'path_status' ] ],
65 [ path => [ qw(PS P) ],
66 [ prop => 'PS', \'path', 'P' ] ],
67 [ info_prop => [ qw(PS N V) ],
68 [ exists => [ qw(PSI) ],
69 [ prop => 'PS', \'info', 'PSI' ],
70 [ prop => 'PSI', 'N', 'V' ] ] ],
71 [ mode => [ qw(PS M) ],
72 [ info_prop => 'PS', \'mode', 'M' ] ],
73 [ exists_path => [ qw(PS) ],
74 [ info_prop => 'PS', \'is_directory', \1 ] ],
75 [ exists_path => [ qw(PS) ],
76 [ info_prop => 'PS', \'is_file', \1 ] ],
77 [ is_directory => [ qw(PS) ],
78 [ info_prop => 'PS', \'is_directory', \1 ] ],
79 [ is_file => [ qw(PS) ],
80 [ info_prop => 'PS', \'is_file', \1 ] ],
83 %path_status = %protos;
85 sub paths_for_simple {
86 join ' ', map $_->value_for('PS')->path, $solver->query(
87 [ qw(PS) ], [ path_status => 'PS' ], @_
91 is(paths_for_simple(), '.ssh .ssh/authorized_keys');
93 is(paths_for_simple([ is_directory => 'PS' ]), '.ssh');
95 is(paths_for_simple([ is_file => 'PS' ]), '.ssh/authorized_keys');
97 is(paths_for_simple([ mode => 'PS', [ value => '0755' ] ]), '.ssh');
100 path_status_at => [ 'PS', 'P' ],
101 [ path_status => 'PS' ],
102 [ path => qw(PS P) ],
105 path_status_at => [ 'PS', 'P' ],
106 [ constrain => [] => sub { die "ARGH" } ]
112 [ path_status_at => 'PS', [ value => '.ssh' ] ]
116 delete $solver->rule_set->rules->{'path_status_at/2'};
119 path_status_at => [ 'PS', 'P' ],
120 [ path_status => 'PS' ],
121 [ path => qw(PS P) ],
125 path_status_at => [ 'PS', 'P' ],
126 [ constrain => [] => sub { die "ARGH" } ]
132 @res = $solver->query(
134 [ path_status_at => 'PS', [ value => '.ssh' ] ]
138 is(join(' ', map $_->value_for('PS')->path, @res), '.ssh');
140 delete $solver->rule_set->rules->{'path_status_at/2'};
143 path_status_at => [ 'PS', 'P' ],
144 [ path_status => 'PS' ],
145 [ path => qw(PS P) ],
152 path_status_at => [ 'PS', 'P' ],
153 [ observe => [ 'P' ],
156 DX::Observer::FromCode->new(
157 code => sub { (path_status => $ob_res{$path}) }
161 [ path_status => 'PS' ],
162 [ path => qw(PS P) ],
167 $ob_res{'.ssh'} = $protos{'.ssh'};
170 join ' ', map $_->value_for('PS')->path, $solver->query([ 'PS' ], @_)->results;
174 paths_for([ path_status => 'PS' ], [ path => 'PS', [ value => '.ssh' ] ]),
179 throws_ok { paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]) }
182 $solver->{observation_policy} = sub { 1 };
185 paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]),
190 is($path_status{'.ssh'}, $ob_res{'.ssh'});
192 delete $solver->{observation_policy};
194 lives_ok { paths_for([ path_status_at => 'PS', [ value => '.ssh' ] ]) }
195 'No observation required anymore';
197 $path_status{'.ssh/authorized_keys'} = $protos{'.ssh/authorized_keys'};
200 paths_for([ path_status => 'PS' ], [ not => [ is_directory => 'PS' ] ]),
201 '.ssh/authorized_keys',
205 $solver->add_rule(@$_) for (
206 [ directory_at => [ qw(PS P) ],
207 [ path_status_at => qw(PS P) ],
208 [ is_directory => 'PS' ] ],
209 [ file_at => [ qw(PS P) ],
210 [ path_status_at => qw(PS P) ],
211 [ is_file => 'PS' ] ],
216 $ob_res{'.ssh'} = $empty{'.ssh'};
218 #%path_status = %protos;
220 $solver->{observation_policy} = sub { 1 };
223 $solver->query([ 'PS' ], [ directory_at => 'PS' => [ value => '.ssh' ] ]);
227 [ dot_ssh_query()->results ],
231 #::Dwarn(paths_for([ directory_at => 'PS', [ value => '.ssh' ] ]));
233 $solver->add_rule(@$_) for (
234 [ is_directory => [ qw(PS) ],
235 [ not => [ exists_path => 'PS' ] ],
239 DX::Action::FromCode->new(
241 (path_status => My::PathStatus->new(
242 path => $value->path,
243 info => My::PathStatusInfo->new(
244 is_directory => 1, mode => ''
249 $ob_res{$value->path} = $protos{$value->path};
250 (path_status => $value);
258 @res = dot_ssh_query()->results;
260 is(scalar(@res),1,'Single result');
262 is($path_status{'.ssh'}, $empty{'.ssh'}, 'Empty observed');
265 scalar(my ($action) = $res[0]->actions), 1
268 $solver->run_action($action);
270 ok(!$path_status{'.ssh'}, 'Empty retracted');
272 @res = dot_ssh_query()->results;
274 is(scalar(@res),1,'Single result');
276 is($path_status{'.ssh'}, $protos{'.ssh'}, 'Created observed');
278 ok(!$res[0]->actions, 'No action');
280 $solver->add_predicate(
281 catfile => [ qw(DirPath FileName FilePath) ],
282 [ qw(+ + -) ] => sub {
283 +(FilePath => [ value => File::Spec->catfile($_{DirPath}, $_{FileName}) ])
287 $solver->add_rule(@$_) for (
288 [ file_in => [ qw(DirStatus FileName FileStatus) ],
289 [ is_directory => qw(DirStatus) ],
290 [ exists => [ qw(DirPath) ],
291 [ path => qw(DirStatus DirPath) ],
292 [ exists => [ qw(FilePath) ],
293 [ catfile => qw(DirPath FileName FilePath) ],
294 [ file_at => qw(FileStatus FilePath) ] ] ] ],
295 [ is_file => [ qw(PS) ],
296 [ not => [ exists_path => 'PS' ] ],
300 DX::Action::FromCode->new(
302 (path_status => My::PathStatus->new(
303 path => $value->path,
304 info => My::PathStatusInfo->new(
305 is_file => 1, mode => ''
310 $ob_res{$value->path} = $protos{$value->path};
311 (path_status => $value);
321 $solver->query([ qw(D F) ],
322 [ directory_at => 'D' => \'.ssh' ],
323 [ file_in => 'D' => \'authorized_keys' => 'F' ],
327 @res = keys_file()->results;
329 is(scalar @res, 1, 'One result');
331 is(scalar(my @act = $res[0]->actions), 2, 'Two actions');
335 is(scalar(my ($poss) = grep !@{$_->dependencies}, @act), 1, 'One possible');
337 $solver->run_action($poss);
339 @res = keys_file()->results;
341 is(scalar @res, 1, 'One result');
344 scalar(($poss) = grep !@{$_->dependencies}, $res[0]->actions), 1,
348 $solver->run_action($poss);
350 @res = keys_file()->results;
352 is(scalar @res, 1, 'One result');
354 is(scalar($res[0]->actions), 0, 'No actions');