File Coverage

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

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3package CheckSpelling::CleanupFiles;
4
5
1
1
1
110936
0
24
use Cwd 'realpath';
6
1
1
1
1
1
16
use File::Spec;
7
1
1
1
170
1
16
use CheckSpelling::Util;
8
1
1
1
239
1
16
use CheckSpelling::CheckDictionary;
9
1
1
1
166
1
15
use CheckSpelling::CheckPattern;
10
1
1
1
225
1
16
use CheckSpelling::EnglishList;
11
1
1
1
1
1
477
use Cwd qw(abs_path);
12
13sub identity {
14
13
4
  my ($line) = @_;
15
13
11
  return ($line, '');
16}
17
18sub call_check_line {
19
23
15
  my ($line) = @_;
20
23
6
  my $warning;
21
23
7
  our ($check_line, $output_fh, $warnings_fh);
22
23
15
  ($line, $warning) = $check_line->($line);
23
23
21
  if ($warning ne '') {
24
2
5
    print $warnings_fh "$ARGV:$.:$warning";
25  }
26
23
13
  if ($line ne '') {
27
21
42
    print $output_fh $line."\n";
28  }
29}
30
31sub clean_files {
32
8
10724
  my @files = @_;
33
8
8
  CheckSpelling::CheckPattern::reset_seen();
34
8
8
  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
6
  my $workspace_path=abs_path(CheckSpelling::Util::get_file_from_env('GITHUB_WORKSPACE', '.'));
37
8
5
  my $used_config_files=CheckSpelling::Util::get_file_from_env('used_config_files', '/dev/null');
38
8
21
  $ENV{comment_char}='\s*#';
39
1
1
1
8
2
0
3
9
  open our $warnings_fh, '>>:encoding(UTF-8)', CheckSpelling::Util::get_file_from_env('early_warnings', '/dev/null');
40
8
888
  open our $output_fh, '>>:encoding(UTF-8)', $output;
41
8
233
  open my $used_config_files_fh, '>>:encoding(UTF-8)', $used_config_files;
42
8
139
  my $old_file;
43
8
2
  our $check_line;
44
45
8
18
  if ($type =~ /^(?:line_forbidden|patterns|excludes|only|reject)$/) {
46
1
1
    $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
6
    $check_line = \&identity;
51  }
52
53
8
7
  for my $file (@files) {
54
12
191
    my $maybe_bad=abs_path($file);
55
12
39
    if ($maybe_bad !~ /^\Q$workspace_path\E/) {
56
1
18
      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
11
      return 3;
59    }
60
11
12
    if ($maybe_bad =~ m{/\.git/}i) {
61
1
13
      print "::error ::Configuration files must not live within `.git/`...\n";
62
1
2
      print "::error ::Unfortunately, file '$file' appears to.\n";
63
1
7
      return 4;
64    }
65
10
6
    my $fh;
66
10
77
    if (open($fh, '<:encoding(UTF-8)', $file)) {
67
9
144
      $ARGV = $file;
68
9
14
      print $used_config_files_fh "$file\0";
69
9
12
      seek($fh, -1, 2);
70
9
69
      read($fh, $buffer, 1);
71
9
23
      my $length = tell($fh);
72
9
9
      seek($fh, 0, 0);
73
9
5
      my $add_nl_at_eof = 0;
74
9
8
      if ($length == 0) {
75
3
36
        print STDERR "$file:1:1 ... 1, Notice - File is empty (empty-file)\n";
76      } else {
77
6
10
        if ($buffer !~ /\R/) {
78
1
0
          $add_nl_at_eof = 1;
79        }
80        # local $/ = undef;
81
6
5
        my ($nl, $first_end, $end, $line);
82
6
0
        my %eol_counts;
83
6
4
        my $content = '';
84
6
14
        while (!eof($fh)) {
85
6
20
          read $fh, $buffer, 4096;
86
6
6
          $content .= $buffer;
87
6
14
          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
15
            ++$.;
89
22
18
            my ($line, $end) = ($1, $2);
90
22
38
            unless (defined $nl) {
91
6
5
              $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
16
            ++$eol_counts{$end};
96
22
7
            my $warning;
97
22
10
            call_check_line($line);
98          }
99        }
100
6
5
        if ($content ne '') {
101
1
1
          call_check_line($content);
102        }
103
6
5
        if ($add_nl_at_eof) {
104
1
1
          my $line_length = length $_;
105
1
4
          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
8
        my $eol_d = $eol_counts{"\r"} || 0;
110
6
12
        my $eol_d_a = $eol_counts{"\r\n"} || 0;
111
6
3
        my @line_endings;
112
6
6
        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
5
        push @line_endings, "Mac classic [$eol_d]" if $eol_d;
115
6
7
        if (scalar @line_endings > 1) {
116
1
0
          my $line_length = length $_;
117
1
1
          my $mixed_endings = CheckSpelling::EnglishList::build(@line_endings);
118
1
18
          printf STDERR "$file:$.:1 ... $length, Warning - Mixed $mixed_endings line endings (mixed-line-endings)\n";
119        }
120      }
121
9
40
      close($fh);
122    }
123  }
124
6
28
  close $used_config_files_fh;
125
6
17
  close $warnings_fh;
126
6
12
  return 0;
127}
128
1291;