Skip to content

Commit

Permalink
refactor test
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Mar 12, 2024
1 parent 72add89 commit e5033cb
Show file tree
Hide file tree
Showing 80 changed files with 89 additions and 141 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
CI: true

on:
pull_request:
paths:
Expand Down Expand Up @@ -86,6 +89,15 @@ jobs:
- uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2
- name: Run Cargo Test
run: cargo +${{ matrix.rust }} test -r --all-targets --all-features --workspace --examples
id: run_tests
env:
INSTA_UPDATE: new
- name: Upload snapshots of failed tests
if: ${{ failure() && steps.run_tests.outcome == 'failure' }}
uses: actions/upload-artifact@v3
with:
name: failed-snapshots-${{ matrix.job.os }}
path: "**/snapshots/*.snap.new"

docs:
name: Build docs
Expand Down
Binary file added crates/core/tests/fixtures/backup-data/0/9/0
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/1
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/10
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/11
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/12
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/13
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/14
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/15
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/16
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/17
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/18
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/19
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/2
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/20
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/21
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/22
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/23
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/24
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/25
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/26
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/27
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/28
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/29
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/3
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/30
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/31
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/32
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/33
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/34
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/35
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/36
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/37
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/38
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/39
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/4
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/40
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/41
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/42
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/43
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/44
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/45
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/46
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/47
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/48
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/49
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/5
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/50
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/51
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/52
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/53
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/54
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/55
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/56
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/57
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/58
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/59
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/6
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/60
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/61
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/62
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/63
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/64
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/65
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/66
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/67
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/68
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/7
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/8
Binary file not shown.
Binary file added crates/core/tests/fixtures/backup-data/0/9/9
Binary file not shown.
Empty file.
1 change: 1 addition & 0 deletions crates/core/tests/fixtures/backup-data/tests/testfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test file.
99 changes: 74 additions & 25 deletions crates/core/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use std::{
env,
fs::File,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};

use anyhow::Result;
use flate2::read::GzDecoder;
use insta::assert_debug_snapshot;
use pretty_assertions::assert_eq;
use rstest::fixture;
use rstest::rstest;
use rustic_core::{
repofile::SnapshotFile, BackupOptions, ConfigOptions, InMemoryBackend, KeyOptions,
NoProgressBars, OpenStatus, PathList, Repository, RepositoryBackends, RepositoryOptions,
};
use std::{
env,
fs::File,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};
// uncomment for logging output
// use simplelog::{Config, SimpleLogger};
use tar::Archive;
use tempfile::{tempdir, TempDir};
use tempfile::tempdir;

fn set_up_repo() -> Result<Repository<NoProgressBars, OpenStatus>> {
let be = InMemoryBackend::new();
Expand All @@ -30,28 +31,35 @@ fn set_up_repo() -> Result<Repository<NoProgressBars, OpenStatus>> {
Ok(repo)
}

struct TestSource(TempDir);
struct TestSource(PathBuf);

impl TestSource {
fn new(tmp: TempDir) -> Self {
Self(tmp)
fn new(tmp: impl Into<PathBuf>) -> Self {
Self(tmp.into())
}

fn paths(&self) -> PathList {
PathList::from_iter(Some(self.0.path().to_path_buf()))
fn path_list(&self) -> PathList {
PathList::from_iter(Some(self.0.clone()))
}
}

fn set_up_testdata(path: impl AsRef<Path>) -> Result<TestSource> {
#[fixture]
fn tar_gz_testdata() -> Result<TestSource> {
let dir = tempdir()?;
let path = Path::new("tests/testdata").join(path);
let path = Path::new("tests/fixtures/backup-data.tar.gz");
let tar_gz = File::open(path)?;
let tar = GzDecoder::new(tar_gz);
let mut archive = Archive::new(tar);
archive.set_preserve_permissions(true);
archive.set_preserve_mtime(true);
archive.unpack(&dir)?;
Ok(TestSource::new(dir))
Ok(TestSource::new(dir.as_ref()))
}

#[fixture]
fn dir_testdata() -> Result<TestSource> {
let path = Path::new("tests/fixtures/backup-data/");
Ok(TestSource::new(path))
}

// Parts of the snapshot summary we want to test against references
Expand Down Expand Up @@ -85,12 +93,53 @@ impl<'a> std::fmt::Debug for TestSummary<'a> {
}
}

#[test]
fn backup() -> Result<()> {
#[rstest]
fn test_backup_with_dir_passes(dir_testdata: Result<TestSource>) -> Result<()> {
// uncomment for logging output
// SimpleLogger::init(log::LevelFilter::Debug, Config::default())?;
let source = dir_testdata?;
let paths = &source.path_list();

let repo = set_up_repo()?.to_indexed_ids()?;
// we use as_path to not depend on the actual tempdir
let opts = BackupOptions::default().as_path(PathBuf::from_str("test")?);

// first backup
let first_backup = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_debug_snapshot!(TestSummary(&first_backup));
assert_eq!(first_backup.parent, None);

// get all snapshots and check them
let all_snapshots = repo.get_all_snapshots()?;
assert_eq!(vec![first_backup.clone()], all_snapshots);
// save list of pack files
let packs1: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();

// re-read index
let repo = repo.to_indexed_ids()?;
// second backup
let snap2 = repo.backup(&opts, paths, SnapshotFile::default())?;
assert_debug_snapshot!(TestSummary(&snap2));
assert_eq!(snap2.parent, Some(first_backup.id));
assert_eq!(first_backup.tree, snap2.tree);

// get all snapshots and check them
let mut all_snapshots = repo.get_all_snapshots()?;
all_snapshots.sort_unstable();
assert_eq!(vec![first_backup, snap2], all_snapshots);

// pack files should be unchanged
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
assert_eq!(packs1, packs2);
Ok(())
}

#[rstest]
fn test_backup_with_tar_gz_passes(tar_gz_testdata: Result<TestSource>) -> Result<()> {
// uncomment for logging output
// SimpleLogger::init(log::LevelFilter::Debug, Config::default())?;
let source = set_up_testdata("backup-data.tar.gz")?;
let paths = &source.paths();
let source = tar_gz_testdata?;
let paths = &source.path_list();

let repo = set_up_repo()?.to_indexed_ids()?;
// we use as_path to not depend on the actual tempdir
Expand Down Expand Up @@ -126,10 +175,10 @@ fn backup() -> Result<()> {
Ok(())
}

#[test]
fn backup_dry_run() -> Result<()> {
let source = &set_up_testdata("backup-data.tar.gz")?;
let paths = &source.paths();
#[rstest]
fn test_backup_dry_run_with_tar_gz_passes(tar_gz_testdata: Result<TestSource>) -> Result<()> {
let source = tar_gz_testdata?;
let paths = &source.path_list();
let repo = set_up_repo()?.to_indexed_ids()?;
// we use as_path to not depend on the actual tempdir
let opts = BackupOptions::default()
Expand Down
29 changes: 0 additions & 29 deletions crates/core/tests/snapshots/integration__backup-2.snap

This file was deleted.

29 changes: 0 additions & 29 deletions crates/core/tests/snapshots/integration__backup.snap

This file was deleted.

29 changes: 0 additions & 29 deletions crates/core/tests/snapshots/integration__backup_dry_run-2.snap

This file was deleted.

29 changes: 0 additions & 29 deletions crates/core/tests/snapshots/integration__backup_dry_run.snap

This file was deleted.

0 comments on commit e5033cb

Please sign in to comment.