fix(python): improve parsing of pyvenv.cfg files (#6145)

This commit is contained in:
苏向夜 2024-08-12 12:25:04 -04:00 committed by GitHub
parent baaa54bd83
commit 7b65ad5163
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -121,8 +121,9 @@ fn get_python_virtual_env(context: &Context) -> Option<String> {
}) })
}) })
} }
fn get_prompt_from_venv(venv_path: &Path) -> Option<String> { fn get_prompt_from_venv(venv_path: &Path) -> Option<String> {
Ini::load_from_file(venv_path.join("pyvenv.cfg")) Ini::load_from_file_noescape(venv_path.join("pyvenv.cfg"))
.ok()? .ok()?
.general_section() .general_section()
.get("prompt") .get("prompt")
@ -424,6 +425,33 @@ prompt = '(foo)'
dir.close() 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<toml::Table>) { fn check_python2_renders(dir: &tempfile::TempDir, starship_config: Option<toml::Table>) {
let config = starship_config.unwrap_or(toml::toml! { let config = starship_config.unwrap_or(toml::toml! {
[python] [python]