From c638fbbc716b1e1b62068448511bdb697779a88f Mon Sep 17 00:00:00 2001 From: Andy Kipp Date: Wed, 3 Jul 2024 08:10:03 +0200 Subject: [PATCH] refactoring: fix test_main on mac (#5570) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## For community ⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍 comment** --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- tests/test_main.py | 17 +++++++++++++---- xonsh/history/main.py | 14 +++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index f82f577da..019619340 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -6,6 +6,7 @@ import os import os.path import sys from contextlib import contextmanager +from pathlib import Path import pytest @@ -353,7 +354,9 @@ def test_force_interactive_custom_rc_with_script(shell, tmpdir, monkeypatch, xes monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False") f = tmpdir.join("wakkawakka") f.write("print('hi')") - args = xonsh.main.premain(["-i", "--rc", f.strpath, "tests/sample.xsh"]) + args = xonsh.main.premain( + ["-i", "--rc", f.strpath, str(Path(__file__).parent / "sample.xsh")] + ) assert args.mode == XonshMode.interactive assert f.strpath in xession.rc_files @@ -364,7 +367,9 @@ def test_force_interactive_custom_rc_with_script_and_no_rc( monkeypatch.setitem(os.environ, "XONSH_CACHE_SCRIPTS", "False") f = tmpdir.join("wakkawakka") f.write("print('hi')") - args = xonsh.main.premain(["-i", "--no-rc", "--rc", f.strpath, "tests/sample.xsh"]) + args = xonsh.main.premain( + ["-i", "--no-rc", "--rc", f.strpath, str(Path(__file__).parent / "sample.xsh")] + ) assert args.mode == XonshMode.interactive assert len(xession.rc_files) == 0 @@ -375,7 +380,9 @@ def test_custom_rc_with_script(shell, tmpdir, xession): """ f = tmpdir.join("wakkawakka") f.write("print('hi')") - args = xonsh.main.premain(["--rc", f.strpath, "tests/sample.xsh"]) + args = xonsh.main.premain( + ["--rc", f.strpath, str(Path(__file__).parent / "sample.xsh")] + ) assert not (args.mode == XonshMode.interactive) assert f.strpath in xession.rc_files @@ -386,7 +393,9 @@ def test_custom_rc_with_script_and_no_rc(shell, tmpdir, xession): """ f = tmpdir.join("wakkawakka") f.write("print('hi')") - args = xonsh.main.premain(["--no-rc", "--rc", f.strpath, "tests/sample.xsh"]) + args = xonsh.main.premain( + ["--no-rc", "--rc", f.strpath, str(Path(__file__).parent / "sample.xsh")] + ) assert not (args.mode == XonshMode.interactive) assert len(xession.rc_files) == 0 diff --git a/xonsh/history/main.py b/xonsh/history/main.py index 30fd193d2..3e37579c3 100644 --- a/xonsh/history/main.py +++ b/xonsh/history/main.py @@ -103,9 +103,17 @@ def _xh_bash_hist_parser(location=None, **kwargs): os.path.join("~", ".bash_history"), ) if location: - with open(location, errors="backslashreplace") as bash_hist: - for ind, line in enumerate(bash_hist): - yield {"inp": line.rstrip(), "ts": 0.0, "ind": ind} + try: + with open(location, errors="backslashreplace") as bash_hist: + for ind, line in enumerate(bash_hist): + yield {"inp": line.rstrip(), "ts": 0.0, "ind": ind} + except PermissionError: + print(f"Bash history permission error in {location!r}", file=sys.stderr) + yield { + "inp": f"# Bash history permission error in {location!r}", + "ts": 0.0, + "ind": 0, + } else: print("No bash history file", file=sys.stderr)