PERL - traverse sub directories and get test case results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - traverse sub directories and get test case results
# 1  
Old 01-08-2011
PERL - traverse sub directories and get test case results

Hello,

I need help in creating a PERL script for parsing test result files to get the results (pass or fail). Each test case execution generates a directory with few files among which we are interested in .result file.

Lets say Testing is home directory. If i executed 2 test cases. It will create case1, case2 under Testing directory. Below is sample content of .result files present in case directories.

Testing/case1/file1.result
Code:
test case name:    testcase1.txt
passed:           1
failed:           0

Trial    Trial Start Time             Result
1    20110106 06:15:19.044 PST    PASSED

Testing/case2/file2.result
Code:
test case name:    testcase2.txt
passed:           1
failed:           0

Trial    Trial Start Time             Result
1    20110106 06:15:19.044 PST    FAILED

Script should traverse through sub directories, read .result file, get the last word in last line (1st file contains PASSED, 2nd contains FAILED). Now this result should be updated in testing XL based on test case name. Below is content of testing XL.

contents of XL before running script
Code:
S.No    TestCaseName    Result
1    testcase1.txt    
2    testcase2.txt

contents of XL after running script
Code:
S.No    TestCaseName    Result
1    testcase1.txt    PASSED
2    testcase2.txt    FAILED

Appreciate your help on this.

Last edited by ravi.videla; 01-08-2011 at 06:09 PM..
# 2  
Old 01-08-2011
While I am the first to work with PERL, I would turn to find and awk:
Code:
find "${@}" -type f -name .results -print | xargs awk '/^test case name:/ { testcase = $NF; } NR == FNR { printf "%-30s  %s\n", testcase, $NF; }'

but with PERL
Code:
use strict;
use warnings;

$\ = "\n";
$, = '';

my @FILELIST = ();
my @DIRLIST  = qw{ . };

# search the current directory and all subdirectories for result files

while (0 < @DIRLIST) {

    my $dir = shift @DIRLIST;

    unless (opendir DH, $dir) {
        print STDERR $dir, ': ', $!;
        next;
    }

    my @DL = readdir DH;
    close DH;

    for (@DL) {
        next if /^\./;

        my $entry = $dir . '/' . $_;

        if (m{\.results$}) {
            push @FILELIST, $entry;
            next;
        }
          
        if (-d $entry) {
            push @DIRLIST, $entry;
        }
    }

    closedir DH;
}

undef $/;
 
my %T = ();

# parse all result files

foreach my $file (@FILELIST) {
       
    unless (open FH, '<', $file) {
        print STDERR $file, ': ', $!;
        next;
    }

    $_ = <FH>;
    close FH;

    my ($testcase) = m{^test case name:\s+(\S+)\s*$}m;
    my ($status)   = m{(PASSED|FAILED)\s*$}m;

    unless (defined $testcase and defined $status) {
        print STDERR $file, ': not a results file';
        next;
    }

    if (defined $T{$testcase}) {
        print STDERR $file, ': duplicate testcase - ', $testcase, ' (', $status, ')';
        next;
    }
       
    $T{$testcase} = $status
}

$/ = "\n";

$_ = <>;
chomp;
print $_;

while (<>) {
    chomp;
    my ($sno, $testcase) = m{^\s*(\d+)\s+(\S+)\s*$};

    unless (defined $testcase) {
        print STDERR $ARGV, '(', $., '): malformed line - ', $_;
        next;
    }

    $T{$testcase} = 'UNDEFINED' unless defined $T{$testcase};
    printf "\%-4d \%-19s \%s\n", $sno, $testcase, $T{$testcase};
}


Last edited by m.d.ludwig; 01-08-2011 at 06:41 PM.. Reason: typo in "awk"
# 3  
Old 01-08-2011
on a side note Perl is the proper way to reference the language.
# 4  
Old 01-09-2011
I thought it would be perl. :-)
# 5  
Old 01-09-2011
Quote:
Originally Posted by m.d.ludwig
I thought it would be perl. :-)
nope - perl is the interpreter.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Traverse through directories

Hi all, Require help in completing a shell script. i do have a total of 90 directories where in we have different sub-directories and one of the sub directory named logs I need to go inside the logs subdirectory and check if a particular log is present or not. for example below is the... (3 Replies)
Discussion started by: bhaskar t
3 Replies

2. Shell Programming and Scripting

Trying to test for both upper and lower case directories

I am trying to get a script to print out whether a directory is lowercase uppercase or both. This is what I've got so far: echo -e read "enter name" read server for DIR in $(find /tmp/$server -type d -prune | sed 's/\.\///g');do if expr match "$server" "*$" > /dev/null; then echo "$server -... (7 Replies)
Discussion started by: newbie2010
7 Replies

3. OS X (Apple)

OS X 'find' nogroup/nouser doesn't traverse directories?

flamingo:~ joliver$ sudo find / -nogroup find: /dev/fd/4: No such file or directory find: /home: No such file or directory find: /Library: No such file or directory find: /net: No such file or directory find: /Network: No such file or directory find: /private: No such file or directory find:... (2 Replies)
Discussion started by: jnojr
2 Replies

4. Shell Programming and Scripting

Prefixing test case methods with letter 'test'

Hi, I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
Discussion started by: royalibrahim
5 Replies

5. Shell Programming and Scripting

find in given path do not want to traverse to its sub-directories

Hi All, My UNIX Version is: OS Name Release Version AIX appma538 3 5 I want to find certain files with some criterias under the given path. At the same time i want to find the files which resides under the given directory, but normal find traverse to its sub-directories... (4 Replies)
Discussion started by: Arunprasad
4 Replies

6. Programming

Ignore case in a test?

How do I ignore the case in an if condition..? EDIT: I put this in the wrong board...this is a linux script. if then echo "Same name." else echo "Different name." fi (1 Reply)
Discussion started by: Bandit390
1 Replies

7. HP-UX

Different results from awk, sed, tr in different directories???

I cannot explain why it gives different results when I try the awk, sed, tr command combination with the same input. (aondufd1)psoftfs:/aon/dev/psoft/FSTST88/autosys>echo ${MailFile} + echo mfint003pr.in1* mfint003pr.in1* (aondufd1)psoftfs:/aon/dev/psoft/FSTST88/autosys/scripts>echo ${MailFile}... (2 Replies)
Discussion started by: james_falco
2 Replies

8. UNIX for Dummies Questions & Answers

Fastest way to traverse through large directories

Hi! I have thousands of sub-directories, and hundreds of thousands of files in them. What is the fast way to find out which files are older than a certain date? Is the "find" command the fastest? Or is there some other way? Right now I have a C script that traverses through and checks... (5 Replies)
Discussion started by: sreedharange
5 Replies
Login or Register to Ask a Question