Skip to content

Commit 3230b50

Browse files
authored
Merge pull request #10 from RickvdStaaij/patch-1
Follow symbolic links when searching for files with environment variable to replace.
2 parents a5a252b + 0bdcaad commit 3230b50

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

files/bin/instateEnvironment.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ if [ -z "$1" ]; then
55
exit 1;
66
fi
77

8+
if [ ! -f "$1" ]; then
9+
(>&2 echo "$1 No such file or directory")
10+
exit 2;
11+
fi
12+
813
grep -o "{container.env.[a-zA-Z0-9_]\+}" $1 | while read -r GREPRESULT ; do
914
# Remove the {container.env.} from the grep result
1015
ENVNAME=$(echo $GREPRESULT | cut -c16- | rev | cut -c2- | rev)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/with-contenv sh
2-
find /var/www -type f -exec /bin/instateEnvironment.sh {} \;
2+
find -L /var/www -type f -exec /bin/instateEnvironment.sh {} \;

test/files/symlink_to_singleline.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
singleline.txt

test/files/symlink_to_symlink

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
symlink_to_singleline.txt

test/symlinks.bats

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#/usr/bin/env bats
2+
3+
load helpers/environment
4+
load helpers/assert_file
5+
load helpers/assertions/all
6+
7+
setup () {
8+
# @todo: fix test for [] since it breaks the grep assertion
9+
10+
__setup_environment
11+
__create_run_directory
12+
export BATS_VARIABLE='nstapelbroek-was-here'
13+
}
14+
15+
@test "Replacing a normal symlink" {
16+
FILE=$BATS_RUN_DIR/files/symlink_to_singleline.txt
17+
run /bin/sh $BATS_BINARY_DIR/instateEnvironment.sh $FILE
18+
assert_success "Replacing {container.env.BATS_VARIABLE} with $BATS_VARIABLE in $FILE"
19+
assert_file_refute_contains $FILE {container.env.BATS_VARIABLE}
20+
assert_file_contains $FILE $BATS_VARIABLE
21+
}
22+
23+
@test "Replacing a symlink to a symlink" {
24+
FILE=$BATS_RUN_DIR/files/symlink_to_symlink
25+
run /bin/sh $BATS_BINARY_DIR/instateEnvironment.sh $FILE
26+
assert_success "Replacing {container.env.BATS_VARIABLE} with $BATS_VARIABLE in $FILE"
27+
assert_file_refute_contains $FILE {container.env.BATS_VARIABLE}
28+
assert_file_contains $FILE $BATS_VARIABLE
29+
}
30+
31+
@test "Replacing a dead_symlink" {
32+
FILE=$BATS_RUN_DIR/files/dead_symlink
33+
ln -s $BATS_RUN_DIR/files/does_not_exists $FILE
34+
run /bin/sh $BATS_BINARY_DIR/instateEnvironment.sh $FILE
35+
assert_failure
36+
rm -rf $FILE
37+
}
38+
39+
@test "Replacing a non existing file" {
40+
FILE=$BATS_RUN_DIR/files/does_not_exist
41+
run /bin/sh $BATS_BINARY_DIR/instateEnvironment.sh $FILE
42+
assert_failure
43+
}

0 commit comments

Comments
 (0)