Skip to content

Commit d195e08

Browse files
committed
All libraries are loaded by init script
This saves individual libs from having to load each other and end user scripts from having to source multiple libs. Related: conjurinc/ops#423
1 parent 08b960f commit d195e08

File tree

16 files changed

+55
-54
lines changed

16 files changed

+55
-54
lines changed

filehandling/lib

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/helpers/lib"
54

65
#https://stackoverflow.com/a/23002317
76
function abs_path() {
87
# generate absolute path from relative path
9-
# $1 : relative filename
8+
# path : relative filename
109
# return : absolute path
11-
if [ -d "$1" ]; then
10+
if [[ -z "${1:-}" ]]; then
11+
path="."
12+
else
13+
path="${1}"
14+
fi
15+
if [ -d "${path}" ]; then
1216
# dir
13-
(spushd "$1"; pwd)
14-
elif [ -f "$1" ]; then
17+
(spushd "${path}"; pwd)
18+
elif [ -f "${path}" ]; then
1519
# file
16-
if [[ $1 = /* ]]; then
17-
echo "$1"
18-
elif [[ $1 == */* ]]; then
19-
echo "$(spushd "${1%/*}"; pwd)/${1##*/}"
20+
if [[ ${path} = /* ]]; then
21+
echo "${path}"
22+
elif [[ ${path} == */* ]]; then
23+
echo "$(spushd "${path%/*}"; pwd)/${path##*/}"
2024
else
21-
echo "$(pwd)/$1"
25+
echo "$(pwd)/${path}"
2226
fi
2327
fi
24-
}
28+
}

git/lib

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/helpers/lib"
54

65
# Get the top level of a git repo
76
function repo_root(){
@@ -76,4 +75,4 @@ space seperated with three fields: subtree_path renmote_url remote_name"
7675
function tracked_files_excluding_subtrees(){
7776
subtrees="$(cat_gittrees | awk '{print $1}' | paste -sd '|' -)"
7877
all_files_in_repo | grep -E -v "${subtrees}"
79-
}
78+
}

helpers/lib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ function spushd(){
1717
#safe popd
1818
function spopd(){
1919
popd >/dev/null || die "popd failed :("
20-
}
20+
}

init

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ BASH_LIB_DIR="${BASH_LIB_DIR_RELATIVE}"
2020

2121
# Load the filehandling module for the abspath
2222
# function
23-
. "${BASH_LIB_DIR_RELATIVE}/filehandling/lib"
23+
for lib in helpers logging filehandling git k8s test-utils; do
24+
. "${BASH_LIB_DIR_RELATIVE}/${lib}/lib"
25+
done
2426

2527
# Export the absolute path
2628
# shellcheck disable=SC2086
2729
BASH_LIB_DIR="$(abs_path ${BASH_LIB_DIR_RELATIVE})"
2830
export BASH_LIB_DIR
2931

30-
. "${BASH_LIB_DIR}/helpers/lib"
31-
3232
# Update Submodules
3333
spushd "${BASH_LIB_DIR}"
3434
git submodule update --init --recursive
3535
spopd
3636

37-
export BATS_CMD="${BASH_LIB_DIR}/test-utils/bats/bin/bats"
37+
export BATS_CMD="${BASH_LIB_DIR}/test-utils/bats/bin/bats"

k8s/lib

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/helpers/lib"
54

65
# Sets additional required environment variables that aren't available in the
76
# secrets.yml file, and performs other preparatory steps
@@ -52,4 +51,4 @@ function run_docker_gke_command() {
5251
/scripts/platform_login
5352
${1}
5453
"
55-
}
54+
}

run-tests

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# is not assumed to have been run
55
# shellcheck disable=SC2086
66
. "$(dirname ${BASH_SOURCE[0]})/init"
7-
. "${BASH_LIB_DIR}/helpers/lib"
87

98
# Run BATS Tests
109
"${BASH_LIB_DIR}/tests-for-this-repo/run-bats-tests"

test-utils/lib

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#!/bin/bash
22

33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
4-
. "${BASH_LIB_DIR}/git/lib"
5-
. "${BASH_LIB_DIR}/helpers/lib"
64

7-
readonly SHELLCHECK_IMAGE="${SHELLCHECK_IMAGE:-koalaman/shellcheck}"
8-
readonly SHELLCHECK_TAG="${SHELLCHECK_TAG:-v0.6.0}"
5+
SHELLCHECK_IMAGE="${SHELLCHECK_IMAGE:-koalaman/shellcheck}"
6+
SHELLCHECK_TAG="${SHELLCHECK_TAG:-v0.6.0}"
97

108
# Check a single shell script for syntax
119
# and common errors.
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
. "${BASH_LIB_DIR}/test-utils/bats-support/load.bash"
22
. "${BASH_LIB_DIR}/test-utils/bats-assert-1/load.bash"
33

4-
. "${BASH_LIB_DIR}/filehandling/lib"
4+
. "${BASH_LIB_DIR}/init"
55

66
@test "abs_path returns absolute path for PWD" {
77
run abs_path .
88
assert_output $PWD
99
assert_success
1010
}
1111

12+
@test "abs_path returns PWD when no arg specified" {
13+
run abs_path
14+
assert_output $PWD
15+
assert_success
16+
}
17+
1218
@test "abs_path returns same path when already absolute" {
1319
run abs_path /tmp
1420
assert_output /tmp
1521
assert_success
16-
}
22+
}

tests-for-this-repo/git.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
. "${BASH_LIB_DIR}/test-utils/bats-support/load.bash"
22
. "${BASH_LIB_DIR}/test-utils/bats-assert-1/load.bash"
33

4-
. "${BASH_LIB_DIR}/git/lib"
4+
. "${BASH_LIB_DIR}/init"
55

66
# run before every test
77
setup(){
@@ -137,4 +137,4 @@ EOF
137137
assert_success
138138
assert_output --partial a_file
139139
assert_success
140-
}
140+
}

tests-for-this-repo/helpers.bats

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
. "${BASH_LIB_DIR}/test-utils/bats-support/load.bash"
22
. "${BASH_LIB_DIR}/test-utils/bats-assert-1/load.bash"
33

4-
. "${BASH_LIB_DIR}/helpers/lib"
4+
. "${BASH_LIB_DIR}/init"
5+
56

67
@test "die exits and prints message" {
7-
run bash -c ". ${BASH_LIB_DIR}/helpers/lib; die msg"
8+
run bash -c ". ${BASH_LIB_DIR}/init; die msg"
89
assert_output msg
910
assert_failure
1011
}
1112

1213
@test "spushd is quiet on stdout" {
1314
run spushd /tmp
14-
refute_output
15+
assert_output ""
1516
assert_success
1617
}
1718

1819
@test "spopd is quiet on stdout" {
1920
pushd .
2021
run spopd
21-
refute_output
22+
assert_output ""
2223
assert_success
2324
}
2425

2526
@test "spushd dies on failure" {
26-
run bash -c ". ${BASH_LIB_DIR}/helpers/lib; spushd /this-doesnt-exist"
27+
run bash -c ". ${BASH_LIB_DIR}/init; spushd /this-doesnt-exist"
2728
assert_output --partial "No such file or directory"
2829
assert_failure
2930
}
3031

3132
@test "spopd dies on failure" {
32-
run bash -c ". ${BASH_LIB_DIR}/helpers/lib; spopd"
33+
run bash -c ". ${BASH_LIB_DIR}/init; spopd"
3334
assert_output --partial "stack empty"
3435
assert_failure
35-
}
36+
}

0 commit comments

Comments
 (0)