From 7b65ad51635463003941ec0ecea1d4d315b2df53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= <46275354+fu050409@users.noreply.github.com> Date: Mon, 12 Aug 2024 12:25:04 -0400 Subject: [PATCH] fix(python): improve parsing of `pyvenv.cfg` files (#6145) --- src/modules/python.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/modules/python.rs b/src/modules/python.rs index df4cfab5f..299741638 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -121,8 +121,9 @@ fn get_python_virtual_env(context: &Context) -> Option { }) }) } + fn get_prompt_from_venv(venv_path: &Path) -> Option { - Ini::load_from_file(venv_path.join("pyvenv.cfg")) + Ini::load_from_file_noescape(venv_path.join("pyvenv.cfg")) .ok()? .general_section() .get("prompt") @@ -424,6 +425,33 @@ prompt = '(foo)' dir.close() } + #[test] + fn with_active_venv_and_line_break_like_prompt() -> io::Result<()> { + let dir = tempfile::tempdir()?; + create_dir_all(dir.path().join("my_venv"))?; + let mut venv_cfg = File::create(dir.path().join("my_venv").join("pyvenv.cfg"))?; + venv_cfg.write_all( + br" +home = something +prompt = foo\nbar + ", + )?; + venv_cfg.sync_all()?; + + let actual = ModuleRenderer::new("python") + .path(dir.path()) + .env("VIRTUAL_ENV", dir.path().join("my_venv").to_str().unwrap()) + .collect(); + + let expected = Some(format!( + "via {}", + Color::Yellow.bold().paint(r"🐍 v3.8.0 (foo\nbar) ") + )); + + assert_eq!(actual, expected); + dir.close() + } + fn check_python2_renders(dir: &tempfile::TempDir, starship_config: Option) { let config = starship_config.unwrap_or(toml::toml! { [python]