Test::Whitespaces
Perl is very flexible in may ways including whitespaces. Perl does not force you to use tab or spaces. The script works fine no matter if it has DOS or UNIX line breaks.
But there are some good practices. I preffer these rules:
- every line ends with "\n" (including the last line)
- UNIX way for new lines (only "\n", not "\r\n")
- no tabs, but 4 spaces instead
- no trailing spaces
- no empty lines in the end of the file
I want to make sure that all that rules are followed in my source code. So I've written Test::Whitespaces. There are several good things in this module.
You can create test file xt/whitespaces.t:
use Test::Whitespaces {
dirs => [ 'lib', 'bin', 't' ],
};
It will check all the files in these directories for errors in whitespaces. And it will output the errors in a very friendly manner.
Instead of writing test file you can use script test_whitespaces
that will show the problems. Here is the sample output. It is perfect, isn't it? =)
In case you have found some errors there is a simple way to fix them. There is a script whiter
that can fix all the errors in the project files.
I'm using this module to check and fix whitespaces not only in Perl source code, but also in test files and in templates and css/js.
I've written this module with the main idea to simplify the developers work. It has good documentation and it is as simple as possible.
I really hope that this module can be helpful for the Perl developers. And I would be happy in you express you opinion about this module. =)
PS And I want to thank play-perl.org for endorsing my work.
Isn't Perl::Tidy a better choice for this? You can also make Perl::Critic check the tidiness...
It doesn't check for code formatting, but Test::EOL and Dist::Zilla::Plugin::EOLTests checks for trailing whitespaces and for tabs.
I think this is nice and useful and I hope to use it soon.
But: Running the test script from the synopsis using prove doesn't output anything helpful:
Which files need fixes where?
There is also Test::NoTabs and Test::Tabs (mine) on CPAN.
> Isn't Perl::Tidy a better choice for this? You can also make Perl::Critic check the tidiness...
Yes, you are 100% correct. Perl::Tidy and Perl::Critic can solve some whitespaces errors.
Test::Whitespaces has this advantages: Test::Whitespaces is much easier than Perl:Tidy and Perl:Critic and Test::Whitespaces test can be used to check not only Perl files, but also some templates or css/js files.
Actually I would appreciate very much If somebody will show me configs for Perl::Tidy and Perl::Critic that can check and fix all the errors that can fix Test::Whitespaces.
> It doesn't check for code formatting, but Test::EOL and Dist::Zilla::Plugin::EOLTests checks for trailing whitespaces and for tabs.
Thank you Ether. Yes, Test::Whitespaces works on the same problem as Test::EOL. And Test::EOL is in the "SEE ALSO" section of Test::Whitespaces (as well as some other modules).
> Running the test script from the synopsis using prove doesn't output anything helpful
Thank you for finding this issue! You will certainly see information about errors if you run `prove -v t/Whitespace.t`.
I'm not sure if there should be info about errors if you run `prove` without `-v`, I will check it in https://github.com/bessarabov/Test-Whitespaces/issues/10
Thank you!
> There is also Test::NoTabs and Test::Tabs (mine) on CPAN.
Great! Thank you! There was link to Test::NoTabs in the "SEE ALSO" section, I've added link to your module Test::Tabs https://github.com/bessarabov/Test-Whitespaces/commit/d40d2eb0acf0f68994a3a7f06cc2cde4a99c19c3
@confuseAcat, I want to thank you again for your bug report. =)
Today I have released new version of Test::Whitespaces 1.0.0 that output all errors not only when started with `prove -v`, but also when you run `prove` without `-v`.
https://github.com/bessarabov/Test-Whitespaces