Repository: takluyver/flit
Pull request #192 (2018-07-17 02:46:12)

Catch the NoDocstringError when building a package without docstring.
Closes https://github.com/takluyver/flit/issues/174


Patch:
diff --git a/flit/__init__.py b/flit/__init__.py
index c3535a1..f806c89 100644
--- a/flit/__init__.py
+++ b/flit/__init__.py
@@ -98,7 +98,10 @@ def main(argv=None):
 
     if args.subcmd == 'build':
         from .build import main
-        main(args.ini_file, formats=set(args.format or []))
+        try:
+            main(args.ini_file, formats=set(args.format or []))
+        except(common.NoDocstringError) as e:
+            sys.exit(e.args[0])
     elif args.subcmd == 'publish':
         from .upload import main
         main(args.ini_file, args.repository, formats=set(args.format or []))
diff --git a/flit/common.py b/flit/common.py
index b73ebc9..f63a9d9 100644
--- a/flit/common.py
+++ b/flit/common.py
@@ -111,8 +111,8 @@ def get_info_from_module(target):
         docstring, version = get_docstring_and_version_via_import(target)
 
     if (not docstring) or not docstring.strip():
-        raise NoDocstringError('Cannot package module without docstring, or empty docstring. '
-                                'Please add a docstring to your module.')
+        raise NoDocstringError('Flit cannot package module without docstring, or empty docstring. '
+                               'Please add a docstring to your module.')
 
 
     version = check_version(version)


Test patch:
diff --git a/tests/samples/no_docstring-pkg.toml b/tests/samples/no_docstring-pkg.toml
new file mode 100644
index 0000000..b68827f
--- /dev/null
+++ b/tests/samples/no_docstring-pkg.toml
@@ -0,0 +1,12 @@
+[build-system]
+requires = ["flit"]
+
+[tool.flit.metadata]
+module = "no_docstring"
+author = "Sir Robin"
+author-email = "robin@camelot.uk"
+home-page = "http://github.com/sirrobin/no_docstring"
+description-file = "EG_README.rst"
+
+[tool.flit.metadata.urls]
+Documentation = "https://example.com/no_docstring"
diff --git a/tests/samples/no_docstring.py b/tests/samples/no_docstring.py
new file mode 100644
index 0000000..29524eb
--- /dev/null
+++ b/tests/samples/no_docstring.py
@@ -0,0 +1,1 @@
+__version__ = '7.0'
diff --git a/tests/test_build.py b/tests/test_build.py
index 552bd2a..80ef1ce 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -1,4 +1,5 @@
 from pathlib import Path
+import pytest
 import shutil
 import sys
 from tempfile import TemporaryDirectory
@@ -8,15 +9,15 @@ from flit import build
 
 samples_dir = Path(__file__).parent / 'samples'
 
-LIST_FILES = """\
+LIST_FILES_TEMPLATE = """\
 #!{python}
 import sys
 from os.path import join
 if '--deleted' not in sys.argv:
     print('pyproject.toml')
-    print('module1.py')
+    print('{module}')
     print('EG_README.rst')
-""".format(python=sys.executable)
+"""
 
 def test_build_main():
     with TemporaryDirectory() as td:
@@ -26,9 +27,28 @@ def test_build_main():
         shutil.copy(str(samples_dir / 'EG_README.rst'), td)
         Path(td, '.git').mkdir()   # Fake a git repo
 
-        with MockCommand('git', LIST_FILES):
+        with MockCommand('git', LIST_FILES_TEMPLATE.format(
+                python=sys.executable, module='module1.py')):
             res = build.main(pyproject)
         assert res.wheel.file.suffix == '.whl'
         assert res.sdist.file.name.endswith('.tar.gz')
 
         assert_isdir(Path(td, 'dist'))
+
+
+def test_build_module_no_docstring():
+    with TemporaryDirectory() as td:
+        pyproject = Path(td, 'pyproject.toml')
+        shutil.copy(str(samples_dir / 'no_docstring-pkg.toml'), str(pyproject))
+        shutil.copy(str(samples_dir / 'no_docstring.py'), td)
+        shutil.copy(str(samples_dir / 'EG_README.rst'), td)
+        Path(td, '.git').mkdir()   # Fake a git repo
+
+
+        with MockCommand('git', LIST_FILES_TEMPLATE.format(
+                python=sys.executable, module='no_docstring.py')):
+            with pytest.raises(ValueError) as exc_info:
+                build.main(pyproject)
+            assert str(
+                exc_info.value) == ('Flit cannot package module without docstring, or empty docstring. '
+                                    'Please add a docstring to your module.')