Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 14, 2023
1 parent d32430c commit c029ef5
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ url = "2.4.1"

[dev-dependencies]
starbase_sandbox = { workspace = true }
tokio = { workspace = true }
4 changes: 2 additions & 2 deletions crates/core/src/version_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{env, path::Path};
use tracing::{debug, trace};
use version_spec::*;

async fn detect_version_first_available(
pub async fn detect_version_first_available(
tool: &Tool,
start_dir: &Path,
end_dir: &Path,
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn detect_version_first_available(
Ok(None)
}

async fn detect_version_prefer_prototools(
pub async fn detect_version_prefer_prototools(
tool: &Tool,
start_dir: &Path,
end_dir: &Path,
Expand Down
5 changes: 4 additions & 1 deletion crates/core/tests/user_config_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proto_core::{PinType, UserConfig, USER_CONFIG_NAME};
use proto_core::{DetectStrategy, PinType, UserConfig, USER_CONFIG_NAME};
use starbase_sandbox::create_empty_sandbox;
use std::collections::BTreeMap;
use std::env;
Expand All @@ -17,6 +17,7 @@ mod user_config {
UserConfig {
auto_clean: false,
auto_install: false,
detect_strategy: DetectStrategy::default(),
node_intercept_globals: true,
http: HttpOptions::default(),
pin_latest: None,
Expand Down Expand Up @@ -46,6 +47,7 @@ pin-latest = "global"
UserConfig {
auto_clean: true,
auto_install: true,
detect_strategy: DetectStrategy::default(),
node_intercept_globals: false,
http: HttpOptions::default(),
pin_latest: Some(PinType::Global),
Expand All @@ -71,6 +73,7 @@ pin-latest = "global"
UserConfig {
auto_clean: true,
auto_install: true,
detect_strategy: DetectStrategy::default(),
node_intercept_globals: false,
http: HttpOptions::default(),
pin_latest: None,
Expand Down
103 changes: 103 additions & 0 deletions crates/core/tests/version_detector_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use proto_core::{
detect_version_first_available, detect_version_prefer_prototools, load_tool_from_locator,
ProtoEnvironment, Tool, ToolsConfig, UnresolvedVersionSpec, UserConfig,
};
use starbase_sandbox::create_empty_sandbox;
use std::path::Path;
use warpgate::Id;

mod version_detector {
use super::*;

async fn create_node(_root: &Path) -> Tool {
load_tool_from_locator(
Id::raw("node"),
ProtoEnvironment::new().unwrap(),
ToolsConfig::builtin_plugins().get("node").unwrap(),
&UserConfig::default(),
)
.await
.unwrap()
}

#[tokio::test]
async fn uses_deepest_prototools() {
let sandbox = create_empty_sandbox();
sandbox.create_file("a/.prototools", "node = \"20\"");
sandbox.create_file("a/b/.prototools", "node = \"18\"");
sandbox.create_file("a/b/c/.prototools", "node = \"16\"");

let tool = create_node(sandbox.path()).await;

assert_eq!(
detect_version_first_available(&tool, &sandbox.path().join("a/b/c"), sandbox.path())
.await
.unwrap(),
Some(UnresolvedVersionSpec::parse("~16").unwrap())
);

assert_eq!(
detect_version_first_available(&tool, &sandbox.path().join("a/b"), sandbox.path())
.await
.unwrap(),
Some(UnresolvedVersionSpec::parse("~18").unwrap())
);

assert_eq!(
detect_version_first_available(&tool, &sandbox.path().join("a"), sandbox.path())
.await
.unwrap(),
Some(UnresolvedVersionSpec::parse("~20").unwrap())
);
}

#[tokio::test]
async fn finds_first_available_prototools() {
let sandbox = create_empty_sandbox();
sandbox.create_file("a/.prototools", "node = \"20\"");
sandbox.create_file("package.json", r#"{ "engines": { "node": "18" } }"#);

let tool = create_node(sandbox.path()).await;

assert_eq!(
detect_version_first_available(&tool, &sandbox.path().join("a/b"), sandbox.path())
.await
.unwrap(),
Some(UnresolvedVersionSpec::parse("~20").unwrap())
);
}

#[tokio::test]
async fn finds_first_available_ecosystem() {
let sandbox = create_empty_sandbox();
sandbox.create_file(".prototools", "node = \"20\"");
sandbox.create_file("a/package.json", r#"{ "engines": { "node": "18" } }"#);

let tool = create_node(sandbox.path()).await;

assert_eq!(
detect_version_first_available(&tool, &sandbox.path().join("a/b"), sandbox.path())
.await
.unwrap(),
Some(UnresolvedVersionSpec::parse("~18").unwrap())
);
}

#[tokio::test]
async fn prefers_prototools() {
let sandbox = create_empty_sandbox();
sandbox.create_file("a/.prototools", "node = \"20\"");
sandbox.create_file("a/b/.prototools", "node = \"18\"");
sandbox.create_file("a/b/package.json", r#"{ "engines": { "node": "17" } }"#);
sandbox.create_file("a/b/c/package.json", r#"{ "engines": { "node": "19" } }"#);

let tool = create_node(sandbox.path()).await;

assert_eq!(
detect_version_prefer_prototools(&tool, &sandbox.path().join("a/b/c"), sandbox.path())
.await
.unwrap(),
Some(UnresolvedVersionSpec::parse("~18").unwrap())
);
}
}

0 comments on commit c029ef5

Please sign in to comment.