diff --git a/flit/init.py b/flit/init.py index 794c9645..d4b7ee3d 100644 --- a/flit/init.py +++ b/flit/init.py @@ -67,6 +67,9 @@ def validate_email(self, s): # Properly validating an email address is much more complex return bool(re.match(r'.+@.+', s)) + def validate_homepage(self, s): + return not s or s.startswith('http://') or s.startswith('https://') + def guess_module_name(self): packages, modules = [], [] for p in self.directory.iterdir(): @@ -87,7 +90,7 @@ def guess_module_name(self): return modules[0] else: return None - + def update_defaults(self, author, author_email, module, home_page, license): new_defaults = {'author': author, 'author_email': author_email, 'license': license} @@ -111,7 +114,7 @@ def write_license(self, name, author): class TerminalIniter(IniterBase): - def prompt_text(self, prompt, default, validator): + def prompt_text(self, prompt, default, validator, retry_msg="Try again."): if default is not None: p = "{} [{}]: ".format(prompt, default) else: @@ -123,7 +126,7 @@ def prompt_text(self, prompt, default, validator): if validator(response): return response - print("Try again.") + print(retry_msg) def prompt_options(self, prompt, options, default=None): default_ix = None @@ -165,8 +168,8 @@ def initialise(self): '{modulename}', module) else: home_page_default = None - home_page = self.prompt_text('Home page', home_page_default, - lambda s: s != '') + home_page = self.prompt_text('Home page', home_page_default, self.validate_homepage, + retry_msg="Should start with http:// or https:// - try again.") license = self.prompt_options('Choose a license (see http://choosealicense.com/ for more info)', license_choices, self.defaults.get('license')) @@ -177,8 +180,9 @@ def initialise(self): ('module', module), ('author', author), ('author-email', author_email), - ('home-page', home_page), ]) + if home_page: + metadata['home-page'] = home_page if license != 'skip': metadata['classifiers'] = [license_names_to_classifiers[license]] self.write_license(license, author) diff --git a/tests/test_init.py b/tests/test_init.py index 8c685e69..eead8a27 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -5,6 +5,8 @@ from testpath import assert_isfile from unittest.mock import patch +import pytoml + from flit import init @@ -89,3 +91,48 @@ def test_init(): ti.initialise() assert_isfile('pyproject.toml') assert_isfile('LICENSE') + +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_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', + }