File Coverage

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

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