| 1 | package Dist::Zilla::Plugin::Test::MixedScripts; | ||
| 2 | |||
| 3 | use v5.20; | ||
| 4 | |||
| 5 | # ABSTRACT: author tests to ensure there is no mixed Unicode | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | |||
| 9 | use List::Util 1.45 qw( uniqstr ); | ||
| 10 | use Path::Tiny; | ||
| 11 | use Sub::Exporter::ForMethods 'method_installer'; | ||
| 12 | use Data::Section 0.004 { installer => method_installer }, '-setup'; | ||
| 13 | use Dist::Zilla::File::InMemory; | ||
| 14 | use Types::Common qw( ArrayRef ConsumerOf NonEmptySimpleStr ); | ||
| 15 | |||
| 16 | use namespace::autoclean; | ||
| 17 | |||
| 18 | use experimental qw( postderef signatures ); | ||
| 19 | |||
| 20 | with | ||
| 21 | 'Dist::Zilla::Role::FileGatherer', | ||
| 22 | 'Dist::Zilla::Role::FileMunger', | ||
| 23 | 'Dist::Zilla::Role::TextTemplate', | ||
| 24 | 'Dist::Zilla::Role::FileFinderUser' => { | ||
| 25 | method => 'found_files', | ||
| 26 | finder_arg_names => ['finder'], | ||
| 27 | default_finders => [ ':InstallModules', ':ExecFiles', ':TestFiles' ], | ||
| 28 | }, | ||
| 29 | 'Dist::Zilla::Role::PrereqSource'; | ||
| 30 | |||
| 31 | our $VERSION = 'v0.2.4'; | ||
| 32 | |||
| 33 | |||
| 34 | has filename => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'Str', | ||
| 37 | lazy => 1, | ||
| 38 | default => sub { return 'xt/author/mixed-unicode-scripts.t' }, | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | sub mvp_multivalue_args { qw( files exclude scripts ) } | ||
| 43 | |||
| 44 | sub mvp_aliases { return { file => 'files', script => 'scripts' } } | ||
| 45 | |||
| 46 | |||
| 47 | has files => ( | ||
| 48 | isa => ArrayRef[NonEmptySimpleStr], | ||
| 49 | traits => ['Array'], | ||
| 50 | handles => { files => 'elements' }, | ||
| 51 | lazy => 1, | ||
| 52 | default => sub { [] }, | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | has exclude => ( | ||
| 57 | is => 'ro', | ||
| 58 | isa => ArrayRef[NonEmptySimpleStr], | ||
| 59 | default => sub { [] }, | ||
| 60 | ); | ||
| 61 | |||
| 62 | |||
| 63 | has scripts => ( | ||
| 64 | isa => ArrayRef[NonEmptySimpleStr], | ||
| 65 | traits => ['Array'], | ||
| 66 | handles => { scripts => 'elements' }, | ||
| 67 | lazy => 1, | ||
| 68 | default => sub { [] }, | ||
| 69 | ); | ||
| 70 | |||
| 71 | |||
| 72 | has _file_obj => ( | ||
| 73 | is => 'rw', | ||
| 74 | isa => ConsumerOf[ 'Dist::Zilla::Role::File' ], | ||
| 75 | ); | ||
| 76 | |||
| 77 | around dump_config => sub( $orig, $self ) { | ||
| 78 | my $config = $self->$orig; | ||
| 79 | $config->{ +__PACKAGE__ } = { | ||
| 80 | filename => $self->filename, | ||
| 81 | finder => [ sort $self->finder->@* ], | ||
| 82 | scripts => [ sort $self->scripts ], | ||
| 83 | blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (), | ||
| 84 | }; | ||
| 85 | return $config; | ||
| 86 | }; | ||
| 87 | |||
| 88 | sub gather_files($self) { | ||
| 89 | |||
| 90 | $self->add_file( | ||
| 91 | $self->_file_obj( | ||
| 92 | Dist::Zilla::File::InMemory->new( | ||
| 93 | name => $self->filename, | ||
| 94 | content => $self->section_data('__TEST__')->$*, | ||
| 95 | ) | ||
| 96 | ) | ||
| 97 | ); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub munge_files($self) { | ||
| 102 | |||
| 103 | my $pattern = join("|", map { "(?:$_)" } '\000', $self->exclude->@* ); | ||
| 104 | my $exclude = qr/(?:$pattern)/; | ||
| 105 | |||
| 106 | my @filenames = map { path( $_->name )->relative('.')->stringify } | ||
| 107 | grep { not( $_->can('is_bytes') and $_->is_bytes ) } | ||
| 108 | grep { $_->name !~ $exclude } | ||
| 109 | $self->found_files->@*; | ||
| 110 | push @filenames, $self->files; | ||
| 111 | $self->log_debug( 'adding file ' . $_ ) for @filenames; | ||
| 112 | |||
| 113 | my @scripts = $self->scripts; | ||
| 114 | |||
| 115 | my $file = $self->_file_obj; | ||
| 116 | $file->content( | ||
| 117 | $self->fill_in_string( | ||
| 118 | $file->content, | ||
| 119 | { | ||
| 120 | dist => \( $self->zilla ), | ||
| 121 | plugin => \$self, | ||
| 122 | filenames => [ sort @filenames ], | ||
| 123 | scripts => [ sort ( uniqstr( @scripts ) ) ], | ||
| 124 | }, | ||
| 125 | ) | ||
| 126 | ); | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub register_prereqs($self) { | ||
| 131 | $self->zilla->register_prereqs( | ||
| 132 | { | ||
| 133 | phase => 'develop', | ||
| 134 | type => 'requires', | ||
| 135 | }, | ||
| 136 | 'Test2::Tools::Basic' => '1.302200', | ||
| 137 | 'Test::MixedScripts' => 'v0.3.0', | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | __PACKAGE__->meta->make_immutable; | ||
| 142 | |||
| 143 | =pod | ||
| 144 | |||
| 145 | =encoding UTF-8 | ||
| 146 | |||
| 147 | =head1 NAME | ||
| 148 | |||
| 149 | Dist::Zilla::Plugin::Test::MixedScripts - author tests to ensure there is no mixed Unicode | ||
| 150 | |||
| 151 | =head1 VERSION | ||
| 152 | |||
| 153 | version v0.2.4 | ||
| 154 | |||
| 155 | =for stopwords Cushing Etheridge Florian Ragwitz Unicode | ||
| 156 | |||
| 157 | =head1 SYNOPSIS | ||
| 158 | |||
| 159 | In the F<dist.ini> add: | ||
| 160 | |||
| 161 | [Test::MixedScripts] | ||
| 162 | ; authordep Test::MixedScripts | ||
| 163 | script = Latin | ||
| 164 | script = Common | ||
| 165 | |||
| 166 | =head1 DESCRIPTION | ||
| 167 | |||
| 168 | This generates an author L<Test::MixedScripts>. | ||
| 169 | |||
| 170 | This is an extension of L<Dist::Zilla::Plugin::InlineFiles>, providing the file F<xt/author/mixed-unicode-scripts.t> for | ||
| 171 | testing against mixed Unicode scripts that are potentially confusing or malicious. | ||
| 172 | |||
| 173 | For example, the text for the domain names C<< E<0x043e>nE<0x0435>.example.com >> and C<one.example.com> look indistinguishable in many fonts, | ||
| 174 | but the first one has Cyrillic letters. If your software interacted with a service on the second domain, then someone | ||
| 175 | can operate a service on the first domain and attempt to fool developers into using their domain instead. | ||
| 176 | |||
| 177 | This might be through a malicious patch submission, or even text from an email or web page that they have convinced a | ||
| 178 | developer to copy and paste into their code. | ||
| 179 | |||
| 180 | =head1 CONFIGURATION OPTIONS | ||
| 181 | |||
| 182 | =head2 filename | ||
| 183 | |||
| 184 | This is the filename of the test to add. Defaults to F<xt/author/mixed-unicode-scripts.t>. | ||
| 185 | |||
| 186 | =head2 finder | ||
| 187 | |||
| 188 | This is the name of a C<FileFinder> for finding files to check. The default value is C<:InstallModules>, C<:ExecFiles> (see also | ||
| 189 | L<Dist::Zilla::Plugin::ExecDir>) and C<:TestFiles>. | ||
| 190 | |||
| 191 | This option can be used more than once. | ||
| 192 | |||
| 193 | Other predefined finders are listed in "default_finders" in L<Dist::Zilla::Role::FileFinderUser>. | ||
| 194 | You can define your own with the L<FileFinder::ByName plugin|Dist::Zilla::Plugin::FileFinder::ByName>. | ||
| 195 | |||
| 196 | =head2 file | ||
| 197 | |||
| 198 | This is a filename to also test, in addition to any files found earlier. | ||
| 199 | |||
| 200 | This option can be repeated to specify multiple additional files. | ||
| 201 | |||
| 202 | =head2 exclude | ||
| 203 | |||
| 204 | This is a regular expression of filenames to exclude. | ||
| 205 | |||
| 206 | This option can be repeated to specify multiple patterns. | ||
| 207 | |||
| 208 | =head2 script | ||
| 209 | |||
| 210 | This specifies the scripts to test for. If none are specified, it defaults to the defaults for L<Test::MixedScripts>. | ||
| 211 | |||
| 212 | =for Pod::Coverage dump_config | ||
| 213 | |||
| 214 | =for Pod::Coverage gather_files | ||
| 215 | |||
| 216 | =for Pod::Coverage munge_files | ||
| 217 | |||
| 218 | =for Pod::Coverage mvp_aliases | ||
| 219 | |||
| 220 | =for Pod::Coverage register_prereqs | ||
| 221 | |||
| 222 | =head1 KNOWN ISSUES | ||
| 223 | |||
| 224 | The default L</finder> does not include XS-related files. You will have to add them manually using the L</file> option, | ||
| 225 | for example, in the F<dist.ini>: | ||
| 226 | |||
| 227 | [Test::MixedScripts] | ||
| 228 | file = XS.xs | ||
| 229 | file = XS.c | ||
| 230 | |||
| 231 | =head1 SUPPORT | ||
| 232 | |||
| 233 | Only the latest version of this module will be supported. | ||
| 234 | |||
| 235 | This module requires Perl v5.20 or later, based on the minimum Perl supported by L<Dist::Zilla>. | ||
| 236 | |||
| 237 | =head2 Reporting Bugs and Submitting Feature Requests | ||
| 238 | |||
| 239 | Please report any bugs or feature requests on the bugtracker website | ||
| 240 | L<https://github.com/robrwo/perl-Dist-Zilla-Plugin-Test-MixedScripts/issues> | ||
| 241 | |||
| 242 | When submitting a bug or request, please include a test-file or a | ||
| 243 | patch to an existing test-file that illustrates the bug or desired | ||
| 244 | feature. | ||
| 245 | |||
| 246 | If the bug you are reporting has security implications which make it inappropriate to send to a public issue tracker, | ||
| 247 | then see F<SECURITY.md> for instructions how to report security vulnerabilities. | ||
| 248 | |||
| 249 | =head1 SOURCE | ||
| 250 | |||
| 251 | The development version is on github at L<https://github.com/robrwo/perl-Dist-Zilla-Plugin-Test-MixedScripts> | ||
| 252 | and may be cloned from L<git://github.com/robrwo/perl-Dist-Zilla-Plugin-Test-MixedScripts.git> | ||
| 253 | |||
| 254 | =head1 AUTHOR | ||
| 255 | |||
| 256 | Robert Rothenberg <rrwo@cpan.org> | ||
| 257 | |||
| 258 | This code was based on L<Dist::Zilla::Plugin::Test::EOL> by Florian Ragwitz <rafl@debian.org>, Caleb Cushing | ||
| 259 | <xenoterracide@gmail.com> and Karen Etheridge <ether@cpan.org>. | ||
| 260 | |||
| 261 | =head1 CONTRIBUTOR | ||
| 262 | |||
| 263 | =for stopwords Graham Knop | ||
| 264 | |||
| 265 | Graham Knop <haarg@haarg.org> | ||
| 266 | |||
| 267 | =head1 COPYRIGHT AND LICENSE | ||
| 268 | |||
| 269 | This software is copyright (c) 2025-2026 by Robert Rothenberg. | ||
| 270 | |||
| 271 | This is free software; you can redistribute it and/or modify it under | ||
| 272 | the same terms as the Perl 5 programming language system itself. | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | __DATA__ |