Skip to content

Commit

Permalink
overwork integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed May 10, 2023
1 parent 18f09bd commit 13bea6f
Showing 1 changed file with 51 additions and 89 deletions.
140 changes: 51 additions & 89 deletions tests/backup_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,32 @@
//! You can run them with 'nextest':
//! `cargo nextest run -E 'test(backup)'`.

use abscissa_core::{fs::create_dir_all, testing::prelude::*};

use once_cell::sync::Lazy;
use abscissa_core::testing::prelude::*;

use aho_corasick::{AhoCorasick, PatternID};
use pretty_assertions::assert_eq;
use std::{
env::{remove_var, set_var},
error::Error,
io::Read,
path::PathBuf,
};
use std::{error::Error, io::Read};
use tempfile::{tempdir, TempDir};

type Result<T> = std::result::Result<T, Box<dyn Error>>;

/// Storing this value as a [`Lazy`] static ensures that all instances of
/// the runner acquire a mutex when executing commands and inspecting
/// exit statuses, serializing what would otherwise be multithreaded
/// invocations as `cargo test` executes tests in parallel by default.
pub static RUNNER: Lazy<CmdRunner> = Lazy::new(|| {
fn cmd_runner(temp_dir: &TempDir) -> CmdRunner {
let password = "test";
let repo_dir = temp_dir.path().join("repo");
let mut runner = CmdRunner::new(env!("CARGO_BIN_EXE_rustic"));
runner.exclusive().capture_stdout();
runner
});

fn cmd_runner() -> CmdRunner {
RUNNER.clone()
.arg("-r")
.arg(repo_dir)
.arg("--password")
.arg(password)
.arg("--no-progress")
.capture_stdout();
runner
}

fn setup(repo_dir: TempDir, repo: impl Into<PathBuf>) -> Result<()> {
let password = "test";
let repo = repo.into();

// # Be extra careful to not mess with a user repository
remove_var("RUSTIC_PASSWORD_FILE");
remove_var("RUSTIC_PASSWORD_COMMAND");

set_var("REPO_DIR", repo_dir.path());
set_var("RUSTIC_REPOSITORY", &repo);
set_var("RUSTIC_PASSWORD", password);

create_dir_all(repo_dir)?;

let mut runner = cmd_runner();
fn setup() -> Result<TempDir> {
let temp_dir = tempdir()?;
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.args(["init"]).run();

let mut output = String::new();
Expand All @@ -71,28 +52,18 @@ fn setup(repo_dir: TempDir, repo: impl Into<PathBuf>) -> Result<()> {
);

cmd.wait()?.expect_success();
Ok(())
Ok(temp_dir)
}

#[test]
fn test_backup_and_check_passes() -> Result<()> {
let repo_dir = tempdir()?;
let repo = repo_dir.path().join("repo");
let backup_file_name = "README.md";

setup(repo_dir, repo)?;

// actual repository root to backup
let current_dir = std::env::current_dir()?;
let backup_file = current_dir.join(backup_file_name);
let temp_dir = setup()?;
let backup = "crates/";

{
// Run `backup` for the first time
let mut runner = cmd_runner();
let mut cmd = runner
.arg("backup")
.arg(backup_file.display().to_string())
.run();
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.arg("backup").arg(backup).run();

let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
Expand All @@ -111,7 +82,7 @@ fn test_backup_and_check_passes() -> Result<()> {

{
// Run `snapshots`
let mut runner = cmd_runner();
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.arg("snapshots").run();
let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
Expand All @@ -131,11 +102,8 @@ fn test_backup_and_check_passes() -> Result<()> {

{
// Run `backup` a second time
let mut runner = cmd_runner();
let mut cmd = runner
.arg("backup")
.arg(backup_file.display().to_string())
.run();
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.arg("backup").arg(backup).run();

let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
Expand All @@ -158,7 +126,7 @@ fn test_backup_and_check_passes() -> Result<()> {

{
// Run `snapshots` a second time
let mut runner = cmd_runner();
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.arg("snapshots").run();
let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
Expand All @@ -178,7 +146,7 @@ fn test_backup_and_check_passes() -> Result<()> {

{
// Run `check --read-data`
let mut runner = cmd_runner();
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.args(["check", "--read-data"]).run();
let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
Expand All @@ -201,24 +169,14 @@ fn test_backup_and_check_passes() -> Result<()> {

#[test]
fn test_backup_and_restore_passes() -> Result<()> {
let repo_dir = tempdir()?;
let repo = repo_dir.path().join("repo");
let restore_dir = repo_dir.path().join("restore");
let backup_file_name = "README.md";

setup(repo_dir, repo)?;

// actual repository root to backup
let current_dir = std::env::current_dir()?;
let backup_file = current_dir.join(backup_file_name);
let temp_dir = setup()?;
let restore_dir = temp_dir.path().join("restore");
let backup = "crates/";

{
// Run `backup` for the first time
let mut runner = cmd_runner();
let mut cmd = runner
.arg("backup")
.arg(backup_file.display().to_string())
.run();
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.arg("backup").arg(backup).run();

let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
Expand All @@ -236,12 +194,8 @@ fn test_backup_and_restore_passes() -> Result<()> {
}
{
// Run `restore`
let mut runner = cmd_runner();
let mut cmd = runner
.arg("restore")
.arg(format!("latest:{}", current_dir.display()))
.arg(&restore_dir)
.run();
let mut runner = cmd_runner(&temp_dir);
let mut cmd = runner.arg("restore").arg("latest").arg(&restore_dir).run();

let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
Expand All @@ -261,23 +215,31 @@ fn test_backup_and_restore_passes() -> Result<()> {
// diff the directories
#[cfg(not(windows))]
{
let proc = std::process::Command::new("diff")
.arg(backup_file)
.arg(restore_dir.join(backup_file_name))
.output()?;

assert!(proc.stdout.is_empty());
let mut runner = CmdRunner::new("diff");
runner
.arg("-r")
.arg(backup)
.arg(restore_dir.join(backup))
.capture_stdout();
let mut cmd = runner.run();

let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;
assert_eq!(output, "");
cmd.wait()?.expect_success();
}

#[cfg(windows)]
{
let proc = std::process::Command::new("fc.exe")
let mut runner = CmdRunner::new("diff");
runner
.arg("/L")
.arg(backup_file)
.arg(backup)
.arg(restore_dir.join(backup_file_name))
.output()?;

let output = String::from_utf8(proc.stdout)?;
.capture_stdout();
let mut cmd = runner.run();
let mut output = String::new();
cmd.stdout().read_to_string(&mut output)?;

let patterns = &["FC: no differences encountered"];
let ac = AhoCorasick::new(patterns)?;
Expand Down

0 comments on commit 13bea6f

Please sign in to comment.