Permalink
Browse files

Start organizing the code a bit and adding some tests

  • Loading branch information...
1 parent e7e8a0b commit 3a5429619cf7f1b30bc0712c7e957faeb26b601d @holdenk committed Sep 4, 2012
View
1 README
@@ -1,4 +1,5 @@
A very simple github bot. Uses an accout of the same name. It provides spelling corrections for README files and other simple suggestions.
+Requires pfff for the php code fixing to work.
See:
https://twitter.com/#!/HoldensUnicorn
https://github.com/holdensmagicalunicorn
@@ -1,14 +1,24 @@
+package Unicorn::Shell;
+
+use strict;
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw{check_shell fix_shell};
+
+
sub fix_shell {
my $file = shift @_;
my $rt = shift @_;
+ my @r = split(/\n/,$rt);
#Is it a shell script
if ($r[0] =~ /\#\!\/bin\/(ba|z|)sh/ || $r[0] =~ /\#\!\/bin\/env (ba|z|)sh/) {
#Probably!
#Handle with [-e foo -e bar]
# http://www.pixelbeat.org/programming/shell_script_mistakes.html
- $rt =~ /\[\s*\-e\s*(\w+?)\s*\-e(\w+?)\s*\]/[-e $1] || [-e $2]/;
+# $rt =~ s/if\s*\[\s*\-e\s*(\w+?)\s*\-e(\w+?)\s*\]\s*$/if [-e $1] || [-e $2]/;
+ $rt =~ s/if\s*\[\s*\-e\s*(\w+?)\s*\-e\s*(\w+?)\s*\]\s*$/if [-e $1] || [\-e $2]/;
#Double negative
- $rt =~ /\[\s*\!\-z\s*(\"\$\w*\"\)]/[$1]/;
+ $rt =~ s/\[\s*\!\-z\s*(\"\$\w*\")\]/[$1]/;
#Check for cat pipe to grep
#i.e cat foo | grep baz
$rt =~ s/^\s*cat\s*(\w+)\s*\|\s*grep\s+([\w\"]+)\s*$/grep $2 < $1/
@@ -25,7 +35,7 @@ sub check_shell {
if ($r[0] =~ /\#\!\/bin\/(ba|z|)sh/ || $r[0] =~ /\#\!\/bin\/env (ba|z|)sh/) {
#Probably!
#Handle with [-e foo -e bar]
- if ($rt =~ /\[\s*\-e\s*\w*?\s*\-e\w*?\s*\]/) {
+ if ($rt =~ /\[\s*\-e\s*\w*?\s*\-e\s*\w*?\s*\]/) {
return 1;
}
#Double negative
@@ -42,8 +52,6 @@ sub check_shell {
return 0;
}
-use base 'Exporter';
-our @EXPORT = qw{check_shell fix_shell};
1;
File renamed without changes.
File renamed without changes.
@@ -1,9 +1,8 @@
-package lib::unicorn::errorcheck;
+package unicorn::errorcheck;
use strict;
use php;
use shell;
-
sub check_go {
my $file = shift @_;
my $rt = shift @_;
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,8 @@
+#Test the shell script thing
+use Test::More tests => 2;
+use Unicorn::Shell qw( fix_shell check_shell);
+
+is (fix_shell("fake","#!/bin/bash\nif [-e foo -e bar]"), "#!/bin/bash\nif [-e foo] || [-e bar]", "Expands -e works");
+is (fix_shell("fake","#!/bin/bash\nif [-e foo -f bar]"), "#!/bin/bash\nif [-e foo -f bar]", "Does not expand -e / -f");
+
+done_testing();

0 comments on commit 3a54296

Please sign in to comment.