Skip to content

Commit

Permalink
Add deprecation warning
Browse files Browse the repository at this point in the history
FLOG category "deprecated-test", run `fish -d deprecated-test` and it
will show any test call that would change in future.
  • Loading branch information
faho committed Apr 12, 2024
1 parent 91a16bb commit 3dbffcc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
35 changes: 25 additions & 10 deletions 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::*;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/flog.rs
Expand Up @@ -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");
Expand Down
21 changes: 21 additions & 0 deletions tests/checks/test-posix.fish
Expand Up @@ -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: ^

0 comments on commit 3dbffcc

Please sign in to comment.