File Coverage

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

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