diff --git a/requirements.txt b/requirements.txt index af7e1bc4..914c3163 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,6 @@ flake8>=2.2.3 ipython pytest-cov pytest>=3.9 -sh>=2 tox twine wheel diff --git a/tests/test_cli.py b/tests/test_cli.py index fc309b48..f911c2cd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,8 @@ import os -import sh +import subprocess from pathlib import Path from typing import Optional +import contextlib import pytest @@ -151,61 +152,94 @@ def test_set_no_file(cli): assert "Missing argument" in result.output +@contextlib.contextmanager +def cd(path): + """Cross platform directory change context manager""" + old_dir = os.getcwd() + os.chdir(path) + try: + yield + finally: + os.chdir(old_dir) + + def test_get_default_path(tmp_path): - with sh.pushd(tmp_path): + with cd(tmp_path): (tmp_path / ".env").write_text("a=b") - result = sh.dotenv("get", "a") + result = subprocess.run(["dotenv", "get", "a"], + capture_output=True, + text=True, + check=True) - assert result == "b\n" + assert result.stdout == "b\n" def test_run(tmp_path): - with sh.pushd(tmp_path): + with cd(tmp_path): (tmp_path / ".env").write_text("a=b") - result = sh.dotenv("run", "printenv", "a") + cmd = ["dotenv", "run", "printenv" if os.name != 'nt' else "set", "a"] + result = subprocess.run(cmd, capture_output=True, text=True, check=True) - assert result == "b\n" + assert result.stdout.strip() == "b" def test_run_with_existing_variable(tmp_path): - with sh.pushd(tmp_path): + with cd(tmp_path): (tmp_path / ".env").write_text("a=b") env = dict(os.environ) env.update({"LANG": "en_US.UTF-8", "a": "c"}) - result = sh.dotenv("run", "printenv", "a", _env=env) + cmd = ["dotenv", "run", "printenv" if os.name != 'nt' else "set", "a"] + result = subprocess.run(cmd, + capture_output=True, + text=True, + env=env, + check=True) - assert result == "b\n" + assert result.stdout.strip() == "b" def test_run_with_existing_variable_not_overridden(tmp_path): - with sh.pushd(tmp_path): + with cd(tmp_path): (tmp_path / ".env").write_text("a=b") env = dict(os.environ) env.update({"LANG": "en_US.UTF-8", "a": "c"}) - result = sh.dotenv("run", "--no-override", "printenv", "a", _env=env) + cmd = ["dotenv", "run", "--no-override", "printenv" if os.name != 'nt' else "set", "a"] + result = subprocess.run(cmd, + capture_output=True, + text=True, + env=env, + check=True) - assert result == "c\n" + assert result.stdout.strip() == "c" def test_run_with_none_value(tmp_path): - with sh.pushd(tmp_path): + with cd(tmp_path): (tmp_path / ".env").write_text("a=b\nc") - result = sh.dotenv("run", "printenv", "a") + cmd = ["dotenv", "run", "printenv" if os.name != 'nt' else "set", "a"] + result = subprocess.run(cmd, + capture_output=True, + text=True, + check=True) - assert result == "b\n" + assert result.stdout.strip() == "b" def test_run_with_other_env(dotenv_path): dotenv_path.write_text("a=b") - result = sh.dotenv("--file", dotenv_path, "run", "printenv", "a") + cmd = ["dotenv", "--file", str(dotenv_path), "run", "printenv" if os.name != 'nt' else "set", "a"] + result = subprocess.run(cmd, + capture_output=True, + text=True, + check=True) - assert result == "b\n" + assert result.stdout.strip() == "b" def test_run_without_cmd(cli): diff --git a/tests/test_main.py b/tests/test_main.py index fd5e3903..537a6e70 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -4,9 +4,9 @@ import sys import textwrap from unittest import mock +import subprocess import pytest -import sh import dotenv @@ -195,8 +195,8 @@ def prepare_file_hierarchy(path): test_find_dotenv0/ └── child1 ├── child2 - │   └── child3 - │   └── child4 + │ └── child3 + │ └── child4 └── .env Then try to automatically `find_dotenv` starting in `child4` @@ -329,9 +329,12 @@ def test_load_dotenv_in_current_dir(tmp_path): """)) os.chdir(tmp_path) - result = sh.Command(sys.executable)(code_path) + process = subprocess.run([sys.executable, str(code_path)], + capture_output=True, + text=True, + check=True) - assert result == 'b\n' + assert process.stdout == 'b\n' def test_dotenv_values_file(dotenv_path): diff --git a/tests/test_zip_imports.py b/tests/test_zip_imports.py index 46d3c02e..83aaa2b1 100644 --- a/tests/test_zip_imports.py +++ b/tests/test_zip_imports.py @@ -1,6 +1,6 @@ import os import sys -import sh +import subprocess import textwrap from typing import List from unittest import mock @@ -96,6 +96,6 @@ def test_load_dotenv_outside_zip_file_when_called_in_zipfile(tmp_path): ) os.chdir(str(tmp_path)) - result = sh.Command(sys.executable)(code_path) + result = subprocess.run([sys.executable, code_path], capture_output=True, text=True, check=True) - assert result == "b\n" + assert result.stdout == "b\n"