File Coverage

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

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