From 47e45704b149c31daa6729b4e3b266b413285084 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 26 Nov 2021 18:29:10 +0100 Subject: [PATCH] Allow set --query to check for pathvarness (#8494) Currently, set -q --unpath PATH simply ignores the "--unpath" bit (and same for "--path"). This changes it, so just like exportedness you can check pathness. --- CHANGELOG.rst | 1 + doc_src/cmds/set.rst | 2 +- src/env.cpp | 12 ++++++++++++ tests/checks/set.fish | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 27a85a8a7..d4f78605b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -75,6 +75,7 @@ Scripting improvements - ``math`` now correctly prints negative values and values larger than ``2**31`` when in hex or octal bases (:issue:`8417`). - ``dirs`` always produces an exit status of 0, instead of sometimes returning 1 (:issue:`8211`). - ``cd ""`` no longer crashes fish (:issue:`8147`). +- ``set --query`` can now query whether a variable is a pathvar via ``--path`` or ``--unpath`` (:issue:`8494`). Interactive improvements ------------------------ diff --git a/doc_src/cmds/set.rst b/doc_src/cmds/set.rst index b7b81dbab..6ffac2bfc 100644 --- a/doc_src/cmds/set.rst +++ b/doc_src/cmds/set.rst @@ -90,7 +90,7 @@ The exporting rules when creating or updating a variable are identical to the sc - If a variable is not explicitly set to be either exported or unexported and has never before been defined, the variable will not be exported. -In query mode, the scope to be examined can be specified. +In query mode, the scope to be examined can be specified. Whether the variable has to be a path variable or exported can also be specified. In erase mode, if variable indices are specified, only the specified slices of the list variable will be erased. diff --git a/src/env.cpp b/src/env.cpp index 261d1a697..b33345062 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -509,6 +509,14 @@ struct query_t { return true; } } + + bool pathvar_matches(const env_var_t &var) const { + if (has_pathvar_unpathvar) { + return var.is_pathvar() ? pathvar : unpathvar; + } else { + return true; + } + } }; // Struct representing one level in the function variable stack. @@ -804,6 +812,10 @@ maybe_t env_scoped_impl_t::get(const wcstring &key, env_mode_flags_t if (result && !query.export_matches(*result)) { result = none(); } + // Same for pathvars + if (result && !query.pathvar_matches(*result)) { + result = none(); + } return result; } diff --git a/tests/checks/set.fish b/tests/checks/set.fish index 4fa4895dc..7849800b5 100644 --- a/tests/checks/set.fish +++ b/tests/checks/set.fish @@ -415,10 +415,30 @@ set --unpath __fish_test_PATH $__fish_test_PATH echo "$__fish_test_path_not $__fish_test_PATH" $__fish_test_path_not $__fish_test_PATH # CHECK: a b c 1 2 3 a b c 1 2 3 +set -q --path __fish_test_PATH +and echo __fish_test_PATH is a pathvar +or echo __fish_test_PATH is not a pathvar +# CHECK: __fish_test_PATH is not a pathvar + +set -q --unpath __fish_test_PATH +and echo __fish_test_PATH is not a pathvar +or echo __fish_test_PATH is not not a pathvar +# CHECK: __fish_test_PATH is not a pathvar + set --path __fish_test_path_not $__fish_test_path_not echo "$__fish_test_path_not $__fish_test_PATH" $__fish_test_path_not $__fish_test_PATH # CHECK: a:b:c 1 2 3 a b c 1 2 3 +set -q --path __fish_test_path_not +and echo __fish_test_path_not is a pathvar +or echo __fish_test_path_not is not a pathvar +# CHECK: __fish_test_path_not is a pathvar + +set -q --unpath __fish_test_path_not +and echo __fish_test_path_not is not a pathvar +or echo __fish_test_path_not is not not a pathvar +# CHECK: __fish_test_path_not is not not a pathvar + set --path __fish_test_PATH $__fish_test_PATH echo "$__fish_test_path_not $__fish_test_PATH" $__fish_test_path_not $__fish_test_PATH # CHECK: a:b:c 1:2:3 a b c 1 2 3