From 22a8d770880de877f48fa2940f01ccdcedefb4a6 Mon Sep 17 00:00:00 2001 From: tali auster <120901234+atalii@users.noreply.github.com> Date: Fri, 27 Oct 2023 03:25:52 -0600 Subject: [PATCH] implement -C/--chdir flag (#1479) * implement -C/--chdir flag * use `alr` instead of Alire in help text. * test --chdir flag * clean up with-chdir test --- src/alr/alr-commands.adb | 15 +++++++++++++++ testsuite/tests/init/with-chdir/test.py | 22 ++++++++++++++++++++++ testsuite/tests/init/with-chdir/test.yaml | 4 ++++ 3 files changed, 41 insertions(+) create mode 100644 testsuite/tests/init/with-chdir/test.py create mode 100644 testsuite/tests/init/with-chdir/test.yaml diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index 07b75283..b14916eb 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -59,6 +59,8 @@ package body Alr.Commands is Command_Line_Config_Path : aliased GNAT.OS_Lib.String_Access; + Command_Line_Chdir_Target_Path : aliased GNAT.OS_Lib.String_Access; + -- Following aliased booleans are used by GNAT.Command_Line processing: Log_Quiet : Boolean renames Alire_Early_Elaboration.Switch_Q; @@ -143,6 +145,11 @@ package body Alr.Commands is "-c=", "--config=", "Override configuration folder location"); + Define_Switch (Config, + Command_Line_Chdir_Target_Path'Access, + "-C=", "--chdir=", + "Run `alr` in the given directory"); + Define_Switch (Config, Alire.Force'Access, "-f", "--force", @@ -501,6 +508,14 @@ package body Alr.Commands is end; end if; + -- chdir(2) if necessary. + + if Command_Line_Chdir_Target_Path /= null and then + Command_Line_Chdir_Target_Path.all /= "" + then + Ada.Directories.Set_Directory (Command_Line_Chdir_Target_Path.all); + end if; + Create_Alire_Folders; begin diff --git a/testsuite/tests/init/with-chdir/test.py b/testsuite/tests/init/with-chdir/test.py new file mode 100644 index 00000000..9e5fb785 --- /dev/null +++ b/testsuite/tests/init/with-chdir/test.py @@ -0,0 +1,22 @@ +""" +Test --chdir switch. +""" + +import os + +from drivers.alr import run_alr +from drivers.asserts import assert_match + +test_dir = os.getcwd() + +# Make a binary crate in test that it runs without cd'ing. +run_alr("init", "--bin", "xxx") +run_alr("--chdir=xxx", "run") + +# Test that changing to a non-existent directory fails. Technically, +# /dev/null/cantexist can exist, but it's more than reasonable to assume a +# system with this directory has bigger issues. +bad_chdir = run_alr("-C", "/dev/null/cantexist", "run", complain_on_error=False) +assert_match(".*directory \"/dev/null/cantexist\" does not exist.*", bad_chdir.out) + +print('SUCCESS') diff --git a/testsuite/tests/init/with-chdir/test.yaml b/testsuite/tests/init/with-chdir/test.yaml new file mode 100644 index 00000000..e7e3556b --- /dev/null +++ b/testsuite/tests/init/with-chdir/test.yaml @@ -0,0 +1,4 @@ +driver: python-script +indexes: + basic_index: # needed to avoid cloning the community index + in_fixtures: true -- 2.39.5