diff --git a/framework/test/test_fix_test_suite.sh b/framework/test/test_fix_test_suite.sh index 121290c0f..dad4c959f 100755 --- a/framework/test/test_fix_test_suite.sh +++ b/framework/test/test_fix_test_suite.sh @@ -11,6 +11,14 @@ HERE=$(cd `dirname $0` && pwd) source "$HERE/test.include" || exit 1 init +pid="Math" # any pid-bid should work +bid="1b" + +# Directory for test suites +suites_dir="$TMP_DIR/test_suites_dir" + +# ------------------------------------------------------------- Common functions + _check_output() { local USAGE="Usage: _check_output " if [ "$#" != 2 ]; then @@ -25,67 +33,102 @@ _check_output() { return 0 } -pid="Math" # any pid-bid should work -bid="1b" +_create_tar_bz2_file() { + local USAGE="Usage: _create_tar_bz2_file " + if [ "$#" != 3 ]; then + echo "$USAGE" >&2 + return 1 + fi -# Create a directory for test suites -suites_dir="$TMP_DIR/test_suites_dir" -rm -rf "$suites_dir" -mkdir -p "$suites_dir" + local pid="$1" + local bid="$2" + local suites_dir="$3" + + # Create a .tar.bz2 file with all test suites + pushd . > /dev/null 2>&1 + cd "$HERE/resources/input" + tar_bz2_file="$suites_dir/$pid-$bid-test.0.tar.bz2" + tar -jcvf "$tar_bz2_file" \ + foo/bar/FailingTests.java \ + foo/bar/InvalidImport.java \ + foo/bar/UnitTestsWithCompilationIssues.java \ + foo/bar/ValidTestClass.java || return 1 + popd > /dev/null 2>&1 -# Create a .tar.bz2 file with all test suites -pushd . > /dev/null 2>&1 -cd "$HERE/resources/input" - tar_bz2_file="$suites_dir/$pid-$bid-test.0.tar.bz2" - tar -jcvf "$tar_bz2_file" \ - foo/bar/FailingTests.java \ - foo/bar/InvalidImport.java \ - foo/bar/UnitTestsWithCompilationIssues.java \ - foo/bar/ValidTestClass.java || die "Was not possible to create '$tar_bz2_file' file!" -popd > /dev/null 2>&1 + return 0 +} -# Fix test suites -fix_test_suite.pl -p "$pid" -d "$suites_dir" -v "$bid" || die "Script 'fix_test_suite.pl' has failed!" +# ------------------------------------------------------------------- Test Cases -tar -jxvf "$tar_bz2_file" -C "$suites_dir" +_test_deletion_of_test_classes_and_test_cases() { + rm -rf "$suites_dir"; mkdir -p "$suites_dir" -# Check output of 'fix_test_suite.pl' script + _create_tar_bz2_file "$pid" "$bid" "$suites_dir" || die "Was not possible to create a .tar.bz2 file with all test suites!" -_check_output \ - "$HERE/resources/output/foo/bar/FailingTests.java" \ - "$suites_dir/foo/bar/FailingTests.java" + # Fix test suites + fix_test_suite.pl -p "$pid" -d "$suites_dir" -v "$bid" || die "Script 'fix_test_suite.pl' has failed!" -_check_output \ - "$HERE/resources/output/foo/bar/FailingTests.java.bak" \ - "$suites_dir/foo/bar/FailingTests.java.bak" + tar -jxvf "$tar_bz2_file" -C "$suites_dir" -_check_output \ - "$HERE/resources/output/foo/bar/InvalidImport.java.broken" \ - "$suites_dir/foo/bar/InvalidImport.java.broken" + # Check output of 'fix_test_suite.pl' script -_check_output \ - "$HERE/resources/output/foo/bar/UnitTestsWithCompilationIssues.java" \ - "$suites_dir/foo/bar/UnitTestsWithCompilationIssues.java" + _check_output \ + "$HERE/resources/output/foo/bar/FailingTests.java" \ + "$suites_dir/foo/bar/FailingTests.java" -_check_output \ - "$HERE/resources/output/foo/bar/UnitTestsWithCompilationIssues.java.bak" \ - "$suites_dir/foo/bar/UnitTestsWithCompilationIssues.java.bak" + _check_output \ + "$HERE/resources/output/foo/bar/FailingTests.java.bak" \ + "$suites_dir/foo/bar/FailingTests.java.bak" -_check_output \ - "$HERE/resources/output/foo/bar/ValidTestClass.java" \ - "$suites_dir/foo/bar/ValidTestClass.java" + _check_output \ + "$HERE/resources/output/foo/bar/InvalidImport.java.broken" \ + "$suites_dir/foo/bar/InvalidImport.java.broken" -fix_db="$suites_dir/fix" -[ -s "$fix_db" ] || die "There is not any 'fix' database file or it is empty!" + _check_output \ + "$HERE/resources/output/foo/bar/UnitTestsWithCompilationIssues.java" \ + "$suites_dir/foo/bar/UnitTestsWithCompilationIssues.java" -num_rows=$(wc -l "$fix_db" | cut -f1 -d' ') -[ "$num_rows" -eq "2" ] || die "Fix database file does not have 2 rows!" + _check_output \ + "$HERE/resources/output/foo/bar/UnitTestsWithCompilationIssues.java.bak" \ + "$suites_dir/foo/bar/UnitTestsWithCompilationIssues.java.bak" + + _check_output \ + "$HERE/resources/output/foo/bar/ValidTestClass.java" \ + "$suites_dir/foo/bar/ValidTestClass.java" + + return 0 +} + +_test_L_option_enabled() { + # are DBI and DBD:CSV available? + if perl -e 'use DBI;' 2>/dev/null && perl -e 'use DBD::CSV;' 2>/dev/null; then + rm -rf "$suites_dir"; mkdir -p "$suites_dir" + + _create_tar_bz2_file "$pid" "$bid" "$suites_dir" || die "Was not possible to create a .tar.bz2 file with all test suites!" + + # enable DBI by calling the script with -L option + fix_test_suite.pl -p "$pid" -d "$suites_dir" -v "$bid" -L || die "Script 'fix_test_suite.pl' has failed!" + + fix_db="$suites_dir/fix" + [ -s "$fix_db" ] || die "There is not any 'fix' database file or it is empty!" + + num_rows=$(wc -l "$fix_db" | cut -f1 -d' ') + [ "$num_rows" -eq "2" ] || die "Fix database file does not have 2 rows!" + + # project_id,version_id,test_suite_source,test_id,num_uncompilable_tests,num_uncompilable_test_classes,num_failing_tests + expected="$pid,$bid,test,1,2,1,3" + # convert DOS/Windows newline (CRLF) to Unix newline and check data of last row + actual=$(tr -d '\r' < "$fix_db" | tail -n1) + [ "$actual" == "$expected" ] || die "Expected '$expected' got '$actual'!" + else + die "Please make sure perl modules 'DBI' and 'DBD:CSV' are installed." + fi + + return 0 +} -# project_id,version_id,test_suite_source,test_id,num_uncompilable_tests,num_uncompilable_test_classes,num_failing_tests -expected="$pid,$bid,test,1,2,1,3" -# convert DOS/Windows newline (CRLF) to Unix newline and check data of last row -actual=$(tr -d '\r' < "$fix_db" | tail -n1) -[ "$actual" == "$expected" ] || die "Expected '$expected' got '$actual'!" +_test_deletion_of_test_classes_and_test_cases || die "Test '_test_deletion_of_test_classes_and_test_cases' has failed!" +_test_L_option_enabled || die "Test '_test_L_option_enabled' has failed!" rm -rf "$suites_dir" diff --git a/framework/util/fix_test_suite.pl b/framework/util/fix_test_suite.pl index 4f0798a48..6084f9a35 100755 --- a/framework/util/fix_test_suite.pl +++ b/framework/util/fix_test_suite.pl @@ -30,7 +30,7 @@ =head1 NAME =head1 SYNOPSIS - fix_test_suite.pl -p project_id -d suite_dir [-f include_file_pattern] [-v version_id] [-s test_suite_src] [-t tmp_dir] [-A] [-D] + fix_test_suite.pl -p project_id -d suite_dir [-f include_file_pattern] [-v version_id] [-s test_suite_src] [-t tmp_dir] [-A] [-D] [-L] =head1 OPTIONS @@ -77,6 +77,11 @@ =head1 OPTIONS Debug: Enable verbose logging and do not delete the temporary check-out directory (optional). +=item -L + +Logging: Enable logging of runtime information (optional). By default no database +is used to keep the runtime information generated by this script. + =back =head1 DESCRIPTION @@ -126,13 +131,12 @@ =head1 DESCRIPTION use Project; use Utils; use Log; -use DB; # # Process arguments and issue usage message if necessary. # my %cmd_opts; -getopts('p:d:v:s:t:f:AD', \%cmd_opts) or pod2usage(1); +getopts('p:d:v:s:t:f:ADL', \%cmd_opts) or pod2usage(1); pod2usage(1) unless defined $cmd_opts{p} and defined $cmd_opts{d}; @@ -149,6 +153,25 @@ =head1 DESCRIPTION if (defined $VID) { Utils::check_vid($VID); } + +# Enable logging if flag is set +undef my $dbh_out; # by default no DB is used +undef my $sth; +undef my @COLS; +if (defined $cmd_opts{L}) { + require DB; + import DB; + + # Get database handle for result table + $dbh_out = DB::get_db_handle($DB::TAB_FIX, $SUITE_DIR); + + $sth = $dbh_out->prepare("SELECT * FROM $DB::TAB_FIX WHERE $DB::PROJECT=? AND $DB::TEST_SUITE=? AND $DB::ID=? AND $DB::TEST_ID=?") + or die $dbh_out->errstr; + + # Cache column names for table fix + @COLS = DB::get_tab_columns($DB::TAB_FIX) or die "Cannot obtain table columns!"; +} + =pod =head2 Test Suites @@ -213,15 +236,6 @@ =head2 Logging # Line separator my $sep = "-"x80; -# Get database handle for result table -my $dbh_out = DB::get_db_handle($TAB_FIX, $SUITE_DIR); - -my $sth = $dbh_out->prepare("SELECT * FROM $TAB_FIX WHERE $PROJECT=? AND $TEST_SUITE=? AND $ID=? AND $TEST_ID=?") - or die $dbh_out->errstr; - -# Cache column names for table fix -my @COLS = DB::get_tab_columns($TAB_FIX) or die "Cannot obtain table columns!"; - # Log current time $LOG->log_time("Start fixing tests"); $LOG->log_msg("- Found " . scalar(@list) . " test archive(s)"); @@ -235,11 +249,13 @@ =head2 Logging my $project = Project::create_project($pid); $project->{prog_root} = $TMP_DIR; - # Skip existing entries - $sth->execute($pid, $src, $vid, $tid); - if ($sth->rows !=0) { - $LOG->log_msg(" - Skipping $name since results already exist in database!"); - next; + if (defined $sth) { + # Skip existing entries + $sth->execute($pid, $src, $vid, $tid); + if ($sth->rows != 0) { + $LOG->log_msg(" - Skipping $name since results already exist in database!"); + next; + } } my $num_failing_tests = 0; @@ -342,7 +358,9 @@ =head2 Logging _insert_row($pid, $vid, $src, $tid, $num_uncompilable_tests, $num_uncompilable_test_classes, $num_failing_tests); } -$dbh_out->disconnect(); +if (defined $dbh_out) { + $dbh_out->disconnect(); +} # Log current time $LOG->log_time("End fixing tests"); $LOG->close(); @@ -440,15 +458,19 @@ sub _insert_row { @_ >= 4 or die $ARG_ERROR; my ($pid, $vid, $suite, $test_id, $num_uncompilable_tests, $num_uncompilable_test_classes, $num_failing_tests) = @_; + if (not defined $dbh_out) { + return ; # explicitly do nothing + } + # Build data hash my $data = { - $PROJECT => $pid, - $ID => $vid, - $TEST_SUITE => $suite, - $TEST_ID => $test_id, - $NUM_UNCOMPILABLE_TESTS => $num_uncompilable_tests, - $NUM_UNCOMPILABLE_TEST_CLASSES => $num_uncompilable_test_classes, - $NUM_FAILING_TESTS => $num_failing_tests, + $DB::PROJECT => $pid, + $DB::ID => $vid, + $DB::TEST_SUITE => $suite, + $DB::TEST_ID => $test_id, + $DB::NUM_UNCOMPILABLE_TESTS => $num_uncompilable_tests, + $DB::NUM_UNCOMPILABLE_TEST_CLASSES => $num_uncompilable_test_classes, + $DB::NUM_FAILING_TESTS => $num_failing_tests, }; # Build row based on data hash @@ -460,5 +482,5 @@ sub _insert_row { # Concat values and write to database table my $row = join(",", @tmp); - $dbh_out->do("INSERT INTO $TAB_FIX VALUES ($row)"); + $dbh_out->do("INSERT INTO $DB::TAB_FIX VALUES ($row)"); }