File Coverage

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

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