summaryrefslogtreecommitdiff
path: root/t/dh_installchangelogs/dh_installchangelogs.t
blob: 09560575dd729c198aeef67316446ece17e9b5b3 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Time::Piece;
use Time::Seconds qw(ONE_MONTH ONE_YEAR);

use File::Basename qw(dirname);
use lib dirname(dirname(__FILE__));
use Test::DH;

use constant TEST_DIR => dirname($0);
our @TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
    debian/changelog
    debian/control
));

use constant CUTOFF_DATE_STR => "2019-07-06"; # oldstable = Debian 10 Buster
use constant CUTOFF_DATE => Time::Piece->strptime(CUTOFF_DATE_STR, "%Y-%m-%d");

sub install_changelog {
	my ($latest_offset_years, $num_years, $is_binnmu) = @_;
	$is_binnmu //= 0;

	my $entry_date_first = CUTOFF_DATE->add_years($latest_offset_years);
	my $entry_date_stop = $entry_date_first->add_years(-$num_years);

	my $changelog = "${\TEST_DIR}/debian/changelog";

	open(my $fd, ">", $changelog) or error("open($changelog): $!");

	if ($is_binnmu) {
		my $nmu_date = $entry_date_first->add_months(-1);
		my $nmu_entry = entry_text($nmu_date, 1);
		print($fd $nmu_entry);
	}

	my $entry_date = $entry_date_first;
	while ($entry_date > $entry_date_stop) {
		my $entry = entry_text($entry_date, 0);
		print($fd $entry);

		$entry_date = $entry_date->add_months(-3);
	}
	close($fd);
}

sub entry_text {
	my ($entry_date, $is_binnmu) = @_;
	my $entry_date_str = $entry_date->strftime("%a, %d %b %Y %T %z");
	my $ver = $entry_date->year . "." . $entry_date->mon . "-1";
	my $binnmu_text = "";

	if ($is_binnmu) {
		$binnmu_text = " binary-only=yes";
		$ver .= "+b1";
	}

	my $entry = "";
	$entry .= "foo ($ver) unstable; urgency=low$binnmu_text\n\n";
	$entry .= "  * New release.\n\n";
	$entry .= " -- Test <testing\@nowhere>  $entry_date_str\n\n";

	return $entry;
}

sub changelog_lines_pkg {
	return changelog_lines("debian/changelog");
}
sub changelog_lines_installed {
	return changelog_lines("debian/foo/usr/share/doc/foo/changelog.Debian");
}
sub changelog_lines_binnmu {
	return changelog_lines("debian/foo/usr/share/doc/foo/changelog.Debian.all");
}
sub changelog_lines {
	my ($changelog) = @_;
	open(my $fd, $changelog) or error("open($changelog): $!");
	my @lines = @{readlines($fd)};
	@lines = grep(!/^$/, @lines);
	return @lines;
}

sub dates_in_lines {
	my @lines = @_;
	my @lines_dates = grep(/^ -- /, @lines);
	@lines_dates = map { (my $l = $_) =~ s/^\s*--\s+.*?\s+<[^>]*>\s+[A-Za-z]+, +//; $l }  @lines_dates;
	@lines_dates = map { Time::Piece->strptime($_, "%d %b %Y %T %z") }  @lines_dates;
	return @lines_dates;
}

plan(tests => 6);

# Test changelog with only recent entries (< oldstable)
my $years_after_cutoff = 2;
my $years_of_changelog = 2;
install_changelog($years_after_cutoff, $years_of_changelog);
each_compat_subtest {
	my @lines_orig = changelog_lines_pkg();
	ok(run_dh_tool("dh_installchangelogs"));
	my @lines = changelog_lines_installed();
	my @comments = grep(/^#/, @lines);

	is(@lines, @lines_orig);
	is(@comments, 0);
};

# Test changelog with both recent and old entries
$years_after_cutoff = 1;
$years_of_changelog = 4;
install_changelog($years_after_cutoff, $years_of_changelog);
each_compat_subtest {
	my @lines_orig = changelog_lines_pkg();
	ok(run_dh_tool("dh_installchangelogs"));
	my @lines = changelog_lines_installed();
	my @entries = dates_in_lines(@lines);
	my @entries_old = grep { $_ < CUTOFF_DATE } @entries;
	my @comments = grep(/^#/, @lines);

	cmp_ok(@lines, "<", @lines_orig);
	cmp_ok(@entries, ">", 1);
	is(@entries_old, 0);
	cmp_ok(@comments, ">=", 1);
};

# Test changelog with only old entries
$years_after_cutoff = -1;
$years_of_changelog = 2;
install_changelog($years_after_cutoff, $years_of_changelog);
each_compat_subtest {
	my @lines_orig = changelog_lines_pkg();
	ok(run_dh_tool("dh_installchangelogs"));
	my @lines = changelog_lines_installed();
	my @entries = dates_in_lines(@lines);
	my @entries_old = grep { $_ < CUTOFF_DATE } @entries;
	my @comments = grep(/^#/, @lines);

	cmp_ok(@lines, "<", @lines_orig);
	is(@entries, 1);
	is(@entries_old, 1);
	cmp_ok(@comments, ">=", 1);
};

# Test changelog with only recent entries (< oldstable) + binNUM
$years_after_cutoff = 2;
$years_of_changelog = 2;
install_changelog($years_after_cutoff, $years_of_changelog, 1);
each_compat_subtest {
	my @lines_orig = changelog_lines_pkg();
	my @entries_orig = dates_in_lines(@lines_orig);
	ok(run_dh_tool("dh_installchangelogs"));
	my @lines = changelog_lines_installed();
	my @entries = dates_in_lines(@lines);
	my @entries_nmu = dates_in_lines(changelog_lines_binnmu());
	my @comments = grep(/^#/, @lines);

	is(@entries, @entries_orig-1);
	is($entries[0], $entries_orig[1]);
	is(@comments, 0);

	is(@entries_nmu, 1);
};

# Test changelog with both recent and old entries + binNMU
$years_after_cutoff = 1;
$years_of_changelog = 4;
install_changelog($years_after_cutoff, $years_of_changelog, 1);
each_compat_subtest {
	my @lines_orig = changelog_lines_pkg();
	my @entries_orig = dates_in_lines(@lines_orig);
	ok(run_dh_tool("dh_installchangelogs"));
	my @lines = changelog_lines_installed();
	my @entries = dates_in_lines(@lines);
	my @entries_old = grep { $_ < CUTOFF_DATE } @entries;
	my @entries_nmu = dates_in_lines(changelog_lines_binnmu());
	my @comments = grep(/^#/, @lines);

	cmp_ok(@entries, "<", @entries_orig-1);
	is($entries[0], $entries_orig[1]);
	is(@entries_old, 0);
	cmp_ok(@comments, ">=", 1);

	is(@entries_nmu, 1);
};

# Test changelog with only old entries + binNMU
$years_after_cutoff = -1;
$years_of_changelog = 2;
install_changelog($years_after_cutoff, $years_of_changelog, 1);
each_compat_subtest {
	my @lines_orig = changelog_lines_pkg();
	my @entries_orig = dates_in_lines(@lines_orig);
	ok(run_dh_tool("dh_installchangelogs"));
	my @lines = changelog_lines_installed();
	my @entries = dates_in_lines(@lines);
	my @entries_old = grep { $_ < CUTOFF_DATE } @entries;
	my @entries_nmu = dates_in_lines(changelog_lines_binnmu());
	my @comments = grep(/^#/, @lines);

	is(@entries, 1);
	is($entries[0], $entries_orig[1]);
	is(@entries_old, 1);
	cmp_ok(@comments, ">=", 1);

	is(@entries_nmu, 1);
};

unlink("${\TEST_DIR}/debian/changelog");