Skip to content

Commit

Permalink
skip assignment and export of $TEST_FILE variable if the command line…
Browse files Browse the repository at this point in the history
… option '-e' is used
  • Loading branch information
jsf116 committed Aug 2, 2023
1 parent 2ceb12a commit 8f9f142
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 64 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Changelog for Test-Expander

2.1.5 2023-08-02
- Skip assignment and export of $TEST_FILE variable if the command line option '-e' is used.

2.1.4 2023-07-28
- Skip assignment of environment variables in case of undefined value.

Expand Down
6 changes: 3 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
"provides" : {
"Test::Expander" : {
"file" : "lib/Test/Expander.pm",
"version" : "2.1.4"
"version" : "2.1.5"
},
"Test::Expander::Constants" : {
"file" : "lib/Test/Expander/Constants.pm",
"version" : "2.1.4"
"version" : "2.1.5"
}
},
"release_status" : "stable",
Expand All @@ -76,5 +76,5 @@
"web" : "https://github.com/jsf116/Test-Expander"
}
},
"version" : "2.1.4"
"version" : "2.1.5"
}
6 changes: 3 additions & 3 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ no_index:
provides:
Test::Expander:
file: lib/Test/Expander.pm
version: '2.1.4'
version: '2.1.5'
Test::Expander::Constants:
file: lib/Test/Expander/Constants.pm
version: '2.1.4'
version: '2.1.5'
requires:
B: '0'
Const::Fast: '0'
Expand All @@ -39,4 +39,4 @@ requires:
resources:
bugtracker: https://github.com/jsf116/Test-Expander
repository: git://github.com/jsf116/Test-Expander.git
version: '2.1.4'
version: '2.1.5'
6 changes: 3 additions & 3 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use strict;
use warnings
FATAL => qw(all),
NONFATAL => qw(deprecated exec internal malloc newline portable recursion);
FATAL => qw( all ),
NONFATAL => qw( deprecated exec internal malloc newline portable recursion );

use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
Expand Down Expand Up @@ -33,7 +33,7 @@ WriteMakefile(
},
VERSION_FROM => 'lib/Test/Expander.pm',
test => {
TESTS => 't/Test/Expander/*.t t/Test/Expander/NoCLASS/*.t',
TESTS => 't/*/*/*.t t/*/*/*/*.t',
},
META_MERGE => {
'meta-spec' => { version => 2 },
Expand Down
28 changes: 15 additions & 13 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ This, of course, can be stored in additional variables declared somewhere at the
a single change of path and / or base name of the corresponding test file.

An additional benefit of suggested approach is a better readability of tests, where chunks like
```perl
```perl
Foo::Bar->baz( $arg0, $arg1 )
```
```
now look like
```perl
```perl
$CLASS->$METHOD( $arg0, $arg1 )
```
```
and hence clearly manifest that this chunk is about the testee.

- The frequent necessity of introduction of temporary directory and / or temporary file usually leads to the usage of
Expand All @@ -81,9 +81,9 @@ providing the methods / funtions **tempdir** and **tempfile**.

This, however, can significantly be simplified (and the size of test file can be reduced) requesting such introduction
via the options supported by **Test::Expander**:
```perl
```perl
use Test::Expander -tempdir => {}, -tempfile => {};
```
```
- Another fuctionality frequently used in tests relates to the work with files and directories:
reading, writing, creation, etc. Because almost all features required in such cases are provided by
[Path::Tiny](https://metacpan.org/pod/Path::Tiny), some functions of this module is also exported from
Expand Down Expand Up @@ -316,7 +316,7 @@ if the method / subroutine recognition is not disable and possible,
if the option **-tempdir** is supplied,
- variable **$TEMP\_FILE** containing the name of a temporary file created at compile time
if the option **-tempfile** is supplied.
- variable **$TEST\_FILE** containing the absolute name of the current test file.
- variable **$TEST\_FILE** containing the absolute name of the current test file (if any).
In fact its content is identical with the content of special token
[\_\_FILE\_\_](https://perldoc.perl.org/functions/__FILE__), but only in the test file itself!
If, however, you need the test file name in a test submodule or in a **.env** file belonging to this test,
Expand All @@ -333,10 +333,10 @@ In this case they are logged to STDOUT using [note](https://metacpan.org/pod/Tes

1. When another module is used, which in turn is based on [Test::Builder](https://metacpan.org/pod/Test::Builder) e.g.
[Test::Output](https://metacpan.org/pod/Test::Output):
```perl
```perl
use Test::Output;
use Test::Expander;
```
```
2. When some actions performed on the module level (e.g. determination of constants)
rely upon results of other actions (e.g. mocking of built-ins).

Expand All @@ -345,21 +345,23 @@ In this case they are logged to STDOUT using [note](https://metacpan.org/pod/Tes
the option **-builtin** should be used instead!)
to verify if the testee properly reacts both on its success and failure.
For this purpose a reasonable implementation might look as follows:
```perl
```perl
my $close_success;
BEGIN {
*CORE::GLOBAL::close = sub (*) { $close_success ? CORE::close( shift ) : 0 }
}

use Test::Expander;
```
```
- Array elements of the value supplied along with the option **-lib** are evaluated using
[string eval](https://perldoc.perl.org/functions/eval) so that constant strings would need duplicated quotes e.g.
```perl
```perl
use Test::Expander -lib => [ q('my_test_lib') ];
```
```
- If the value to be assigned to an environment variable after evaluation of an **.env** file is undefined,
such assignment is skipped.
- If **Test::Expander** is used in one-line mode (with the **-e** option),
the variable **$TEST\_FILE** is unset and not exported.

# AUTHOR

Expand Down
6 changes: 4 additions & 2 deletions lib/Test/Expander.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package Test::Expander;

# The versioning is conform with https://semver.org
our $VERSION = '2.1.4'; ## no critic (RequireUseStrict, RequireUseWarnings)
our $VERSION = '2.1.5'; ## no critic (RequireUseStrict, RequireUseWarnings)

use strict;
use warnings
Expand Down Expand Up @@ -186,7 +186,7 @@ sub _error {
sub _export_most_symbols {
my ( $options, $test_file ) = @_;

$TEST_FILE = path( $test_file )->absolute->stringify;
$TEST_FILE = path( $test_file )->absolute->stringify if path( $test_file )->exists;

return _export_symbols( %MOST_CONSTANTS_TO_EXPORT );
}
Expand Down Expand Up @@ -319,6 +319,8 @@ sub _read_env_file {
sub _set_env {
my ( $class, $test_file ) = @_;

return unless path( $test_file )->exists;

my $env_found = $FALSE;
my $new_env = {};
{
Expand Down
7 changes: 6 additions & 1 deletion lib/Test/Expander.pod
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ if the option B<-tempfile> is supplied.

=item

variable B<$TEST_FILE> containing the absolute name of the current test file.
variable B<$TEST_FILE> containing the absolute name of the current test file (if any).
In fact its content is identical with the content of special token
L<__FILE__|https://perldoc.perl.org/functions/__FILE__>, but only in the test file itself!
If, however, you need the test file name in a test submodule or in a B<.env> file belonging to this test,
Expand Down Expand Up @@ -591,6 +591,11 @@ L<string eval|https://perldoc.perl.org/functions/eval> so that constant strings
If the value to be assigned to an environment variable after evaluation of an B<.env> file is undefined,
such assignment is skipped.

=item

If B<Test::Expander> is used in one-line mode (with the B<-e> option),
the variable B<$TEST_FILE> is unset and not exported.

=back

=head1 AUTHOR
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Expander/Constants.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Test::Expander::Constants;

our $VERSION = '2.1.4'; ## no critic (RequireUseStrict, RequireUseWarnings)
our $VERSION = '2.1.5'; ## no critic (RequireUseStrict, RequireUseWarnings)

use strict;
use warnings
Expand Down
60 changes: 32 additions & 28 deletions t/Test/Expander/_set_env.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ can_ok( $CLASS, $METHOD );

ok( -d $TEMP_DIR, "temporary directory '$TEMP_DIR' created" );

my $classPath = $CLASS =~ s{::}{/}gr;
my $testPath = path( $TEMP_DIR )->child( 't' );
$testPath->child( $classPath )->mkpath;
my $class_path = $CLASS =~ s{::}{/}gr;
my $test_path = path( $TEMP_DIR )->child( 't' );
$test_path->child( $class_path )->mkpath;

{
local $CWD = $testPath->parent->stringify; ## no critic (ProhibitLocalVars)
local $CWD = $test_path->parent->stringify; ## no critic (ProhibitLocalVars)

my $testFile = path( 't' )->child( $classPath )->child( $METHOD . '.t' )->stringify;
my $envFile = path( 't' )->child( $classPath )->child( $METHOD . '.env' );
my $test_file = path( 't' )->child( $class_path )->child( $METHOD . '.t' )->stringify;
my $env_file = path( 't' )->child( $class_path )->child( $METHOD . '.env' );

is( Test2::Plugin::SRand->from, 'import arg', "random seed is supplied as 'time'" );

ok( lives { $METHOD_REF->( $CLASS, $test_file ) }, 'no test file detected' );

path( $test_file )->touch;

subtest '1st env variable filled from a variable, 2nd one kept from %ENV, 3rd one ignored' => sub {
our $var = 'abc';
my $name = 'ABC';
my $value = '$' . __PACKAGE__ . '::var';
$envFile->spew( "$name = $value\nJust a comment line\nX\nY" );
$env_file->spew( "$name = $value\nJust a comment line\nX\nY" );
%ENV = ( XXX => 'yyy', X => 'A' );

ok( lives { $METHOD_REF->( $CLASS, $testFile ) }, 'successfully executed' );
ok( lives { $METHOD_REF->( $CLASS, $test_file ) }, 'successfully executed' );
my $expected = { $name => lc( $name ), X => 'A' };
$expected->{ PWD } = $ENV{ PWD } if exists( $ENV{ PWD } );
is( \%ENV, $expected, "'%ENV' has the expected content" );
Expand All @@ -44,10 +48,10 @@ $testPath->child( $classPath )->mkpath;
subtest 'env variable filled by a self-implemented sub' => sub {
my $name = 'ABC';
my $value = __PACKAGE__ . "::testEnv( lc( '$name' ) )";
$envFile->spew( "$name = $value" );
$env_file->spew( "$name = $value" );
%ENV = ( XXX => 'yyy' );

ok( lives { $METHOD_REF->( $CLASS, $testFile ) }, 'successfully executed' );
ok( lives { $METHOD_REF->( $CLASS, $test_file ) }, 'successfully executed' );
my $expected = { $name => lc( $name ) };
$expected->{ PWD } = $ENV{ PWD } if exists( $ENV{ PWD } );
is( \%ENV, $expected, "'%ENV' has the expected content" );
Expand All @@ -56,68 +60,68 @@ $testPath->child( $classPath )->mkpath;
subtest "env variable filled by a 'File::Temp::tempdir'" => sub {
my $name = 'ABC';
my $value = 'File::Temp::tempdir';
$envFile->spew( "$name = $value" );
$env_file->spew( "$name = $value" );
%ENV = ( XXX => 'yyy' );

ok( lives { $METHOD_REF->( $CLASS, $testFile ) }, 'successfully executed' );
ok( lives { $METHOD_REF->( $CLASS, $test_file ) }, 'successfully executed' );
my %expected = ( $name => $value );
$expected{ PWD } = $ENV{ PWD } if exists( $ENV{ PWD } );
is( [ sort keys( %ENV ) ], [ sort keys( %expected ) ], "'%ENV' has the expected keys" );
ok( -d $ENV{ $name }, 'temporary directory exists' );
};

subtest 'env file does not exist' => sub {
$envFile->remove;
$env_file->remove;
%ENV = ( XXX => 'yyy' );

ok( lives { $METHOD_REF->( $CLASS, $testFile ) }, 'successfully executed' );
ok( lives { $METHOD_REF->( $CLASS, $test_file ) }, 'successfully executed' );
my $expected = { XXX => 'yyy' };
$expected->{ PWD } = $ENV{ PWD } if exists( $ENV{ PWD } );
is( \%ENV, $expected, "'%ENV' remained unchanged" );
};

subtest 'directory structure does not correspond to class hierarchy' => sub {
$envFile->remove;
$env_file->remove;
%ENV = ( XXX => 'yyy' );

ok( lives { $METHOD_REF->( 'ABC::' . $CLASS, $testFile ) }, 'successfully executed' );
ok( lives { $METHOD_REF->( 'ABC::' . $CLASS, $test_file ) }, 'successfully executed' );
my $expected = { XXX => 'yyy' };
$expected->{ PWD } = $ENV{ PWD } if exists( $ENV{ PWD } );
is( \%ENV, $expected, "'%ENV' remained unchanged" );
};

subtest 'multiple levels of env files, cascade usage of their entries, overwrite entry' => sub {
path( $envFile->parent->parent . '.env' )->spew( "C = '0'" );
path( $envFile->parent . '.env' )->spew( "A = '1'\nB = '2'\nD = \$ENV{ A } . \$ENV{ C }" );
$envFile->spew( "C = '3'" );
path( $env_file->parent->parent . '.env' )->spew( "C = '0'" );
path( $env_file->parent . '.env' )->spew( "A = '1'\nB = '2'\nD = \$ENV{ A } . \$ENV{ C }" );
$env_file->spew( "C = '3'" );
%ENV = ( XXX => 'yyy' );

local $CWD = $TEMP_DIR; ## no critic (ProhibitLocalVars)
ok( lives { $METHOD_REF->( $CLASS, $testFile ) }, 'successfully executed' );
ok( lives { $METHOD_REF->( $CLASS, $test_file ) }, 'successfully executed' );
my $expected = { A => '1', B => '2', C => '3', D => '10' };
$expected->{ PWD } = $ENV{ PWD } if exists( $ENV{ PWD } );
is( \%ENV, $expected, "'%ENV' has the expected content" );

path( $envFile->parent->parent . '.env' )->remove;
path( $envFile->parent . '.env' )->remove;
path( $env_file->parent->parent . '.env' )->remove;
path( $env_file->parent . '.env' )->remove;
};

subtest 'env file with invalid syntax' => sub {
my $name = 'ABC';
my $value = 'abc->';
$envFile->spew( "$name = $value" );
$env_file->spew( "$name = $value" );

my $expected = $FMT_INVALID_ENV_ENTRY =~ s/%d/0/r =~ s/%s/$envFile/r =~ s/%s/$name = $value/r =~ s/%s/.+/r;
like( dies { $METHOD_REF->( $CLASS, $testFile ) }, qr/$expected/, 'expected exception raised' );
my $expected = $FMT_INVALID_ENV_ENTRY =~ s/%d/0/r =~ s/%s/$env_file/r =~ s/%s/$name = $value/r =~ s/%s/.+/r;
like( dies { $METHOD_REF->( $CLASS, $test_file ) }, qr/$expected/, 'expected exception raised' );
};

subtest 'env file with undefined values' => sub {
my $name = 'ABC';
my $value = '$undefined';
$envFile->spew( "$name = $value" );
$env_file->spew( "$name = $value" );

my $expected = $FMT_INVALID_ENV_ENTRY =~ s/%d/0/r =~ s/%s/$envFile/r =~ s/%s/$name = $value/r =~ s/%s/.+/r =~ s/(\$)/\\$1/r;
like( dies { $METHOD_REF->( $CLASS, $testFile ) }, qr/$expected/m, 'expected exception raised' );
my $expected = $FMT_INVALID_ENV_ENTRY =~ s/%d/0/r =~ s/%s/$env_file/r =~ s/%s/$name = $value/r =~ s/%s/.+/r =~ s/(\$)/\\$1/r;
like( dies { $METHOD_REF->( $CLASS, $test_file ) }, qr/$expected/m, 'expected exception raised' );
};
}

Expand Down
Loading

0 comments on commit 8f9f142

Please sign in to comment.