Skip to content

Commit

Permalink
Consolidate test helper functions to simplify scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurty authored and elasticdog committed Apr 17, 2020
1 parent 12b1b65 commit f3a9914
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 84 deletions.
28 changes: 28 additions & 0 deletions tests/_test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,31 @@ function cleanup_all {
rm $BATS_TEST_DIRNAME/.gitattributes
rm $BATS_TEST_DIRNAME/sensitive_file
}

function init_transcrypt {
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
}

function encrypt_named_file {
filename=$1
content=$2
if [ "$content" ]; then
echo "$content" > $filename
fi
echo "$filename filter=crypt diff=crypt" >> .gitattributes
git add .gitattributes $filename
git commit -m "Encrypt file $filename"
}

function setup {
pushd $BATS_TEST_DIRNAME
init_git_repo
if [ ! "$SETUP_SKIP_INIT_TRANSCRYPT" ]; then
init_transcrypt
fi
}

function teardown {
cleanup_all
popd
}
57 changes: 17 additions & 40 deletions tests/test_crypt.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,61 @@ load $BATS_TEST_DIRNAME/_test_helper.bash
SECRET_CONTENT="My secret content"
SECRET_CONTENT_ENC="U2FsdGVkX1/kkWK36bn3fbq5DY2d+JXL2YWoN/eoXA1XJZEk9JS7j/856rXK9gPn"

function init_transcrypt {
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
}

function encrypt_file {
echo $SECRET_CONTENT > sensitive_file
echo 'sensitive_file filter=crypt diff=crypt' >> .gitattributes
git add .gitattributes sensitive_file
git commit -m 'Add encrypted version of a sensitive file'
}

function check_repo_is_clean {
git diff-index --quiet HEAD --
}


function setup {
pushd $BATS_TEST_DIRNAME
init_git_repo
init_transcrypt
}

function teardown {
cleanup_all
popd
}

@test "git ls-crypt command is available" {
@test "crypt: git ls-crypt command is available" {
# No encrypted file yet, so command should work with no output
run git ls-crypt
[ "$status" -eq 0 ]
[ "${lines[0]}" = "" ]
}

@test "encrypt a file" {
encrypt_file
@test "crypt: encrypt a file" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"
}

@test "encrypted file contents are decrypted in working copy" {
encrypt_file
@test "crypt: encrypted file contents are decrypted in working copy" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"
run cat sensitive_file
[ "$status" -eq 0 ]
[ "${lines[0]}" = "$SECRET_CONTENT" ]
}

@test "encrypted file contents are encrypted in git (via git show)" {
encrypt_file
@test "crypt: encrypted file contents are encrypted in git (via git show)" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"
run git show HEAD:sensitive_file --no-textconv
[ "$status" -eq 0 ]
[ "${lines[0]}" = "$SECRET_CONTENT_ENC" ]
}

@test "transcrypt --show-raw shows encrypted content" {
encrypt_file
@test "crypt: transcrypt --show-raw shows encrypted content" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"
run ../transcrypt --show-raw sensitive_file
[ "$status" -eq 0 ]
[ "${lines[0]}" = "==> sensitive_file <==" ]
[ "${lines[1]}" = "$SECRET_CONTENT_ENC" ]
}

@test "git ls-crypt lists encrypted file" {
encrypt_file
@test "crypt: git ls-crypt lists encrypted file" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"

run git ls-crypt
[ "$status" -eq 0 ]
[ "${lines[0]}" = "sensitive_file" ]
}

@test "transcrypt --list lists encrypted file" {
encrypt_file
@test "crypt: transcrypt --list lists encrypted file" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"

run ../transcrypt --list
[ "$status" -eq 0 ]
[ "${lines[0]}" = "sensitive_file" ]
}

@test "transcrypt --uninstall leaves decrypted file and repo dirty" {
encrypt_file
@test "crypt: transcrypt --uninstall leaves decrypted file and repo dirty" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"

run ../transcrypt --uninstall --yes
[ "$status" -eq 0 ]
Expand All @@ -98,8 +75,8 @@ function teardown {
[ "$status" -ne 0 ]
}

@test "git reset after uninstall leaves encrypted file" {
encrypt_file
@test "crypt: git reset after uninstall leaves encrypted file" {
encrypt_named_file sensitive_file "$SECRET_CONTENT"

../transcrypt --uninstall --yes

Expand Down
14 changes: 2 additions & 12 deletions tests/test_init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@

load $BATS_TEST_DIRNAME/_test_helper.bash

function init_transcrypt {
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
}

function setup {
pushd $BATS_TEST_DIRNAME
init_git_repo
}
# Custom setup: don't init transcrypt
SETUP_SKIP_INIT_TRANSCRYPT=1

function teardown {
cleanup_all
popd
}

@test "init: works at all" {
# Use literal command not function to confirm command works at least once
Expand Down
14 changes: 4 additions & 10 deletions tests/test_not_inited.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

load $BATS_TEST_DIRNAME/_test_helper.bash

function setup {
pushd $BATS_TEST_DIRNAME
# Need to init and tear down Git repo for these tests, mainly to avoid falling
# back to the transcrypt repo's Git config and partial transcrypt setup
init_git_repo
}
# Custom setup: don't init transcrypt
# We need to init and tear down Git repo for these tests, mainly to avoid
# falling back to the transcrypt repo's Git config and partial transcrypt setup
SETUP_SKIP_INIT_TRANSCRYPT=1

function teardown {
nuke_git_repo
popd
}

# Operations that should work in a repo not yet initialised

Expand Down
22 changes: 0 additions & 22 deletions tests/test_pre_commit.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,6 @@

load $BATS_TEST_DIRNAME/_test_helper.bash

function init_transcrypt {
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
}

function encrypt_named_file {
filename=$1
echo "$filename filter=crypt diff=crypt merge=crypt" >> .gitattributes
git add .gitattributes $filename
git commit -m "Encrypt file $filename"
}

function setup {
pushd $BATS_TEST_DIRNAME
init_git_repo
init_transcrypt
}

function teardown {
cleanup_all
popd
}

@test "pre-commit: pre-commit hook installed on init" {
# Confirm pre-commit-crypt file is installed
[ -f .git/hooks/pre-commit-crypt ]
Expand Down

0 comments on commit f3a9914

Please sign in to comment.