File Coverage

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

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3package CheckSpelling::CleanupFiles;
4
5
1
1
1
100311
0
22
use Cwd 'realpath';
6
1
1
1
1
0
10
use File::Spec;
7
1
1
1
145
1
15
use CheckSpelling::Util;
8
1
1
1
201
1
14
use CheckSpelling::CheckDictionary;
9
1
1
1
141
1
13
use CheckSpelling::CheckPattern;
10
1
1
1
207
1
15
use CheckSpelling::EnglishList;
11
1
1
1
3
1
439
use Cwd qw(abs_path);
12
13sub identity {
14
13
7
  my ($line) = @_;
15
13
11
  return ($line, '');
16}
17
18sub call_check_line {
19
26
14
  my ($line) = @_;
20
26
9
  my $warning;
21
26
8
  our ($check_line, $output_fh, $warnings_fh);
22
26
20
  ($line, $warning) = $check_line->($line);
23
26
20
  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
10380
  my @files = @_;
31
8
8
  CheckSpelling::CheckPattern::reset_seen();
32
8
8
  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
2
  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
1
1
1
4
  open our $warnings_fh, '>>:encoding(UTF-8)', CheckSpelling::Util::get_file_from_env('early_warnings', '/dev/null');
38
8
17695
  open our $output_fh, '>>:encoding(UTF-8)', $output;
39
8
179
  open my $used_config_files_fh, '>>:encoding(UTF-8)', $used_config_files;
40
8
108
  my $old_file;
41
8
1
  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
5
    $check_line = \&identity;
49  }
50
51
8
5
  for my $file (@files) {
52
13
200
    my $maybe_bad=abs_path($file);
53
13
38
    if ($maybe_bad !~ /^\Q$workspace_path\E/) {
54
1
18
      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
9
      return 3;
57    }
58
12
12
    if ($maybe_bad =~ m{/\.git/}i) {
59
1
10
      print "::error ::Configuration files must not live within `.git/`...\n";
60
1
2
      print "::error ::Unfortunately, file '$file' appears to.\n";
61
1
6
      return 4;
62    }
63
11
5
    my $fh;
64
11
81
    if (open($fh, '<:encoding(UTF-8)', $file)) {
65
10
161
      $ARGV = $file;
66
10
15
      print $used_config_files_fh "$file\0";
67
10
12
      seek($fh, -1, 2);
68
10
110
      read($fh, $buffer, 1);
69
10
30
      my $length = tell($fh);
70
10
11
      seek($fh, 0, 0);
71
10
8
      my $add_nl_at_eof = 0;
72
10
5
      if ($length == 0) {
73
3
35
        print STDERR "$file:1:1 ... 1, Notice - File is empty (empty-file)\n";
74      } else {
75
7
13
        if ($buffer !~ /\R/) {
76
1
1
          $add_nl_at_eof = 1;
77        }
78        # local $/ = undef;
79
7
6
        my ($nl, $first_end, $end, $line);
80
7
0
        my %eol_counts;
81
7
1
        my $content = '';
82
7
20
        while (!eof($fh)) {
83
7
45
          read $fh, $buffer, 4096;
84
7
6
          $content .= $buffer;
85
7
17
          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
19
            ++$.;
87
25
18
            my ($line, $end) = ($1, $2);
88
25
16
            unless (defined $nl) {
89
7
4
              $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
17
            ++$eol_counts{$end};
94
25
11
            my $warning;
95
25
12
            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
0
          my $line_length = length $_;
103
1
5
          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
10
        my $eol_a = $eol_counts{"\n"} || 0;
107
7
10
        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
6
        push @line_endings, "DOS [$eol_d_a]" if $eol_d_a;
111
7
6
        push @line_endings, "UNIX [$eol_a]" if $eol_a;
112
7
7
        push @line_endings, "Mac classic [$eol_d]" if $eol_d;
113
7
13
        if (scalar @line_endings > 1) {
114
1
0
          my $line_length = length $_;
115
1
1
          my $mixed_endings = CheckSpelling::EnglishList::build(@line_endings);
116
1
19
          printf STDERR "$file:$.:1 ... $length, Warning - Mixed $mixed_endings line endings (mixed-line-endings)\n";
117        }
118      }
119
10
49
      close($fh);
120    }
121  }
122
6
27
  close $used_config_files_fh;
123
6
69
  close $output_fh;
124
6
18
  close $warnings_fh;
125
6
14
  return 0;
126}
127
1281;