Skip to content

Commit

Permalink
fix(nginx): allow skip envsubst (#946)
Browse files Browse the repository at this point in the history
* Update README.md

* add test

* fix: tests

* fix test

* fix

* fix

* fix

* fix
  • Loading branch information
Julien Bouquillon committed Feb 9, 2022
1 parent 6222644 commit d4e43ea
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions nginx/README.md
Expand Up @@ -18,6 +18,8 @@ add_header X-Content-Type-Options "nosniff";

As default, in every files in the `/usr/share/nginx/html` directory, the [`envsub.sh`](./envsub.sh) script replaces `%%KEY%%` by `VALUE` where `export KEY=VALUE` in the global env var.

You can disable this by setting the `SKIP_ENVSUBST` environment variable.

So :

```sh
Expand Down
2 changes: 1 addition & 1 deletion nginx/envsub.sh
Expand Up @@ -21,7 +21,7 @@ echo "PORT=${PORT:="80"}" >> /tmp/env-vars
while IFS='=' read -r KEY VALUE
do
# In every files in the dir, replace the environment variables value
find ${WWW_DIRECTORY} -type f -regex ".*\.\(conf\|txt\|html\|htm\|js\|css\)" -exec \
[ -z "$SKIP_ENVSUBST" ] && find ${WWW_DIRECTORY} -type f -regex ".*\.\(conf\|txt\|html\|htm\|js\|css\)" -exec \
sed -i -e "s|${DELIMITER}${KEY}${DELIMITER}|${VALUE}|g" {} \;
# replace in nginx.conf too
sed -i -e "s|${DELIMITER}${KEY}${DELIMITER}|${VALUE}|g" /etc/nginx/nginx.conf
Expand Down
6 changes: 3 additions & 3 deletions nginx/tests/404.bats
Expand Up @@ -5,7 +5,7 @@ load '../../.bats/common.bats.bash'
setup_file() {
docker-compose run \
--detach \
--publish 8888:80 \
--publish 8889:80 \
--rm \
--volume ${BATS_TEST_DIRNAME}/fixtures:/usr/share/nginx/html \
alpine
Expand All @@ -16,12 +16,12 @@ teardown_file() {
}

@test "nginx: should return status 404 (not a SPA)" {
run wget --server-response --quiet http://localhost:8888/pouet
run wget --server-response --quiet http://localhost:8889/pouet
assert_output --partial "HTTP/1.1 404 Not Found"
}

@test "nginx: should return custom 404 page (not a SPA)" {
run wget --content-on-error --output-document - http://localhost:8888/pouet
run wget --content-on-error --output-document - http://localhost:8889/pouet
assert_output --partial "CUSTOM 404 PAGE"
}

1 change: 1 addition & 0 deletions nginx/tests/fixtures-envsubst/404.html
@@ -0,0 +1 @@
CUSTOM 404 PAGE
1 change: 1 addition & 0 deletions nginx/tests/fixtures-envsubst/foo/bar/bar.js
@@ -0,0 +1 @@
// nginx/test/foo/bar/bar.js with VERSION=%%VERSION%%
1 change: 1 addition & 0 deletions nginx/tests/fixtures-envsubst/foo/bar/bar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions nginx/tests/fixtures-envsubst/index.html
@@ -0,0 +1 @@
nginx/test/index.html with VERSION=%%VERSION%%
34 changes: 34 additions & 0 deletions nginx/tests/skip-envsub.bats
@@ -0,0 +1,34 @@
#!/usr/bin/env bats

load '../../.bats/common.bats.bash'

setup_file() {
cp -r ${BATS_TEST_DIRNAME}/fixtures-envsubst/ ${BATS_RUN_TMPDIR}/www2

docker-compose run \
--detach \
-e VERSION=x.y.z \
-e SKIP_ENVSUBST=true \
--publish 8888:80 \
--rm \
--volume ${BATS_RUN_TMPDIR}/www2:/usr/share/nginx/html \
alpine
}

teardown_file() {
docker-compose rm -sf
}

@test "nginx: should NOT replace the VERSION with x.y.z in index.html" {
run wget -qO - localhost:8888
assert_output "nginx/test/index.html with VERSION=%%VERSION%%"
assert_success
}

@test "nginx: should NOT replace the VERSION with x.y.z in foo/bar/bar.js" {
run wget -qO - localhost:8888/foo/bar/bar.js
assert_output "// nginx/test/foo/bar/bar.js with VERSION=%%VERSION%%" ]
assert_success
}


2 changes: 2 additions & 0 deletions nginx4spa/README.md
Expand Up @@ -19,6 +19,8 @@ add_header X-Content-Type-Options "nosniff";

As default, in every files in the `/usr/share/nginx/html` directory, the [`envsub.sh`](./envsub.sh) script replaces `%%KEY%%` by `VALUE` where `export KEY=VALUE` in the global env var.

You can disable this by setting the `SKIP_ENVSUBST` environment variable.

So :

```sh
Expand Down
2 changes: 1 addition & 1 deletion nginx4spa/envsub.sh
Expand Up @@ -21,7 +21,7 @@ echo "PORT=${PORT:="80"}" >> /tmp/env-vars
while IFS='=' read -r KEY VALUE
do
# In every files in the dir, replace the environment variables value
find ${WWW_DIRECTORY} -type f -regex ".*\.\(conf\|txt\|html\|htm\|js\|css\)" -exec \
[ -z "$SKIP_ENVSUBST" ] && find ${WWW_DIRECTORY} -type f -regex ".*\.\(conf\|txt\|html\|htm\|js\|css\)" -exec \
sed -i -e "s|${DELIMITER}${KEY}${DELIMITER}|${VALUE}|g" {} \;
# replace in nginx.conf too
sed -i -e "s|${DELIMITER}${KEY}${DELIMITER}|${VALUE}|g" /etc/nginx/nginx.conf
Expand Down
1 change: 1 addition & 0 deletions nginx4spa/tests/fixtures-envsubst/foo/bar/bar.js
@@ -0,0 +1 @@
// nginx4spa/test/foo/bar/bar.js with VERSION=%%VERSION%%
1 change: 1 addition & 0 deletions nginx4spa/tests/fixtures-envsubst/foo/bar/bar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions nginx4spa/tests/fixtures-envsubst/index.html
@@ -0,0 +1 @@
nginx4spa/test/index.html with VERSION=%%VERSION%%
34 changes: 34 additions & 0 deletions nginx4spa/tests/skip-envsub.bats
@@ -0,0 +1,34 @@
#!/usr/bin/env bats

load '../../.bats/common.bats.bash'

setup_file() {
cp -r ${BATS_TEST_DIRNAME}/fixtures-envsubst/ ${BATS_RUN_TMPDIR}/www2

docker-compose run \
--detach \
-e VERSION=x.y.z \
-e SKIP_ENVSUBST=true \
--publish 8889:80 \
--rm \
--volume ${BATS_RUN_TMPDIR}/www2:/usr/share/nginx/html \
alpine
}

teardown_file() {
docker-compose rm -sf
}

@test "nginx4spa: should NOT replace the VERSION with x.y.z in index.html" {
run wget -qO - localhost:8889
assert_output "nginx4spa/test/index.html with VERSION=%%VERSION%%"
assert_success
}

@test "nginx4spa: should NOT replace the VERSION with x.y.z in foo/bar/bar.js" {
run wget -qO - localhost:8889/foo/bar/bar.js
assert_output "// nginx4spa/test/foo/bar/bar.js with VERSION=%%VERSION%%" ]
assert_success
}


0 comments on commit d4e43ea

Please sign in to comment.