Skip to content

Commit

Permalink
new: Support task scripts (as an alternative to commands). (#1530)
Browse files Browse the repository at this point in the history
* Add script field.

* Add builder tests.

* Test tokens.

* Fix cache.

* Fix lints.

* Fix tests.

* Add tests.

* Add tests.

* Update tests.
  • Loading branch information
milesj committed Jul 15, 2024
1 parent 0336fa9 commit 426354a
Show file tree
Hide file tree
Showing 63 changed files with 839 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
with:
auto-install: true
cache: ${{ runner.os == 'Linux' }}
proto-version: '0.38.1' # Keep in sync
proto-version: '0.38.2' # Keep in sync
- uses: mozilla-actions/sccache-action@v0.0.5
if: ${{ vars.ENABLE_SCCACHE == 'true' }}
- name: Checking coverage status
Expand Down
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.

2 changes: 1 addition & 1 deletion crates/app/src/commands/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub async fn project(session: CliSession, args: ProjectArgs) -> AppResult {
console.write_line(format!(
" {} {}",
color::muted("›"),
color::shell(format!("{} {}", task.command, task.args.join(" "))),
color::shell(task.get_command_line()),
))?;

if let Some(description) = &task.description {
Expand Down
8 changes: 6 additions & 2 deletions crates/app/src/commands/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ pub async fn task(session: CliSession, args: TaskArgs) -> AppResult {

console.print_entry_header("Process")?;
console.print_entry(
"Command",
color::shell(format!("{} {}", task.command, task.args.join(" "))),
if task.script.is_some() {
"Script"
} else {
"Command"
},
color::shell(task.get_command_line()),
)?;

if !task.env.is_empty() {
Expand Down
11 changes: 11 additions & 0 deletions crates/cli/tests/run_bun_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@ mod bun {
assert!(predicate::str::contains("bun:mjs | stderr").eval(&output));
}

#[test]
fn runs_script_task() {
let sandbox = bun_sandbox();

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("bun:viaScript");
});

assert_snapshot!(assert.output());
}

mod package_manager {
use super::*;

Expand Down
15 changes: 15 additions & 0 deletions crates/cli/tests/run_deno_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ mod deno {
assert!(predicate::str::contains("exit code 1").eval(&output));
}

#[test]
fn runs_script_task() {
let sandbox = deno_sandbox();

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("deno:viaScript");
});

let output = assert.output();

// Output includes the arch, so can't be snapshotted
assert!(predicate::str::contains("deno platform").eval(&output));
assert!(predicate::str::contains("deno 1.40.0").eval(&output));
}

mod workspace_overrides {
use super::*;

Expand Down
68 changes: 66 additions & 2 deletions crates/cli/tests/run_node_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,17 @@ fn can_exec_global_bin_as_child_process_from_postinstall() {
assert.success();
}

#[test]
fn runs_script_task() {
let sandbox = node_sandbox();

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("node:viaScript");
});

assert_snapshot!(assert.output());
}

mod install_deps {
use super::*;

Expand Down Expand Up @@ -747,6 +758,17 @@ mod npm {

assert!(predicate::str::contains("8.0.0").eval(&assert.output()));
}

#[test]
fn runs_script_task() {
let sandbox = depman_sandbox("npm");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("npm:viaScript");
});

assert_snapshot!(assert.output());
}
}

mod pnpm {
Expand Down Expand Up @@ -860,6 +882,17 @@ mod pnpm {

assert!(predicate::str::contains("7.5.0").eval(&assert.output()));
}

#[test]
fn runs_script_task() {
let sandbox = depman_sandbox("pnpm");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("pnpm:viaScript");
});

assert_snapshot!(assert.output());
}
}

mod yarn1 {
Expand Down Expand Up @@ -951,6 +984,17 @@ mod yarn1 {

assert!(predicate::str::contains("1.22.0").eval(&assert.output()));
}

#[test]
fn runs_script_task() {
let sandbox = depman_sandbox("yarn1");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("yarn1:viaScript");
});

assert_snapshot!(assert.output());
}
}

mod yarn {
Expand Down Expand Up @@ -1042,10 +1086,19 @@ mod yarn {

assert!(predicate::str::contains("3.3.0").eval(&assert.output()));
}

#[test]
fn runs_script_task() {
let sandbox = depman_sandbox("yarn");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("yarn:viaScript");
});

assert_snapshot!(assert.output());
}
}

// TODO: Bun doesn't support Windows yet!
#[cfg(not(windows))]
mod bun {
use super::*;

Expand Down Expand Up @@ -1118,6 +1171,17 @@ mod bun {

assert!(predicate::str::contains("7.5.0").eval(&assert.output()));
}

#[test]
fn runs_script_task() {
let sandbox = depman_sandbox("bun");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("bun:viaScript");
});

assert_snapshot!(assert.output());
}
}

mod profile {
Expand Down
14 changes: 14 additions & 0 deletions crates/cli/tests/run_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ fn retries_on_failure_till_count() {
assert!(predicate::str::contains("exit code 1").eval(&output));
}

#[test]
fn runs_script_task() {
let sandbox = rust_sandbox();

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("rust:viaScript");
});

let output = assert.output();

// Versions aren't pinned, so don't use snapshots
assert!(predicate::str::contains("rust platform").eval(&output));
}

mod rustup_toolchain {
use super::*;

Expand Down
73 changes: 71 additions & 2 deletions crates/cli/tests/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,11 @@ mod hashing {
// Hashes change because `.moon/workspace.yml` is different from `walk_strategy`
assert_eq!(
hash_vcs,
"7e215e62b9638062652afe57741e2d0c8f2f00a6266f7a08afae064c765bef00"
"6327629ca97be90906a73654ab686bcc1ac032ae9d1f6baba2945a9ee9847a7a"
);
assert_eq!(
hash_glob,
"0c47617895ad77d9dc87afbcb71de5717349de3c9b9004312a0e054f2b2aefc5"
"ef9068f77d3a2f21c38b389eafeab2fce98d50f2dc647cbb865633089139588a"
);
}
}
Expand Down Expand Up @@ -2078,3 +2078,72 @@ mod sync_vcs_hooks {
assert!(sandbox.path().join(".moon/hooks").exists());
}
}

// Tasks are using unix commands!
#[cfg(unix)]
mod task_scripts {
use super::*;

#[test]
fn supports_multiple_commands() {
let sandbox = cases_sandbox();
sandbox.enable_git();

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("taskScript:multi");
});

assert_snapshot!(assert.output());

assert.success();
}

#[test]
fn supports_pipes() {
let sandbox = cases_sandbox();
sandbox.enable_git();

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("taskScript:pipe");
});

assert_snapshot!(assert.output());

assert.success();
}

#[test]
fn supports_redirects() {
let sandbox = cases_sandbox();
sandbox.enable_git();

sandbox
.run_moon(|cmd| {
cmd.arg("run").arg("taskScript:redirect");
})
.success();

sandbox.debug_files();

assert_eq!(
fs::read_to_string(sandbox.path().join("task-script/file.txt")).unwrap(),
"contents\n"
);
}

#[test]
fn doesnt_passthrough_args() {
let sandbox = cases_sandbox();
sandbox.enable_git();

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run")
.arg("taskScript:args")
.args(["--", "a", "-b", "--c"]);
});

assert_snapshot!(assert.output());

assert.success();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/cli/tests/run_bun_test.rs
expression: assert.output()
---
▪▪▪▪ bun:viaScript
bun platform
1.1.3
▪▪▪▪ bun:viaScript (100ms)

Tasks: 1 completed
Time: 100ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/cli/tests/run_deno_test.rs
expression: assert.output()
---
▪▪▪▪ deno:viaScript
deno platform
deno 1.40.0 (release, aarch64-apple-darwin)
v8 12.1.285.6
typescript 5.3.3
▪▪▪▪ deno:viaScript (100ms)

Tasks: 1 completed
Time: 100ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/cli/tests/run_node_test.rs
expression: assert.output()
---
▪▪▪▪ bun:viaScript
node+bun platform
v18.0.0
1.1.19
▪▪▪▪ bun:viaScript (100ms)

Tasks: 1 completed
Time: 100ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/cli/tests/run_node_test.rs
expression: assert.output()
---
▪▪▪▪ npm:viaScript
node+npm platform
v18.0.0
8.0.0
▪▪▪▪ npm:viaScript (100ms)

Tasks: 1 completed
Time: 100ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/cli/tests/run_node_test.rs
expression: assert.output()
---
▪▪▪▪ pnpm:viaScript
node+pnpm platform
v18.0.0
7.5.0
▪▪▪▪ pnpm:viaScript (100ms)

Tasks: 1 completed
Time: 100ms
11 changes: 11 additions & 0 deletions crates/cli/tests/snapshots/run_node_test__runs_script_task.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/cli/tests/run_node_test.rs
expression: assert.output()
---
▪▪▪▪ node:viaScript
node platform
v18.0.0
▪▪▪▪ node:viaScript (100ms)

Tasks: 1 completed
Time: 100ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/cli/tests/run_node_test.rs
expression: assert.output()
---
▪▪▪▪ yarn1:viaScript
node+yarn1 platform
v18.0.0
1.22.0
▪▪▪▪ yarn1:viaScript (100ms)

Tasks: 1 completed
Time: 100ms
Loading

0 comments on commit 426354a

Please sign in to comment.