Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Nov 30, 2024
1 parent bc3b5ca commit 660534f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
65 changes: 65 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 29 additions & 9 deletions tests/backup_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
//! You can run them with 'nextest':
//! `cargo nextest run -E 'test(backup)'`.
use std::path::PathBuf;

use dircmp::Comparison;
use tempfile::{tempdir, TempDir};

use assert_cmd::Command;
use predicates::prelude::{predicate, PredicateBooleanExt};

use rustic_testing::TestResult;
mod repositories;
use repositories::src_snapshot;

use rustic_testing::{files_differ, TestResult};

pub fn rustic_runner(temp_dir: &TempDir) -> TestResult<Command> {
let password = "test";
Expand Down Expand Up @@ -46,13 +51,13 @@ fn setup() -> TestResult<TempDir> {
#[test]
fn test_backup_and_check_passes() -> TestResult<()> {
let temp_dir = setup()?;
let backup = "src/";
let backup = src_snapshot()?.into_path().into_path();

{
// Run `backup` for the first time
rustic_runner(&temp_dir)?
.arg("backup")
.arg(backup)
.arg(&backup)
.assert()
.success()
.stdout(predicate::str::contains("successfully saved."));
Expand Down Expand Up @@ -100,20 +105,23 @@ fn test_backup_and_check_passes() -> TestResult<()> {
Ok(())
}

fn fixture_test_tar() -> PathBuf {
["tests", "dump-fixtures", "test.tar"].iter().collect()
}

#[test]
fn test_backup_and_restore_passes() -> TestResult<()> {
let temp_dir = setup()?;
let restore_dir = temp_dir.path().join("restore");
let backup = "src/";

// actual repository root to backup
let backup_files = std::env::current_dir()?.join(backup);
let backup_files = src_snapshot()?.into_path().into_path();

{
// Run `backup` for the first time
rustic_runner(&temp_dir)?
.arg("backup")
.arg(&backup_files)
.arg("--as-path")
.arg("/")
.assert()
.success()
.stdout(predicate::str::contains("successfully saved."));
Expand All @@ -130,11 +138,23 @@ fn test_backup_and_restore_passes() -> TestResult<()> {
}

// Compare the backup and the restored directory
let compare_result =
Comparison::default().compare(&backup_files, &restore_dir.join(&backup_files))?;
let compare_result = Comparison::default().compare(&backup_files, &restore_dir)?;

// no differences
assert!(compare_result.is_empty());

let dump_tar_file = restore_dir.join("test.tar");
{
// Run `dump`
rustic_runner(&temp_dir)?
.arg("dump")
.arg("latest")
.arg("--file")
.arg(&dump_tar_file)
.assert()
.success();
}
assert!(!files_differ(fixture_test_tar(), dump_tar_file)?);

Ok(())
}
Binary file added tests/dump-fixtures/test.tar
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tar::Archive;
use tempfile::{tempdir, TempDir};

#[derive(Debug)]
struct TestSource(TempDir);
pub struct TestSource(TempDir);

impl TestSource {
pub fn new(tmp: TempDir) -> Self {
Expand Down Expand Up @@ -57,7 +57,7 @@ fn rustic_copy_repo() -> Result<TestSource> {
}

#[fixture]
fn src_snapshot() -> Result<TestSource> {
pub fn src_snapshot() -> Result<TestSource> {
let dir = tempdir()?;
let path = "tests/repository-fixtures/src-snapshot.tar.gz";
open_and_unpack(path, &dir)?;
Expand Down

0 comments on commit 660534f

Please sign in to comment.