diff --git a/packages/perseus-cli/tests/build.rs b/packages/perseus-cli/tests/build.rs index 316c31c4d8..cfd3505757 100644 --- a/packages/perseus-cli/tests/build.rs +++ b/packages/perseus-cli/tests/build.rs @@ -6,6 +6,8 @@ use assert_fs::{ use predicates::prelude::*; use std::process::Command; +use crate::utils::init_test; + /// Makes sure that `perseus build` produces the correct artifacts. /// /// This test is tightly coupled to the form of the static artifacts, and can @@ -14,13 +16,7 @@ use std::process::Command; #[ignore] fn build_produces_artifacts() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Build the app let mut cmd = Command::cargo_bin("perseus")?; diff --git a/packages/perseus-cli/tests/clean.rs b/packages/perseus-cli/tests/clean.rs index 134e24f6b5..128dd4bd9a 100644 --- a/packages/perseus-cli/tests/clean.rs +++ b/packages/perseus-cli/tests/clean.rs @@ -6,19 +6,15 @@ use assert_fs::{ use predicates::prelude::*; use std::process::Command; +use crate::utils::init_test; + /// Makes sure that `perseus clean` removes the `dist/` directory entirely after /// a build. #[test] #[ignore] fn clean_removes_dist() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Build the app and make sure `clean` removes the `dist/` directory let mut cmd = Command::cargo_bin("perseus")?; diff --git a/packages/perseus-cli/tests/export.rs b/packages/perseus-cli/tests/export.rs index 1c62c95c1c..5b8c5d1194 100644 --- a/packages/perseus-cli/tests/export.rs +++ b/packages/perseus-cli/tests/export.rs @@ -1,4 +1,4 @@ -use crate::utils::test_serve; +use crate::utils::{init_test, test_serve}; use assert_cmd::prelude::*; use assert_fs::{ prelude::{PathAssert, PathChild}, @@ -12,13 +12,7 @@ use std::process::Command; #[ignore] fn export_produces_artifacts() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Export the app let mut cmd = Command::cargo_bin("perseus")?; @@ -45,13 +39,7 @@ fn export_produces_artifacts() -> Result<(), Box> { #[ignore] fn export_serve_serves() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Export the app first let mut cmd = Command::cargo_bin("perseus")?; diff --git a/packages/perseus-cli/tests/export_error_page.rs b/packages/perseus-cli/tests/export_error_page.rs index 866bfe34f5..042d309e76 100644 --- a/packages/perseus-cli/tests/export_error_page.rs +++ b/packages/perseus-cli/tests/export_error_page.rs @@ -6,18 +6,14 @@ use assert_fs::{ use predicates::prelude::*; use std::process::Command; +use crate::utils::init_test; + /// Makes sure that `perseus export-error-page` produces the correct error page. #[test] #[ignore] fn export_error_page_produces_page() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Build the app let mut cmd = Command::cargo_bin("perseus")?; diff --git a/packages/perseus-cli/tests/lib.rs b/packages/perseus-cli/tests/lib.rs index 986984d58f..60aa489150 100644 --- a/packages/perseus-cli/tests/lib.rs +++ b/packages/perseus-cli/tests/lib.rs @@ -1,5 +1,6 @@ mod build; mod clean; +mod deploy; mod export; mod export_error_page; mod help; @@ -11,8 +12,55 @@ mod snoop_wasm_build; // mod tools; mod utils { + use assert_cmd::prelude::*; + use assert_fs::{prelude::PathChild, TempDir}; + use predicates::prelude::*; use std::process::Command; + /// Initializes a Perseus CLI test by creating a new example app and setting + /// it to use the bleeding-edge version of the core, so that it tests + /// correctly. + /// + /// This uses the `init` command of the CLI under the hood. + pub fn init_test(dir: &TempDir) -> Result<(), Box> { + let mut cmd = Command::cargo_bin("perseus")?; + cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! + .arg("init") + .arg("my-app"); + cmd.assert() + .success() + .stdout(predicate::str::contains("Your new app has been created!")); + + // Switch to the development version so we're not testing the bleeding-edge CLI + // with the most recently released version of the core itself (where a + // lot of bugs will originate) + let manifest = dir.child("Cargo.toml"); + let contents = std::fs::read_to_string(&manifest).unwrap(); + // The manifest directory is `packages/perseus-cli` within the project + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap() + .to_string_lossy() + .to_string(); + let updated_contents = contents + .replace( + r#"perseus = { "#, + &format!(r#"perseus = {{ path = "{}/packages/perseus", "#, &path), + ) + .replace( + r#"perseus-axum = { "#, + &format!( + r#"perseus-axum = {{ path = "{}/packages/perseus-axum", "#, + &path + ), + ); + std::fs::write(manifest, updated_contents).unwrap(); + + Ok(()) + } + /// Tests a running app by executing the given command. This will safely /// terminate any child processes in the event of an error. pub fn test_serve(cmd: &mut Command, path: &str) -> Result<(), Box> { diff --git a/packages/perseus-cli/tests/new.rs b/packages/perseus-cli/tests/new.rs index c895adf9f8..3512af264f 100644 --- a/packages/perseus-cli/tests/new.rs +++ b/packages/perseus-cli/tests/new.rs @@ -5,6 +5,8 @@ use std::process::Command; /// Makes sure `perseus new` successfully generates the hardcoded example with /// the right version of Perseus. +/// +/// This does not bother using the bleeding-edge core. #[test] #[ignore] fn new_creates_files() -> Result<(), Box> { @@ -38,6 +40,8 @@ fn new_creates_files() -> Result<(), Box> { /// /// Init uses the same code as `new`, so there's no point in testing whether or /// not it can be properly served. +/// +/// This does not bother using the bleeding-edge core. #[test] #[ignore] fn init_creates_files() -> Result<(), Box> { @@ -63,24 +67,3 @@ fn init_creates_files() -> Result<(), Box> { Ok(()) } - -/// Makes sure the output of `perseus new` can be successfully served. -#[test] -#[ignore] -fn new_can_be_served() -> Result<(), Box> { - let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()).arg("new").arg("my-app"); - cmd.assert().success(); - - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path().join("my-app")) - .arg("serve") - // We don't want to instantiate a full server - .arg("--no-run"); - cmd.assert().success().stdout(predicate::str::contains( - "Not running server because `--no-run` was provided.", - )); - - Ok(()) -} diff --git a/packages/perseus-cli/tests/serve.rs b/packages/perseus-cli/tests/serve.rs index eaed49625d..8a152cbaac 100644 --- a/packages/perseus-cli/tests/serve.rs +++ b/packages/perseus-cli/tests/serve.rs @@ -1,4 +1,4 @@ -use crate::utils::test_serve; +use crate::utils::{init_test, test_serve}; use assert_cmd::prelude::*; use assert_fs::{ prelude::{PathAssert, PathChild}, @@ -15,13 +15,7 @@ use std::process::Command; #[ignore] fn serve_produces_artifacts_and_serves() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Serve the app without running it let mut cmd = Command::cargo_bin("perseus")?; diff --git a/packages/perseus-cli/tests/snoop_build.rs b/packages/perseus-cli/tests/snoop_build.rs index 95a1a98575..4a24b95c46 100644 --- a/packages/perseus-cli/tests/snoop_build.rs +++ b/packages/perseus-cli/tests/snoop_build.rs @@ -6,6 +6,8 @@ use assert_fs::{ use predicates::prelude::*; use std::process::Command; +use crate::utils::init_test; + /// Makes sure that `perseus snoop build` produces the correct artifacts. /// /// This test is tightly coupled to the form of the static artifacts, and can @@ -14,13 +16,7 @@ use std::process::Command; #[ignore] fn snoop_build_produces_artifacts() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Build the app let mut cmd = Command::cargo_bin("perseus")?; @@ -62,13 +58,7 @@ fn snoop_build_produces_artifacts() -> Result<(), Box> { #[ignore] fn snoop_build_prints_dbg() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; let index_template = dir.child("src/templates/index.rs"); let contents = std::fs::read_to_string(&index_template).unwrap(); diff --git a/packages/perseus-cli/tests/snoop_serve.rs b/packages/perseus-cli/tests/snoop_serve.rs index 42aaed7523..a5d6431e82 100644 --- a/packages/perseus-cli/tests/snoop_serve.rs +++ b/packages/perseus-cli/tests/snoop_serve.rs @@ -1,4 +1,4 @@ -use crate::utils::test_serve; +use crate::utils::{init_test, test_serve}; use assert_cmd::prelude::*; use assert_fs::TempDir; use predicates::prelude::*; @@ -9,13 +9,7 @@ use std::process::Command; #[ignore] fn snoop_serve_serves() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Build the app first let mut cmd = Command::cargo_bin("perseus")?; diff --git a/packages/perseus-cli/tests/snoop_wasm_build.rs b/packages/perseus-cli/tests/snoop_wasm_build.rs index fe86cdf91a..be95479914 100644 --- a/packages/perseus-cli/tests/snoop_wasm_build.rs +++ b/packages/perseus-cli/tests/snoop_wasm_build.rs @@ -6,6 +6,8 @@ use assert_fs::{ use predicates::prelude::*; use std::process::Command; +use crate::utils::init_test; + /// Makes sure that `perseus snoop wasm-build` produces the correct artifacts. /// /// This test is tightly coupled to the form of the static artifacts, and can @@ -14,13 +16,7 @@ use std::process::Command; #[ignore] fn snoop_wasm_build_produces_artifacts() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Build the app let mut cmd = Command::cargo_bin("perseus")?; diff --git a/packages/perseus-cli/tests/tools.rs b/packages/perseus-cli/tests/tools.rs index e29e6e04bb..365c13a993 100644 --- a/packages/perseus-cli/tests/tools.rs +++ b/packages/perseus-cli/tests/tools.rs @@ -9,6 +9,8 @@ use assert_fs::{ use predicates::prelude::*; use std::process::Command; +use crate::utils::init_test; + /// Makes sure that `perseus build` with tools installed in the local directory /// works fully, which tests the tool installation process and use (their paths /// are adjustable, and it's not really feasible to test the system-wide @@ -17,13 +19,7 @@ use std::process::Command; #[ignore] fn local_tool_installation_works() -> Result<(), Box> { let dir = TempDir::new()?; - let mut cmd = Command::cargo_bin("perseus")?; - cmd.env("TEST_EXAMPLE", dir.path()) // In dev, the CLI can be made to run anywhere! - .arg("init") - .arg("my-app"); - cmd.assert() - .success() - .stdout(predicate::str::contains("Your new app has been created!")); + init_test(&dir)?; // Build the app let mut cmd = Command::cargo_bin("perseus")?;