1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
import pytest
from briefcase.exceptions import BriefcaseCommandError
def create_file_and_parents(path, content):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
def test_src_is_used_by_default(convert_command):
module_path = convert_command.base_path / "src" / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
def test_flat_is_used_if_src_name_is_wrong(convert_command):
module_path = convert_command.base_path / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
def test_fallback_is_used_if_no_src_or_flat(convert_command):
module_path = convert_command.base_path / "something/module_name"
create_file_and_parents(module_path / "__main__.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
def test_src_is_preferred_over_flat(convert_command):
module_path = convert_command.base_path / "src" / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
flat_module_path = convert_command.base_path / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
assert repr(flat_module_path) not in info
def test_flat_is_preferred_over_fallback(convert_command):
module_path = convert_command.base_path / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
fallback_module_path = convert_command.base_path / "something/module_name"
create_file_and_parents(module_path / "__main__.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
assert repr(fallback_module_path) not in info
def test_flat_is_used_if_src_is_wrong(convert_command):
module_path = convert_command.base_path / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
wrong_module_path = convert_command.base_path / "src" / "different_module_name"
create_file_and_parents(wrong_module_path / "__main__.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
assert repr(module_path.name) in info
def test_flat_is_used_if_src_doesnt_have_main_file(convert_command):
module_path = convert_command.base_path / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
wrong_module_path = convert_command.base_path / "src" / "module_name"
create_file_and_parents(wrong_module_path / "not_main.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
assert repr(module_path.name) in info
def test_fallback_is_used_if_flat_doesnt_have_main_file(convert_command):
module_path = convert_command.base_path / "something" / "module_name"
create_file_and_parents(module_path / "__main__.py", "")
wrong_module_path = convert_command.base_path / "module_name"
create_file_and_parents(wrong_module_path / "not_main.py", "")
default, info = convert_command.get_source_dir_hint("module-name", "module_name")
assert default == str(module_path.relative_to(convert_command.base_path)).replace(
"\\", "/"
)
assert repr(default) in info
assert (
repr(str(module_path.relative_to(convert_command.base_path))).replace(
r"\\", "/"
)
in info
)
def test_exception_is_raised_if_no_source_dir(convert_command):
module_path = convert_command.base_path / "src" / "different_module_name"
create_file_and_parents(module_path / "__main__.py", "")
with pytest.raises(BriefcaseCommandError):
convert_command.get_source_dir_hint("module-name", "module_name")
def test_exception_is_raised_if_no_source_dir_with_main(convert_command):
module_path = convert_command.base_path / "src" / "module_name"
create_file_and_parents(module_path / "not_main.py", "")
with pytest.raises(BriefcaseCommandError):
convert_command.get_source_dir_hint("module-name", "module_name")
|