File Coverage

File:lib/CheckSpelling/CleanupFiles.pm
Coverage:79.6%

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3package CheckSpelling::CleanupFiles;
4
5
1
1
1
113573
1
27
use Cwd 'realpath';
6
1
1
1
2
0
13
use File::Spec;
7
1
1
1
148
1
16
use CheckSpelling::Util;
8
1
1
1
203
1
16
use CheckSpelling::CheckDictionary;
9
1
1
1
142
1
14
use CheckSpelling::CheckPattern;
10
1
1
1
210
1
16
use CheckSpelling::EnglishList;
11
1
1
1
1
1
450
use Cwd qw(abs_path);
12
13sub identity {
14
0
0
  return @_;
15}
16
17sub clean_files {
18
3
4610
  my @files = @_;
19
3
3
  CheckSpelling::CheckPattern::reset_seen();
20
3
3
  my $type=CheckSpelling::Util::get_file_from_env('type');
21
3
3
  my $output=CheckSpelling::Util::get_file_from_env('output', '/dev/null');
22
3
2
  my $workspace_path=abs_path(CheckSpelling::Util::get_file_from_env('GITHUB_WORKSPACE', '.'));
23
3
3
  my $used_config_files=CheckSpelling::Util::get_file_from_env('used_config_files', '/dev/null');
24
3
10
  $ENV{comment_char}='\s*#';
25
1
1
1
3
6
1
3
3
  open our $warnings_fh, '>>:encoding(UTF-8)', CheckSpelling::Util::get_file_from_env('early_warnings', '/dev/null');
26
3
561
  open our $output_fh, '>>:encoding(UTF-8)', $output;
27
3
76
  open my $used_config_files_fh, '>>:encoding(UTF-8)', $used_config_files;
28
3
43
  my $old_file;
29  my $check_line;
30
31
3
7
  if ($type =~ /^(?:line_forbidden|patterns|excludes|only|reject)$/) {
32
1
1
    $check_line = \&CheckSpelling::CheckPattern::process_line;
33  } elsif ($type =~ /^(?:dictionary|expect|allow)$/) {
34
0
0
    $check_line = \&CheckSpelling::CheckDictionary::process_line;
35  } else {
36
2
2
    $check_line = \&identity;
37  }
38
39
3
3
  for my $file (@files) {
40
5
64
    my $maybe_bad=abs_path($file);
41
5
23
    if ($maybe_bad !~ /^\Q$workspace_path\E/) {
42
1
22
      print "::error ::Configuration files must live within $workspace_path...\n";
43
1
2
      print "::error ::Unfortunately, file '$file' appears to reside elsewhere.\n";
44
1
10
      return 3;
45    }
46
4
5
    if ($maybe_bad =~ m{/\.git/}i) {
47
1
12
      print "::error ::Configuration files must not live within `.git/`...\n";
48
1
2
      print "::error ::Unfortunately, file '$file' appears to.\n";
49
1
6
      return 4;
50    }
51
3
0
    my $fh;
52
3
28
    if (open($fh, '<:encoding(UTF-8)', $file)) {
53
3
57
      $ARGV = $file;
54
3
9
      print $used_config_files_fh "$file\0";
55
3
5
      seek($fh, -1, 2);
56
3
21
      read($fh, $buffer, 1);
57
3
9
      my $length = tell($fh);
58
3
4
      seek($fh, 0, 0);
59
3
0
      my $add_nl_at_eof = 0;
60
3
3
      if ($length == 0) {
61
1
13
        print STDERR "$file:1:1 ... 1, Notice - File is empty (empty-file)\n";
62      } else {
63
2
4
        if ($buffer !~ /\R/) {
64
0
0
          print STDERR "$file does not have newline at eof\n";
65
0
0
          $add_nl_at_eof = 1;
66        }
67        # local $/ = undef;
68
2
1
        my ($nl, $first_end, $end, $line);
69
2
0
        my %eol_counts;
70
2
2
        my $content = '';
71
2
5
        while (!eof($fh)) {
72
2
12
          read $fh, $buffer, 4096;
73
2
1
          $content .= $buffer;
74
2
4
          while ($content =~ s/([^\r\n\x0b\f\x85\x{2028}\x{2029}]*)(\r\n|\n|\r|\x0b|\f|\x85|\x{2028}|\x{2029})//m) {
75
10
10
            ++$.;
76
10
8
            my ($line, $end) = ($1, $2);
77
10
5
            unless (defined $nl) {
78
2
1
              $nl = $end;
79            } elsif ($end ne $nl) {
80              print WARNINGS "$file:$.:$-[0] ... $+[0], Warning - Entry has inconsistent line endings (unexpected-line-ending)\n";
81            }
82
10
7
            ++$eol_counts{$end};
83
10
5
            my $warning;
84
10
5
            ($line, $warning) = $check_line->($line);
85
10
7
            if ($warning) {
86
2
5
              print $warnings_fh "$file:$.:$warning";
87            }
88
10
19
            print $output_fh $line."\n";
89          }
90        }
91
2
2
        if ($content ne '') {
92
0
0
          my ($line, $warning) = $check_line->($content);
93
0
0
          if ($warning ne '') {
94
0
0
            print $warnings_fh "$file$warning";
95          } elsif ($line ne '') {
96
0
0
            print $output_fh "$line\n";
97          }
98        }
99
2
2
        if ($add_nl_at_eof) {
100
0
0
          my $line_length = length $_;
101
0
0
          print STDERR "$file:$.:1 ... $length, Warning - Missing newline at end of file (no-newline-at-eof)\n";
102
0
0
          print $output_fh "\n";
103        }
104
2
2
        my $eol_a = $eol_counts{"\n"} || 0;
105
2
6
        my $eol_d = $eol_counts{"\r"} || 0;
106
2
3
        my $eol_d_a = $eol_counts{"\r\n"} || 0;
107
2
0
        my @line_endings;
108
2
3
        push @line_endings, "DOS [$eol_d_a]" if $eol_d_a;
109
2
2
        push @line_endings, "UNIX [$eol_a]" if $eol_a;
110
2
1
        push @line_endings, "Mac classic [$eol_d]" if $eol_d;
111
2
3
        if (scalar @line_endings > 1) {
112
0
0
          my $line_length = length $_;
113
0
0
          my $mixed_endings = CheckSpelling::EnglishList::build(@line_endings);
114
0
0
          printf STDERR "$file:$.:1 ... $length, Warning - Mixed $mixed_endings line endings (mixed-line-endings)\n";
115        }
116      }
117
3
13
      close($fh);
118    }
119  }
120
1
33
  close $used_config_files_fh;
121
1
9
  close $warnings_fh;
122
1
3
  return 0;
123}
124
1251;