Skip to content

Commit a97da63

Browse files
authored
Tests: Run detect/compile/release in a clean environment (#1016)
To prevent external environment variables from leaking into the tests, which otherwise causes problems trying to write tests for #1011. Several tests which were relying on this leak had to be fixed, so that the env vars they were using are set using `ENV_DIR`, as happens in production. Fixes #1014. Fixes #1015.
1 parent d9b1c73 commit a97da63

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

test/run-deps

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
source "bin/default_pythons"
66

77
testAirflow() {
8-
export SLUGIFY_USES_TEXT_UNIDECODE="yes"
9-
compile "airflow"
8+
local env_dir="$(mktmpdir)"
9+
echo 'yes' > "${env_dir}/SLUGIFY_USES_TEXT_UNIDECODE"
10+
compile 'airflow' '' "${env_dir}"
1011
assertCaptured "apache-airflow==1.10.2"
1112
assertCapturedSuccess
1213
}
@@ -17,27 +18,30 @@ testCollectstatic() {
1718
}
1819

1920
testGEOS() {
20-
export BUILD_WITH_GEO_LIBRARIES=1
21-
compile "geos"
21+
local env_dir="$(mktmpdir)"
22+
echo '1' > "${env_dir}/BUILD_WITH_GEO_LIBRARIES"
23+
compile 'geos' '' "${env_dir}"
2224
assertCaptured "geos"
2325
assertCapturedSuccess
2426
}
2527

2628
testGEOSDeprecation() {
27-
export BUILD_WITH_GEO_LIBRARIES=1
28-
compile "geos"
29+
local env_dir="$(mktmpdir)"
30+
echo '1' > "${env_dir}/BUILD_WITH_GEO_LIBRARIES"
31+
compile 'geos' '' "${env_dir}"
2932
assertCaptured " ! The GDAL, GEOS and PROJ binaries and BUILD_WITH_GEO_LIBRARIES functonality are now deprecated.
3033
! An alternative buildpack to enable GDAL, GEOS and PROJ use is available here - https://github.com/heroku/heroku-geo-buildpack"
3134
assertCapturedSuccess
3235
}
3336

3437
testNLTK() {
38+
local env_dir="$(mktmpdir)"
3539
# NOTE: This is a RuntimeWarning emitted by Python 3's runpy.py script
3640
# which is what is used when you call `python -m <module>`. This is due to
3741
# how nltk imports things. It's not actually an error, but it would probably
3842
# be bad to silence in Production.
39-
export PYTHONWARNINGS="ignore::RuntimeWarning"
40-
compile "nltk"
43+
echo 'ignore::RuntimeWarning' > "${env_dir}/PYTHONWARNINGS"
44+
compile 'nltk' '' "${env_dir}"
4145
assertCaptured "[nltk_data] Downloading package city_database" "STD_ERR"
4246
assertCapturedSuccess
4347
}

test/utils

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,18 @@ default_process_types_cleanup() {
234234
fi
235235
}
236236

237+
run_in_clean_env() {
238+
# Prevent stray environment variables from outside the test runner being exposed to tests.
239+
env -i HOME="${HOME}" LANG="${LANG}" PATH="${PATH}" STACK="${STACK}" "$@"
240+
}
241+
237242
compile() {
238243
default_process_types_cleanup
239244
bp_dir=$(mktmpdir)
240245
compile_dir=$(mktmpdir)
241246
cp -a $(pwd)/* ${bp_dir}
242247
cp -a ${bp_dir}/test/fixtures/$1/. ${compile_dir}
243-
capture ${bp_dir}/bin/compile ${compile_dir} ${2:-$(mktmpdir)} $3
248+
capture run_in_clean_env ${bp_dir}/bin/compile ${compile_dir} ${2:-$(mktmpdir)} $3
244249
}
245250

246251
compileDir() {
@@ -252,13 +257,13 @@ compileDir() {
252257
local env_dir=$3
253258

254259
cp -a $(pwd)/* ${bp_dir}
255-
capture ${bp_dir}/bin/compile ${compile_dir} ${cache_dir} ${env_dir}
260+
capture run_in_clean_env ${bp_dir}/bin/compile ${compile_dir} ${cache_dir} ${env_dir}
256261
}
257262

258263
release() {
259264
bp_dir=$(mktmpdir)
260265
cp -a $(pwd)/* ${bp_dir}
261-
capture ${bp_dir}/bin/release ${bp_dir}/test/fixtures/$1
266+
capture run_in_clean_env ${bp_dir}/bin/release ${bp_dir}/test/fixtures/$1
262267
}
263268

264269
assertFile() {

0 commit comments

Comments
 (0)