Skip to content

Commit

Permalink
Issue #2490: support passing an explcit file or dir
Browse files Browse the repository at this point in the history
in the case all other options to Dev::UnitTest::Run,
 that select the tests scripts, are ignored
  • Loading branch information
bschmalhofer committed Aug 26, 2023
1 parent 6b89a69 commit a5a7803
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 70 deletions.
7 changes: 7 additions & 0 deletions Kernel/System/Console/Command/Dev/UnitTest/Run.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,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 @@ -118,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
162 changes: 92 additions & 70 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 @@ -131,6 +132,7 @@ sub Run {
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.
Expand All @@ -146,91 +148,111 @@ sub Run {
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;

# 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} );

# collect all test scripts below scripts/test
push @ExecuteTestPatterns,
map {qr!/\Q$_\E$!smx}
grep {m!^scripts/test/!}
map { $_->{Location} }
$Structure{Filelist}->@*;
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} );

# 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} );

# collect all test scripts below scripts/test
push @ExecuteTestPatterns,
map {qr!/\Q$_\E$!smx}
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 Down

0 comments on commit a5a7803

Please sign in to comment.