Begin to thread environments explicitly through completions

This commit is contained in:
ridiculousfish
2018-09-10 09:58:02 -07:00
parent e6872b83b0
commit e6b13c6bac
9 changed files with 95 additions and 93 deletions

View File

@@ -2338,8 +2338,9 @@ static void test_complete() {
const wcstring_list_t names(name_strs, name_strs + count);
std::vector<completion_t> completions;
complete_set_variable_names(&names);
env_vars_snapshot_t vars;
complete(L"$", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"$", &completions, COMPLETION_REQUEST_DEFAULT, vars);
completions_sort_and_prioritize(&completions);
do_test(completions.size() == 6);
do_test(completions.at(0).completion == L"Bar1");
@@ -2350,7 +2351,7 @@ static void test_complete() {
do_test(completions.at(5).completion == L"Foo3");
completions.clear();
complete(L"$F", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"$F", &completions, COMPLETION_REQUEST_DEFAULT, vars);
completions_sort_and_prioritize(&completions);
do_test(completions.size() == 3);
do_test(completions.at(0).completion == L"oo1");
@@ -2358,12 +2359,13 @@ static void test_complete() {
do_test(completions.at(2).completion == L"oo3");
completions.clear();
complete(L"$1", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"$1", &completions, COMPLETION_REQUEST_DEFAULT, vars);
completions_sort_and_prioritize(&completions);
do_test(completions.empty());
completions.clear();
complete(L"$1", &completions, COMPLETION_REQUEST_DEFAULT | COMPLETION_REQUEST_FUZZY_MATCH);
complete(L"$1", &completions, COMPLETION_REQUEST_DEFAULT | COMPLETION_REQUEST_FUZZY_MATCH,
vars);
completions_sort_and_prioritize(&completions);
do_test(completions.size() == 2);
do_test(completions.at(0).completion == L"$Bar1");
@@ -2375,24 +2377,25 @@ static void test_complete() {
if (system("chmod 700 'test/complete_test/testfile'")) err(L"chmod failed");
completions.clear();
complete(L"echo (test/complete_test/testfil", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo (test/complete_test/testfil", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"e");
completions.clear();
complete(L"echo (ls test/complete_test/testfil", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo (ls test/complete_test/testfil", &completions, COMPLETION_REQUEST_DEFAULT,
vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"e");
completions.clear();
complete(L"echo (command ls test/complete_test/testfil", &completions,
COMPLETION_REQUEST_DEFAULT);
COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"e");
// Completing after spaces - see #2447
completions.clear();
complete(L"echo (ls test/complete_test/has\\ ", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo (ls test/complete_test/has\\ ", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"space");
@@ -2405,104 +2408,104 @@ static void test_complete() {
// Complete a function name.
completions.clear();
complete(L"echo (scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo (scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"t");
// But not with the command prefix.
completions.clear();
complete(L"echo (command scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo (command scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 0);
// Not with the builtin prefix.
completions.clear();
complete(L"echo (builtin scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo (builtin scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 0);
// Not after a redirection.
completions.clear();
complete(L"echo hi > scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo hi > scuttlebut", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 0);
// Trailing spaces (#1261).
complete_add(L"foobarbaz", false, wcstring(), option_type_args_only, NO_FILES, NULL, L"qux",
NULL, COMPLETE_AUTO_SPACE);
completions.clear();
complete(L"foobarbaz ", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"foobarbaz ", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"qux");
// Don't complete variable names in single quotes (#1023).
completions.clear();
complete(L"echo '$Foo", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo '$Foo", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.empty());
completions.clear();
complete(L"echo \\$Foo", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo \\$Foo", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.empty());
// File completions.
completions.clear();
complete(L"cat test/complete_test/te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat test/complete_test/te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
completions.clear();
complete(L"echo sup > test/complete_test/te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo sup > test/complete_test/te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
completions.clear();
complete(L"echo sup > test/complete_test/te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"echo sup > test/complete_test/te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
if (!pushd("test/complete_test")) return;
complete(L"cat te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
do_test(!(completions.at(0).flags & COMPLETE_REPLACES_TOKEN));
do_test(!(completions.at(0).flags & COMPLETE_DUPLICATES_ARGUMENT));
completions.clear();
complete(L"cat testfile te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat testfile te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
do_test(completions.at(0).flags & COMPLETE_DUPLICATES_ARGUMENT);
completions.clear();
complete(L"cat testfile TE", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat testfile TE", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"testfile");
do_test(completions.at(0).flags & COMPLETE_REPLACES_TOKEN);
do_test(completions.at(0).flags & COMPLETE_DUPLICATES_ARGUMENT);
completions.clear();
complete(L"something --abc=te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"something --abc=te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
completions.clear();
complete(L"something -abc=te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"something -abc=te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
completions.clear();
complete(L"something abc=te", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"something abc=te", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"stfile");
completions.clear();
complete(L"something abc=stfile", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"something abc=stfile", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.size() == 0);
completions.clear();
complete(L"something abc=stfile", &completions, COMPLETION_REQUEST_FUZZY_MATCH);
complete(L"something abc=stfile", &completions, COMPLETION_REQUEST_FUZZY_MATCH, vars);
do_test(completions.size() == 1);
do_test(completions.at(0).completion == L"abc=testfile");
// Zero escapes can cause problems. See issue #1631.
completions.clear();
complete(L"cat foo\\0", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat foo\\0", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.empty());
completions.clear();
complete(L"cat foo\\0bar", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat foo\\0bar", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.empty());
completions.clear();
complete(L"cat \\0", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat \\0", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.empty());
completions.clear();
complete(L"cat te\\0", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"cat te\\0", &completions, COMPLETION_REQUEST_DEFAULT, vars);
do_test(completions.empty());
popd();
@@ -2510,11 +2513,12 @@ static void test_complete() {
complete_set_variable_names(NULL);
// Test abbreviations.
auto &pvars = parser_t::principal_parser().vars();
function_data_t fd;
fd.name = L"testabbrsonetwothreefour";
function_add(fd, parser_t::principal_parser());
int ret = env_set_one(L"_fish_abbr_testabbrsonetwothreezero", ENV_LOCAL, L"expansion");
complete(L"testabbrsonetwothree", &completions, COMPLETION_REQUEST_DEFAULT);
complete(L"testabbrsonetwothree", &completions, COMPLETION_REQUEST_DEFAULT, pvars);
do_test(ret == 0);
do_test(completions.size() == 2);
do_test(completions.at(0).completion == L"four");
@@ -2594,7 +2598,7 @@ static void test_completion_insertions() {
static void perform_one_autosuggestion_cd_test(const wcstring &command, const wcstring &expected,
long line) {
std::vector<completion_t> comps;
complete(command, &comps, COMPLETION_REQUEST_AUTOSUGGESTION);
complete(command, &comps, COMPLETION_REQUEST_AUTOSUGGESTION, env_vars_snapshot_t{});
bool expects_error = (expected == L"<error>");
@@ -2630,7 +2634,7 @@ static void perform_one_autosuggestion_cd_test(const wcstring &command, const wc
static void perform_one_completion_cd_test(const wcstring &command, const wcstring &expected,
long line) {
std::vector<completion_t> comps;
complete(command, &comps, COMPLETION_REQUEST_DEFAULT);
complete(command, &comps, COMPLETION_REQUEST_DEFAULT, env_vars_snapshot_t{});
bool expects_error = (expected == L"<error>");
@@ -2758,7 +2762,7 @@ static void test_autosuggest_suggest_special() {
static void perform_one_autosuggestion_should_ignore_test(const wcstring &command, long line) {
completion_list_t comps;
complete(command, &comps, COMPLETION_REQUEST_AUTOSUGGESTION);
complete(command, &comps, COMPLETION_REQUEST_AUTOSUGGESTION, env_vars_snapshot_t{});
do_test(comps.empty());
if (!comps.empty()) {
const wcstring &suggestion = comps.front().completion;