Skip to content

Commit

Permalink
Break apart integration tests for "add" and "check" subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Notgnoshi committed Jul 14, 2024
1 parent b46bcf9 commit 7736ec2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 105 deletions.
1 change: 1 addition & 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 @@ -18,5 +18,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
[dev-dependencies]
assert_cmd = { version = "2.0.14", features = ["color"] }
ctor = "0.2.7"
lazy_static = "1.4.0"
predicates = "3.1.0"
tempfile = "3.10.1"
38 changes: 38 additions & 0 deletions tests/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
mod common;

use common::CommandExt;

#[test]
#[ignore = "Slow; performs git clone"]
fn clone_herostratus() {
let (mut cmd, temp) = common::herostratus(None);
let data_dir = temp.as_ref().unwrap().path();

let expected_bare_repo = data_dir
.join("git")
.join("Notgnoshi")
.join("herostratus.git");

let url = "https://github.com/Notgnoshi/herostratus.git";
cmd.arg("add").arg(url);

assert!(!data_dir.join("git").exists());

let output = cmd.captured_output().unwrap();
assert!(output.status.success());
assert!(expected_bare_repo.exists());

// Adding the same URL again in the same data_dir fails ...
let (mut cmd, _temp) = common::herostratus(Some(data_dir));
cmd.arg("add").arg(url);

let output = cmd.captured_output().unwrap();
assert!(!output.status.success());

// ... unless the --force flag is given
let (mut cmd, _temp) = common::herostratus(Some(data_dir));
cmd.arg("add").arg("--force").arg(url);

let output = cmd.captured_output().unwrap();
assert!(output.status.success());
}
41 changes: 41 additions & 0 deletions tests/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
mod common;

use common::CommandExt;
use predicates::prelude::*;
use predicates::str;

#[test]
fn search_current_repo_for_test_simple_branch() {
let (mut cmd, _temp) = common::herostratus(None);
cmd.arg("check").arg(".").arg("origin/test/simple");

let output = cmd.captured_output().unwrap();
assert!(output.status.success());
}

#[test]
fn search_current_repo_for_branch_that_does_not_exist() {
let (mut cmd, _temp) = common::herostratus(None);
cmd.arg("check")
.arg(".")
.arg("origin/test/this-branch-will-never-exist");

let output = cmd.captured_output().unwrap();
assert!(!output.status.success());
}

#[test]
fn search_current_repo_for_fixup_commits() {
let (mut cmd, _temp) = common::herostratus(None);
cmd.arg("check").arg(".").arg("origin/test/fixup");

let output = cmd.captured_output().unwrap();
assert!(output.status.success());

// These are the three fixup! commits in the test/fixup branch
let assertion = str::contains("60b480b554dbd5266eec0f2378f72df5170a6702")
.and(str::contains("a987013884fc7dafbe9eb080d7cbc8625408a85f"))
.and(str::contains("2721748d8fa0b0cc3302b41733d37e30161eabfd"));
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(assertion.eval(&stdout));
}
49 changes: 49 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::path::{Path, PathBuf};
use std::process::Output;

use assert_cmd::Command;
use tempfile::{tempdir, TempDir};

// Cache the path to the binary as suggested by https://github.com/assert-rs/assert_cmd/issues/6 to
// avoid expensive repeated lookups.
lazy_static::lazy_static! {
static ref HEROSTRATUS: PathBuf = assert_cmd::cargo::cargo_bin("herostratus");
}

/// Get a [`Command`] for the herostratus binary and the [`TempDir`] data dir used in the test
pub fn herostratus(data_dir: Option<&Path>) -> (Command, Option<TempDir>) {
let (tempdir, path) = if let Some(data_dir) = data_dir {
(None, data_dir.to_path_buf())
} else {
let temp = tempdir().unwrap();
let data_dir = temp.path().to_path_buf();
(Some(temp), data_dir)
};

let mut cmd = Command::new(&*HEROSTRATUS);
cmd.arg("--log-level=DEBUG").arg("--data-dir").arg(path);

(cmd, tempdir)
}

fn capture_output(output: &Output) {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);

// Test output capture relies on magic in the print! and println! macros
print!("{stdout}");
print!("{stderr}");
}

pub trait CommandExt {
/// Same as [Command::output], except with hooks to print stdout and stderr for failed tests
fn captured_output(&mut self) -> std::io::Result<Output>;
}

impl CommandExt for Command {
fn captured_output(&mut self) -> std::io::Result<Output> {
let output = self.output()?;
capture_output(&output);
Ok(output)
}
}
105 changes: 0 additions & 105 deletions tests/test_simple.rs

This file was deleted.

0 comments on commit 7736ec2

Please sign in to comment.