From 10ff0bd58c01ed4c8e09c02bd0f0c6af05911334 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Thu, 22 Sep 2022 15:44:39 +0200 Subject: [PATCH] Add bats tests --- .github/workflows/tests.yaml | 34 +++++++++--------- test-bats/run-tests.sh | 14 ++++++++ test-bats/simple.bats | 68 ++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 17 deletions(-) create mode 100755 test-bats/run-tests.sh create mode 100644 test-bats/simple.bats diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a57bd32..2052abc 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -7,9 +7,15 @@ on: name: Tests jobs: - test_linux: - name: Test on Linux - runs-on: ubuntu-latest + test: + name: Test + runs-on: ${{ matrix.job.os }} + strategy: + matrix: + rust: [stable] + job: + - os: macos-latest + - os: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 @@ -21,19 +27,13 @@ jobs: - uses: actions-rs/cargo@v1 with: command: test - - test_macos: - name: Test on MacOS - runs-on: macos-latest - steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 + - name: Cargo build + uses: actions-rs/cargo@v1 with: - profile: minimal + command: build toolchain: stable - override: true - target: x86_64-apple-darwin - - uses: Swatinem/rust-cache@v2 - - uses: actions-rs/cargo@v1 - with: - command: test + args: --release + - name: Running bats tests + shell: bash + run: | + ./test-bats/run-tests.sh diff --git a/test-bats/run-tests.sh b/test-bats/run-tests.sh new file mode 100755 index 0000000..9edb1bb --- /dev/null +++ b/test-bats/run-tests.sh @@ -0,0 +1,14 @@ +#!/bin/bash +dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +# install bats +git clone https://github.com/bats-core/bats-core.git $dir/bats +git clone https://github.com/bats-core/bats-support.git $dir/bats-support +git clone https://github.com/bats-core/bats-assert.git $dir/bats-assert +PATH=$PATH:$dir/bats/bin +# run tests +bats $dir +# clean up +rm -rf $dir/bats +rm -rf $dir/bats-support +rm -rf $dir/bats-assert + diff --git a/test-bats/simple.bats b/test-bats/simple.bats new file mode 100644 index 0000000..6ed6e9a --- /dev/null +++ b/test-bats/simple.bats @@ -0,0 +1,68 @@ +# Be extra careful to not mess with a user repository +unset RUSTIC_REPOSITORY +unset RUSTIC_PASSWORD +unset RUSTIC_PASSWORD_FILE +unset RUSTIC_PASSWORD_COMMAND + +export REPO_DIR="${BATS_TMPDIR%/}/repo" +export RESTORE_DIR="${BATS_TMPDIR%/}/restore" +export RUSTIC_REPOSITORY=$REPO_DIR +export RUSTIC_PASSWORD=test + +setup() { + # get the containing directory of this file + # use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0, + # as those will point to the bats executable's location or the preprocessed file respectively + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + BASEDIR="$( cd $DIR/.. >/dev/null 2>&1 && pwd )" + load "$DIR/bats-support/load" + load "$DIR/bats-assert/load" + # make executables in src/ visible to PATH + RUSTIC="$DIR/../target/release/rustic" + echo $RUSTIC + rm -rf "$REPO_DIR" + run $RUSTIC init + assert_success + assert_output -p "successfully created" +} + +teardown () { + chmod -R 700 "$REPO_DIR" + rm -rf "$REPO_DIR" +} + +@test "backup and check" { + run $RUSTIC backup $BASEDIR/src + assert_success + assert_output -p "successfully saved" + + run $RUSTIC snapshots + assert_success + assert_output -p "1 snapshot(s)" + + run $RUSTIC backup $BASEDIR/src + assert_success + assert_output -p "Added to the repo: 0 B" + assert_output -p "successfully saved" + + run $RUSTIC snapshots + assert_success + assert_output -p "2 snapshot(s)" + + run $RUSTIC check + assert_success +} + +@test "backup and restore" { + run $RUSTIC backup $BASEDIR/src + assert_success + assert_output -p "successfully saved" + + run $RUSTIC restore latest:$BASEDIR/src $RESTORE_DIR + assert_success + assert_output -p "restore done" + + run diff -r $BASEDIR/src $RESTORE_DIR + assert_output "" +} +