Skip to content

Commit f3a9914

Browse files
jmurtyelasticdog
authored andcommitted
Consolidate test helper functions to simplify scripts
1 parent 12b1b65 commit f3a9914

File tree

5 files changed

+51
-84
lines changed

5 files changed

+51
-84
lines changed

tests/_test_helper.bash

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,31 @@ function cleanup_all {
3232
rm $BATS_TEST_DIRNAME/.gitattributes
3333
rm $BATS_TEST_DIRNAME/sensitive_file
3434
}
35+
36+
function init_transcrypt {
37+
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
38+
}
39+
40+
function encrypt_named_file {
41+
filename=$1
42+
content=$2
43+
if [ "$content" ]; then
44+
echo "$content" > $filename
45+
fi
46+
echo "$filename filter=crypt diff=crypt" >> .gitattributes
47+
git add .gitattributes $filename
48+
git commit -m "Encrypt file $filename"
49+
}
50+
51+
function setup {
52+
pushd $BATS_TEST_DIRNAME
53+
init_git_repo
54+
if [ ! "$SETUP_SKIP_INIT_TRANSCRYPT" ]; then
55+
init_transcrypt
56+
fi
57+
}
58+
59+
function teardown {
60+
cleanup_all
61+
popd
62+
}

tests/test_crypt.bats

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,84 +5,61 @@ load $BATS_TEST_DIRNAME/_test_helper.bash
55
SECRET_CONTENT="My secret content"
66
SECRET_CONTENT_ENC="U2FsdGVkX1/kkWK36bn3fbq5DY2d+JXL2YWoN/eoXA1XJZEk9JS7j/856rXK9gPn"
77

8-
function init_transcrypt {
9-
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
10-
}
11-
12-
function encrypt_file {
13-
echo $SECRET_CONTENT > sensitive_file
14-
echo 'sensitive_file filter=crypt diff=crypt' >> .gitattributes
15-
git add .gitattributes sensitive_file
16-
git commit -m 'Add encrypted version of a sensitive file'
17-
}
18-
198
function check_repo_is_clean {
209
git diff-index --quiet HEAD --
2110
}
2211

23-
24-
function setup {
25-
pushd $BATS_TEST_DIRNAME
26-
init_git_repo
27-
init_transcrypt
28-
}
29-
30-
function teardown {
31-
cleanup_all
32-
popd
33-
}
34-
35-
@test "git ls-crypt command is available" {
12+
@test "crypt: git ls-crypt command is available" {
3613
# No encrypted file yet, so command should work with no output
3714
run git ls-crypt
3815
[ "$status" -eq 0 ]
3916
[ "${lines[0]}" = "" ]
4017
}
4118

42-
@test "encrypt a file" {
43-
encrypt_file
19+
@test "crypt: encrypt a file" {
20+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
4421
}
4522

46-
@test "encrypted file contents are decrypted in working copy" {
47-
encrypt_file
23+
@test "crypt: encrypted file contents are decrypted in working copy" {
24+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
4825
run cat sensitive_file
4926
[ "$status" -eq 0 ]
5027
[ "${lines[0]}" = "$SECRET_CONTENT" ]
5128
}
5229

53-
@test "encrypted file contents are encrypted in git (via git show)" {
54-
encrypt_file
30+
@test "crypt: encrypted file contents are encrypted in git (via git show)" {
31+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
5532
run git show HEAD:sensitive_file --no-textconv
5633
[ "$status" -eq 0 ]
5734
[ "${lines[0]}" = "$SECRET_CONTENT_ENC" ]
5835
}
5936

60-
@test "transcrypt --show-raw shows encrypted content" {
61-
encrypt_file
37+
@test "crypt: transcrypt --show-raw shows encrypted content" {
38+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
6239
run ../transcrypt --show-raw sensitive_file
6340
[ "$status" -eq 0 ]
6441
[ "${lines[0]}" = "==> sensitive_file <==" ]
6542
[ "${lines[1]}" = "$SECRET_CONTENT_ENC" ]
6643
}
6744

68-
@test "git ls-crypt lists encrypted file" {
69-
encrypt_file
45+
@test "crypt: git ls-crypt lists encrypted file" {
46+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
7047

7148
run git ls-crypt
7249
[ "$status" -eq 0 ]
7350
[ "${lines[0]}" = "sensitive_file" ]
7451
}
7552

76-
@test "transcrypt --list lists encrypted file" {
77-
encrypt_file
53+
@test "crypt: transcrypt --list lists encrypted file" {
54+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
7855

7956
run ../transcrypt --list
8057
[ "$status" -eq 0 ]
8158
[ "${lines[0]}" = "sensitive_file" ]
8259
}
8360

84-
@test "transcrypt --uninstall leaves decrypted file and repo dirty" {
85-
encrypt_file
61+
@test "crypt: transcrypt --uninstall leaves decrypted file and repo dirty" {
62+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
8663

8764
run ../transcrypt --uninstall --yes
8865
[ "$status" -eq 0 ]
@@ -98,8 +75,8 @@ function teardown {
9875
[ "$status" -ne 0 ]
9976
}
10077

101-
@test "git reset after uninstall leaves encrypted file" {
102-
encrypt_file
78+
@test "crypt: git reset after uninstall leaves encrypted file" {
79+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
10380

10481
../transcrypt --uninstall --yes
10582

tests/test_init.bats

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,9 @@
22

33
load $BATS_TEST_DIRNAME/_test_helper.bash
44

5-
function init_transcrypt {
6-
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
7-
}
8-
9-
function setup {
10-
pushd $BATS_TEST_DIRNAME
11-
init_git_repo
12-
}
5+
# Custom setup: don't init transcrypt
6+
SETUP_SKIP_INIT_TRANSCRYPT=1
137

14-
function teardown {
15-
cleanup_all
16-
popd
17-
}
188

199
@test "init: works at all" {
2010
# Use literal command not function to confirm command works at least once

tests/test_not_inited.bats

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22

33
load $BATS_TEST_DIRNAME/_test_helper.bash
44

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

12-
function teardown {
13-
nuke_git_repo
14-
popd
15-
}
1610

1711
# Operations that should work in a repo not yet initialised
1812

tests/test_pre_commit.bats

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,6 @@
22

33
load $BATS_TEST_DIRNAME/_test_helper.bash
44

5-
function init_transcrypt {
6-
$BATS_TEST_DIRNAME/../transcrypt --cipher=aes-256-cbc --password=abc123 --yes
7-
}
8-
9-
function encrypt_named_file {
10-
filename=$1
11-
echo "$filename filter=crypt diff=crypt merge=crypt" >> .gitattributes
12-
git add .gitattributes $filename
13-
git commit -m "Encrypt file $filename"
14-
}
15-
16-
function setup {
17-
pushd $BATS_TEST_DIRNAME
18-
init_git_repo
19-
init_transcrypt
20-
}
21-
22-
function teardown {
23-
cleanup_all
24-
popd
25-
}
26-
275
@test "pre-commit: pre-commit hook installed on init" {
286
# Confirm pre-commit-crypt file is installed
297
[ -f .git/hooks/pre-commit-crypt ]

0 commit comments

Comments
 (0)