Skip to content

Commit

Permalink
Add tests for interactive commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ksew1 committed Jul 25, 2024
1 parent 3eb4cf5 commit 3a87409
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 13 deletions.
32 changes: 32 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 @@ -96,6 +96,7 @@ pathdiff = { version = "0.2", features = ["camino"] }
petgraph = "0.6"
predicates = "3"
proc-macro2 = "1"
ptyprocess = "0.4.1"
quote = "1"
ra_ap_toolchain = "0.0.218"
rayon = "1.10"
Expand Down
2 changes: 1 addition & 1 deletion scarb-metadata/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn manifest_path() {

fn init_project(t: &TempDir) {
Command::new(scarb_bin())
.args(["init", "--name", "hello"])
.args(["init", "--name", "hello", "--test-runner", "cairo-test"])
.current_dir(t)
.assert()
.success();
Expand Down
2 changes: 1 addition & 1 deletion scarb-metadata/tests/scarb_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn sample_project() {

fn init_project(t: &TempDir) {
Command::new(scarb_bin())
.args(["init", "--name", "hello"])
.args(["init", "--name", "hello", "--test-runner", "cairo-test"])
.current_dir(t)
.assert()
.success();
Expand Down
8 changes: 3 additions & 5 deletions scarb/src/bin/scarb/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ pub fn run(args: InitArgs, config: &Config) -> Result<()> {
VersionControl::Git
},
snforge: matches!(
if let Some(test_runner) = args.test_runner {
test_runner
} else {
ask_for_test_runner()?
},
Ok(args.test_runner)
.transpose()
.unwrap_or_else(ask_for_test_runner)?,
TestRunner::StarknetFoundry
),
},
Expand Down
8 changes: 3 additions & 5 deletions scarb/src/bin/scarb/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ pub fn run(args: NewArgs, config: &Config) -> Result<()> {
VersionControl::Git
},
snforge: matches!(
if let Some(test_runner) = args.init.test_runner {
test_runner
} else {
ask_for_test_runner()?
},
Ok(args.init.test_runner)
.transpose()
.unwrap_or_else(ask_for_test_runner)?,
TestRunner::StarknetFoundry
),
},
Expand Down
2 changes: 1 addition & 1 deletion scarb/src/bin/scarb/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn ask_for_test_runner() -> Result<TestRunner> {
} else {
vec![
"Cairo Test (default)",
"Starknet Foundry (recommended, requires snforge installed)",
"Starknet Foundry (recommended, requires snforge installed: https://github.com/foundry-rs/starknet-foundry)",
]
};

Expand Down
71 changes: 71 additions & 0 deletions scarb/tests/new_and_init.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs;

use assert_fs::prelude::*;
use indoc::indoc;
use predicates::prelude::*;
Expand All @@ -6,8 +8,10 @@ use toml::{Table, Value};
use scarb::core::TomlManifest;
use scarb_test_support::command::Scarb;
use scarb_test_support::fsx::AssertFsUtf8Ext;
use scarb_test_support::interactive::InteractiveCommand;

const CAIRO_NATIVE_RUNNER: [&str; 2] = ["--test-runner", "cairo-test"];
const ARROW_DOWN: &str = "\x1B[B";

#[test]
fn new_simple() {
Expand Down Expand Up @@ -403,3 +407,70 @@ fn init_core_name() {
.assert()
.success();
}

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

let mut command = Scarb::new().std();
command.arg("new").arg("hello").current_dir(&pt);

let output = InteractiveCommand::run(command)
.send_line(ARROW_DOWN)
.exit();

assert!(output.contains("Created `hello` package."));

let t = pt.child("hello");
assert!(t.is_dir());
assert!(t.child("Scarb.toml").is_file());
assert!(t.child("src/lib.cairo").is_file());
assert!(t.child(".gitignore").is_file());
assert!(t.child(".git").is_dir());

let toml_manifest = TomlManifest::read_from_path(t.child("Scarb.toml").utf8_path()).unwrap();
assert_eq!(toml_manifest.package.unwrap().name.as_str(), "hello");

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.success();

t.child("target/dev/hello.sierra.json")
.assert(predicates::str::is_empty().not());
}

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

let mut command = Scarb::new().std();
command.arg("init").current_dir(&t);

let output = InteractiveCommand::run(command)
.send_line(ARROW_DOWN)
.exit();

assert!(output.contains("Created package."));

assert!(t.is_dir());
assert!(t.child("Scarb.toml").is_file());
assert!(t.child("src/lib.cairo").is_file());
assert!(t.child(".gitignore").is_file());
assert!(t.child(".git").is_dir());

let toml_manifest = TomlManifest::read_from_path(t.child("Scarb.toml").utf8_path()).unwrap();
assert_eq!(toml_manifest.package.unwrap().name.as_str(), "hello");

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.success();

t.child("target/dev/hello.sierra.json")
.assert(predicates::str::is_empty().not());
}
49 changes: 49 additions & 0 deletions scarb/tests/snforge_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use assert_fs::TempDir;
use scarb::core::TomlManifest;
use scarb_test_support::command::Scarb;
use scarb_test_support::fsx::AssertFsUtf8Ext;
use scarb_test_support::interactive::InteractiveCommand;

#[test]
#[ignore = "run this test by name"]
Expand Down Expand Up @@ -52,3 +53,51 @@ fn new_simple() {
.assert()
.success();
}

#[test]
#[ignore = "run this test by name"]
fn new_simple_interactive() {
let pt = TempDir::new().unwrap();

let mut command = Scarb::new().std();
command.arg("new").arg("hello").current_dir(&pt);

let output = InteractiveCommand::run(command).send_line("").exit();

assert!(output.contains("Created `hello` package."));

let t = pt.child("hello");
assert!(t.is_dir());
assert!(t.child("Scarb.toml").is_file());
assert!(t.child("src/lib.cairo").is_file());
assert!(t.child(".gitignore").is_file());
assert!(t.child("tests").is_dir());
assert!(t.child("tests/test_contract.cairo").is_file());
assert!(t.child(".git").is_dir());

let toml_manifest = TomlManifest::read_from_path(t.child("Scarb.toml").utf8_path()).unwrap();
assert_eq!(toml_manifest.package.unwrap().name.as_str(), "hello");
let deps = toml_manifest.dependencies.unwrap();
assert_eq!(deps.len(), 1);
assert!(deps.contains_key("starknet"));
let deps = toml_manifest.dev_dependencies.unwrap();
assert_eq!(deps.len(), 1);
assert!(deps.contains_key("snforge_std"));
assert_eq!(
toml_manifest
.scripts
.unwrap()
.get("test")
.unwrap()
.as_defined()
.unwrap()
.to_string(),
"snforge test"
);

Scarb::quick_snapbox()
.arg("check")
.current_dir(&t)
.assert()
.success();
}
1 change: 1 addition & 0 deletions utils/scarb-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hyper = "0.14"
indoc.workspace = true
itertools.workspace = true
once_cell.workspace = true
ptyprocess.workspace = true
scarb = { path = "../../scarb" }
scarb-build-metadata = { path = "../scarb-build-metadata" }
scarb-ui = { path = "../scarb-ui" }
Expand Down
33 changes: 33 additions & 0 deletions utils/scarb-test-support/src/interactive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use ptyprocess::PtyProcess;
use std::io::{BufReader, Read, Write};
use std::process::Command;

pub struct InteractiveCommand {
process: PtyProcess, // we need to use PtyProcess to imitate a terminal
}

impl InteractiveCommand {
pub fn run(command: Command) -> Self {
let process = PtyProcess::spawn(command).unwrap();

Self { process }
}

pub fn send_line(&mut self, line: &str) -> &mut Self {
let mut stream = self.process.get_pty_stream().unwrap();
writeln!(stream, "{}", line).unwrap();
self
}

pub fn exit(&mut self) -> String {
let stream = self.process.get_pty_stream().unwrap();

let mut reader = BufReader::new(stream);
let mut output = String::new();
reader.read_to_string(&mut output).unwrap();

assert!(self.process.exit(false).unwrap());

output
}
}
1 change: 1 addition & 0 deletions utils/scarb-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod contracts;
pub mod filesystem;
pub mod fsx;
pub mod gitx;
pub mod interactive;
pub mod manifest_edit;
pub mod project_builder;
pub mod registry;
Expand Down

0 comments on commit 3a87409

Please sign in to comment.