Actually this script is called from another, which passes a $testDir variable so while this script has it initialized as empty it does get one on the command line. But that's a good point to check on and put in some error capturing, since that directory is consistent I can probably add something to make sure its going to get that value anyway and even if called on the command line it will be set.
This works well cross platform, the script is run on AIX, HPUX, Linux, Solaris and Windows and its been pretty solid on those, it just means any change I make has to be checked across those platforms as well. Ah well, life goes on. | [reply] |
Ok, here is the updated script. I can run it with the warnings on and get every single warning, but each system call is completing properly. Each status is being returned as 0. Very strange. If anyone can thing of something else to check on the system calls, I'd appreciate it.
use strict;
use warnings;
use Cwd;
use Getopt::Long;
my ($help) = 0;
my ($num) = 20; # Number of loops to run
my ($testdir) = "";
my ($verbose) = 0;
GetOptions('help|?' => \$help,
"num=i" => \$num,
"testdir=s" => \$testdir,
'verbose' => \$verbose) or &usage();
&usage() if $help;
# Header
print "Beginning Directory Manipulation Testing...\n" if $verbose;
$| = 1;
# Get the current working directory
my $home_dir = getcwd;
# Change to the test directory
chdir($testdir) or die "Can't make $testdir: $!\n";
# Set up test dirs if they do not exist
if (!-d "test")
{
mkdir("test") or die "Can't make $testdir\\test: $!\n";
}
if (!-d "test1")
{
mkdir("test1") or die "Can't make $testdir\\test1: $!\n";
}
# Change to the test area
chdir("test") or die "Can't change to test directory: $!\n";
# Directory Manipulation
for (my $dir = 0; $dir < $num; $dir++)
{
# Make the directory
if (!-d "directory$dir" ) {
mkdir("directory$dir", 0755) or die "Can't make $testdir\\test
+\\directory$dir: $!\n";
}
# Change permissions of the directory
if ($^O =~ /Win32/)
{
print "\nRunning chmod from - $home_dir\\..\\..\\bin\\chmod on
+ $testdir\\test\\directory$dir.\n" if ($verbose);
if ($verbose)
{
system($home_dir . "\\..\\..\\bin\\chmod -v a+rwx $testdir
+\\test\\directory$dir");
}
else
{
system($home_dir . "\\..\\..\\bin\\chmod a+rwx $testdir\\t
+est\\directory$dir");
}
}
else
{
system("/bin/chmod 777 directory$dir");
or warn "Could not chmod directory$dir: $?\n";
}
# Copy the directory
if ($^O =~ /Win32/)
{
print "\nRunning $home_dir\\..\\..\\bin\\cp
on $testdir\\test\\directory$dir.\n" if ($verbose);
system("$home_dir\\..\\..\\bin\\cp -rv directory$dir copieddir
+ectory$dir");
}
else
{
system("/bin/cp -r directory$dir copieddirectory$dir");
or warn "Could not copy directory$dir to copieddirectory$dir
+: $?\n";
}
# Rename the directory
if (!-d "copieddirectory$dir")
{
print "copieddirectory$dir does not exist!\n" if $verbose;
}
if ($^O =~ /Win32/)
{
system("move copieddirectory$dir ..\\test1\\moveddirectory$dir
+ > out.txt");
if (-e "out.txt")
{
unlink("out.txt") or die "Can't unlink out.txt: $!\n";
}
}
else
{
system("/bin/mv copieddirectory$dir ../test1/moveddirectory$di
+r");
or warn "Could not move directory$dir to test1: $?\n";
}
# Delete the copied directory
if ($^O =~ /Win32/)
{
rmdir("..\\test1\\moveddirectory$dir");
}
else
{
system("/bin/rm -r ../test1/moveddirectory$dir");
or warn "Could not remove test1\\directory$dir: $?\n";
}
# Remove the directory
if ($^O =~ /Win32/)
{
rmdir("directory$dir") or die "Can't remove directory: $!\n";
}
else
{
system("/bin/rm -r directory$dir");
or warn "Could not remove directory$dir: $?\n";
}
}
print "complete!\n" if ($verbose);
# Change back to the top directory
chdir($home_dir) or die "Can't move back to $home_dir: $!\n";
sub usage
{
print "
$0 [options]
Options:
-num The number of loops to run, default $num
-testdir Location to run the test within
-verbose Run with messaging
\n";
exit(0);
}
| [reply] [d/l] |
use strict;
use warnings;
use Cwd;
use Getopt::Long;
use File::Spec; # this will automatically load OS-appropriate functi
+ons
# ... [snip] ...
# Get the current working directory
my $home_dir = getcwd;
# Change to the test directory
chdir($testdir) or die "$0: chdir $testdir: $!";
# Set up test dirs if they do not exist
for my $t ( qw/test test1/ ) {
mkdir $t or die "mkdir ". File::Spec->catfile($testdir, $t) .": $!
+\n";
}
# Change to the test area
chdir("test") or die "chdir test: $!\n";
# Directory Manipulation
for my $dir ( 0 .. $num-1 ) {
my $dirname = "directory$dir";
# Make the directory
if (!-d $dirname ) {
mkdir $dirname or
die "mkdir ". File::Spec->catfile( $testdir, "test", $dirn
+ame ) .": $!\n";
}
#...
I hope that gets the general idea across. It looks like your script is simply meant to check that certain operations work as hoped for, and doesn't really accomplish anything other than testing, so I won't pursue it further.
I'll just emphasize that you should use perl built-in functions and core modules (e.g. File::Copy whenever they are available, rather than system calls to shell commands -- perl already provides a lot of OS-independence for you, and you'll have fewer problems and less code to write that way.
If you really must use system calls to run OS-dependent tools, modularize and/or group those things so that they hang together for each OS. Your code tests the OS type in five different places, and you shouldn't have to do that test more than once (or not at all, with proper use of existing modules). | [reply] [d/l] [select] |
As I mentioned this is to test a filter driver, so I need to make the OS calls with system as they are checked differently than calls made within Perl; and really that is what I need to test. Things like the chmod example you mention are part of the test, to make sure the filter captures and does not barf on things like a chmod call on a directory. So, yes, it is testing and checking that certain operations work as hoped for.
I should do better with the OS dependency and group better, honestly though this script is much cleaner than the one I inherited, and its about 33% smaller than the original one as well. More cleanup on the OS stuff is on my list, I still have no idea why the System calls are suddenly acting up, but at least I can get by with them for now.
Thanks!
| [reply] |