From 712d3403efb8f4cb8d3e4133625ae4cd8a09e9dc Mon Sep 17 00:00:00 2001 From: Evgeny Date: Thu, 22 Jul 2021 20:47:35 +0300 Subject: [PATCH] Get PWD from shell instead of the os.environ (#239) --- CHANGELOG.md | 2 ++ src/shell.rs | 4 ++++ templates/xonsh.txt | 24 +++++++++--------------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8c0be7..75629b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Nushell: errors on 0.33.0. - PowerShell: errors when initializing in `StrictMode`. - Bash/POSIX: remove conflicting alias definitions when initializing. +- Bash: remove extra semicolon when setting `$PROMPT_COMMAND`. +- Xonsh: use shell environment instead of `os.environ`. ## [0.7.2] - 2021-06-10 diff --git a/src/shell.rs b/src/shell.rs index 48d60f5..19f3ef4 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -334,8 +334,12 @@ mod tests { let mut source = Xonsh(&opts).render().unwrap(); source.push('\n'); + let tempdir = tempfile::tempdir().unwrap(); + let tempdir = tempdir.path().to_str().unwrap(); + Command::new("pylint") .args(&["--from-stdin", "zoxide"]) + .env("HOME", tempdir) .write_stdin(source) .assert() .success() diff --git a/templates/xonsh.txt b/templates/xonsh.txt index 0fb3148..601ae3d 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -3,17 +3,11 @@ """Initialize zoxide on Xonsh.""" +import builtins # pylint: disable=unused-import import os import os.path import subprocess import sys -{%- if cmd.is_some() %} -from builtins import aliases # type: ignore # pylint: disable=no-name-in-module -{%- endif %} -{%- if hook != InitHook::None %} -from builtins import events # type: ignore # pylint: disable=no-name-in-module -{%- endif %} -from subprocess import CalledProcessError from typing import AnyStr, List, Optional import xonsh.dirstack # type: ignore # pylint: disable=import-error @@ -37,9 +31,9 @@ def __zoxide_pwd() -> str: {%- if resolve_symlinks %} pwd = os.getcwd() {%- else %} - pwd = os.getenv("PWD") + pwd = builtins.__xonsh__.env.get("PWD") # type: ignore # pylint:disable=no-member if pwd is None: - raise Exception("$PWD not found in env") + raise Exception("$PWD not found") {%- endif %} return pwd @@ -91,9 +85,9 @@ if globals().get("__zoxide_hooked") is not True: {%- when InitHook::None %} {{ not_configured }} {%- when InitHook::Prompt %} - @events.on_post_prompt # type: ignore # pylint:disable=undefined-variable + @builtins.events.on_post_prompt # type: ignore # pylint:disable=no-member {%- when InitHook::Pwd %} - @events.on_chdir # type: ignore # pylint:disable=undefined-variable + @builtins.events.on_chdir # type: ignore # pylint:disable=no-member {%- endmatch %} def __zoxide_hook(**_kwargs): """Hook to add new entries to the database.""" @@ -125,7 +119,7 @@ def __zoxide_z(args: List[str]): check=True, stdout=subprocess.PIPE, ) - except CalledProcessError as exc: + except subprocess.CalledProcessError as exc: raise ZoxideSilentException() from exc __zoxide_result = __zoxide_cmd.stdout[:-1] @@ -139,7 +133,7 @@ def __zoxide_zi(args: List[str]): __zoxide_cmd = subprocess.run( [zoxide, "query", "-i", "--"] + args, check=True, stdout=subprocess.PIPE ) - except CalledProcessError as exc: + except subprocess.CalledProcessError as exc: raise ZoxideSilentException() from exc __zoxide_result = __zoxide_cmd.stdout[:-1] @@ -153,8 +147,8 @@ def __zoxide_zi(args: List[str]): {%- match cmd %} {%- when Some with (cmd) %} -aliases["{{cmd}}"] = __zoxide_z -aliases["{{cmd}}i"] = __zoxide_zi +builtins.aliases["{{cmd}}"] = __zoxide_z # type: ignore # pylint:disable=no-member +builtins.aliases["{{cmd}}i"] = __zoxide_zi # type: ignore # pylint:disable=no-member {%- when None %}