Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop test reporter #109

Merged
merged 11 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 50 additions & 46 deletions crates/pet-conda/tests/ci_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,33 @@ fn setup() {
#[allow(dead_code)]
// We should detect the conda install along with the base env
fn detect_conda_root() {
use std::sync::Arc;

use pet_conda::Conda;
use pet_core::{
manager::EnvManagerType, os_environment::EnvironmentApi,
python_environment::PythonEnvironmentKind, Locator,
};
use pet_reporter::test::create_reporter;
use pet_reporter::{cache::CacheReporter, collect};

setup();
let env = EnvironmentApi::new();

let reporter = create_reporter();
let reporter = Arc::new(collect::create_reporter());
let conda = Conda::from(&env);
conda.find(&reporter);
let result = reporter.get_result();
conda.find(&CacheReporter::new(reporter.clone()));

assert_eq!(result.managers.len(), 1);
let environments = reporter.environments.lock().unwrap().clone();
let managers = reporter.managers.lock().unwrap().clone();

let info = get_conda_info();
let conda_dir = PathBuf::from(info.conda_prefix.clone());
let manager = &result.managers[0];
let manager = &managers[0];
assert_eq!(manager.executable, conda_dir.join("bin").join("conda"));
assert_eq!(manager.tool, EnvManagerType::Conda);
assert_eq!(manager.version, info.conda_version.into());

let env = &result
.environments
let env = &environments
.iter()
.find(|e| e.name == Some("base".into()))
.unwrap();
Expand Down Expand Up @@ -107,8 +108,8 @@ fn detect_new_conda_env() {
use pet_core::{
os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator,
};
use pet_reporter::test::create_reporter;
use std::path::PathBuf;
use pet_reporter::{cache::CacheReporter, collect};
use std::{path::PathBuf, sync::Arc};

setup();
let env_name = "env_with_python";
Expand All @@ -119,24 +120,23 @@ fn detect_new_conda_env() {
let env = EnvironmentApi::new();

let conda = Conda::from(&env);
let reporter = create_reporter();
conda.find(&reporter);
let result = reporter.get_result();
let reporter = Arc::new(collect::create_reporter());
conda.find(&CacheReporter::new(reporter.clone()));

assert_eq!(result.managers.len(), 1);
let environments = reporter.environments.lock().unwrap().clone();
let managers = reporter.managers.lock().unwrap().clone();

let manager = &result.managers[0];
let manager = &managers[0];

let info = get_conda_info();
let conda_dir = PathBuf::from(info.conda_prefix.clone());
let env = result
.environments
let env = environments
.iter()
.find(|x| x.name == Some(env_name.into()))
.expect(
format!(
"New Environment not created, detected envs {:?}",
result.environments
environments
)
.as_str(),
);
Expand Down Expand Up @@ -211,33 +211,32 @@ fn detect_new_conda_env_without_python() {
use pet_core::{
os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator,
};
use pet_reporter::test::create_reporter;
use std::path::PathBuf;
use pet_reporter::{cache::CacheReporter, collect};
use std::{path::PathBuf, sync::Arc};

setup();
let env_name = "env_without_python";
create_conda_env(&CondaCreateEnvNameOrPath::Name(env_name.into()), None);
let env = EnvironmentApi::new();

let conda = Conda::from(&env);
let reporter = create_reporter();
conda.find(&reporter);
let result = reporter.get_result();
let reporter = Arc::new(collect::create_reporter());
conda.find(&CacheReporter::new(reporter.clone()));

assert_eq!(result.managers.len(), 1);
let environments = reporter.environments.lock().unwrap().clone();
let managers = reporter.managers.lock().unwrap().clone();

let manager = &result.managers[0];
let manager = &managers[0];

let info = get_conda_info();
let conda_dir = PathBuf::from(info.conda_prefix.clone());
let env = result
.environments
let env = environments
.iter()
.find(|x| x.name == Some(env_name.into()))
.expect(
format!(
"New Environment not created, detected envs {:?}",
result.environments
environments
)
.as_str(),
);
Expand All @@ -257,12 +256,14 @@ fn detect_new_conda_env_without_python() {
#[allow(dead_code)]
// Detect envs created without Python in a custom directory using the -p flag
fn detect_new_conda_env_created_with_p_flag_without_python() {
use std::sync::Arc;

use common::resolve_test_path;
use pet_conda::Conda;
use pet_core::{
os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator,
};
use pet_reporter::test::create_reporter;
use pet_reporter::{cache::CacheReporter, collect};

setup();
let env_name = "env_without_python3";
Expand All @@ -271,22 +272,21 @@ fn detect_new_conda_env_created_with_p_flag_without_python() {
let env = EnvironmentApi::new();

let conda = Conda::from(&env);
let reporter = create_reporter();
conda.find(&reporter);
let result = reporter.get_result();
let reporter = Arc::new(collect::create_reporter());
conda.find(&CacheReporter::new(reporter.clone()));

assert_eq!(result.managers.len(), 1);
let environments = reporter.environments.lock().unwrap().clone();
let managers = reporter.managers.lock().unwrap().clone();

let manager = &result.managers[0];
let manager = &managers[0];

let env = result
.environments
let env = environments
.iter()
.find(|x| x.prefix == Some(prefix.clone()))
.expect(
format!(
"New Environment ({:?}) not created, detected envs {:?}",
prefix, result.environments
prefix, environments
)
.as_str(),
);
Expand All @@ -305,12 +305,17 @@ fn detect_new_conda_env_created_with_p_flag_without_python() {
#[allow(dead_code)]
// Detect envs created Python in a custom directory using the -p flag
fn detect_new_conda_env_created_with_p_flag_with_python() {
use std::sync::Arc;

use common::resolve_test_path;
use pet_conda::Conda;
use pet_core::{
os_environment::EnvironmentApi, python_environment::PythonEnvironmentKind, Locator,
};
use pet_reporter::test::create_reporter;
use pet_reporter::{
cache::{self, CacheReporter},
collect,
};

setup();
let env_name = "env_with_python3";
Expand All @@ -323,22 +328,21 @@ fn detect_new_conda_env_created_with_p_flag_with_python() {
let env = EnvironmentApi::new();

let conda = Conda::from(&env);
let reporter = create_reporter();
conda.find(&reporter);
let result = reporter.get_result();
let reporter = Arc::new(collect::create_reporter());
conda.find(&CacheReporter::new(reporter.clone()));

assert_eq!(result.managers.len(), 1);
let environments = reporter.environments.lock().unwrap().clone();
let managers = reporter.managers.lock().unwrap().clone();

let manager = &result.managers[0];
let manager = &managers[0];

let env = result
.environments
let env = environments
.iter()
.find(|x| x.prefix == Some(prefix.clone()))
.expect(
format!(
"New Environment not created, detected envs {:?}",
result.environments
environments
)
.as_str(),
);
Expand Down
46 changes: 25 additions & 21 deletions crates/pet-pyenv/tests/pyenv_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn does_not_find_any_pyenv_envs() {
use pet_core::{self, Locator};
use pet_pyenv;
use pet_pyenv::PyEnv;
use pet_reporter::test::create_reporter;
use pet_reporter::{cache::CacheReporter, collect};
use std::{collections::HashMap, path::PathBuf, sync::Arc};

let environment = create_test_environment(
Expand All @@ -23,12 +23,14 @@ fn does_not_find_any_pyenv_envs() {

let conda = Arc::new(Conda::from(&environment));
let locator = PyEnv::from(&environment, conda);
let reporter = create_reporter();
locator.find(&reporter);
let result = reporter.get_result();
let reporter = Arc::new(collect::create_reporter());
locator.find(&CacheReporter::new(reporter.clone()));

assert_eq!(result.managers.is_empty(), true);
assert_eq!(result.environments.is_empty(), true);
let environments = reporter.environments.lock().unwrap().clone();
let managers = reporter.managers.lock().unwrap().clone();

assert_eq!(managers.is_empty(), true);
assert_eq!(environments.is_empty(), true);
}

#[test]
Expand All @@ -44,7 +46,7 @@ fn does_not_find_any_pyenv_envs_even_with_pyenv_installed() {
};
use pet_pyenv;
use pet_pyenv::PyEnv;
use pet_reporter::test::create_reporter;
use pet_reporter::{cache::CacheReporter, collect};
use serde_json::json;
use std::{collections::HashMap, path::PathBuf, sync::Arc};

Expand All @@ -67,19 +69,19 @@ fn does_not_find_any_pyenv_envs_even_with_pyenv_installed() {

let conda = Arc::new(Conda::from(&environment));
let locator = PyEnv::from(&environment, conda);
let reporter = create_reporter();
locator.find(&reporter);
let result = reporter.get_result();
let reporter = Arc::new(collect::create_reporter());
locator.find(&CacheReporter::new(reporter.clone()));

let managers = reporter.managers.lock().unwrap().clone();

let managers = result.clone().managers;
assert_eq!(managers.len(), 1);

let expected_manager = EnvManager {
executable: pyenv_exe.clone(),
version: None,
tool: EnvManagerType::Pyenv,
};
assert_eq!(json!(expected_manager), json!(result.managers[0]));
assert_eq!(json!(expected_manager), json!(managers[0]));
}

#[test]
Expand All @@ -97,7 +99,7 @@ fn find_pyenv_envs() {
};
use pet_pyenv;
use pet_pyenv::PyEnv;
use pet_reporter::test::create_reporter;
use pet_reporter::{cache::CacheReporter, collect};
use serde_json::json;
use std::{collections::HashMap, path::PathBuf, sync::Arc};

Expand All @@ -123,11 +125,13 @@ fn find_pyenv_envs() {

let conda = Arc::new(Conda::from(&environment));
let locator = PyEnv::from(&environment, conda);
let reporter = create_reporter();
locator.find(&reporter);
let mut result = reporter.get_result();
let reporter = Arc::new(collect::create_reporter());
locator.find(&CacheReporter::new(reporter.clone()));

let mut environments = reporter.environments.lock().unwrap().clone();
let mut managers = reporter.managers.lock().unwrap().clone();

assert_eq!(result.managers.len(), 2);
assert_eq!(managers.len(), 2);

let expected_pyenv_manager = EnvManager {
executable: pyenv_exe.clone(),
Expand All @@ -144,9 +148,9 @@ fn find_pyenv_envs() {
expected_pyenv_manager.clone(),
expected_conda_manager.clone(),
];
result.managers.sort();
managers.sort();
expected.sort();
assert_eq!(expected, result.managers);
assert_eq!(expected, managers);

let expected_3_9_9 = PythonEnvironment {
display_name: None,
Expand Down Expand Up @@ -356,8 +360,8 @@ fn find_pyenv_envs() {
expected_pypy,
];
expected_envs.sort();
result.environments.sort();
assert_eq!(json!(expected_envs), json!(result.environments));
environments.sort();
assert_eq!(json!(expected_envs), json!(environments));
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions crates/pet-python-utils/src/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ pub fn should_search_for_environments_in_path<P: AsRef<Path>>(path: &P) -> bool
// Mostly copied from https://github.com/github/gitignore/blob/main/Python.gitignore
let folders_to_ignore = [
"node_modules",
".cargo",
".devcontainer",
".github",
".git",
".tox",
".nox",
Expand All @@ -195,6 +198,7 @@ pub fn should_search_for_environments_in_path<P: AsRef<Path>>(path: &P) -> bool
".pyre",
".ptype",
".pytest_cache",
".vscode",
"__pycache__",
"__pypackages__",
".mypy_cache",
Expand Down
4 changes: 4 additions & 0 deletions crates/pet-reporter/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use std::{
sync::{Arc, Mutex},
};

/// Poorly named, needs to be renamed,
/// The purpose of this reporter was to act as a cache, but since then
/// the requirements of caching have changed and this is no longer a cache.
/// This is merely a decorator class that ensures we do not report the same env/manager more than once.
pub struct CacheReporter {
reporter: Arc<dyn Reporter>,
reported_managers: Arc<Mutex<HashMap<PathBuf, EnvManager>>>,
Expand Down
1 change: 0 additions & 1 deletion crates/pet-reporter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ pub mod collect;
pub mod environment;
pub mod jsonrpc;
pub mod stdio;
pub mod test;
Loading
Loading