From be58598a3e8c9bdd57dadf85ab8bb86624f09f1d Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Sun, 24 Jun 2018 19:37:30 -0700 Subject: [PATCH] Make web tests no longer mutate original files --- server/Cargo.lock | 7 ++++--- server/Cargo.toml | 7 ++++--- server/tests/test_util/mod.rs | 32 ++++++++++++++++++++++++++++++++ server/tests/web.rs | 22 +++++++++++++++------- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index f777c0ab..bf03e25e 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -647,7 +647,8 @@ dependencies = [ "serde 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -758,7 +759,7 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.0.1" +version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1075,7 +1076,7 @@ dependencies = [ "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum syn 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)" = "61b8f1b737f929c6516ba46a3133fd6d5215ad8a62f66760f851f7048aebedfb" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" -"checksum tempfile 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8cddbd26c5686ece823b507f304c8f188daef548b4cb753512d929ce478a093c" +"checksum tempfile 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "47776f63b85777d984a50ce49d6b9e58826b6a3766a449fc95bc66cd5663c15b" "checksum term 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "f2077e54d38055cf1ca0fd7933a2e00cd3ec8f6fed352b2a377f06dcdaaf3281" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c0b59b6b4b44d867f1370ef1bd91bfb262bf07bf0ae65c202ea2fbc16153b693" diff --git a/server/Cargo.toml b/server/Cargo.toml index 05bc6d60..e38cedd9 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -15,15 +15,16 @@ name = "rojo" path = "src/bin.rs" [dependencies] -clap = "2.27.1" +clap = "2.27" rouille = "2.1" serde = "1.0" serde_derive = "1.0" serde_json = "1.0" -notify = "4.0.0" +notify = "4.0" rand = "0.4" regex = "1.0" lazy_static = "1.0" [dev-dependencies] -tempfile = "3" +tempfile = "3.0" +walkdir = "2.1" diff --git a/server/tests/test_util/mod.rs b/server/tests/test_util/mod.rs index cca6744b..ec3c51fd 100644 --- a/server/tests/test_util/mod.rs +++ b/server/tests/test_util/mod.rs @@ -1,5 +1,11 @@ +use std::fs::{create_dir, copy}; +use std::path::Path; +use std::io; + use rouille::Request; +use walkdir::WalkDir; + use librojo::web::Server; pub trait HttpTestUtil { @@ -20,3 +26,29 @@ impl HttpTestUtil for Server { body } } + +pub fn copy_recursive(from: &Path, to: &Path) -> io::Result<()> { + for entry in WalkDir::new(from) { + let entry = entry?; + let path = entry.path(); + let new_path = to.join(path.strip_prefix(from).unwrap()); + + let file_type = entry.file_type(); + + if file_type.is_dir() { + match create_dir(new_path) { + Ok(_) => {}, + Err(err) => match err.kind() { + io::ErrorKind::AlreadyExists => {}, + _ => panic!(err), + } + } + } else if file_type.is_file() { + copy(path, new_path)?; + } else { + unimplemented!("no symlinks please"); + } + } + + Ok(()) +} diff --git a/server/tests/web.rs b/server/tests/web.rs index 1e4863c9..fcd10fd4 100644 --- a/server/tests/web.rs +++ b/server/tests/web.rs @@ -1,6 +1,8 @@ extern crate rouille; extern crate serde_json; extern crate serde; +extern crate tempfile; +extern crate walkdir; extern crate librojo; @@ -21,12 +23,17 @@ use librojo::{ #[test] fn empty() { - let project_path = { + let original_project_path = { let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); path.push("test-projects/empty"); path }; + let project_tempdir = tempfile::tempdir().unwrap(); + let project_path = project_tempdir.path(); + + copy_recursive(&original_project_path, &project_path).unwrap(); + let project = Project::load(&project_path).unwrap(); let mut session = Session::new(project.clone()); session.start(); @@ -64,12 +71,17 @@ fn empty() { #[test] fn one_partition() { - let project_path = { + let original_project_path = { let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); path.push("test-projects/one-partition"); path }; + let project_tempdir = tempfile::tempdir().unwrap(); + let project_path = project_tempdir.path(); + + copy_recursive(&original_project_path, &project_path).unwrap(); + let project = Project::load(&project_path).unwrap(); let mut session = Session::new(project.clone()); session.start(); @@ -202,11 +214,7 @@ fn one_partition() { }; { - let temp_name = { - let mut path = project_path.clone(); - path.push("lib/c.client.lua"); - path - }; + let temp_name = project_path.join("lib/c.client.lua"); { let mut file = File::create(&temp_name).unwrap();