Skip to content

Commit

Permalink
Move test fixtures to new herostratus-tests crate
Browse files Browse the repository at this point in the history
This enables sharing them between herostratus proper and the integration
tests, while keeping them out of the public API of the herostratus
library.
Notgnoshi committed Nov 1, 2024
1 parent 3675e71 commit 1c06779
Showing 22 changed files with 101 additions and 174 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
@@ -22,6 +22,7 @@ clap = { version = "4.5.2", features = ["derive"] }
color-eyre = "0.6.2"
ctor = "0.2.7"
directories = "5.0.1"
escargot = "0.5.13"
eyre = "0.6.12"
git2 = "0.19"
inventory = "0.3.15"
6 changes: 4 additions & 2 deletions herostratus-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -6,15 +6,17 @@ license.workspace = true
version.workspace = true
edition.workspace = true

[dev-dependencies]
[dependencies]
herostratus.path = "../herostratus"

assert_cmd.workspace = true
ctor.workspace = true
eyre.workspace = true
git2.workspace = true
lazy_static.workspace = true
predicates.workspace = true
tempfile.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true

[dev-dependencies]
predicates.workspace = true
47 changes: 47 additions & 0 deletions herostratus-tests/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::path::{Path, PathBuf};
use std::process::Output;

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

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)
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod config;
pub mod repository;
pub mod rule;
Original file line number Diff line number Diff line change
@@ -52,11 +52,12 @@ pub fn with_empty_commits(messages: &[&str]) -> eyre::Result<TempRepository> {
Ok(TempRepository { tempdir, repo })
}

#[cfg(test)]
mod tests {
use git2::{Index, Odb, Repository};
use herostratus::git::{rev_parse, rev_walk};

use super::*;
use crate::git::{rev_parse, rev_walk};

#[test]
fn test_in_memory() {
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cmd;
pub mod fixtures;

use tracing::Level;
22 changes: 10 additions & 12 deletions herostratus-tests/tests/add.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod common;

use common::CommandExt;
use herostratus::config::{config_path, read_config, Config, RepositoryConfig};
use herostratus_tests::cmd::{herostratus, CommandExt};

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

let expected_bare_repo = data_dir
@@ -41,7 +39,7 @@ fn required_clone_herostratus() {
assert_eq!(repo_config, &expected);

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

let output = cmd.captured_output().unwrap();
@@ -55,7 +53,7 @@ fn required_clone_herostratus() {
#[test]
#[ignore = "Slow; Performs clone"]
fn required_clone_herostratus_branch() {
let (mut cmd, temp) = common::herostratus(None);
let (mut cmd, temp) = herostratus(None);
let clone_dir = temp
.as_ref()
.unwrap()
@@ -99,7 +97,7 @@ fn required_clone_herostratus_branch() {
#[test]
#[ignore = "Slow; Performs clone; Requires SSH (not available in CI)"]
fn clone_herostratus_ssh() {
let (mut cmd, temp) = common::herostratus(None);
let (mut cmd, temp) = herostratus(None);
let clone_dir = temp
.as_ref()
.unwrap()
@@ -131,9 +129,9 @@ fn clone_herostratus_ssh() {

#[test]
fn add_the_same_repo_twice() {
let (mut cmd1, temp) = common::herostratus(None);
let (mut cmd2, _) = common::herostratus(Some(temp.as_ref().unwrap().path()));
let (mut cmd3, _) = common::herostratus(Some(temp.as_ref().unwrap().path()));
let (mut cmd1, temp) = herostratus(None);
let (mut cmd2, _) = herostratus(Some(temp.as_ref().unwrap().path()));
let (mut cmd3, _) = herostratus(Some(temp.as_ref().unwrap().path()));
let data_dir = temp.as_ref().unwrap().path();
let clone_dir = temp
.as_ref()
@@ -201,8 +199,8 @@ fn add_the_same_repo_twice() {
#[test]
#[ignore = "Slow; Performs clone;"]
fn required_two_branches_share_one_bare_repo() {
let (mut cmd1, temp) = common::herostratus(None);
let (mut cmd2, _) = common::herostratus(Some(temp.as_ref().unwrap().path()));
let (mut cmd1, temp) = herostratus(None);
let (mut cmd2, _) = herostratus(Some(temp.as_ref().unwrap().path()));

let clone_dir = temp
.as_ref()
12 changes: 5 additions & 7 deletions herostratus-tests/tests/check.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
mod common;

use std::path::Path;

use common::CommandExt;
use herostratus::git::clone::find_local_repository;
use herostratus_tests::cmd::{herostratus, CommandExt};
use predicates::prelude::*;
use predicates::str;

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

let output = cmd.captured_output().unwrap();
@@ -18,7 +16,7 @@ fn search_current_repo_for_test_simple_branch() {

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

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

let output = cmd.captured_output().unwrap();
@@ -53,7 +51,7 @@ fn smoke_test_on_all_own_branches() {
let (branch, _local_or_remote) = branch.unwrap();
let name = branch.name().unwrap().unwrap();

let (mut cmd, _temp) = common::herostratus(None);
let (mut cmd, _temp) = herostratus(None);
cmd.arg("check").arg(".").arg(name);

let output = cmd.captured_output().unwrap();
8 changes: 3 additions & 5 deletions herostratus-tests/tests/check_all.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
mod common;

use common::CommandExt;
use herostratus_tests::cmd::{herostratus, CommandExt};
use predicates::prelude::*;
use predicates::str;

#[test]
fn add_self_and_then_check_all() {
let self_dir = format!("file://{}/..", env!("CARGO_MANIFEST_DIR"));
let (mut cmd, temp) = common::herostratus(None);
let (mut cmd, temp) = herostratus(None);
cmd.arg("add").arg("--skip-clone").arg(self_dir);

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

let (mut cmd, _) = common::herostratus(Some(temp.as_ref().unwrap().path()));
let (mut cmd, _) = herostratus(Some(temp.as_ref().unwrap().path()));
// If 'add' skips the clone, using 'fetch-all' or 'check-all' without '--no-fetch' will clone
cmd.arg("check-all");
let output = cmd.captured_output().unwrap();
123 changes: 0 additions & 123 deletions herostratus-tests/tests/common/mod.rs

This file was deleted.

Loading

0 comments on commit 1c06779

Please sign in to comment.