Skip to content

Commit 0b3abe5

Browse files
Jayman2000adrienverge
authored andcommitted
tests: Move code for deleting env vars to __init__
The motivation behind this change is to make it easier to create a future commit. That future commit will make yamllint change its behavior if an environment variable named YAMLLINT_FILE_ENCODING is found. That new environment variable will potentially cause interference with many different tests. Before this change, environment variables would only be deleted when the tests.test_cli module was used. At the moment, it’s OK to do that because that’s the only test module that will fail if certain environment variables are set. Once yamllint is updated to look for the YAMLLINT_FILE_ENCODING variable, pretty much every test will be likely to fail if YAMLLINT_FILE_ENCODING is set to a certain values. This change makes the code for deleting environment variables get run for all tests (not just tests.test_cli). As an alternative, we could have kept most of the code for deleting environment variables in tests/test_cli.py, and only included code for deleting YAMLLINT_FILE_ENCODING in tests/__init__.py. I decided to put all of the environment variable deletion code in tests/__init__.py in order to make things more consistent and easier to understand. I had also considered adding a function for deleting environment variables to tests/common.py and then adding this to every test module that needs to have environment variables deleted: from tests.common import remove_env_vars_that_might_interfere setUpModule = remove_env_vars_that_might_interfere() I decided to not do that because pretty much every single test module will fail if YAMLLINT_FILE_ENCODING is set to certain values, and there’s a lot of test modules.
1 parent 82a57b7 commit 0b3abe5

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

tests/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (C) 2016 Adrien Vergé
2+
# Copyright (C) 2025 Jason Yundt
23
#
34
# This program is free software: you can redistribute it and/or modify
45
# it under the terms of the GNU General Public License as published by
@@ -14,5 +15,23 @@
1415
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1516

1617
import locale
18+
import os
1719

1820
locale.setlocale(locale.LC_ALL, 'C')
21+
# yamllint uses these environment variables to find a config file.
22+
env_vars_that_could_interfere_with_tests = (
23+
'YAMLLINT_CONFIG_FILE',
24+
'XDG_CONFIG_HOME',
25+
# These variables are used to determine where the user’s home
26+
# directory is. See
27+
# https://docs.python.org/3/library/os.path.html#os.path.expanduser
28+
'HOME',
29+
'USERPROFILE',
30+
'HOMEPATH',
31+
'HOMEDRIVE'
32+
)
33+
for name in env_vars_that_could_interfere_with_tests:
34+
try:
35+
del os.environ[name]
36+
except KeyError:
37+
pass

tests/test_cli.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,6 @@ def utf8_available():
3838
return True
3939

4040

41-
def setUpModule():
42-
# yamllint uses these environment variables to find a config file.
43-
env_vars_that_could_interfere = (
44-
'YAMLLINT_CONFIG_FILE',
45-
'XDG_CONFIG_HOME',
46-
# These variables are used to determine where the user’s home
47-
# directory is. See
48-
# https://docs.python.org/3/library/os.path.html#os.path.expanduser
49-
'HOME',
50-
'USERPROFILE',
51-
'HOMEPATH',
52-
'HOMEDRIVE'
53-
)
54-
for name in env_vars_that_could_interfere:
55-
try:
56-
del os.environ[name]
57-
except KeyError:
58-
pass
59-
60-
6141
class CommandLineTestCase(unittest.TestCase):
6242
@classmethod
6343
def setUpClass(cls):

0 commit comments

Comments
 (0)