Skip to content

Commit

Permalink
better validator for home-page field
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Nov 3, 2018
1 parent e4d5833 commit 23ef400
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
16 changes: 10 additions & 6 deletions flit/init.py
Expand Up @@ -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():
Expand All @@ -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}
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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'))

Expand All @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_init.py
Expand Up @@ -5,6 +5,8 @@
from testpath import assert_isfile
from unittest.mock import patch

import pytoml

from flit import init


Expand Down Expand Up @@ -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',
}

0 comments on commit 23ef400

Please sign in to comment.