summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-06-02 19:43:58 +0200
committerGitHub <noreply@github.com>2022-06-02 19:43:58 +0200
commitdee9de56f84837be3de915aec8c2e9629dd9c548 (patch)
treebb6970562ac3dc60932d4012797544e3a4430b53
parentdc7b03266fad7e885da1374ac312dfc0b63ebbfd (diff)
downloadpylint-git-dee9de56f84837be3de915aec8c2e9629dd9c548.tar.gz
Don't crash if we can't find the user's home directory (#6806)
-rw-r--r--doc/whatsnew/2/2.14/full.rst3
-rw-r--r--pylint/config/find_default_config_files.py8
-rw-r--r--tests/config/test_find_default_config_files.py15
3 files changed, 24 insertions, 2 deletions
diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst
index 879097358..97797e8ad 100644
--- a/doc/whatsnew/2/2.14/full.rst
+++ b/doc/whatsnew/2/2.14/full.rst
@@ -5,6 +5,9 @@ What's New in Pylint 2.14.1?
----------------------------
Release date: TBA
+* Don't crash if we can't find the user's home directory.
+
+ Closes #6802
What's New in Pylint 2.14.0?
diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py
index 595548285..36917a380 100644
--- a/pylint/config/find_default_config_files.py
+++ b/pylint/config/find_default_config_files.py
@@ -63,8 +63,12 @@ def find_default_config_files() -> Iterator[Path]:
if Path(os.environ["PYLINTRC"]).is_file():
yield Path(os.environ["PYLINTRC"]).resolve()
else:
- user_home = Path.home()
- if str(user_home) not in ("~", "/root"):
+ try:
+ user_home = Path.home()
+ except RuntimeError:
+ # If the home directory does not exist a RuntimeError will be raised
+ user_home = None
+ if user_home is not None and str(user_home) not in ("~", "/root"):
home_rc = user_home / ".pylintrc"
if home_rc.is_file():
yield home_rc.resolve()
diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py
index 0872c3d88..cf588bdd7 100644
--- a/tests/config/test_find_default_config_files.py
+++ b/tests/config/test_find_default_config_files.py
@@ -12,6 +12,7 @@ import sys
import tempfile
from collections.abc import Iterator
from pathlib import Path
+from unittest import mock
import pytest
from pytest import CaptureFixture
@@ -223,3 +224,17 @@ def test_cfg_has_config(content: str, expected: str, tmp_path: Path) -> None:
with open(fake_cfg, "w", encoding="utf8") as f:
f.write(content)
assert _cfg_has_config(fake_cfg) == expected
+
+
+def test_non_existent_home() -> None:
+ """Test that we handle a non-existent home directory.
+
+ Reported in https://github.com/PyCQA/pylint/issues/6802.
+ """
+ with mock.patch("pathlib.Path.home", side_effect=RuntimeError):
+ current_dir = os.getcwd()
+ os.chdir(os.path.dirname(os.path.abspath(sys.executable)))
+
+ assert not list(config.find_default_config_files())
+
+ os.chdir(current_dir)