use std::process::Command; use anyhow::Result; use assert_cmd::prelude::*; use assert_fs::prelude::*; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; use common::{BIN_NAME, INSTA_FILTERS}; mod common; #[test] fn no_arguments() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .current_dir(&temp_dir)); Ok(()) } #[test] fn invalid_requirement() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("flask==1.0.x") .current_dir(&temp_dir)); Ok(()) } #[test] fn missing_requirements_txt() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("-r") .arg("requirements.txt") .current_dir(&temp_dir)); Ok(()) } #[test] fn invalid_requirements_txt_requirement() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; let requirements_txt = temp_dir.child("requirements.txt"); requirements_txt.touch()?; requirements_txt.write_str("flask==1.0.x")?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("-r") .arg("requirements.txt") .current_dir(&temp_dir)); Ok(()) } #[test] fn missing_pyproject_toml() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("-r") .arg("pyproject.toml") .current_dir(&temp_dir)); Ok(()) } #[test] fn invalid_pyproject_toml_syntax() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; let pyproject_toml = temp_dir.child("pyproject.toml"); pyproject_toml.touch()?; pyproject_toml.write_str("123 - 456")?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("-r") .arg("pyproject.toml") .current_dir(&temp_dir)); Ok(()) } #[test] fn invalid_pyproject_toml_schema() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; let pyproject_toml = temp_dir.child("pyproject.toml"); pyproject_toml.touch()?; pyproject_toml.write_str("[project]")?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("-r") .arg("pyproject.toml") .current_dir(&temp_dir)); Ok(()) } #[test] fn invalid_pyproject_toml_requirement() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; let pyproject_toml = temp_dir.child("pyproject.toml"); pyproject_toml.touch()?; pyproject_toml.write_str( r#"[project] name = "project" dependencies = ["flask==1.0.x"] "#, )?; assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("-r") .arg("pyproject.toml") .current_dir(&temp_dir)); Ok(()) } #[test] fn uninstall() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; let cache_dir = assert_fs::TempDir::new()?; let venv = temp_dir.child(".venv"); Command::new(get_cargo_bin(BIN_NAME)) .arg("venv") .arg(venv.as_os_str()) .arg("--cache-dir") .arg(cache_dir.path()) .current_dir(&temp_dir) .assert() .success(); venv.assert(predicates::path::is_dir()); let requirements_txt = temp_dir.child("requirements.txt"); requirements_txt.touch()?; requirements_txt.write_str("MarkupSafe==2.1.3")?; Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") .arg("requirements.txt") .arg("--cache-dir") .arg(cache_dir.path()) .env("VIRTUAL_ENV", venv.as_os_str()) .current_dir(&temp_dir) .assert() .success(); Command::new(venv.join("bin").join("python")) .arg("-c") .arg("import markupsafe") .current_dir(&temp_dir) .assert() .success(); insta::with_settings!({ filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-uninstall") .arg("MarkupSafe") .arg("--cache-dir") .arg(cache_dir.path()) .env("VIRTUAL_ENV", venv.as_os_str()) .current_dir(&temp_dir)); }); Command::new(venv.join("bin").join("python")) .arg("-c") .arg("import markupsafe") .current_dir(&temp_dir) .assert() .failure(); Ok(()) }