From 308e45038121a1201c77b218b166009a8d0ad9eb Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Wed, 19 Mar 2025 17:48:57 +0100 Subject: [PATCH] fix: test from subdir (#1911) * Test * fix: move to root before testing * Revert alire-meta.ads * More readable test * Self-review --- src/alr/alr-commands-test.adb | 90 +++++++++++----------- testsuite/drivers/helpers.py | 11 ++- testsuite/tests/test/from-subdir/test.py | 23 ++++++ testsuite/tests/test/from-subdir/test.yaml | 3 + 4 files changed, 82 insertions(+), 45 deletions(-) create mode 100644 testsuite/tests/test/from-subdir/test.py create mode 100644 testsuite/tests/test/from-subdir/test.yaml diff --git a/src/alr/alr-commands-test.adb b/src/alr/alr-commands-test.adb index d7776b73..7d1104f2 100644 --- a/src/alr/alr-commands-test.adb +++ b/src/alr/alr-commands-test.adb @@ -95,7 +95,6 @@ package body Alr.Commands.Test is end if; end; end if; - if not Args.Is_Empty and then (Cmd.Jobs >= 0 or else All_Settings.Length > 1) then @@ -114,32 +113,37 @@ package body Alr.Commands.Test is & """--"" in the command line."); end if; - for Test_Setting of All_Settings loop - if Alire.Directories.Is_Directory (Settings (Test_Setting).Directory) - then - declare - use Alire.Directories; - - function Get_Args return AAA.Strings.Vector - is (if All_Settings.Length = 1 then Args - else AAA.Strings.Empty_Vector); - -- Only forward arguments if the runner is the only one. - - S : constant Settings := Settings (Test_Setting); - - Dir : constant Alire.Relative_Path := S.Directory; - Failures : Integer; - - Guard : Alire.Directories.Guard (Enter (Dir)) - with Unreferenced; - begin - Cmd.Optional_Root.Discard; - - if All_Settings.Length > 1 then - Alire.Put_Info ("running test with" & S.Image); - end if; - - case S.Runner.Kind is + declare + package Dirs renames Alire.Directories; + CD : Dirs.Guard (Dirs.Enter (Cmd.Root.Path)) with Unreferenced; + begin + for Test_Setting of All_Settings loop + if Alire.Directories.Is_Directory + (Settings (Test_Setting).Directory) + then + declare + use Alire.Directories; + + function Get_Args return AAA.Strings.Vector + is (if All_Settings.Length = 1 then Args + else AAA.Strings.Empty_Vector); + -- Only forward arguments if the runner is the only one. + + S : constant Settings := Settings (Test_Setting); + + Dir : constant Alire.Relative_Path := S.Directory; + Failures : Integer; + + Guard : Alire.Directories.Guard (Enter (Dir)) + with Unreferenced; + begin + Cmd.Optional_Root.Discard; + + if All_Settings.Length > 1 then + Alire.Put_Info ("running test with" & S.Image); + end if; + + case S.Runner.Kind is when Alire_Runner => Cmd.Requires_Workspace; @@ -156,22 +160,23 @@ package body Alr.Commands.Test is S.Runner.Command.Tail.Append (Get_Args), Dim_Output => False); - end case; + end case; - if Failures /= 0 then - Reportaise_Command_Failed - (if S.Runner.Kind = Alire_Runner then "" - else "test failure"); - end if; - end; - else - Trace.Error ("while running" & (Settings (Test_Setting).Image)); - Reportaise_Command_Failed - ("directory '" - & (Settings (Test_Setting).Directory) - & "' does not exist."); - end if; - end loop; + if Failures /= 0 then + Reportaise_Command_Failed + (if S.Runner.Kind = Alire_Runner then "" + else "test failure"); + end if; + end; + else + Trace.Error ("while running" & (Settings (Test_Setting).Image)); + Reportaise_Command_Failed + ("directory '" + & (Settings (Test_Setting).Directory) + & "' does not exist."); + end if; + end loop; + end; Alire.Put_Success ("Successful test run"); end Execute; @@ -216,7 +221,6 @@ package body Alr.Commands.Test is & " if 0", Default => -1, Argument => "N"); - Define_Switch (Config, Cmd.By_Id'Access, diff --git a/testsuite/drivers/helpers.py b/testsuite/drivers/helpers.py index a1911836..b7a47101 100644 --- a/testsuite/drivers/helpers.py +++ b/testsuite/drivers/helpers.py @@ -8,12 +8,19 @@ import platform import re import shutil import stat -import sys from subprocess import run from typing import Union from zipfile import ZipFile +def mkcd(dir: str, exist_ok: bool = True): + """ + Create a directory and cd into it + """ + os.makedirs(dir, exist_ok=exist_ok) + os.chdir(dir) + + # Return the entries (sorted) under a given folder, both folders and files # Optionally, return only those matching regex. Uses '/' always as separator. def contents(dir, regex=""): @@ -140,7 +147,7 @@ def offset_timestamp(file, seconds): """ Add offset to the modification time of a file """ - os.utime(file, (os.path.getatime(file), + os.utime(file, (os.path.getatime(file), os.path.getmtime(file) + seconds)) diff --git a/testsuite/tests/test/from-subdir/test.py b/testsuite/tests/test/from-subdir/test.py new file mode 100644 index 00000000..35b9d88a --- /dev/null +++ b/testsuite/tests/test/from-subdir/test.py @@ -0,0 +1,23 @@ +""" +Test that running `alr test` from a subdirectory other than the root and the +`tests` directory works as expected. +""" + +from drivers.alr import init_local_crate, run_alr +from drivers.asserts import assert_substring +from drivers.helpers import mkcd + +# Initialize a local crate with a test +init_local_crate("xxx", with_test=True) + +# Create a subdirectory and run the test from there +mkcd("subdir") +p = run_alr("test") +assert_substring("[ PASS ]", p.out) + +# Create another level of subdirectory +mkcd("subsubdir") +p = run_alr("test") +assert_substring("[ PASS ]", p.out) + +print("SUCCESS") diff --git a/testsuite/tests/test/from-subdir/test.yaml b/testsuite/tests/test/from-subdir/test.yaml new file mode 100644 index 00000000..fa855459 --- /dev/null +++ b/testsuite/tests/test/from-subdir/test.yaml @@ -0,0 +1,3 @@ +driver: python-script +indexes: + compiler_only_index: {} -- 2.39.5