Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init():
responses = ['foo', # Module name
'Test Author', # Author
'test@example.com', # Author email
'http://example.com/', # Home page
'1' # License (1 -> MIT)
]
with TemporaryDirectory() as td, \
patch_data_dir(), \
faking_input(responses):
ti = init.TerminalIniter(td)
ti.initialise()
generated = Path(td) / 'pyproject.toml'
assert_isfile(generated)
with generated.open() as f:
data = pytoml.load(f)
assert data['tool']['flit']['metadata'][
'author-email'] == "test@example.com"
license = Path(td) / 'LICENSE'
assert_isfile(license)
with license.open() as f:
license_text = f.read()
assert license_text.startswith("The MIT License (MIT)")
assert "{year}" not in license_text
assert "Test Author" in license_text
def test_init_homepage_validator():
responses = ['test_module_name',
'Test Author',
'test_email@example.com',
'www.uh-oh-spagghetti-o.com', # fails validation
'https://www.example.org', # passes
'4', # Skip - choose a license later
]
with TemporaryDirectory() as td, \
patch_data_dir(), \
faking_input(responses):
ti = init.TerminalIniter(td)
ti.initialise()
with Path(td, 'pyproject.toml').open() as f:
data = pytoml.load(f)
metadata = data['tool']['flit']['metadata']
assert metadata == {
'author': 'Test Author',
'author-email': 'test_email@example.com',
'home-page': 'https://www.example.org',
'module': 'test_module_name',
}
def test_init_homepage_and_license_are_optional():
responses = ['test_module_name',
'Test Author',
'test_email@example.com',
'', # Home page omitted
'4', # Skip - choose a license later
]
with TemporaryDirectory() as td, \
patch_data_dir(), \
faking_input(responses):
ti = init.TerminalIniter(td)
ti.initialise()
with Path(td, 'pyproject.toml').open() as f:
data = pytoml.load(f)
assert not Path(td, 'LICENSE').exists()
metadata = data['tool']['flit']['metadata']
assert metadata == {
'author': 'Test Author',
'author-email': 'test_email@example.com',
'module': 'test_module_name',
}
def test_prompt_options():
ti = init.TerminalIniter()
with faking_input(['4', '1']):
res = ti.prompt_options('Pick one', [('A', 'Apple'), ('B', 'Banana')])
assert res == 'A'
# Test with a default
with faking_input(['']):
res = ti.prompt_options('Pick one', [('A', 'Apple'), ('B', 'Banana')],
default='B')
assert res == 'B'
def test_author_email_field_is_optional():
responses = ['test_module_name',
'Test Author',
'', # Author-email field is skipped
'https://www.example.org',
'4',
]
with TemporaryDirectory() as td, \
patch_data_dir(), \
faking_input(responses):
ti = init.TerminalIniter(td)
ti.initialise()
with Path(td, 'pyproject.toml').open() as f:
data = pytoml.load(f)
assert not Path(td, 'LICENSE').exists()
metadata = data['tool']['flit']['metadata']
assert metadata == {
'author': 'Test Author',
'module': 'test_module_name',
'home-page': 'https://www.example.org',
}
main(args.ini_file, repository, formats=set(args.format or []),
gen_setup_py=args.setup_py)
elif args.subcmd == 'install':
from .install import Installer
try:
python = find_python_executable(args.python)
Installer.from_ini_path(args.ini_file, user=args.user, python=python,
symlink=args.symlink, deps=args.deps, extras=args.extras,
pth=args.pth_file).install()
except (ConfigError, PythonNotFoundError, common.NoDocstringError, common.NoVersionError) as e:
sys.exit(e.args[0])
elif args.subcmd == 'init':
from .init import TerminalIniter
TerminalIniter().initialise()
else:
ap.print_help()
sys.exit(1)
f.write(TEMPLATE.format(metadata=toml.dumps(metadata)))
print()
print("Written pyproject.toml; edit that file to add optional extra info.")
TEMPLATE = """\
[build-system]
requires = ["flit_core >=2,<4"]
build-backend = "flit_core.buildapi"
[tool.flit.metadata]
{metadata}
"""
if __name__ == '__main__':
TerminalIniter().initialise()