Skip to content

Commit

Permalink
Make scarb new and init command interactive (#1472)
Browse files Browse the repository at this point in the history
Closes #1207 
Closes
[#1982](foundry-rs/starknet-foundry#1982)

Make scarb new and init command interactive
  • Loading branch information
ksew1 authored Jul 29, 2024
1 parent 9d0e0ca commit 51f7015
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 9 deletions.
14 changes: 14 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ darling = "0.20"
data-encoding = "2"
deno_task_shell = ">=0.13"
derive_builder = ">=0.12"
dialoguer = "0.11.0"
directories = "5"
dunce = "1"
expect-test = "1.5"
Expand Down
1 change: 1 addition & 0 deletions scarb-metadata/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn manifest_path() {
fn init_project(t: &TempDir) {
Command::new(scarb_bin())
.args(["init", "--name", "hello"])
.env("SCARB_INIT_TEST_RUNNER", "cairo-test")
.current_dir(t)
.assert()
.success();
Expand Down
1 change: 1 addition & 0 deletions scarb-metadata/tests/scarb_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn sample_project() {
fn init_project(t: &TempDir) {
Command::new(scarb_bin())
.args(["init", "--name", "hello"])
.env("SCARB_INIT_TEST_RUNNER", "cairo-test")
.current_dir(t)
.assert()
.success();
Expand Down
1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ create-output-dir = { path = "../utils/create-output-dir" }
data-encoding.workspace = true
deno_task_shell.workspace = true
derive_builder.workspace = true
dialoguer.workspace = true
directories.workspace = true
dunce.workspace = true
fs4.workspace = true
Expand Down
12 changes: 9 additions & 3 deletions scarb/src/bin/scarb/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ pub struct ScriptsRunnerArgs {
pub args: Vec<OsString>,
}

#[derive(ValueEnum, Clone, Debug)]
pub enum TestRunner {
StarknetFoundry,
CairoTest,
}

/// Arguments accepted by the `init` command.
#[derive(Parser, Clone, Debug)]
pub struct InitArgs {
Expand All @@ -283,9 +289,9 @@ pub struct InitArgs {
#[arg(long)]
pub no_vcs: bool,

/// Use a Starknet Foundry Forge template.
#[arg(long)]
pub snforge: bool,
/// Test runner to use. Starts interactive session if not specified.
#[arg(long, env = "SCARB_INIT_TEST_RUNNER")]
pub test_runner: Option<TestRunner>,
}

/// Arguments accepted by the `metadata` command.
Expand Down
8 changes: 6 additions & 2 deletions scarb/src/bin/scarb/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use camino::Utf8PathBuf;
use scarb::core::Config;
use scarb::ops::{self, VersionControl};

use crate::args::InitArgs;
use crate::args::{InitArgs, TestRunner};
use crate::interactive::get_or_ask_for_test_runner;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: InitArgs, config: &Config) -> Result<()> {
Expand All @@ -24,7 +25,10 @@ pub fn run(args: InitArgs, config: &Config) -> Result<()> {
} else {
VersionControl::Git
},
snforge: args.snforge,
snforge: matches!(
get_or_ask_for_test_runner(args.test_runner)?,
TestRunner::StarknetFoundry
),
},
config,
)?;
Expand Down
8 changes: 6 additions & 2 deletions scarb/src/bin/scarb/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use anyhow::Result;
use scarb::core::Config;
use scarb::ops::{self, VersionControl};

use crate::args::NewArgs;
use crate::args::{NewArgs, TestRunner};
use crate::interactive::get_or_ask_for_test_runner;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: NewArgs, config: &Config) -> Result<()> {
Expand All @@ -18,7 +19,10 @@ pub fn run(args: NewArgs, config: &Config) -> Result<()> {
} else {
VersionControl::Git
},
snforge: args.init.snforge,
snforge: matches!(
get_or_ask_for_test_runner(args.init.test_runner)?,
TestRunner::StarknetFoundry
),
},
config,
)?;
Expand Down
45 changes: 45 additions & 0 deletions scarb/src/bin/scarb/interactive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::io::{self, IsTerminal};

use crate::args::TestRunner;
use anyhow::{ensure, Result};
use dialoguer::theme::ColorfulTheme;
use dialoguer::Select;
use indoc::indoc;
use which::which;

pub fn get_or_ask_for_test_runner(test_runner: Option<TestRunner>) -> Result<TestRunner> {
Ok(test_runner)
.transpose()
.unwrap_or_else(ask_for_test_runner)
}

fn ask_for_test_runner() -> Result<TestRunner> {
ensure!(
io::stdout().is_terminal(),
indoc! {r"
you are not running in terminal
help: please provide the --test-runner flag
"}
);

let options = if which("snforge").is_ok() {
vec!["Starknet Foundry (default)", "Cairo Test"]
} else {
vec![
"Cairo Test (default)",
"Starknet Foundry (recommended, requires snforge installed: https://github.com/foundry-rs/starknet-foundry)",
]
};

let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Which test runner do you want to set up?")
.items(&options)
.default(0)
.interact()?;

if options[selection].starts_with("Cairo Test") {
Ok(TestRunner::CairoTest)
} else {
Ok(TestRunner::StarknetFoundry)
}
}
1 change: 1 addition & 0 deletions scarb/src/bin/scarb/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::errors::ErrorWithExitCode;
mod args;
mod commands;
mod errors;
mod interactive;

fn main() {
let args = ScarbArgs::parse();
Expand Down
37 changes: 36 additions & 1 deletion scarb/tests/new_and_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn new_no_path_arg() {
error: the following required arguments were not provided:
<PATH>
Usage: scarb[..] new <PATH>
Usage: scarb[..] new [..] <PATH>
For more information, try '--help'.
"#});
Expand All @@ -146,6 +146,41 @@ fn new_existing() {
"#});
}

#[test]
fn new_interactive_not_in_terminal() {
let pt = assert_fs::TempDir::new().unwrap();

Scarb::quick_snapbox()
.arg("new")
.arg("hello")
.env_remove("SCARB_INIT_TEST_RUNNER")
.current_dir(&pt)
.assert()
.failure()
.stdout_eq(indoc! {r"
error: you are not running in terminal
help: please provide the --test-runner flag
"});
}

#[test]
fn init_interactive_not_in_terminal() {
let pt = assert_fs::TempDir::new().unwrap();
let t = pt.child("hello");
t.create_dir_all().unwrap();

Scarb::quick_snapbox()
.arg("init")
.env_remove("SCARB_INIT_TEST_RUNNER")
.current_dir(&t)
.assert()
.failure()
.stdout_eq(indoc! {r"
error: you are not running in terminal
help: please provide the --test-runner flag
"});
}

#[test]
fn issue_148() {
let pt = assert_fs::TempDir::new().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion scarb/tests/snforge_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn new_simple() {
Scarb::quick_snapbox()
.arg("new")
.arg("hello")
.arg("--snforge")
.args(["--test-runner", "starknet-foundry"])
.current_dir(&pt)
.assert()
.success();
Expand Down
1 change: 1 addition & 0 deletions utils/scarb-test-support/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Scarb {
cmd.env("SCARB_LOG", self.log);
cmd.env("SCARB_CACHE", self.cache.path());
cmd.env("SCARB_CONFIG", self.config.path());
cmd.env("SCARB_INIT_TEST_RUNNER", "cairo-test");
cmd
}

Expand Down

0 comments on commit 51f7015

Please sign in to comment.