tools: check CARGO_HOME before Path.home() and handle unavailable home directory

This commit is contained in:
morebrackets
2026-12-02 07:16:47 -08:00
parent 65d20d1886
commit 20e5dfbf2a
4 changed files with 35 additions and 15 deletions

View File

@ -40,8 +40,16 @@ class CargoTestRunnersTest(unittest.TestCase):
with (
mock.patch.dict(os.environ, {"CARGO_HOME": tmp}, clear=True),
mock.patch.object(shutil, "which", return_value=None),
mock.patch.object(Path, "home", side_effect=RuntimeError("no home")),
):
self.assertEqual(mod.find_cargo(), str(fake))
with (
mock.patch.dict(os.environ, {}, clear=True),
mock.patch.object(shutil, "which", return_value=None),
mock.patch.object(Path, "home", side_effect=RuntimeError("no home")),
):
with self.assertRaises(SystemExit):
mod.find_cargo()
if __name__ == "__main__":

View File

@ -16,13 +16,17 @@ def find_cargo() -> str:
which = shutil.which('cargo')
if which:
return which
candidates = []
if os.environ.get('CARGO_HOME'):
candidates.append(Path(os.environ['CARGO_HOME']) / 'bin/cargo')
candidates.append(Path.home() / '.cargo/bin/cargo')
for candidate in candidates:
cargo_home = os.environ.get('CARGO_HOME')
if cargo_home:
candidate = Path(cargo_home) / 'bin/cargo'
if candidate.is_file() and os.access(candidate, os.X_OK):
return str(candidate)
try:
home_cargo = Path.home() / '.cargo/bin/cargo'
except RuntimeError:
home_cargo = None
if home_cargo is not None and home_cargo.is_file() and os.access(home_cargo, os.X_OK):
return str(home_cargo)
raise SystemExit('cargo not found; set CARGO, CARGO_HOME, or add cargo to PATH')

View File

@ -16,13 +16,17 @@ def find_cargo() -> str:
which = shutil.which('cargo')
if which:
return which
candidates = []
if os.environ.get('CARGO_HOME'):
candidates.append(Path(os.environ['CARGO_HOME']) / 'bin/cargo')
candidates.append(Path.home() / '.cargo/bin/cargo')
for candidate in candidates:
cargo_home = os.environ.get('CARGO_HOME')
if cargo_home:
candidate = Path(cargo_home) / 'bin/cargo'
if candidate.is_file() and os.access(candidate, os.X_OK):
return str(candidate)
try:
home_cargo = Path.home() / '.cargo/bin/cargo'
except RuntimeError:
home_cargo = None
if home_cargo is not None and home_cargo.is_file() and os.access(home_cargo, os.X_OK):
return str(home_cargo)
raise SystemExit('cargo not found; set CARGO, CARGO_HOME, or add cargo to PATH')

View File

@ -16,13 +16,17 @@ def find_cargo() -> str:
which = shutil.which('cargo')
if which:
return which
candidates = []
if os.environ.get('CARGO_HOME'):
candidates.append(Path(os.environ['CARGO_HOME']) / 'bin/cargo')
candidates.append(Path.home() / '.cargo/bin/cargo')
for candidate in candidates:
cargo_home = os.environ.get('CARGO_HOME')
if cargo_home:
candidate = Path(cargo_home) / 'bin/cargo'
if candidate.is_file() and os.access(candidate, os.X_OK):
return str(candidate)
try:
home_cargo = Path.home() / '.cargo/bin/cargo'
except RuntimeError:
home_cargo = None
if home_cargo is not None and home_cargo.is_file() and os.access(home_cargo, os.X_OK):
return str(home_cargo)
raise SystemExit('cargo not found; set CARGO, CARGO_HOME, or add cargo to PATH')