adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.83k stars 269 forks source link

1.32.0: Testing fails without `HOME` environment variable #605

Closed mtelka closed 8 months ago

mtelka commented 10 months ago

When tests are run without the HOME environment variable then they fails:

__________ CommandLineTestCase.test_run_with_user_global_config_file ___________

self = <tests.test_cli.CommandLineTestCase testMethod=test_run_with_user_global_config_file>

    @unittest.skipIf(os.environ.get('GITHUB_RUN_ID'), '$HOME not overridable')
    def test_run_with_user_global_config_file(self):
        home = os.path.join(self.wd, 'fake-home')
        dir = os.path.join(home, '.config', 'yamllint')
        os.makedirs(dir)
        config = os.path.join(dir, 'config')

>       self.addCleanup(os.environ.update, HOME=os.environ['HOME'])

tests/test_cli.py:316: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = environ({'LC_CTYPE': 'C.UTF-8', 'PYTHONPATH': '...', 'PYTEST_CURRENT_TEST': 'tests/test_cli.py::CommandLineTestCase::test_run_with_user_global_config_file (call)'})
key = 'HOME'

    def __getitem__(self, key):
        try:
            value = self._data[self.encodekey(key)]
        except KeyError:
            # raise KeyError with the original key value
>           raise KeyError(key) from None
E           KeyError: 'HOME'

/usr/lib/python3.9/os.py:679: KeyError
adrienverge commented 10 months ago

Hello,

Having $HOME defined seems required in order to test the configuration file path. Also, I just read that the POSIX specification requires the OS to always set a value for $HOME.

So, it seems normal that tests fail when run without the $HOME environment variable. What do you think?

mtelka commented 10 months ago

Sorry, the stackexchange article misinterprets POSIX. The linked POSIX page says this:

8.1 Environment Variable Definition ... If the variables in the following two sections are present in the environment during the execution of an application or utility, they shall be given the meaning described below. ... If they are defined in the application's environment, the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 and the functions in the System Interfaces volume of IEEE Std 1003.1-2001 assume they have the specified meaning. Conforming applications shall not set these environment variables to have meanings other than as described. ... 8.2 Internationalization Variables ... 8.3 Other Environment Variables ... HOME The system shall initialize this variable at the time of login to be a pathname of the user's home directory. See .

So in other words, HOME does not need to be set. But if it is then its meaning is as specified by POSIX.