Skip to content

Commit

Permalink
Use pathlib.Path in tests (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro authored Jun 24, 2023
1 parent 137bc3d commit be96be2
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 169 deletions.
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def cli():


@pytest.fixture
def dotenv_file(tmp_path):
file_ = tmp_path / '.env'
file_.write_bytes(b'')
yield str(file_)
def dotenv_path(tmp_path):
path = tmp_path / '.env'
path.write_bytes(b'')
yield path
93 changes: 41 additions & 52 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os

import pytest
import sh
from pathlib import Path
from typing import Optional

import pytest

import dotenv
from dotenv.cli import cli as dotenv_cli
from dotenv.version import __version__
Expand All @@ -23,11 +25,10 @@
("export", "x='a b c'", '''export x='a b c'\n'''),
)
)
def test_list(cli, dotenv_file, format: Optional[str], content: str, expected: str):
with open(dotenv_file, "w") as f:
f.write(content + '\n')
def test_list(cli, dotenv_path, format: Optional[str], content: str, expected: str):
dotenv_path.write_text(content + '\n')

args = ['--file', dotenv_file, 'list']
args = ['--file', dotenv_path, 'list']
if format is not None:
args.extend(['--format', format])

Expand Down Expand Up @@ -56,17 +57,16 @@ def test_list_no_file(cli):
assert (result.exit_code, result.output) == (1, "")


def test_get_existing_value(cli, dotenv_file):
with open(dotenv_file, "w") as f:
f.write("a=b")
def test_get_existing_value(cli, dotenv_path):
dotenv_path.write_text("a=b")

result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'get', 'a'])
result = cli.invoke(dotenv_cli, ['--file', dotenv_path, 'get', 'a'])

assert (result.exit_code, result.output) == (0, "b\n")


def test_get_non_existent_value(cli, dotenv_file):
result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'get', 'a'])
def test_get_non_existent_value(cli, dotenv_path):
result = cli.invoke(dotenv_cli, ['--file', dotenv_path, 'get', 'a'])

assert (result.exit_code, result.output) == (1, "")

Expand All @@ -85,21 +85,20 @@ def test_get_not_a_file(cli):
assert "Error opening env file" in result.output


def test_unset_existing_value(cli, dotenv_file):
with open(dotenv_file, "w") as f:
f.write("a=b")
def test_unset_existing_value(cli, dotenv_path):
dotenv_path.write_text("a=b")

result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'unset', 'a'])
result = cli.invoke(dotenv_cli, ['--file', dotenv_path, 'unset', 'a'])

assert (result.exit_code, result.output) == (0, "Successfully removed a\n")
assert open(dotenv_file, "r").read() == ""
assert dotenv_path.read_text() == ""


def test_unset_non_existent_value(cli, dotenv_file):
result = cli.invoke(dotenv_cli, ['--file', dotenv_file, 'unset', 'a'])
def test_unset_non_existent_value(cli, dotenv_path):
result = cli.invoke(dotenv_cli, ['--file', dotenv_path, 'unset', 'a'])

assert (result.exit_code, result.output) == (1, "")
assert open(dotenv_file, "r").read() == ""
assert dotenv_path.read_text() == ""


@pytest.mark.parametrize(
Expand All @@ -112,31 +111,31 @@ def test_unset_non_existent_value(cli, dotenv_file):
("auto", "a", "$", "a='$'\n"),
)
)
def test_set_quote_options(cli, dotenv_file, quote_mode, variable, value, expected):
def test_set_quote_options(cli, dotenv_path, quote_mode, variable, value, expected):
result = cli.invoke(
dotenv_cli,
["--file", dotenv_file, "--export", "false", "--quote", quote_mode, "set", variable, value]
["--file", dotenv_path, "--export", "false", "--quote", quote_mode, "set", variable, value]
)

assert (result.exit_code, result.output) == (0, "{}={}\n".format(variable, value))
assert open(dotenv_file, "r").read() == expected
assert dotenv_path.read_text() == expected


@pytest.mark.parametrize(
"dotenv_file,export_mode,variable,value,expected",
"dotenv_path,export_mode,variable,value,expected",
(
(".nx_file", "true", "a", "x", "export a='x'\n"),
(".nx_file", "false", "a", "x", "a='x'\n"),
(Path(".nx_file"), "true", "a", "x", "export a='x'\n"),
(Path(".nx_file"), "false", "a", "x", "a='x'\n"),
)
)
def test_set_export(cli, dotenv_file, export_mode, variable, value, expected):
def test_set_export(cli, dotenv_path, export_mode, variable, value, expected):
result = cli.invoke(
dotenv_cli,
["--file", dotenv_file, "--quote", "always", "--export", export_mode, "set", variable, value]
["--file", dotenv_path, "--quote", "always", "--export", export_mode, "set", variable, value]
)

assert (result.exit_code, result.output) == (0, "{}={}\n".format(variable, value))
assert open(dotenv_file, "r").read() == expected
assert dotenv_path.read_text() == expected


def test_set_non_existent_file(cli):
Expand All @@ -153,31 +152,26 @@ def test_set_no_file(cli):


def test_get_default_path(tmp_path):
with sh.pushd(str(tmp_path)):
with open(str(tmp_path / ".env"), "w") as f:
f.write("a=b")
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")

result = sh.dotenv("get", "a")

assert result == "b\n"


def test_run(tmp_path):
with sh.pushd(str(tmp_path)):
dotenv_file = str(tmp_path / ".env")
with open(dotenv_file, "w") as f:
f.write("a=b")
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")

result = sh.dotenv("run", "printenv", "a")

assert result == "b\n"


def test_run_with_existing_variable(tmp_path):
with sh.pushd(str(tmp_path)):
dotenv_file = str(tmp_path / ".env")
with open(dotenv_file, "w") as f:
f.write("a=b")
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")
env = dict(os.environ)
env.update({"LANG": "en_US.UTF-8", "a": "c"})

Expand All @@ -187,10 +181,8 @@ def test_run_with_existing_variable(tmp_path):


def test_run_with_existing_variable_not_overridden(tmp_path):
with sh.pushd(str(tmp_path)):
dotenv_file = str(tmp_path / ".env")
with open(dotenv_file, "w") as f:
f.write("a=b")
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b")
env = dict(os.environ)
env.update({"LANG": "en_US.UTF-8", "a": "c"})

Expand All @@ -200,21 +192,18 @@ def test_run_with_existing_variable_not_overridden(tmp_path):


def test_run_with_none_value(tmp_path):
with sh.pushd(str(tmp_path)):
dotenv_file = str(tmp_path / ".env")
with open(dotenv_file, "w") as f:
f.write("a=b\nc")
with sh.pushd(tmp_path):
(tmp_path / ".env").write_text("a=b\nc")

result = sh.dotenv("run", "printenv", "a")

assert result == "b\n"


def test_run_with_other_env(dotenv_file):
with open(dotenv_file, "w") as f:
f.write("a=b")
def test_run_with_other_env(dotenv_path):
dotenv_path.write_text("a=b")

result = sh.dotenv("--file", dotenv_file, "run", "printenv", "a")
result = sh.dotenv("--file", dotenv_path, "run", "printenv", "a")

assert result == "b\n"

Expand Down
6 changes: 3 additions & 3 deletions tests/test_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_ipython_existing_variable_no_override(tmp_path):

dotenv_file = tmp_path / ".env"
dotenv_file.write_text("a=b\n")
os.chdir(str(tmp_path))
os.chdir(tmp_path)
os.environ["a"] = "c"

ipshell = InteractiveShellEmbed()
Expand All @@ -29,7 +29,7 @@ def test_ipython_existing_variable_override(tmp_path):

dotenv_file = tmp_path / ".env"
dotenv_file.write_text("a=b\n")
os.chdir(str(tmp_path))
os.chdir(tmp_path)
os.environ["a"] = "c"

ipshell = InteractiveShellEmbed()
Expand All @@ -45,7 +45,7 @@ def test_ipython_new_variable(tmp_path):

dotenv_file = tmp_path / ".env"
dotenv_file.write_text("a=b\n")
os.chdir(str(tmp_path))
os.chdir(tmp_path)

ipshell = InteractiveShellEmbed()
ipshell.run_line_magic("load_ext", "dotenv")
Expand Down
Loading

0 comments on commit be96be2

Please sign in to comment.