ShipIt/Step/CheckChangeLog.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 86 CC 12 SCAR 34.2
CC
12
Cov
0%
run
42.0
check_file_for_version
42.0
init
6.0
CRAP 30.7
1 package ShipIt::Step::CheckChangeLog;
2 use strict;
3 use base 'ShipIt::Step';
4 use ShipIt::Util qw(bool_prompt edit_file);
5  
6 sub init {
7 my ($self, $conf) = @_;
8  
9 # by default, we search for these files. if they explicitly provide
10 # one or more, we fail hard if one doesn't exist
11 $self->{explicit} = 0;
12 $self->{files} = [ "Changes", "ChangeLog", "CHANGES" ];
13  
14 # get them both (don't short-circuit), to mark them as known config
15 # keys in the Conf class.
16 my $file = $conf->value("CheckChangeLog.file");
17 my $files = $conf->value("CheckChangeLog.files");
18  
19 if (my $cs_list = ($file || $files)) {
20 $self->{explicit} = 1;
21 $self->{files} = [ split /\s*,\s*/, $cs_list ];
22 }
23 }
24  
25 sub run {
26 my ($self, $state) = @_;
27  
28 foreach my $file (@{ $self->{files} }) {
29 unless (-e $file) {
30 # die if they explicitly listed a changelog file,
31 die "Missing explicit ChangeLog file: $file\n" if $self->{explicit};
32 # else just proceed, hoping we find one of the defaults...
33 next;
34 }
35 $state->add_changelog_file($file);
36  
37 # already have a ChangeLog entry?
38 next if $self->check_file_for_version($file, $state->version);
39  
40 # else, ask if they want to edit the file to correct it.
41 if (bool_prompt("Edit file?", "y")) {
42 edit_file($file);
43 } else {
44 die "Aborting.\n";
45 }
46  
47 $self->check_file_for_version($file, $state->version)
48 or die "Aborting.\n";
49 }
50 die "No changelog file. Either make one named one of {@{$self->{files}}}, or explicitly list 1+ in a comma-separated list using the configuration key 'CheckChangeLog.files'\n" unless $state->changelog_files;;
51 }
52  
53 sub check_file_for_version {
54 my ($self, $file, $version) = @_;
55  
56 # FUTURE:
57 # my $clf = ShipIt::ChangeLogFile->new($file);
58 # $clf is ShipIt::ChangeLogFile::FormatType (some subclass)
59 # $cltype->has_version($version)
60 # $cltype->add_version($version) # adds bullet with "NEW STUFF HERE"
61 # system($EDITOR, $file) to tweak it
62 # die unless $cltype->has_version($version) && ! $cltype->has_template_bullet_still;
63  
64 # For now, assume a version number on one of the first ten unindented lines
65  
66 open(my $fh, $file) or die "Failed to open $file: $!\n";
67 while (<$fh>) {
68 # no blanket lines
69 next unless /\S/;
70 # skip things that look like bullets
71 next if /^\s+/;
72 next if /^\*/;
73 next if /^\-/;
74 # seen it?
75 return 1 if /\Q$version\E/;
76 }
77 warn "No mention of version '$version' in changelog file '$file'\n";
78 return 0;
79 }
80  
81 1;