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 [ prop => 'PS', \'info', 'PSI' ],
69 [ prop => 'PSI', 'N', 'V' ] ],
70 [ mode => [ qw(PS M) ],
71 [ info_prop => 'PS', \'mode', 'M' ] ],
72 [ exists_path => [ qw(PS) ],
73 [ info_prop => 'PS', \'is_directory', \1 ] ],
74 [ exists_path => [ qw(PS) ],
75 [ info_prop => 'PS', \'is_file', \1 ] ],
76 [ is_directory => [ qw(PS) ],
77 [ info_prop => 'PS', \'is_directory', \1 ] ],
78 [ is_file => [ qw(PS) ],
79 [ info_prop => 'PS', \'is_file', \1 ] ],
82 %path_status = %protos;
84 sub paths_for_simple {
85 join ' ', map $_->value_for('PS')->path, $solver->query(
86 [ path_status => 'PS' ], @_
90 is(paths_for_simple(), '.ssh .ssh/authorized_keys');
92 is(paths_for_simple([ is_directory => 'PS' ]), '.ssh');
94 is(paths_for_simple([ is_file => 'PS' ]), '.ssh/authorized_keys');
96 is(paths_for_simple([ mode => 'PS', \'0755' ]), '.ssh');
99 path_status_at => [ 'PS', 'P' ],
100 [ path_status => 'PS' ],
101 [ path => qw(PS P) ],
104 path_status_at => [ 'PS', 'P' ],
105 [ constrain => [] => sub { die "ARGH" } ]
110 [ path_status_at => 'PS', \'.ssh' ]
114 delete $solver->rule_set->rules->{'path_status_at/2'};
117 path_status_at => [ 'PS', 'P' ],
118 [ path_status => 'PS' ],
119 [ path => qw(PS P) ],
123 path_status_at => [ 'PS', 'P' ],
124 [ constrain => [] => sub { die "ARGH" } ]
130 @res = $solver->query(
131 [ path_status_at => 'PS', \'.ssh' ]
135 is(join(' ', map $_->value_for('PS')->path, @res), '.ssh');
137 delete $solver->rule_set->rules->{'path_status_at/2'};
140 path_status_at => [ 'PS', 'P' ],
141 [ path_status => 'PS' ],
142 [ path => qw(PS P) ],
149 path_status_at => [ 'PS', 'P' ],
150 [ observe => [ 'P' ],
153 DX::Observer::FromCode->new(
154 code => sub { (path_status => $ob_res{$path}) }
158 [ path_status => 'PS' ],
159 [ path => qw(PS P) ],
164 $ob_res{'.ssh'} = $protos{'.ssh'};
167 join ' ', map $_->value_for('PS')->path, $solver->query(@_)->results;
171 paths_for([ path_status => 'PS' ], [ path => 'PS', \'.ssh' ]),
176 throws_ok { paths_for([ path_status_at => 'PS', \'.ssh' ]) }
179 $solver->{observation_policy} = sub { 1 };
182 paths_for([ path_status_at => 'PS', \'.ssh' ]),
187 is($path_status{'.ssh'}, $ob_res{'.ssh'});
189 delete $solver->{observation_policy};
191 lives_ok { paths_for([ path_status_at => 'PS', \'.ssh' ]) }
192 'No observation required anymore';
194 $path_status{'.ssh/authorized_keys'} = $protos{'.ssh/authorized_keys'};
197 paths_for([ path_status => 'PS' ], [ not => [ is_directory => 'PS' ] ]),
198 '.ssh/authorized_keys',
202 $solver->add_rule(@$_) for (
203 [ directory_at => [ qw(PS P) ],
204 [ path_status_at => qw(PS P) ],
205 [ is_directory => 'PS' ] ],
206 [ file_at => [ qw(PS P) ],
207 [ path_status_at => qw(PS P) ],
208 [ is_file => 'PS' ] ],
213 $ob_res{'.ssh'} = $empty{'.ssh'};
215 #%path_status = %protos;
217 $solver->{observation_policy} = sub { 1 };
220 $solver->query([ directory_at => 'PS' => \'.ssh' ]);
224 [ dot_ssh_query()->results ],
228 #::Dwarn(paths_for([ directory_at => 'PS', \'.ssh' ]));
230 $solver->add_rule(@$_) for (
231 [ is_directory => [ qw(PS) ],
232 [ not => [ exists_path => 'PS' ] ],
236 DX::Action::FromCode->new(
238 (path_status => My::PathStatus->new(
239 path => $value->path,
240 info => My::PathStatusInfo->new(
241 is_directory => 1, mode => ''
246 $ob_res{$value->path} = $protos{$value->path};
247 (path_status => $value);
255 @res = dot_ssh_query()->results;
257 is(scalar(@res),1,'Single result');
259 is($path_status{'.ssh'}, $empty{'.ssh'}, 'Empty observed');
262 scalar(my ($action) = $res[0]->actions), 1
265 $solver->run_action($action);
267 ok(!$path_status{'.ssh'}, 'Empty retracted');
269 @res = dot_ssh_query()->results;
271 is(scalar(@res),1,'Single result');
273 is($path_status{'.ssh'}, $protos{'.ssh'}, 'Created observed');
275 ok(!$res[0]->actions, 'No action');
277 $solver->add_predicate(
278 catfile => [ qw(DirPath FileName FilePath) ],
279 [ qw(+ + -) ] => sub {
280 +(FilePath => [ value => File::Spec->catfile($_{DirPath}, $_{FileName}) ])
284 $solver->add_rule(@$_) for (
285 [ file_in => [ qw(DirStatus FileName FileStatus) ],
286 [ is_directory => qw(DirStatus) ],
287 [ path => qw(DirStatus DirPath) ],
288 [ catfile => qw(DirPath FileName FilePath) ],
289 [ file_at => qw(FileStatus FilePath) ] ],
290 [ is_file => [ qw(PS) ],
291 [ not => [ exists_path => 'PS' ] ],
295 DX::Action::FromCode->new(
297 (path_status => My::PathStatus->new(
298 path => $value->path,
299 info => My::PathStatusInfo->new(
300 is_file => 1, mode => ''
305 $ob_res{$value->path} = $protos{$value->path};
306 (path_status => $value);
317 [ directory_at => 'D' => \'.ssh' ],
318 [ file_in => 'D' => \'authorized_keys' => 'F' ],
322 @res = keys_file()->results;
324 is(scalar @res, 1, 'One result');
326 is(scalar(my @act = $res[0]->actions), 2, 'Two actions');
330 is(scalar(my ($poss) = grep !@{$_->dependencies}, @act), 1, 'One possible');
332 $solver->run_action($poss);
334 @res = keys_file()->results;
336 is(scalar @res, 1, 'One result');
339 scalar(($poss) = grep !@{$_->dependencies}, $res[0]->actions), 1,
343 $solver->run_action($poss);
345 @res = keys_file()->results;
347 is(scalar @res, 1, 'One result');
349 is(scalar($res[0]->actions), 0, 'No actions');