Skip to content

Commit

Permalink
fix: use relative path to find tests expected results
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarel committed Nov 23, 2021
1 parent 3b6f92f commit 27392a7
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
11 changes: 7 additions & 4 deletions t/attributes.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use Getopt::Long;


use JSON::PP;
use File::Basename "dirname";

my $json = JSON::PP->new->allow_nonref->canonical;

Expand All @@ -34,11 +35,12 @@ init_packaging_taxonomies_regexps();

load_forest_footprint_data();

my $expected_dir = dirname(__FILE__) . "/expected_test_results";
my $testdir = "attributes";

my $usage = <<TXT
The expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -178,11 +180,12 @@ foreach my $test_ref (@tests) {

compute_attributes($product_ref, $product_ref->{lc}, "world", $options_ref);

# Travis has a different $server_domain, so we need to change the resulting URLs
# Travis and docker has a different $server_domain, so we need to change the resulting URLs
# $got->{attribute_groups_fr}[0]{attributes}[0]{icon_url} = 'https://static.off.travis-ci.org/images/attributes/nutriscore-unknown.svg'
# $expected->{attribute_groups_fr}[0]{attributes}[0]{icon_url} = 'https://static.openfoodfacts.dev/images/attributes/nutriscore-unknown.svg'

# code below from https://www.perlmonks.org/?node_id=1031287


use Scalar::Util qw/reftype/;

Expand All @@ -205,7 +208,7 @@ foreach my $test_ref (@tests) {
}
}

walk $product_ref, sub { $_[0] =~ s/https:\/\/([^\/]+)\//https:\/\/server_domain\//; };
walk $product_ref, sub { $_[0] =~ s/https?:\/\/([^\/]+)\//https:\/\/server_domain\//; };

# Save the result

Expand All @@ -217,7 +220,7 @@ foreach my $test_ref (@tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand Down
6 changes: 4 additions & 2 deletions t/ecoscore.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Test::Number::Delta relative => 1.001;
use Log::Any::Adapter 'TAP';

use JSON;
use File::Basename "dirname";
use Getopt::Long;

use ProductOpener::Config qw/:all/;
Expand Down Expand Up @@ -64,11 +65,12 @@ foreach my $tagtype (keys %tags) {
}
}

my $expected_dir = dirname(__FILE__) . "/expected_test_results";
my $testdir = "ecoscore";

my $usage = <<TXT
The expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -535,7 +537,7 @@ foreach my $test_ref (@tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand Down
6 changes: 4 additions & 2 deletions t/forest_footprint.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use Log::Any::Adapter 'TAP';

use JSON;
use Getopt::Long;
use File::Basename "dirname";

use ProductOpener::Config qw/:all/;
use ProductOpener::Tags qw/:all/;
Expand All @@ -18,11 +19,12 @@ use ProductOpener::ForestFootprint qw/:all/;

load_forest_footprint_data();

my $expected_dir = dirname(__FILE__) . "/expected_test_results";
my $testdir = "forest_footprint";

my $usage = <<TXT
The expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -131,7 +133,7 @@ foreach my $test_ref (@tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand Down
10 changes: 6 additions & 4 deletions t/import_gs1.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ use Log::Any qw($log);

use JSON;
use Getopt::Long;
use File::Basename "dirname";

use ProductOpener::Config qw/:all/;
use ProductOpener::GS1 qw/:all/;
use ProductOpener::Food qw/:all/;

my $expected_dir = dirname(__FILE__) . "/expected_test_results";
my $testdir = "import_gs1";

my $usage = <<TXT
The input test files and the expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The input test files and the expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -55,7 +57,7 @@ my $json = JSON->new->allow_nonref->canonical;

my $dh;

opendir ($dh, "$data_root/t/expected_test_results/$testdir") or die("Could not open the $data_root/t/expected_test_results/$testdir directory: $!\n");
opendir ($dh, "$expected_dir/$testdir") or die("Could not open the $expected_dir/$testdir directory: $!\n");

foreach my $file (sort(readdir($dh))) {

Expand All @@ -69,7 +71,7 @@ foreach my $file (sort(readdir($dh))) {
init_csv_fields();

my $products_ref = [];
my $product_ref = read_gs1_json_file("$data_root/t/expected_test_results/$testdir/$file", $products_ref);
my $product_ref = read_gs1_json_file("$expected_dir/$testdir/$file", $products_ref);

# Save the result

Expand All @@ -81,7 +83,7 @@ foreach my $file (sort(readdir($dh))) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$testid.off.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$testid.off.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand Down
6 changes: 4 additions & 2 deletions t/ingredients.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ use Log::Any::Adapter 'TAP';

use JSON;
use Getopt::Long;
use File::Basename "dirname";

use ProductOpener::Config qw/:all/;
use ProductOpener::Tags qw/:all/;
use ProductOpener::TagsEntries qw/:all/;
use ProductOpener::Ingredients qw/:all/;

my $expected_dir = dirname(__FILE__) . "/expected_test_results";
my $testdir = "ingredients";

my $usage = <<TXT
The expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -388,7 +390,7 @@ foreach my $test_ref (@tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand Down
6 changes: 4 additions & 2 deletions t/nutriscore.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use warnings;

use JSON;
use Getopt::Long;
use File::Basename "dirname";

use Test::More;
use Test::Number::Delta relative => 1.001;
Expand All @@ -16,11 +17,12 @@ use ProductOpener::Food qw/:all/;
use ProductOpener::Ingredients qw/:all/;
use ProductOpener::Nutriscore qw/:all/;

my $expected_dir = dirname(__FILE__) . "/expected_test_results";
my $testdir = "nutriscore";

my $usage = <<TXT
The expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -94,7 +96,7 @@ foreach my $test_ref (@tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand Down
6 changes: 4 additions & 2 deletions t/packaging.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ use Log::Any::Adapter 'TAP';

use JSON;
use Getopt::Long;
use File::Basename "dirname";

use ProductOpener::Config qw/:all/;
use ProductOpener::Packaging qw/:all/;

my $testdir = "packaging";
my $expected_dir = dirname(__FILE__) . "/expected_test_results";

my $usage = <<TXT
The expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -417,7 +419,7 @@ foreach my $test_ref (@tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand Down
8 changes: 5 additions & 3 deletions t/recipes.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ use Log::Any::Adapter 'TAP';

use JSON;
use Getopt::Long;
use File::Basename "dirname";

use ProductOpener::Config qw/:all/;
use ProductOpener::Tags qw/:all/;
use ProductOpener::TagsEntries qw/:all/;
use ProductOpener::Ingredients qw/:all/;
use ProductOpener::Recipes qw/:all/;

my $expected_dir = dirname(__FILE__) . "/expected_test_results";
my $testdir = "recipes";

my $usage = <<TXT
The expected results of the tests are saved in $data_root/t/expected_test_results/$testdir
The expected results of the tests are saved in $expected_dir/$testdir
To verify differences and update the expected test results, actual test results
can be saved to a directory by passing --results [path of results directory]
Expand Down Expand Up @@ -148,7 +150,7 @@ foreach my $recipes_test_ref (@recipes_tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$recipes_testid.$testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$recipes_testid.$testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_product_ref = $json->decode(<$expected_result>);
Expand All @@ -172,7 +174,7 @@ foreach my $recipes_test_ref (@recipes_tests) {

# Compare the result with the expected result

if (open (my $expected_result, "<:encoding(UTF-8)", "$data_root/t/expected_test_results/$testdir/$recipes_testid.json")) {
if (open (my $expected_result, "<:encoding(UTF-8)", "$expected_dir/$testdir/$recipes_testid.json")) {

local $/; #Enable 'slurp' mode
my $expected_analysis_ref = $json->decode(<$expected_result>);
Expand Down

0 comments on commit 27392a7

Please sign in to comment.