Skip to content

Commit

Permalink
Add tests for the mainline.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Oct 2, 2022
1 parent ef1ae24 commit fd02607
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/briefcase/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from contextlib import suppress
from pathlib import Path

from briefcase.cmdline import parse_cmdline
from briefcase.console import Console, Log
Expand All @@ -16,7 +17,7 @@ def main():
command = Command(logger=logger, console=console)
options = command.parse_options(extra=extra_cmdline)
command.check_obsolete_data_dir()
command.parse_config("pyproject.toml")
command.parse_config(Path.cwd() / "pyproject.toml")
command(**options)
except HelpText as e:
logger.info()
Expand All @@ -41,8 +42,8 @@ def main():
with suppress(KeyboardInterrupt):
logger.save_log_to_file(command)

sys.exit(result)
return result


if __name__ == "__main__":
main()
sys.exit(main()) # pragma: no cover
7 changes: 5 additions & 2 deletions src/briefcase/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(
console: Console,
tools: ToolCache = None,
apps: dict = None,
base_path: Path = Path.cwd(),
base_path: Path = None,
data_path: Path = None,
is_clone: bool = False,
):
Expand All @@ -132,7 +132,10 @@ def __init__(
:param is_clone: Flag that Command was triggered by the user's requested Command;
for instance, RunCommand can invoke UpdateCommand and/or BuildCommand.
"""
self.base_path = Path(base_path)
if base_path is None:
self.base_path = Path.cwd()
else:
self.base_path = base_path
self.data_path = self.validate_data_path(data_path)
self.apps = {} if apps is None else apps
self.is_clone = is_clone
Expand Down
159 changes: 159 additions & 0 deletions tests/test_mainline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import sys
from pathlib import Path

import pytest

from briefcase.__main__ import main
from briefcase.commands.create import CreateCommand

from .utils import create_file


@pytest.fixture
def pyproject_toml(monkeypatch, tmp_path):
# Monkeypatch cwd() to use a test folder.
monkeypatch.setattr(Path, "cwd", lambda: tmp_path)

create_file(
tmp_path / "pyproject.toml",
"""
[build-system]
requires = ["briefcase"]
[tool.briefcase]
project_name = "Hello World"
bundle = "com.example"
version = "0.0.1"
[tool.briefcase.app.myapp]
description = "My first application"
sources = ["myapp"]
""",
)


def test_help(monkeypatch, tmp_path, capsys):
"Briefcase can output help."
# Set the test command line
monkeypatch.setattr(sys, "argv", ["briefcase", "--help"])

# Help has a return code of -10
assert main() == -10

output = capsys.readouterr().out
assert output.startswith(
"\nusage: briefcase [-h] <command> [<platform>] [<format>] ...\n"
)

# No log file was written
assert len(list(tmp_path.glob("briefcase.*.log"))) == 0


def test_command(monkeypatch, tmp_path, capsys):
"A command can be successful"
# Monkeypatch cwd() to use a test folder.
monkeypatch.setattr(Path, "cwd", lambda: tmp_path)

# Create a dummy empty template to use in the new command
create_file(tmp_path / "template" / "cookiecutter.json", '{"app_name": "app_name"}')
create_file(
tmp_path / "template" / "{{ cookiecutter.app_name }}" / "app", "content"
)

# Set the test command line
monkeypatch.setattr(
sys,
"argv",
["briefcase", "new", "--no-input", "--template", str(tmp_path / "template")],
)

# Successful return is 0
assert main() == 0

output = capsys.readouterr().out
assert output.startswith("\nGenerating a new application 'Hello World'\n")

# No log file was written
assert len(list(tmp_path.glob("briefcase.*.log"))) == 0


def test_command_error(monkeypatch, tmp_path, capsys):
"A command can raise a known error"
# Monkeypatch cwd() to use a test folder.
monkeypatch.setattr(Path, "cwd", lambda: tmp_path)

# Set the test command line
monkeypatch.setattr(sys, "argv", ["briefcase", "create"])

# Help has a return code of -10
assert main() == 100

output = capsys.readouterr().out
assert output.startswith(
"\nBriefcase configuration error: Configuration file not found."
)

# A log file was written
assert len(list(tmp_path.glob("briefcase.*.create.log"))) == 1


def test_unknown_command_error(monkeypatch, pyproject_toml, capsys):
"A command can raise an unknown error"
monkeypatch.setattr(sys, "argv", ["briefcase", "create"])

# Monkeypatch an error into the create command
def bad_generate_app_template(self, app):
raise ValueError("Bad value")

monkeypatch.setattr(
CreateCommand, "generate_app_template", bad_generate_app_template
)

# Error is surfaced to the user
with pytest.raises(ValueError, match=r"Bad value"):
main()


def test_interrupted_command(monkeypatch, pyproject_toml, tmp_path, capsys):
"A command can be interrupted"
monkeypatch.setattr(sys, "argv", ["briefcase", "create"])

# Monkeypatch a keyboard interrupt into the create command
def interrupted_generate_app_template(self, app):
raise KeyboardInterrupt()

monkeypatch.setattr(
CreateCommand, "generate_app_template", interrupted_generate_app_template
)

# Interrupted command return -42
assert main() == -42

output = capsys.readouterr().out
assert "\nAborted by user.\n" in output

# No log file was written
assert len(list(tmp_path.glob("briefcase.*.create.log"))) == 0


def test_interrupted_command_with_log(monkeypatch, pyproject_toml, tmp_path, capsys):
"A log can be generated when a command is interrupted"
monkeypatch.setattr(sys, "argv", ["briefcase", "create", "--log"])

# Monkeypatch a keyboard interrupt into the create command
def interrupted_generate_app_template(self, app):
raise KeyboardInterrupt()

monkeypatch.setattr(
CreateCommand, "generate_app_template", interrupted_generate_app_template
)

# Interrupted command return -42
assert main() == -42

output = capsys.readouterr().out
assert "\nAborted by user.\n" in output

# A log file was written
assert len(list(tmp_path.glob("briefcase.*.create.log"))) == 1

0 comments on commit fd02607

Please sign in to comment.