add more integration tests && implement args

This commit is contained in:
Zeyi Fan 2019-08-15 00:18:00 -07:00
parent 6a9b334721
commit f345f7589d
6 changed files with 115 additions and 15 deletions

3
fixtures/args.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("{}", std::env::args().skip(1).next().unwrap());
}

4
fixtures/edition.rs Normal file
View file

@ -0,0 +1,4 @@
fn main() {
let await = 1;
println!("hello {}", await);
}

View file

@ -56,7 +56,13 @@ fn main() -> Result<(), CargoPlayError> {
let end = if let Some(save) = opt.save {
copy_project(&temp, &save)?
} else {
run_cargo_build(opt.toolchain, &temp, opt.release, opt.cargo_option)?
run_cargo_build(
opt.toolchain,
&temp,
opt.release,
opt.cargo_option,
&opt.args,
)?
};
match end.code() {

View file

@ -1,6 +1,6 @@
use std::ffi::{OsStr, OsString};
use std::iter::FromIterator;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::vec::Vec;
use structopt::StructOpt;
@ -86,6 +86,18 @@ pub struct Opt {
}
impl Opt {
#[allow(unused)]
/// Convenient constructor for testing
pub fn with_files<I: AsRef<Path>>(src: Vec<I>) -> Self {
Opt {
src: src
.into_iter()
.filter_map(|x| std::fs::canonicalize(x).ok())
.collect(),
..Default::default()
}
}
/// Generate a string of hash based on the path passed in
pub fn src_hash(&self) -> String {
let mut hash = sha1::Sha1::new();

View file

@ -118,6 +118,7 @@ pub fn run_cargo_build(
project: &PathBuf,
release: bool,
cargo_option: Option<String>,
program_args: &[String],
) -> Result<ExitStatus, CargoPlayError> {
let mut cargo = Command::new("cargo");
@ -140,6 +141,8 @@ pub fn run_cargo_build(
}
cargo
.arg("--")
.args(program_args)
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.status()

View file

@ -4,7 +4,7 @@ use rand::{thread_rng, Rng};
use std::env;
use std::ffi::OsStr;
use std::io::Result;
use std::path::{PathBuf, Path};
use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Output, Stdio};
struct TestRuntime {
@ -32,7 +32,13 @@ impl TestRuntime {
}
fn random_string() -> String {
format!("cargo-play-test.{}", thread_rng().sample_iter(&Alphanumeric).take(10).collect::<String>())
format!(
"cargo-play-test.{}",
thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.collect::<String>()
)
}
fn temp_dir<I: AsRef<Path>>(&self, path: I) -> PathBuf {
@ -52,9 +58,11 @@ impl TestRuntime {
.output()
.map(From::from)
}
}
fn cleanup(&self) {
let _ = std::fs::remove_dir_all(&self.scratch).map_err(|e| eprintln!("errr: {:?}", e));
impl Drop for TestRuntime {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.scratch);
}
}
@ -87,25 +95,20 @@ impl From<std::process::Output> for StringOutput {
}
#[test]
fn basic_compile() -> Result<()> {
fn basic() -> Result<()> {
let rt = TestRuntime::new()?;
let output = rt.run(&["-c", "fixtures/hello.rs"])?;
let output = rt.run(&["fixtures/hello.rs"])?;
assert_eq!(output.status.code().unwrap(), 0);
assert_eq!(output.stdout, "Hello World!\n");
rt.cleanup();
Ok(())
}
#[test]
fn clean() -> Result<()> {
let rt = TestRuntime::new()?;
let opt = Opt {
src: vec![PathBuf::from("fixtures/hello.rs").canonicalize()?],
..Default::default()
};
let opt = Opt::with_files(vec!["fixtures/hello.rs"]);
let path = rt.temp_dir(opt.temp_dirname());
let canary = path.clone().join("canary");
@ -123,7 +126,76 @@ fn clean() -> Result<()> {
let _ = rt.run(&["--clean", "fixtures/hello.rs"])?;
assert!(!canary.exists());
rt.cleanup();
Ok(())
}
#[test]
fn edition() -> Result<()> {
let rt = TestRuntime::new()?;
// default edition is 2018
let output = rt.run(&["fixtures/edition.rs"])?;
assert_ne!(output.status.code().unwrap(), 0);
let output = rt.run(&["--edition", "2018", "fixtures/edition.rs"])?;
assert_ne!(output.status.code().unwrap(), 0);
// it should pass in 2015
let output = rt.run(&["--edition", "2015", "fixtures/edition.rs"])?;
assert_eq!(output.status.code().unwrap(), 0);
Ok(())
}
#[test]
fn debug_mode() -> Result<()> {
let rt = TestRuntime::new()?;
let opt = Opt::with_files(vec!["fixtures/hello.rs"]);
let path = rt.temp_dir(opt.temp_dirname());
let _ = rt.run(&["fixtures/hello.rs"])?;
assert!(path.join("target").join("debug").exists());
assert!(!path.join("target").join("release").exists());
Ok(())
}
#[test]
fn release_mode() -> Result<()> {
let rt = TestRuntime::new()?;
let opt = Opt::with_files(vec!["fixtures/hello.rs"]);
let path = rt.temp_dir(opt.temp_dirname());
let _ = rt.run(&["--release", "fixtures/hello.rs"])?;
assert!(!path.join("target").join("debug").exists());
assert!(path.join("target").join("release").exists());
Ok(())
}
#[test]
fn cargo_option() -> Result<()> {
let rt = TestRuntime::new()?;
let opt = Opt::with_files(vec!["fixtures/hello.rs"]);
let path = rt.temp_dir(opt.temp_dirname());
let _ = rt.run(&["--cargo-option=--release", "fixtures/hello.rs"])?;
assert!(!path.join("target").join("debug").exists());
assert!(path.join("target").join("release").exists());
Ok(())
}
#[test]
fn program_args() -> Result<()> {
let rt = TestRuntime::new()?;
let output = rt.run(&["fixtures/args.rs", "--", "test"])?;
assert_eq!(output.stdout, "test\n");
Ok(())
}