tails-persistence-setup 2.2.1
[tails-persistence-setup.git] / features / step_definitions / Configuration_steps.pl
blobe1554a6767e4262ca5a2852fb1936997d72f0382
1 #!perl
3 use 5.10.1;
4 use strictures 2;
6 use lib "lib";
8 use Test::More;
9 use Test::BDD::Cucumber::StepFile;
11 use Function::Parameters;
12 use List::Util qw{first};
13 use List::MoreUtils qw{all};
14 use Path::Tiny;
15 use File::Temp qw{tempfile};
17 use Tails::Persistence::Configuration;
18 use Tails::Persistence::Configuration::ConfigFile;
19 use Tails::Persistence::Step::Configure;
21 fun get_temp_file () {
22 my ($fh, $filename) = tempfile();
23 return path($filename);
26 Given qr{^the file does not exist$}, sub {
27 my $config_path = get_temp_file();
28 S->{config_path} = $config_path;
29 ok(defined($config_path));
32 Given qr{^the file is empty$}, sub {
33 my $config_path = get_temp_file();
34 S->{config_path} = $config_path;
35 $config_path->touch;
36 ok(-e $config_path);
39 Given qr{^the file has a valid one-column line$}, sub {
40 my $config_path = get_temp_file();
41 S->{config_path} = $config_path;
42 my $fh = $config_path->openw;
43 say $fh "/home/amnesia";
46 Given qr{^the file has only a commented-out line$}, sub {
47 my $config_path = get_temp_file();
48 S->{config_path} = $config_path;
49 my $fh = $config_path->openw;
50 say $fh " #/home/amnesia";
53 Given qr{^the file has a valid two-different-columns line$}, sub {
54 my $config_path = get_temp_file();
55 S->{config_path} = $config_path;
56 my $fh = $config_path->openw;
57 say $fh "/home/amnesia /something/else";
60 Given qr{^the file has two valid two-columns lines$}, sub {
61 my $config_path = get_temp_file();
62 S->{config_path} = $config_path;
63 my $fh = $config_path->openw;
64 say $fh "/home/amnesia /something/else";
65 say $fh "/var/lib/tor /var/lib/tor";
68 Given qr{^the file has a valid line with options '([^']*)'$}, sub {
69 my $options = C->matches->[0];
70 my $config_path = get_temp_file();
71 S->{config_path} = $config_path;
72 my $fh = $config_path->openw;
73 say $fh "/home/amnesia $options";
76 Given qr{^the file has the following content$}, sub {
77 my $content = C->data;
78 my $config_path = get_temp_file();
79 S->{config_path} = $config_path;
80 my $fh = $config_path->openw;
81 say $fh $content;
84 Given qr{^I have a Configuration object$}, sub {
85 my $config_path = get_temp_file();
86 S->{config_path} = $config_path;
87 $config_path->touch;
88 my $configuration = Tails::Persistence::Configuration->new(
89 config_file_path => $config_path
91 S->{configuration} = $configuration;
92 ok(defined($configuration));
95 Given qr{^I have a Step::Configure object$}, sub {
96 my $configure = Tails::Persistence::Step::Configure->new(
97 name => 'configure',
98 configuration => S->{configuration},
99 drive_model => 'drive model',
100 drive_vendor => 'drive vendor',
101 go_callback => sub { S->{configuration}->save; },
102 success_callback => sub { return 1; },
103 persistence_partition_device_file => 'persistence partition device file',
104 persistence_partition_size => 12000,
106 S->{configure} = $configure;
107 ok(defined($configure));
110 When qr{^I create a ConfigFile object$}, sub {
111 my $config_path = S->{config_path};
112 my $config_file = Tails::Persistence::Configuration::ConfigFile->new(
113 config_file_path => $config_path
115 S->{config_file} = $config_file;
116 ok(defined($config_file));
119 When qr{^I write in-memory configuration to file$}, sub {
120 S->{config_file}->save;
121 ok("in-memory configuration saved to file");
124 When qr{^I merge the presets and the file$}, sub {
125 my $config_path = S->{config_path};
126 my $configuration = Tails::Persistence::Configuration->new(
127 config_file_path => $config_path
129 S->{configuration} = $configuration;
130 ok(defined($configuration));
133 When qr{^I toggle an inactive setting on$}, sub {
134 my $setting = first { ! $_->is_active } S->{configure}->all_settings;
135 ok(defined($setting));
136 $setting->set_active(1);
137 ok($setting->is_active);
140 When qr{^I toggle the "([^"]+)" inactive setting on$}, sub {
141 my $id = C->matches->[0];
142 use Data::Dumper::Concise;
143 my $inactive_setting = first {
144 my $setting = $_;
145 ! $_->is_active && $setting->id eq $id;
146 } S->{configure}->all_settings;
147 ok(defined($inactive_setting));
148 $inactive_setting->set_active(1);
149 ok($inactive_setting->is_active);
152 When qr{^I toggle an active setting off$}, sub {
153 my $setting = first { $_->is_active } S->{configure}->all_settings;
154 $setting->set_active(0);
155 ok(! $setting->is_active);
158 When qr{^I toggle the "([^"]+)" active setting off$}, sub {
159 my $id = C->matches->[0];
160 my $active_setting = first {
161 my $setting = $_;
162 $setting->is_active && $setting->id eq $id;
163 } S->{configure}->all_settings;
164 ok(defined($active_setting));
165 $active_setting->set_active(0);
166 ok(! $active_setting->is_active);
169 When qr{^I click the save button$}, sub {
170 S->{configure}->go_button->clicked;
173 Then qr{^the file should be created$}, sub {
174 ok(-e S->{config_file}->config_file_path->stringify);
177 Then qr{^the list of lines in the file object should be empty$}, sub {
178 is(S->{config_file}->all_lines, 0);
181 Then qr{^the list of options should be empty$}, sub {
182 my @lines = S->{config_file}->all_lines;
183 my $line = $lines[0];
184 is($line->all_options, 0);
187 Then qr{^the output string should contain (\d+) lines$}, sub {
188 my $expected_lines = C->matches->[0];
189 my $output = S->{config_file}->output;
190 chomp $output;
191 my $lines = split(/\n/, $output);
192 is($lines, $expected_lines);
195 Then qr{^the file should contain (\d+) line[s]?$}, sub {
196 my $expected_lines = C->matches->[0];
197 my $config_path = S->{config_path};
198 my @lines = path($config_path)->lines;
199 is(@lines, $expected_lines);
202 Then qr{^the file should contain the "([^"]*)" line$}, sub {
203 my $expected_line = C->matches->[0];
204 my $config_path = S->{configuration}->config_file_path;
205 my $matching_lines = grep {
206 $_ eq $expected_line
207 } path($config_path)->lines({chomp => 1});
208 is($matching_lines, 1);
211 Then qr{^the first line in file should have options '([^']*)'$}, sub {
212 my $expected_options = C->matches->[0];
213 my $config_path = S->{config_path};
214 my @lines = path($config_path)->lines;
215 my $first_line = $lines[0];
216 my ($destination, $options) = split /\s+/, $first_line;
217 ok(defined($options)
218 && length($options)
219 && $options eq $expected_options
223 Then qr/^the options list should contain (\d+) element[s]?$/, sub {
224 my $expected_elements_count = C->matches->[0];
225 my @lines = S->{config_file}->all_lines;
226 my $line = $lines[0];
227 is($line->count_options, $expected_elements_count);
230 Then qr/^'([^']*)' should be part of the options list$/, sub {
231 my $expected_option = C->matches->[0];
232 my @lines = S->{config_file}->all_lines;
233 my $line = $lines[0];
234 is($line->grep_options(sub { $_ eq $expected_option }), 1);
237 Then qr{^the list of configuration atoms should contain (\d+) elements$}, sub {
238 my $expected = C->matches->[0];
239 is(scalar(S->{configuration}->all_atoms), $expected);
242 Then qr{^the list of displayed settings should contain (\d+) elements$}, sub {
243 my $expected = C->matches->[0];
244 is(scalar(S->{configure}->all_settings), $expected);
247 Then qr{^there should be (\d+) enabled configuration lines?$}, sub {
248 my $expected = C->matches->[0];
249 is(S->{configuration}->all_enabled_lines, $expected);
252 Then qr{^I should have a defined Step::Configure object$}, sub {
253 my $configure = S->{configure};
254 ok(defined($configure));
257 Then qr{^there should be (\d+) active setting[s]?$}, sub {
258 my $expected = C->matches->[0];
259 my $nb_active_settings = grep { $_->is_active } S->{configure}->all_settings;
260 is($nb_active_settings, $expected);
263 Then qr{^there should be (\d+) setting[s]? with a configuration button$}, sub {
264 my $expected = C->matches->[0];
265 my $nb_config_buttons = grep {
266 $_->has_configuration_button
267 } S->{configure}->all_settings;
268 is($nb_config_buttons, $expected);
271 Then qr{^every active setting's atoms should be enabled$}, sub {
272 my @active_settings = grep { $_->is_active } S->{configure}->all_settings;
273 ok(all {
274 my $active_setting = $_;
275 all { $_->enabled } @{$active_setting->atoms};
276 } @active_settings);
279 Then qr{^every inactive setting's atoms should be disabled$}, sub {
280 my @inactive_settings = grep { ! $_->is_active } S->{configure}->all_settings;
281 ok(all {
282 my $active_setting = $_;
283 all { ! $_->enabled } @{$active_setting->atoms};
284 } @inactive_settings);
287 Then qr{^the list box should have (\d+) child(?:ren)?(?: including separators)?$}, sub {
288 my @children = S->{configure}->list_box->get_children;
289 is(@children, C->matches->[0]);