aboutsummaryrefslogtreecommitdiff
path: root/t/27-race-sbofind.t
blob: c4384e80c76e4ff7680f9bca7a827c77a3af80a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exit;
use FindBin '$RealBin';
use lib $RealBin;
use Test::Sbotools 'load';
use Capture::Tiny qw/ capture_merged /;
use File::Temp 'tempdir';
use Cwd;
use feature 'state';

plan tests => 9;

load('sbofind');
my $tags_file = '/usr/sbo/repo/TAGS.txt';

# 1: tags file
{
	rename $tags_file, "$tags_file.bak";
	system 'touch', $tags_file;

	no warnings 'redefine';
	local *_race::cond = sub {
		if ($_[0] eq '$tags_file may be deleted after -f check') {
			local *_race::cond = sub {
				if ($_[0] eq '$file could be deleted between -f test and open') {
					unlink $tags_file;
				}
			};
		}
	};

	my $exit;
	capture_merged { $exit = exit_code { perform_search('foo'); }; };

	is ($exit, undef, "perform_search didn't exit");

	rename "$tags_file.bak", $tags_file;
}

# 2-3: slackbuilds.txt file
{
	my $sbt = '/usr/sbo/repo/SLACKBUILDS.TXT';
	system('touch', $tags_file) unless -f $tags_file;

	no warnings 'redefine';
	local *_race::cond = sub {
		if ($_[0] eq '$file could be deleted between -f test and open') {
			state $num++;
			rename $sbt, "$sbt.bak" if $num == 2;
		}
	};

	my $exit;
	my $out = capture_merged { $exit = exit_code { perform_search('foo'); }; };

	is ($out, "Unable to open $sbt.\n", "perform_search gave correct output");
	is ($exit, 6, "perform_search exited with 6");

	rename "$sbt.bak", $sbt;
}

# 4-9: get_file_contents
{
	my $file = tempdir(CLEANUP => 1) . "/foo";

	my ($exit, $ret);
	my $out = capture_merged { $exit = exit_code { $ret = get_file_contents($file); }; };

	is ($out, '', 'get_file_contents gave no output');
	is ($ret, "Unable to open $file.\n", 'get_file_contents returned correctly');
	is ($exit, undef, 'get_file_contents didn\'t exit');

	system 'touch', $file;

	no warnings 'redefine';
	local *_race::cond = sub {
		if ($_[0] eq '$file could be deleted between -f test and open') {
			unlink $file;
		}
	};

	undef $exit;
	undef $ret;
	$out = capture_merged { $exit = exit_code { $ret = get_file_contents($file); }; };

	is ($out, '', 'get_file_contents gave no output');
	is ($ret, "Unable to open $file.\n", 'get_file_contents returned correctly');
	is ($exit, undef, 'get_file_contents still didn\'t exit');
}