Skip to content

Commit

Permalink
test: source files in setup instead of in every test
Browse files Browse the repository at this point in the history
  • Loading branch information
williarin committed Jan 6, 2022
1 parent ca72be2 commit 3c46d95
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 54 deletions.
15 changes: 1 addition & 14 deletions test/unit/create_archive.bats
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down
9 changes: 1 addition & 8 deletions test/unit/decrypt_archive.bats
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
9 changes: 1 addition & 8 deletions test/unit/get_archive_name.bats
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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"

Expand All @@ -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"

Expand Down
9 changes: 1 addition & 8 deletions test/unit/get_env.bats
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -23,8 +20,6 @@ setup() {
}

@test "outputs env value" {
source env.sh

TEST_VAR="secret-passphrase"

run get_env "TEST_VAR"
Expand All @@ -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" ]
Expand Down
9 changes: 1 addition & 8 deletions test/unit/get_file_gid.bats
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -22,8 +19,6 @@ setup() {
}

@test "outputs file's known user gid" {
source file.sh

file="/tmp/test_file"
touch "$file"

Expand All @@ -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"
Expand Down
9 changes: 1 addition & 8 deletions test/unit/get_file_uid.bats
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -22,8 +19,6 @@ setup() {
}

@test "outputs file's known user uid" {
source file.sh

file="/tmp/test_file"
touch "$file"

Expand All @@ -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"
Expand Down

0 comments on commit 3c46d95

Please sign in to comment.