Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 19, 2024
1 parent 9033168 commit 47054c0
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 47 deletions.
1 change: 1 addition & 0 deletions crates/config/src/toolchain_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl ToolchainConfig {
tools.push(Id::raw("rust"));
}

tools.push(Id::raw("system"));
tools
}

Expand Down
2 changes: 2 additions & 0 deletions crates/project-builder/src/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl<'app> ProjectBuilder<'app> {
// Infer toolchains from language
if self.toolchains.is_empty() {
self.toolchains = detect_project_toolchains(
self.context.workspace_root,
&self.root,
&self.language,
self.context.enabled_toolchains,
Expand Down Expand Up @@ -198,6 +199,7 @@ impl<'app> ProjectBuilder<'app> {
language: self.language,
root: self.root,
source: self.source.to_owned(),
toolchains: self.toolchains,
..Project::default()
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: 'javascript'
platform: 'node'

dependsOn:
- 'bar'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: javascript
type: application
platform: bun
platform: bun # deprecated
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: javascript
type: application
platform: deno
platform: deno # deprecated
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
platform: node

tasks:
node-a:
command: noop
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
71 changes: 38 additions & 33 deletions crates/project-builder/tests/project_builder_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use moon_common::path::WorkspaceRelativePathBuf;
use moon_common::Id;
use moon_config::{
ConfigLoader, DependencyConfig, DependencyScope, DependencySource, LanguageType, NodeConfig,
PlatformType, RustConfig, TaskArgs, TaskConfig, ToolchainConfig,
BunConfig, ConfigLoader, DenoConfig, DependencyConfig, DependencyScope, DependencySource,
LanguageType, NodeConfig, RustConfig, TaskArgs, TaskConfig, ToolchainConfig,
};
use moon_file_group::FileGroup;
use moon_project::Project;
Expand All @@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
// We need some top-level struct to hold the data used for lifetime refs.
struct Stub {
config_loader: ConfigLoader,
enabled_toolchains: Vec<Id>,
toolchain_config: ToolchainConfig,
workspace_root: PathBuf,
id: Id,
Expand All @@ -24,13 +25,16 @@ impl Stub {
pub fn new(id: &str, root: &Path) -> Self {
// Enable platforms so that detection works
let toolchain_config = ToolchainConfig {
bun: Some(BunConfig::default()),
deno: Some(DenoConfig::default()),
node: Some(NodeConfig::default()),
rust: Some(RustConfig::default()),
..ToolchainConfig::default()
};

Self {
config_loader: ConfigLoader::default(),
enabled_toolchains: toolchain_config.get_enabled(),
toolchain_config,
workspace_root: root.to_path_buf(),
id: Id::raw(id),
Expand All @@ -44,6 +48,7 @@ impl Stub {
&self.source,
ProjectBuilderContext {
config_loader: &self.config_loader,
enabled_toolchains: &self.enabled_toolchains,
monorepo: true,
root_project_id: None,
toolchain_config: &self.toolchain_config,
Expand Down Expand Up @@ -224,7 +229,7 @@ mod project_builder {
#[tokio::test]
async fn detects_from_env() {
let sandbox = create_sandbox("builder");
let project = build_project_without_inherited("foo", sandbox.path()).await;
let project = build_project_without_inherited("qux", sandbox.path()).await;

assert_eq!(project.language, LanguageType::TypeScript);
}
Expand All @@ -234,67 +239,67 @@ mod project_builder {
let project = build_lang_project("bash").await;

assert_eq!(project.language, LanguageType::Bash);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}

#[tokio::test]
async fn detects_batch() {
let project = build_lang_project("batch").await;

assert_eq!(project.language, LanguageType::Batch);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}

#[tokio::test]
async fn detects_bun() {
let project = build_lang_project("bun").await;

assert_eq!(project.language, LanguageType::JavaScript);
assert_eq!(project.platform, PlatformType::Bun);
assert_eq!(project.toolchains, vec![Id::raw("system")]); // no configs

let project = build_lang_project("bun-config").await;

assert_eq!(project.language, LanguageType::JavaScript);
// assert_eq!(project.platform, PlatformType::Bun);
assert_eq!(project.toolchains, vec![Id::raw("bun")]);
}

#[tokio::test]
async fn detects_deno() {
let project = build_lang_project("deno").await;

assert_eq!(project.language, LanguageType::JavaScript);
assert_eq!(project.platform, PlatformType::Deno);
assert_eq!(project.toolchains, vec![Id::raw("system")]); // no configs

let project = build_lang_project("deno-config").await;

assert_eq!(project.language, LanguageType::TypeScript);
// assert_eq!(project.platform, PlatformType::Deno);
assert_eq!(project.toolchains, vec![Id::raw("deno")]);
}

#[tokio::test]
async fn detects_go() {
let project = build_lang_project("go").await;

assert_eq!(project.language, LanguageType::Go);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);

let project = build_lang_project("go-config").await;

assert_eq!(project.language, LanguageType::Go);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}

#[tokio::test]
async fn detects_js() {
let project = build_lang_project("js").await;

assert_eq!(project.language, LanguageType::JavaScript);
assert_eq!(project.platform, PlatformType::Node);
assert_eq!(project.toolchains, vec![Id::raw("system")]); // no configs

let project = build_lang_project("js-config").await;

assert_eq!(project.language, LanguageType::JavaScript);
assert_eq!(project.platform, PlatformType::Node);
assert_eq!(project.toolchains, vec![Id::raw("node")]);
}

#[tokio::test]
Expand All @@ -305,72 +310,72 @@ mod project_builder {
project.language,
LanguageType::Other("kotlin".try_into().unwrap())
);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}

#[tokio::test]
async fn detects_php() {
let project = build_lang_project("php").await;

assert_eq!(project.language, LanguageType::Php);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);

let project = build_lang_project("php-config").await;

assert_eq!(project.language, LanguageType::Php);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}

#[tokio::test]
async fn detects_python() {
let project = build_lang_project("python").await;

assert_eq!(project.language, LanguageType::Python);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);

let project = build_lang_project("python-config").await;

assert_eq!(project.language, LanguageType::Python);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}

#[tokio::test]
async fn detects_ruby() {
let project = build_lang_project("ruby").await;

assert_eq!(project.language, LanguageType::Ruby);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);

let project = build_lang_project("ruby-config").await;

assert_eq!(project.language, LanguageType::Ruby);
assert_eq!(project.platform, PlatformType::System);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}

#[tokio::test]
async fn detects_rust() {
let project = build_lang_project("rust").await;

assert_eq!(project.language, LanguageType::Rust);
assert_eq!(project.platform, PlatformType::Rust);
assert_eq!(project.toolchains, vec![Id::raw("rust")]);

let project = build_lang_project("rust-config").await;

assert_eq!(project.language, LanguageType::Rust);
assert_eq!(project.platform, PlatformType::Rust);
assert_eq!(project.toolchains, vec![Id::raw("rust")]);
}

#[tokio::test]
async fn detects_ts() {
let project = build_lang_project("ts").await;

assert_eq!(project.language, LanguageType::TypeScript);
assert_eq!(project.platform, PlatformType::Node);
assert_eq!(project.toolchains, vec![Id::raw("system")]);

let project = build_lang_project("ts-config").await;

assert_eq!(project.language, LanguageType::TypeScript);
assert_eq!(project.platform, PlatformType::Node);
assert_eq!(project.toolchains, vec![Id::raw("system")]);
}
}

Expand All @@ -382,42 +387,42 @@ mod project_builder {
let sandbox = create_sandbox("builder");
let project = build_project_without_inherited("baz", sandbox.path()).await;

assert_eq!(project.platform, PlatformType::Node);
assert_eq!(project.toolchains, vec![Id::raw("node")]);
}

#[tokio::test]
async fn infers_from_config_lang() {
let sandbox = create_sandbox("builder");
let project = build_project_without_inherited("bar", sandbox.path()).await;

assert_eq!(project.platform, PlatformType::Rust);
assert_eq!(project.toolchains, vec![Id::raw("rust")]);
}

#[tokio::test]
async fn infers_from_detected_lang() {
let sandbox = create_sandbox("builder");
let project = build_project_without_inherited("foo", sandbox.path()).await;

assert_eq!(project.platform, PlatformType::Node);
assert_eq!(project.toolchains, vec![Id::raw("node")]);
}

#[tokio::test]
async fn fallsback_to_project() {
let project = build_lang_project("project-platform").await;

assert_eq!(
project.tasks.get("node-a").unwrap().platform,
PlatformType::Node
project.tasks.get("node-a").unwrap().toolchains,
vec![Id::raw("node")]
);

assert_eq!(
project.tasks.get("node-b").unwrap().platform,
PlatformType::Node
project.tasks.get("node-b").unwrap().toolchains,
vec![Id::raw("node")]
);

assert_eq!(
project.tasks.get("system").unwrap().platform,
PlatformType::System
project.tasks.get("system").unwrap().toolchains,
vec![Id::raw("system")]
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/project/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ cacheable!(
/// Includes internal tasks!
pub task_targets: Vec<Target>,

/// Toolchains derived from the configured language.
pub toolchains: Vec<Id>,

/// The type of project.
#[serde(rename = "type")]
pub type_of: ProjectType,
Expand Down
2 changes: 1 addition & 1 deletion crates/task-runner/src/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl<'task> TaskRunner<'task> {
// Hash platform fields
trace!(
task_target = self.task.target.as_str(),
toolchains = ?self.task.toolchains.iter().map(|t| t.as_str()).collect::<Vec<_>>(),
toolchains = ?self.task.toolchains.iter().map(|tc| tc.as_str()).collect::<Vec<_>>(),
"Including toolchain specific fields in the hash"
);

Expand Down
34 changes: 26 additions & 8 deletions crates/toolchain/src/detect/project_toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use moon_config::LanguageType;
use std::path::Path;

pub fn detect_project_toolchains(
root: &Path,
workspace_root: &Path,
project_root: &Path,
language: &LanguageType,
enabled_toolchains: &[Id],
) -> Vec<Id> {
Expand All @@ -18,23 +19,40 @@ pub fn detect_project_toolchains(
(Id::raw("bun"), BUN),
(Id::raw("node"), NODE),
];
let mut found = false;

// Detect in project first
for (id, files) in &runtimes {
if has_language_files(project_root, files) {
toolchains.push(id.to_owned());
found = true;
break;
}
}

// Then in workspace
for (id, files) in runtimes {
if enabled_toolchains.contains(&id) && has_language_files(root, files) {
if !found && has_language_files(workspace_root, files) {
toolchains.push(id);
toolchains.extend(language.get_toolchain_ids());
break;
}
}

toolchains.extend(language.get_toolchain_ids());
}
other => {
let id = Id::raw(other.to_string());

if enabled_toolchains.contains(&id) {
toolchains.push(id);
}
toolchains.push(Id::raw(other.to_string()));
}
};

let mut toolchains = toolchains
.into_iter()
.filter(|id| enabled_toolchains.contains(&id))
.collect::<Vec<_>>();

if toolchains.is_empty() {
toolchains.push(Id::raw("system"));
}

toolchains
}

0 comments on commit 47054c0

Please sign in to comment.