diff --git a/src/env.cpp b/src/env.cpp index cb9cec7e3..b839300aa 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -1532,8 +1532,13 @@ std::unique_ptr list_to_array_val(const wcstring_list_t &list) { // Zero element arrays are internally encoded as this placeholder string. val->append(ENV_NULL); } else { + bool need_sep = false; for (auto it : list) { - if (!val->empty()) val->push_back(ARRAY_SEP); + if (need_sep) { + val->push_back(ARRAY_SEP); + } else { + need_sep = true; + } val->append(it); } } diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 065656280..0a51d5ab2 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -948,21 +948,34 @@ static void test_list_to_array() { err(L"test_list_to_array failed on line %lu", __LINE__); } - list.push_back(L"abc"); + list.push_back(L""); val = list_to_array_val(list); if (*val != list[0]) { err(L"test_list_to_array failed on line %lu", __LINE__); } - list.push_back(L"def"); + list.push_back(L"abc"); val = list_to_array_val(list); - if (*val != L"abc" ARRAY_SEP_STR L"def") { + if (*val != L"" ARRAY_SEP_STR L"abc") { err(L"test_list_to_array failed on line %lu", __LINE__); } - list.push_back(L"ghi"); + list.insert(list.begin(), L"ghi"); val = list_to_array_val(list); - if (*val != L"abc" ARRAY_SEP_STR L"def" ARRAY_SEP_STR L"ghi") { + if (*val != L"ghi" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"abc") { + err(L"test_list_to_array failed on line %lu", __LINE__); + } + + list.push_back(L""); + val = list_to_array_val(list); + if (*val != L"ghi" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"abc" ARRAY_SEP_STR L"") { + err(L"test_list_to_array failed on line %lu", __LINE__); + } + + list.push_back(L"def"); + val = list_to_array_val(list); + if (*val != + L"ghi" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"abc" ARRAY_SEP_STR L"" ARRAY_SEP_STR L"def") { err(L"test_list_to_array failed on line %lu", __LINE__); }