Skip to content

Commit

Permalink
Issue #87: Start with support for running scripts as regular perl scr…
Browse files Browse the repository at this point in the history
…ipts.
  • Loading branch information
bschmalhofer committed Jun 29, 2020
1 parent 26da672 commit 6ad9b69
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Kernel/System/UnitTest/Driver.pm
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,16 @@ sub Run {
}

# HERE the actual tests are run.
my $TestSuccess = eval ${$UnitTestFile}; ## no critic
# The test scripts do not necessarily use Kernel::System::UnitTest framework.
# Therefore skip the unconforming scripts.
my $TestSuccess;
if ( index( ${$UnitTestFile}, 'use vars (qw(\$Self));') ) {
$TestSuccess = eval ${$UnitTestFile}; ## no critic
}
else {
$TestSuccess = 1;
say STDERR "Skipped $File at it doesn't use Kernel::System::UnitTest::Driver";
}

if ( !$TestSuccess ) {
if ($@) {
Expand Down
68 changes: 68 additions & 0 deletions Kernel/System/UnitTest/RegisterDriver.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# --
# OTOBO is a web-based ticketing system for service organisations.
# --
# Copyright (C) 2020 Rother OSS GmbH, https://otobo.de/
# --
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# --

package Kernel::System::UnitTest::RegisterDriver;

use 5.24.0;
use warnings;

# assuming that tests will be run from the otobo dir
use lib '.';
use lib 'Kernel/cpan-lib';
use lib 'Custom';

use parent 'Exporter';
our @EXPORT = qw($Self);

# core modules

# CPAN modules

# OTOBO modules
use Kernel::System::ObjectManager;

sub import {
# RegisterDriver is meant for test scripts,
# meaning that each sript has it's own process.
# This means that we don't have to localize $Kernel::OM
$Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::Log' => {
LogPrefix => 'OTOBO-otobo.UnitTest',
},
);

$Kernel::System::UnitTest::RegisterDriver::Self = $Kernel::OM->Create(
'Kernel::System::UnitTest::Driver',
ObjectParams => {
Verbose => 10000000000000,
ANSI => 0,
},
);

# Provide $Self as 'Kernel::System::UnitTest' for convenience.
$Kernel::OM->ObjectInstanceRegister(
Package => 'Kernel::System::UnitTest::Driver',
Object => $Kernel::System::UnitTest::RegisterDriver::Self,
Dependencies => [],
);

$Kernel::System::UnitTest::RegisterDriver::Self->{OutputBuffer} = '';

# Exports $Self to the callers namespace.
# @_ is usually empty.
Kernel::System::UnitTest::RegisterDriver->export_to_level(1, @_);
}

1;
5 changes: 5 additions & 0 deletions scripts/test/Valid.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use strict;
use warnings;
use utf8;

# Set up $Self when we are running as a standalone script.
use if __PACKAGE__ ne 'Kernel::System::UnitTest::Driver', 'lib', '.';
use if __PACKAGE__ ne 'Kernel::System::UnitTest::Driver', 'Kernel::System::UnitTest::RegisterDriver';

use vars (qw($Self));

# get needed objects
Expand Down Expand Up @@ -95,5 +99,6 @@ for my $ValidIDKey ( sort keys %ValidList ) {
}

# cleanup cache is done by RestoreDatabase
# DoneTesting() is called by Kernel::System::UnitTest::DoneTesting

1;

0 comments on commit 6ad9b69

Please sign in to comment.