From 3c46d952f5065c4d96bd6ac2d97bb9818c58c687 Mon Sep 17 00:00:00 2001 From: William Arin Date: Thu, 6 Jan 2022 17:56:13 +0800 Subject: [PATCH] test: source files in setup instead of in every test --- test/unit/create_archive.bats | 15 +-------------- test/unit/decrypt_archive.bats | 9 +-------- test/unit/get_archive_name.bats | 9 +-------- test/unit/get_env.bats | 9 +-------- test/unit/get_file_gid.bats | 9 +-------- test/unit/get_file_uid.bats | 9 +-------- 6 files changed, 6 insertions(+), 54 deletions(-) diff --git a/test/unit/create_archive.bats b/test/unit/create_archive.bats index d534ae7..4ad512c 100644 --- a/test/unit/create_archive.bats +++ b/test/unit/create_archive.bats @@ -1,30 +1,25 @@ setup() { DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" PATH="$DIR/../../src:$DIR/../../src/lib:$PATH" + source file.sh mkdir -p /tmp/backup /backup chown "$USER:$GROUP" /tmp/backup /backup } @test "fails if no argument given" { - source file.sh - run create_archive [ "$status" -eq 1 ] [ "$output" = "2 arguments required, 0 provided" ] } @test "fails if 1 argument given" { - source file.sh - run create_archive "my-backup-name.my_db.day1-Monday.tgz" [ "$status" -eq 1 ] [ "$output" = "2 arguments required, 1 provided" ] } @test "creates a tgz archive with a single file" { - source file.sh - echo "MYSQL_DUMP" > /tmp/backup/my_db.sql run create_archive "my-backup-name.my_db.day1-Monday.tgz" "my_db.sql" @@ -41,8 +36,6 @@ setup() { } @test "creates a tgz archive with multiple files" { - source file.sh - echo "MYSQL_DUMP1" > /tmp/backup/my_db1.sql echo "MYSQL_DUMP2" > /tmp/backup/my_db2.sql echo "MYSQL_DUMP3" > /tmp/backup/my_db3.sql @@ -67,8 +60,6 @@ setup() { } @test "creates a tgz archive with AES encryption" { - source file.sh - echo "MYSQL_DUMP" > /tmp/backup/my_db.sql AES_PASSPHRASE="secret-passphrase" @@ -83,8 +74,6 @@ setup() { } @test "creates a tgz archive with another owner and group" { - source file.sh - echo "MYSQL_DUMP" > /tmp/backup/my_db.sql CHOWN_FILES="1000:2500" @@ -107,8 +96,6 @@ setup() { } @test "creates a tgz archive using 1 CPU only" { - source file.sh - echo "MYSQL_DUMP" > /tmp/backup/my_db.sql MAX_CPU=1 diff --git a/test/unit/decrypt_archive.bats b/test/unit/decrypt_archive.bats index d6337a7..dd1e2e0 100644 --- a/test/unit/decrypt_archive.bats +++ b/test/unit/decrypt_archive.bats @@ -1,30 +1,25 @@ setup() { DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" PATH="$DIR/../../src:$DIR/../../src/lib:$PATH" + source file.sh mkdir -p /tmp/backup /backup chown "$USER:$GROUP" /tmp/backup /backup } @test "fails if no argument given" { - source file.sh - run decrypt_archive [ "$status" -eq 1 ] [ "$output" = "1 argument required, 0 provided" ] } @test "fails if no passphrase given" { - source file.sh - run decrypt_archive "my-backup-name.my_db.day1-Monday.tgz" [ "$status" -eq 1 ] [ "$output" = "You must provide a passphrase to decrypt the file" ] } @test "decrypts an archive" { - source file.sh - echo "MYSQL_DUMP" > /tmp/backup/my_db.sql AES_PASSPHRASE="secret-passphrase" @@ -44,8 +39,6 @@ setup() { } @test "decrypts an archive and set permissions to another user and group" { - source file.sh - echo "MYSQL_DUMP" > /tmp/backup/my_db.sql CHOWN_FILES="1000:1000" diff --git a/test/unit/get_archive_name.bats b/test/unit/get_archive_name.bats index 2e35593..ea144a4 100644 --- a/test/unit/get_archive_name.bats +++ b/test/unit/get_archive_name.bats @@ -1,19 +1,16 @@ setup() { DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" PATH="$DIR/../../src:$DIR/../../src/lib:$PATH" + source file.sh } @test "fails if no argument given" { - source file.sh - run get_archive_name [ "$status" -eq 1 ] [ "$output" = "At least 1 argument required, 0 provided" ] } @test "Wednesday creates a daily backup" { - source file.sh - shopt -s expand_aliases alias date="FAKETIME='2022-01-05 06:35:46' date" @@ -24,8 +21,6 @@ setup() { } @test "Saturday creates a weekly backup" { - source file.sh - shopt -s expand_aliases alias date="FAKETIME='2022-02-19 12:54:11' date" @@ -36,8 +31,6 @@ setup() { } @test "last day of the month creates a monthly backup" { - source file.sh - shopt -s expand_aliases alias date="FAKETIME='2022-02-28 18:43:24' date" diff --git a/test/unit/get_env.bats b/test/unit/get_env.bats index 5d27453..f18ad8d 100644 --- a/test/unit/get_env.bats +++ b/test/unit/get_env.bats @@ -1,19 +1,16 @@ setup() { DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" PATH="$DIR/../../src:$DIR/../../src/lib:$PATH" + source env.sh } @test "fails if no argument given" { - source env.sh - run get_env [ "$status" -eq 1 ] [ "$output" = "1 argument required, 0 provided" ] } @test "outputs env_FILE value" { - source env.sh - echo "super-secret-passphrase" > /tmp/bats-secret TEST_VAR_FILE="/tmp/bats-secret" @@ -23,8 +20,6 @@ setup() { } @test "outputs env value" { - source env.sh - TEST_VAR="secret-passphrase" run get_env "TEST_VAR" @@ -33,8 +28,6 @@ setup() { } @test "outputs env fallback" { - source env.sh - run get_env "TEST_VAR" "fallback-passphrase" [ "$status" -eq 0 ] [ "$output" = "fallback-passphrase" ] diff --git a/test/unit/get_file_gid.bats b/test/unit/get_file_gid.bats index fe19718..e4f2bf8 100644 --- a/test/unit/get_file_gid.bats +++ b/test/unit/get_file_gid.bats @@ -1,19 +1,16 @@ setup() { DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" PATH="$DIR/../../src:$DIR/../../src/lib:$PATH" + source file.sh } @test "fails if no argument given" { - source file.sh - run get_file_gid [ "$status" -eq 1 ] [ "$output" = "1 argument required, 0 provided" ] } @test "fails if file does not exist" { - source file.sh - file="/not_a_file" run get_file_gid "$file" @@ -22,8 +19,6 @@ setup() { } @test "outputs file's known user gid" { - source file.sh - file="/tmp/test_file" touch "$file" @@ -35,8 +30,6 @@ setup() { } @test "outputs file's unknown user gid" { - source file.sh - file="/tmp/test_file" touch "$file" chown "1054:2344" "$file" diff --git a/test/unit/get_file_uid.bats b/test/unit/get_file_uid.bats index 838d442..2d9e9dd 100644 --- a/test/unit/get_file_uid.bats +++ b/test/unit/get_file_uid.bats @@ -1,19 +1,16 @@ setup() { DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" PATH="$DIR/../../src:$DIR/../../src/lib:$PATH" + source file.sh } @test "fails if no argument given" { - source file.sh - run get_file_uid [ "$status" -eq 1 ] [ "$output" = "1 argument required, 0 provided" ] } @test "fails if file does not exist" { - source file.sh - file="/not_a_file" run get_file_uid "$file" @@ -22,8 +19,6 @@ setup() { } @test "outputs file's known user uid" { - source file.sh - file="/tmp/test_file" touch "$file" @@ -35,8 +30,6 @@ setup() { } @test "outputs file's unknown user uid" { - source file.sh - file="/tmp/test_file" touch "$file" chown "1054:2344" "$file"