Skip to content

Commit

Permalink
Improve tests, remove unaccessible code (#35)
Browse files Browse the repository at this point in the history
* tests for clean and config

* remove inaccessible entry type check

click itself already takes care of this!

* remove pyproject.toml not found in initialization

unaccessible code, this is already checked in config

* re-init test without extra pyapp variables

* remove more unaccessible code - optional variables for pyapp on re-init

error already caught in config

* remove unused code

* ensure local folder is used even if other pyapp sources are there
  • Loading branch information
trappitsch authored Mar 29, 2024
1 parent 23d066b commit 3685e01
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 38 deletions.
23 changes: 5 additions & 18 deletions src/box/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,7 @@ def _set_app_entry_type(self):
pass

if self._app_entry_type:
if self._app_entry_type.lower() not in ut.PYAPP_APP_ENTRY_TYPES:
raise click.ClickException(
f"Invalid entry type. "
f"Please choose from {ut.PYAPP_APP_ENTRY_TYPES}."
)
else:
entry_type = self._app_entry_type.lower()
entry_type = self._app_entry_type.lower()
else:
if self._quiet:
entry_type = default_entry_type
Expand Down Expand Up @@ -208,14 +202,11 @@ def _set_optional_deps(self):
def _set_optional_pyapp_variables(self):
"""Set optional environmental variables for PyApp."""
default_opt_vars = ""
try:
tmp = self.pyproj.optional_pyapp_variables
for key, value in tmp.items():
default_opt_vars += f"{key} {value} "
except KeyError:
pass

opt_vars = None
tmp = self.pyproj.optional_pyapp_variables
for key, value in tmp.items():
default_opt_vars += f"{key} {value} "

if self._opt_paypp_vars:
opt_vars = self._opt_paypp_vars
elif not self._quiet:
Expand Down Expand Up @@ -246,10 +237,6 @@ def _set_pyproj(self):
"""Check if the pyproject.toml file is valid."""
try:
self.pyproj = PyProjectParser()
except FileNotFoundError:
raise click.ClickException(
"No `pyproject.toml` file found in current folder."
)
except KeyError:
raise click.ClickException(
"Invalid `pyproject.toml` file. Missing `project` table."
Expand Down
7 changes: 7 additions & 0 deletions tests/cli/test_cli_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ def test_clean_no_pyproject(tmp_path_chdir, rye_project_no_box, init_folder):
assert "This is not a box project." in result.output


def test_clean_nothing_to_do(rye_project):
"""State nothing to clean if already clean."""
runner = CliRunner()
result = runner.invoke(cli, ["clean"])
assert "Nothing to clean." in result.output


@pytest.mark.parametrize(
"option", ["-p", "--pyapp-folder", "-s", "--source-pyapp", "-ps"]
)
Expand Down
39 changes: 21 additions & 18 deletions tests/cli/test_cli_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,34 +125,37 @@ def test_initialize_with_options(rye_project_no_box, gui):
assert pyproj.optional_pyapp_variables == {"PYAPP_FULL_ISOLATION": "1"}


def test_initialize_project_again(rye_project_no_box):
@pytest.mark.parametrize("pyapp_extra_vars", ["PYAPP_FULL_ISOLATION 1", None])
def test_initialize_project_again(rye_project_no_box, pyapp_extra_vars):
"""Initialization of a previous project sets defaults from previous config."""
builder = "build"
entry_point = "myapp:entry"
entry_type = "module"
optional_deps = "gui"
py_version = "3.8"
pyapp_vars = "PYAPP_FULL_ISOLATION 1"

args = [
"init",
"-e",
entry_point,
"-et",
entry_type,
"-py",
py_version,
"-opt",
optional_deps,
"-b",
builder,
"--gui",
]
if pyapp_extra_vars:
args.append("--opt-pyapp-vars")
args.append(pyapp_extra_vars)

runner = CliRunner()
runner.invoke(
cli,
[
"init",
"-e",
entry_point,
"-et",
entry_type,
"-py",
py_version,
"-opt",
optional_deps,
"-b",
builder,
"--opt-pyapp-vars",
pyapp_vars,
"--gui",
],
args,
)

# now re-initialize with quiet and assure that the same options are set
Expand Down
6 changes: 6 additions & 0 deletions tests/func/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@ def test_pyproject_writer_called_with_newline(tmp_path_chdir, mocker):
print(tmp_path_chdir.absolute())

mock_open.assert_called_with(Path(fname), "w", newline="\n")


def test_pyproject_writer_no_pyproject_toml_file(tmp_path_chdir):
"""Raise FileNotFound error if no pyproject.toml file in folder."""
with pytest.raises(FileNotFoundError):
pyproject_writer("builder", "rye")
23 changes: 21 additions & 2 deletions tests/func/test_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def create_pyapp_source(project_path: Path) -> Path:
# TESTS #


def test_builder_invalid(rye_project):
"""Raise an error if an invalid builder is given."""
builder = "invalid" # can only happen in user-modified pyproject.toml
pyproject_writer("builder", builder)

packager = PackageApp()

with pytest.raises(KeyError) as e:
packager.build()

assert f"Unknown {builder=}" in e.value.args[0]


@pytest.mark.parametrize("builder", ["rye", "hatch", "build", "flit", "pdm"])
def test_builders(min_proj_no_box, mocker, builder):
"""Test all builders are called correctly."""
Expand Down Expand Up @@ -176,16 +189,22 @@ def test_get_pyapp_wrong_no_pyapp_folder(rye_project, mocker):
assert "Error: no pyapp source code folder found." in e.value.args[0]


def test_get_pyapp_use_local_folder(rye_project, mocker):
@pytest.mark.parametrize("extra_source", [True, False])
def test_get_pyapp_use_local_folder(rye_project, mocker, extra_source):
"""Use local source code if it already exists provided."""
urlretrieve_mock = mocker.patch.object(urllib.request, "urlretrieve")
tar_mock = mocker.patch("tarfile.open")

# create a fake source code file - tarfile is mocked
rye_project.joinpath("build/").mkdir()
build_dir = rye_project.joinpath("build/")
build_dir.mkdir()
local_source = rye_project.joinpath("build/pyapp-local")
local_source.mkdir(parents=True)

# extra source folder already there - use local
if extra_source:
build_dir.joinpath("pyapp-0.1.0").mkdir()

packager = PackageApp()
packager._get_pyapp()

Expand Down

0 comments on commit 3685e01

Please sign in to comment.