mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
tools: check CARGO_HOME before Path.home() and handle unavailable home directory
This commit is contained in:
@ -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__":
|
||||
|
||||
@ -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')
|
||||
|
||||
|
||||
|
||||
@ -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')
|
||||
|
||||
|
||||
|
||||
@ -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')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user