Skip to content

Commit

Permalink
fix(cli): fixed cli tests to test bleeding-edge core
Browse files Browse the repository at this point in the history
Previously, they were actually testing the most recently deployed
version because of the way `perseus init` works! This led to issues like #259.
  • Loading branch information
arctic-hen7 committed Feb 26, 2023
1 parent c15624e commit 27442d5
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 101 deletions.
10 changes: 3 additions & 7 deletions packages/perseus-cli/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,13 +16,7 @@ use std::process::Command;
#[ignore]
fn build_produces_artifacts() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand Down
10 changes: 3 additions & 7 deletions packages/perseus-cli/tests/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
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")?;
Expand Down
18 changes: 3 additions & 15 deletions packages/perseus-cli/tests/export.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -12,13 +12,7 @@ use std::process::Command;
#[ignore]
fn export_produces_artifacts() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand All @@ -45,13 +39,7 @@ fn export_produces_artifacts() -> Result<(), Box<dyn std::error::Error>> {
#[ignore]
fn export_serve_serves() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand Down
10 changes: 3 additions & 7 deletions packages/perseus-cli/tests/export_error_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
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")?;
Expand Down
48 changes: 48 additions & 0 deletions packages/perseus-cli/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod build;
mod clean;
mod deploy;
mod export;
mod export_error_page;
mod help;
Expand All @@ -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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
Expand Down
25 changes: 4 additions & 21 deletions packages/perseus-cli/tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand Down Expand Up @@ -38,6 +40,8 @@ fn new_creates_files() -> Result<(), Box<dyn std::error::Error>> {
///
/// 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<dyn std::error::Error>> {
Expand All @@ -63,24 +67,3 @@ fn init_creates_files() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

/// Makes sure the output of `perseus new` can be successfully served.
#[test]
#[ignore]
fn new_can_be_served() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
10 changes: 2 additions & 8 deletions packages/perseus-cli/tests/serve.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -15,13 +15,7 @@ use std::process::Command;
#[ignore]
fn serve_produces_artifacts_and_serves() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand Down
18 changes: 4 additions & 14 deletions packages/perseus-cli/tests/snoop_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,13 +16,7 @@ use std::process::Command;
#[ignore]
fn snoop_build_produces_artifacts() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand Down Expand Up @@ -62,13 +58,7 @@ fn snoop_build_produces_artifacts() -> Result<(), Box<dyn std::error::Error>> {
#[ignore]
fn snoop_build_prints_dbg() -> Result<(), Box<dyn std::error::Error>> {
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();
Expand Down
10 changes: 2 additions & 8 deletions packages/perseus-cli/tests/snoop_serve.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -9,13 +9,7 @@ use std::process::Command;
#[ignore]
fn snoop_serve_serves() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand Down
10 changes: 3 additions & 7 deletions packages/perseus-cli/tests/snoop_wasm_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,13 +16,7 @@ use std::process::Command;
#[ignore]
fn snoop_wasm_build_produces_artifacts() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand Down
10 changes: 3 additions & 7 deletions packages/perseus-cli/tests/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,13 +19,7 @@ use std::process::Command;
#[ignore]
fn local_tool_installation_works() -> Result<(), Box<dyn std::error::Error>> {
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")?;
Expand Down

0 comments on commit 27442d5

Please sign in to comment.