From 077643cadf4509e775667dc37b817c82244ff3be Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Mon, 7 Aug 2023 05:09:20 +1200 Subject: [PATCH] Add tests for script subcommands (#9933) # Description Add a few tests to ensure that you can add subcommands to scripts. We've supported this for a long time, though I'm not sure if anyone has actually tried it. As we weren't testing the support, this PR adds a few tests to ensure it stays working. Example script subcommand: ``` def "main addten" [x: int] { print ($x + 10) } ``` then call it with: ``` > nu ./script.nu addten 5 ``` # User-Facing Changes # Tests + Formatting # After Submitting --- tests/shell/mod.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/shell/mod.rs b/tests/shell/mod.rs index 7eac1a14c8..20c8570542 100644 --- a/tests/shell/mod.rs +++ b/tests/shell/mod.rs @@ -1,4 +1,4 @@ -use nu_test_support::fs::Stub::FileWithContentToBeTrimmed; +use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed}; use nu_test_support::playground::Playground; use nu_test_support::{nu, nu_repl_code, pipeline}; use pretty_assertions::assert_eq; @@ -287,3 +287,45 @@ fn run_in_noninteractive_mode() { assert!(child_output.stderr.is_empty()); } + +#[test] +fn main_script_can_have_subcommands1() { + Playground::setup("main_subcommands", |dirs, sandbox| { + sandbox.mkdir("main_subcommands"); + sandbox.with_files(vec![FileWithContent( + "script.nu", + r#"def "main foo" [x: int] { + print ($x + 100) + } + + def "main" [] { + print "usage: script.nu " + }"#, + )]); + + let actual = nu!(cwd: dirs.test(), pipeline("nu script.nu foo 123")); + + assert_eq!(actual.out, "223"); + }) +} + +#[test] +fn main_script_can_have_subcommands2() { + Playground::setup("main_subcommands", |dirs, sandbox| { + sandbox.mkdir("main_subcommands"); + sandbox.with_files(vec![FileWithContent( + "script.nu", + r#"def "main foo" [x: int] { + print ($x + 100) + } + + def "main" [] { + print "usage: script.nu " + }"#, + )]); + + let actual = nu!(cwd: dirs.test(), pipeline("nu script.nu")); + + assert!(actual.out.contains("usage: script.nu")); + }) +}