Skip to content

Commit

Permalink
Replace sh with cross platfom code
Browse files Browse the repository at this point in the history
  • Loading branch information
theskumar committed Nov 1, 2024
1 parent 30b49bf commit 8833296
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ flake8>=2.2.3
ipython
pytest-cov
pytest>=3.9
sh>=2
tox
twine
wheel
70 changes: 52 additions & 18 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import sh
import subprocess
from pathlib import Path
from typing import Optional
import contextlib

import pytest

Expand Down Expand Up @@ -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):
Expand Down
13 changes: 8 additions & 5 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import sys
import textwrap
from unittest import mock
import subprocess

import pytest
import sh

import dotenv

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_zip_imports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import sys
import sh
import subprocess
import textwrap
from typing import List
from unittest import mock
Expand Down Expand Up @@ -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"

0 comments on commit 8833296

Please sign in to comment.