forked from awslabs/git-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.bash
94 lines (84 loc) · 1.99 KB
/
test_helper.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
export TEST_REPO="$BATS_TMPDIR/test-repo"
export TEMP_HOME="$BATS_TMPDIR/home"
export TEMPLATE_DIR="${BATS_TMPDIR}/template"
INITIAL_PATH="${PATH}"
INITIAL_HOME=${HOME}
setup() {
setup_repo
[ -d "${TEMPLATE_DIR}" ] && rm -rf "${TEMPLATE_DIR}"
[ -d "${TEMP_HOME}" ] && rm -rf "${TEMP_HOME}"
mkdir -p $TEMP_HOME
export HOME=$TEMP_HOME
export PATH="${BATS_TEST_DIRNAME}/..:${INITIAL_PATH}"
cd $TEST_REPO
}
teardown() {
delete_repo
export PATH="${INITIAL_PATH}"
export HOME="${INITIAL_HOME}"
[ -d "${TEMP_HOME}" ] && rm -rf "${TEMP_HOME}"
}
delete_repo() {
[ -d $TEST_REPO ] && rm -rf $TEST_REPO || true
}
setup_repo() {
delete_repo
mkdir -p $TEST_REPO
cd $TEST_REPO
git init
git config --local --add secrets.patterns '@todo'
git config --local --add secrets.patterns 'forbidden|me'
git config --local --add secrets.patterns '#hash'
git config --local user.email "[email protected]"
git config --local user.name "Your Name"
cd -
}
repo_run() {
cmd="$1"
shift
cd "${TEST_REPO}"
run "${BATS_TEST_DIRNAME}/../${cmd}" $@
cd -
}
# Creates a repo that should fail
setup_bad_repo() {
cd $TEST_REPO
echo '@todo more stuff' > $TEST_REPO/data.txt
echo 'hi there' > $TEST_REPO/ok.txt
echo 'another line... forbidden' > $TEST_REPO/failure1.txt
echo 'me' > $TEST_REPO/failure2.txt
git add -A
cd -
}
# Creates a repo that should fail
setup_bad_repo_with_spaces() {
cd $TEST_REPO
echo '@todo more stuff' > "$TEST_REPO/da ta.txt"
git add -A
cd -
}
# Creates a repo that should fail
setup_bad_repo_with_hash() {
cd $TEST_REPO
echo '#hash' > "$TEST_REPO/data.txt"
git add -A
cd -
}
# Creates a repo that should fail
setup_bad_repo_history() {
cd $TEST_REPO
echo '@todo' > $TEST_REPO/history_failure.txt
git add -A
git commit -m "Testing history"
echo 'todo' > $TEST_REPO/history_failure.txt
git add -A
cd -
}
# Creates a repo that does not fail
setup_good_repo() {
cd $TEST_REPO
echo 'hello!' > $TEST_REPO/data.txt
git add -A
cd -
}