From 3dbffccd875c04f9aa10a2d5c3e374cbd7b93215 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Sat, 23 Mar 2024 12:15:30 +0100 Subject: [PATCH] Add deprecation warning FLOG category "deprecated-test", run `fish -d deprecated-test` and it will show any test call that would change in future. --- src/builtins/test.rs | 35 +++++++++++++++++++++++++---------- src/flog.rs | 2 ++ tests/checks/test-posix.fish | 21 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/builtins/test.rs b/src/builtins/test.rs index 6b0d546afcaa..e18235843f2c 100644 --- a/src/builtins/test.rs +++ b/src/builtins/test.rs @@ -1,6 +1,7 @@ use super::prelude::*; use crate::common; use crate::future_feature_flags::{feature_test, FeatureFlag}; +use crate::should_flog; mod test_expressions { use super::*; @@ -1036,17 +1037,31 @@ pub fn test(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt return STATUS_CMD_OK; } } - } else { - if argc == 0 { - return STATUS_INVALID_ARGS; // Per 1003.1, exit false. - } else if argc == 1 { - // Per 1003.1, exit true if the arg is non-empty. - return if args[0].is_empty() { - STATUS_CMD_ERROR - } else { - STATUS_CMD_OK - }; + } else if argc == 0 { + if should_flog!(deprecated_test) { + streams.err.appendln(wgettext_fmt!( + "%ls: called with no arguments. This will be an error in future.", + program_name + )); + streams.err.append(parser.current_line()); + } + return STATUS_INVALID_ARGS; // Per 1003.1, exit false. + } else if argc == 1 { + if should_flog!(deprecated_test) { + if args[0] != "-z" { + streams.err.appendln(wgettext_fmt!( + "%ls: called with one argument. This will return false in future.", + program_name + )); + streams.err.append(parser.current_line()); + } } + // Per 1003.1, exit true if the arg is non-empty. + return if args[0].is_empty() { + STATUS_CMD_ERROR + } else { + STATUS_CMD_OK + }; } // Try parsing diff --git a/src/flog.rs b/src/flog.rs index af3763ba5e56..fed77dddd3c0 100644 --- a/src/flog.rs +++ b/src/flog.rs @@ -79,6 +79,8 @@ pub mod categories { (warning_path, "warning-path", "Warnings about unusable paths for config/history (on by default)", true); + (deprecated_test, "deprecated-test", "Warning about using test's zero- or one-argument modes (`test -d $foo`), which will be changed in future."); + (config, "config", "Finding and reading configuration"); (event, "event", "Firing events"); diff --git a/tests/checks/test-posix.fish b/tests/checks/test-posix.fish index 9852e266e97d..c9fcd74c6d90 100644 --- a/tests/checks/test-posix.fish +++ b/tests/checks/test-posix.fish @@ -25,3 +25,24 @@ echo $status test -z "" -a foo echo $status #CHECK: 0 + +set -l fish (status fish-path) +echo 'test foo; test; test -z; test -n; test -d; echo oops' | $fish -d 'deprecated-*' >/dev/null +#CHECKERR: test: called with one argument. This will return false in future. +#CHECKERR: Standard input (line 1): +#CHECKERR: test foo; test; test -z; test -n; test -d; echo oops +#CHECKERR: ^ +#CHECKERR: test: called with no arguments. This will be an error in future. +#CHECKERR: Standard input (line 1): +#CHECKERR: test foo; test; test -z; test -n; test -d; echo oops +#CHECKERR: ^ +#CHECKERR: test: called with one argument. This will return false in future. +# (yes, `test -z` is skipped because it would behave the same) +#CHECKERR: Standard input (line 1): +#CHECKERR: test foo; test; test -z; test -n; test -d; echo oops +#CHECKERR: ^ +#CHECKERR: test: called with one argument. This will return false in future. +#CHECKERR: Standard input (line 1): +#CHECKERR: test foo; test; test -z; test -n; test -d; echo oops +#CHECKERR: ^ +