Skip to content

Commit

Permalink
Merge pull request #2493 from RotherOSS/issue-#2490-dev_unittest_run_…
Browse files Browse the repository at this point in the history
…file

Issue #2490 dev unittest run file
  • Loading branch information
bschmalhofer authored Aug 26, 2023
2 parents e6d7034 + a5a7803 commit 5e28664
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 86 deletions.
20 changes: 14 additions & 6 deletions Kernel/System/Console/Command/Dev/UnitTest/Run.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ sub Configure {
);
$Self->AddOption(
Name => 'test',
Description => "Filter file list, allow to run test scripts matching a pattern, e.g. 'Ticket' or 'Ticket/ArchiveFlags' (can be specified several times).",
Required => 0,
HasValue => 1,
Multiple => 1,
ValueRegex => qr/.*/smx,
Description =>
"Run individual test files. The trailing '.t' is optional. E.g. 'Ticket' or 'Ticket.t'. Add parent dirs for disambiguation, e.g. 'GenericAgent/Run.t'. The option may be specified several times.",
Required => 0,
HasValue => 1,
Multiple => 1,
ValueRegex => qr/.*/smx,
);
$Self->AddOption(
Name => 'sopm',
Expand All @@ -72,7 +73,7 @@ sub Configure {
);
$Self->AddOption(
Name => 'verbose',
Description => 'Show details for all tests, not just failing.',
Description => 'Show details for all tests, not just for the failing tests.',
Required => 0,
HasValue => 0,
);
Expand All @@ -96,6 +97,12 @@ sub Configure {
ValueRegex => qr/.*/smx,
Multiple => 1
);
$Self->AddArgument(
Name => 'test-script-path',
Description => "Path to a directory with test scripts or to a single test script. All other test selection options will be ignored.",
Required => 0,
ValueRegex => qr/.*/smx,
);

return;
}
Expand All @@ -117,6 +124,7 @@ sub Run {

my $FunctionResult = $Kernel::OM->Get('Kernel::System::UnitTest')->Run(
Tests => $Self->GetOption('test'),
TestScriptPath => $Self->GetArgument('test-script-path'),
Directory => $Self->GetOption('directory'),
SOPMFiles => $Self->GetOption('sopm'),
Packages => $Self->GetOption('package'),
Expand Down
187 changes: 107 additions & 80 deletions Kernel/System/UnitTest.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ run all or some tests located in C<scripts/test/**/*.t> and print the result.
$UnitTestObject->Run(
Tests => ['JSON', 'User'], # optional, execute certain test files only
TestScriptPath => 'scripts/test/DB', # optional, execute a single specific test script or scripts in dir
Directory => 'Selenium', # optional, execute only the tests in a subdirectory relative to scripts/test
SOPMFiles => ['FAQ.sopm', 'Fred.sopm' ], # optional, execute only the tests in the Filelist of the .sopm files
Packages => ['Survey', 'TimeAccounting' ], # optional, execute only the tests in the Filelist of the installed package
Expand Down Expand Up @@ -125,106 +126,133 @@ sub Run {
my ( $Self, %Param ) = @_;

# handle parameters
my $Verbosity = $Param{Verbose} // 0; # print test results when set to 1
my $Merge = $Param{Merge} // 0;
my $DoShuffle = $Param{Shuffle} // 0;
my $DirectoryParam = $Param{Directory}; # either a scalar or an array ref
my @ExecuteTestPatterns = ( $Param{Tests} // [] )->@*;
my @SOPMFiles = ( $Param{SOPMFiles} // [] )->@*;
my @Packages = ( $Param{Packages} // [] )->@*;
my $Verbosity = $Param{Verbose} // 0; # print test results when set to 1
my $Merge = $Param{Merge} // 0;
my $DoShuffle = $Param{Shuffle} // 0;
my $DirectoryParam = $Param{Directory}; # either a scalar or an array ref
my @SOPMFiles = ( $Param{SOPMFiles} // [] )->@*;
my @Packages = ( $Param{Packages} // [] )->@*;
my $TestScriptPath = $Param{TestScriptPath};

# The tests specified with the option --test indicate the file name
# or optionally one or more parent directories.
# The trailing .t is appended unless it was already passed.
my @ExecuteTestPatterns =
map {qr!/\Q$_\E$!smx}
map { m/\.t$/ ? $_ : "$_.t" }
( $Param{Tests} // [] )->@*;

# some config stuff
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $Home = $ConfigObject->Get('Home');
my $Product = join ' ', $ConfigObject->Get('Product'), $ConfigObject->Get('Version');
my $Host = hostname();

# run tests in a subdir when requested
my $TestDirectory = "$Home/scripts/test";
my @Directories;
if ( !$DirectoryParam ) {
push @Directories, $TestDirectory;
}
elsif ( ref $DirectoryParam eq 'ARRAY' ) {
for my $Directory ( $DirectoryParam->@* ) {
push @Directories, "$TestDirectory/$Directory";
my @ActualTestScripts;
if ( defined $TestScriptPath ) {

# every other option is ignored
if ( -f $TestScriptPath ) {
push @ActualTestScripts, $TestScriptPath;
}
elsif ( -d $TestScriptPath ) {

# no special handling of 'Custom' dir
push @ActualTestScripts,
$Kernel::OM->Get('Kernel::System::Main')->DirectoryRead(
Directory => $TestScriptPath,
Filter => '*.t',
Recursive => 1,
);
}
else {
# do nothing
}
}
else {
push @Directories, "$TestDirectory/$DirectoryParam";
}
# run tests in a subdir when requested
my $TestDirectory = "$Home/scripts/test";
my @Directories;
if ( !$DirectoryParam ) {
push @Directories, $TestDirectory;
}
elsif ( ref $DirectoryParam eq 'ARRAY' ) {
for my $Directory ( $DirectoryParam->@* ) {
push @Directories, "$TestDirectory/$Directory";
}
}
else {
push @Directories, "$TestDirectory/$DirectoryParam";
}

# some cleanp, why ???
for my $Directory (@Directories) {
$Directory =~ s/\.//g;
}
# some cleanp, why ???
for my $Directory (@Directories) {
$Directory =~ s/\.//g;
}

# add the files from the .sopm files to the whitelist
if (@SOPMFiles) {
my $PackageObject = $Kernel::OM->Get('Kernel::System::Package');
SOPM_FILE:
for my $SopmFile (@SOPMFiles) {

# for now we only consider local files
next SOPM_FILE unless -f $SopmFile;
next SOPM_FILE unless -r $SopmFile;

my $ContentRef = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
Location => $SopmFile,
Mode => 'utf8',
Result => 'SCALAR',
);

return $Self->ExitCodeError unless ref $ContentRef eq 'SCALAR';
return $Self->ExitCodeError unless length $ContentRef->$*;

# Parse package.
my %Structure = $PackageObject->PackageParse(
String => $ContentRef,
);

next SOPM_FILE unless IsArrayRefWithData( $Structure{Filelist} );

# add the files from the .sopm files to the whitelist
if (@SOPMFiles) {
my $PackageObject = $Kernel::OM->Get('Kernel::System::Package');
SOPM_FILE:
for my $SopmFile (@SOPMFiles) {

# for now we only consider local files
next SOPM_FILE unless -f $SopmFile;
next SOPM_FILE unless -r $SopmFile;

my $ContentRef = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
Location => $SopmFile,
Mode => 'utf8',
Result => 'SCALAR',
);

return $Self->ExitCodeError unless ref $ContentRef eq 'SCALAR';
return $Self->ExitCodeError unless length $ContentRef->$*;

# Parse package.
my %Structure = $PackageObject->PackageParse(
String => $ContentRef,
);

next SOPM_FILE unless IsArrayRefWithData( $Structure{Filelist} );

# for some reason the trailing .t is checked seperately
# so remove it here, when the patterns are set up
push @ExecuteTestPatterns,
map {s/\.t$//r}
grep {m!^scripts/test/!}
map { $_->{Location} }
$Structure{Filelist}->@*;
# collect all test scripts below scripts/test
push @ExecuteTestPatterns,
map {qr!/\Q$_\E$!smx}
grep {m!^scripts/test/!}
map { $_->{Location} }
$Structure{Filelist}->@*;
}
}
}

# add the files from the installed packages to the whitelist
if (@Packages) {
# add the files from the installed packages to the whitelist
if (@Packages) {

# get the details of all installed packages
my $PackageObject = $Kernel::OM->Get('Kernel::System::Package');
my @PackageList = $PackageObject->RepositoryList();
my %PackageListLookup = map { $_->{Name}->{Content} => $_ } @PackageList;
# get the details of all installed packages
my $PackageObject = $Kernel::OM->Get('Kernel::System::Package');
my @PackageList = $PackageObject->RepositoryList();
my %PackageListLookup = map { $_->{Name}->{Content} => $_ } @PackageList;

PACKAGE:
for my $Package (@Packages) {
PACKAGE:
for my $Package (@Packages) {

# Silently ignore not installed packages
next PACKAGE unless $PackageListLookup{$Package};
# Silently ignore not installed packages
next PACKAGE unless $PackageListLookup{$Package};

# package is already parsed
my %Structure = $PackageListLookup{$Package}->%*;
# package is already parsed
my %Structure = $PackageListLookup{$Package}->%*;

next PACKAGE unless IsArrayRefWithData( $Structure{Filelist} );
next PACKAGE unless IsArrayRefWithData( $Structure{Filelist} );

# for some reason the trailing .t is checked seperately
push @ExecuteTestPatterns,
map {s/\.t$//r}
grep {m!^scripts/test/!}
map { $_->{Location} }
$Structure{Filelist}->@*;
# collect all test scripts below scripts/test
push @ExecuteTestPatterns,
map {qr!/\Q$_\E$!smx}
grep {m!^scripts/test/!}
map { $_->{Location} }
$Structure{Filelist}->@*;
}
}
}

my @ActualTestScripts;
{
# Collect the files in default directory or in the passed directories.
# An empty list will be returned when $Directory is empty or when it does not exist.
my @Files;
Expand All @@ -249,14 +277,13 @@ sub Run {
if (@ExecuteTestPatterns) {
@Files = grep {
my $File = $_;
any { $File =~ m!/\Q$_\E\.t$!smx } @ExecuteTestPatterns
any { $File =~ $_ } @ExecuteTestPatterns
} @Files;
}

# Check if a file with the same path and name exists in the Custom folder.
FILE:
for my $File (@Files) {

my $CustomFile = $File =~ s{ \A $Home }{$Home/Custom}xmsr;
push @ActualTestScripts, -e $CustomFile ? $CustomFile : $File;
}
Expand Down

0 comments on commit 5e28664

Please sign in to comment.