From a55e95f5fbd67c857ac2b3d521e021700b5eb3bd Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 9 Dec 2023 21:11:12 +0100 Subject: [PATCH] Port test_env_snapshot --- fish-rust/src/tests/env.rs | 69 ++++++++++++++++++++++++++++++++++++++ src/fish_tests.cpp | 66 ------------------------------------ 2 files changed, 69 insertions(+), 66 deletions(-) diff --git a/fish-rust/src/tests/env.rs b/fish-rust/src/tests/env.rs index 6cc9f4064..f0e01e510 100644 --- a/fish-rust/src/tests/env.rs +++ b/fish-rust/src/tests/env.rs @@ -1,6 +1,7 @@ use crate::env::{EnvMode, EnvVar, EnvVarFlags, Environment}; use crate::ffi_tests::add_test; use crate::parser::Parser; +use crate::tests::prelude::*; use crate::wchar::prelude::*; use crate::wutil::wgetcwd; use std::collections::HashMap; @@ -103,3 +104,71 @@ add_test!("test_env_vars", || { assert!(v1 != v3 && !(v1 == v3)); assert!(v1 != v4 && !(v1 == v4)); }); + +add_test!("test_env_snapshot", || { + std::fs::create_dir_all("test/fish_env_snapshot_test/").unwrap(); + pushd("test/fish_env_snapshot_test/"); + let vars = Parser::principal_parser().vars(); + vars.push(true); + let before_pwd = vars.get(L!("PWD")).unwrap().as_string(); + vars.set_one( + L!("test_env_snapshot_var"), + EnvMode::default(), + L!("before").to_owned(), + ); + let snapshot = vars.snapshot(); + vars.set_one(L!("PWD"), EnvMode::default(), L!("/newdir").to_owned()); + vars.set_one( + L!("test_env_snapshot_var"), + EnvMode::default(), + L!("after").to_owned(), + ); + vars.set_one( + L!("test_env_snapshot_var_2"), + EnvMode::default(), + L!("after").to_owned(), + ); + + // vars should be unaffected by the snapshot + assert_eq!(vars.get(L!("PWD")).unwrap().as_string(), L!("/newdir")); + assert_eq!( + vars.get(L!("test_env_snapshot_var")).unwrap().as_string(), + L!("after") + ); + assert_eq!( + vars.get(L!("test_env_snapshot_var_2")).unwrap().as_string(), + L!("after") + ); + + // snapshot should have old values of vars + assert_eq!(snapshot.get(L!("PWD")).unwrap().as_string(), before_pwd); + assert_eq!( + snapshot + .get(L!("test_env_snapshot_var")) + .unwrap() + .as_string(), + L!("before") + ); + assert_eq!(snapshot.get(L!("test_env_snapshot_var_2")), None); + + // snapshots see global var changes except for perproc like PWD + vars.set_one( + L!("test_env_snapshot_var_3"), + EnvMode::GLOBAL, + L!("reallyglobal").to_owned(), + ); + assert_eq!( + vars.get(L!("test_env_snapshot_var_3")).unwrap().as_string(), + L!("reallyglobal") + ); + assert_eq!( + snapshot + .get(L!("test_env_snapshot_var_3")) + .unwrap() + .as_string(), + L!("reallyglobal") + ); + + vars.pop(); + popd(); +}); diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 3cb26fe89..4bc748dcf 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -173,38 +173,6 @@ wcstring comma_join(const std::vector &lst) { static std::vector pushed_dirs; -/// Helper to chdir and then update $PWD. -static bool pushd(const char *path) { - char cwd[PATH_MAX] = {}; - if (getcwd(cwd, sizeof cwd) == nullptr) { - err(L"getcwd() from pushd() failed: errno = %d", errno); - return false; - } - pushed_dirs.emplace_back(cwd); - - // We might need to create the directory. We don't care if this fails due to the directory - // already being present. - mkdir(path, 0770); - - int ret = chdir(path); - if (ret != 0) { - err(L"chdir(\"%s\") from pushd() failed: errno = %d", path, errno); - return false; - } - - env_stack_principal().set_pwd_from_getcwd(); - return true; -} - -static void popd() { - const std::string &old_cwd = pushed_dirs.back(); - if (chdir(old_cwd.c_str()) == -1) { - err(L"chdir(\"%s\") from popd() failed: errno = %d", old_cwd.c_str(), errno); - } - pushed_dirs.pop_back(); - env_stack_principal().set_pwd_from_getcwd(); -} - // Helper to return a string whose length greatly exceeds PATH_MAX. wcstring get_overlong_path() { wcstring longpath; @@ -1099,39 +1067,6 @@ long return_timezone_hour(time_t tstamp, const wchar_t *timezone) { return strtol(ltime_str, &str_ptr, 10); } -// todo!("port this") -static void test_env_snapshot() { - if (system("mkdir -p test/fish_env_snapshot_test/")) err(L"mkdir failed"); - bool pushed = pushd("test/fish_env_snapshot_test"); - do_test(pushed); - env_stack_t vars{parser_principal_parser()->deref().vars_boxed()}; - vars.push(true); - wcstring before_pwd = vars.get(L"PWD")->as_string(); - vars.set(L"test_env_snapshot_var", 0, std::vector{L"before"}); - const auto snapshot = vars.snapshot(); - vars.set(L"PWD", 0, std::vector{L"/newdir"}); - vars.set(L"test_env_snapshot_var", 0, std::vector{L"after"}); - vars.set(L"test_env_snapshot_var_2", 0, std::vector{L"after"}); - - // vars should be unaffected by the snapshot - do_test(vars.get(L"PWD")->as_string() == L"/newdir"); - do_test(vars.get(L"test_env_snapshot_var")->as_string() == L"after"); - do_test(vars.get(L"test_env_snapshot_var_2")->as_string() == L"after"); - - // snapshot should have old values of vars - do_test(snapshot->get(L"PWD")->as_string() == before_pwd); - do_test(snapshot->get(L"test_env_snapshot_var")->as_string() == L"before"); - do_test(snapshot->get(L"test_env_snapshot_var_2") == none()); - - // snapshots see global var changes except for perproc like PWD - vars.set(L"test_env_snapshot_var_3", ENV_GLOBAL, std::vector{L"reallyglobal"}); - do_test(vars.get(L"test_env_snapshot_var_3")->as_string() == L"reallyglobal"); - do_test(snapshot->get(L"test_env_snapshot_var_3")->as_string() == L"reallyglobal"); - - vars.pop(); - popd(); -} - // todo!("no need to port, delete this") void test_maybe() { say(L"Testing maybe_t"); @@ -1333,7 +1268,6 @@ struct test_comparator_t { static const test_t s_tests[]{ {TEST_GROUP("utility_functions"), test_utility_functions}, {TEST_GROUP("dir_iter"), test_dir_iter}, - {TEST_GROUP("env"), test_env_snapshot}, {TEST_GROUP("str_to_num"), test_str_to_num}, {TEST_GROUP("enum"), test_enum_set}, {TEST_GROUP("enum"), test_enum_array},