Repository: pypa/build
Pull request #197 (2020-12-07 11:51:58)

build: return the filename of the built dist from `build`


Patch:
diff --git a/src/build/__init__.py b/src/build/__init__.py
index e74b538..7672736 100644
--- a/src/build/__init__.py
+++ b/src/build/__init__.py
@@ -218,12 +218,13 @@ class ProjectBuilder(object):
         dependencies = self.get_dependencies(distribution).union(self.build_dependencies)
         return {u for d in dependencies for u in check_dependency(d)}
 
-    def build(self, distribution, outdir):  # type: (str, str) -> None
+    def build(self, distribution, outdir):  # type: (str, str) -> str
         """
         Build a distribution.
 
         :param distribution: Distribution to build (``sdist`` or ``wheel``)
         :param outdir: Output directory
+        :returns: The full path to the built distribution
         """
         build = getattr(self._hook, 'build_{}'.format(distribution))
         outdir = os.path.abspath(outdir)
@@ -236,7 +237,8 @@ class ProjectBuilder(object):
 
         try:
             with _working_directory(self.srcdir):
-                build(outdir, self.config_settings)
+                basename = build(outdir, self.config_settings)  # type: str
+                return os.path.join(outdir, basename)
         except pep517.wrappers.BackendUnavailable:
             raise BuildException("Backend '{}' is not available.".format(self._backend))
         except Exception as e:  # noqa: E722
diff --git a/src/build/__main__.py b/src/build/__main__.py
index 8091d00..88d5237 100644
--- a/src/build/__main__.py
+++ b/src/build/__main__.py
@@ -30,7 +30,7 @@ warnings.showwarning = _showwarning
 
 def _error(msg, code=1):  # type: (str, int) -> None  # pragma: no cover
     """
-    Prints an error message and exits. Will color the output when writting to a TTY.
+    Prints an error message and exits. Will color the output when writing to a TTY.
 
     :param msg: Error message
     :param code: Error code


Test patch:
diff --git a/tests/test_projectbuilder.py b/tests/test_projectbuilder.py
index b354a4d..b229a2f 100644
--- a/tests/test_projectbuilder.py
+++ b/tests/test_projectbuilder.py
@@ -228,14 +228,14 @@ def test_build(mocker, test_flit_path, tmp_dir):
 
     builder = build.ProjectBuilder(test_flit_path)
 
-    builder._hook.build_sdist.side_effect = [None, Exception]
-    builder._hook.build_wheel.side_effect = [None, Exception]
+    builder._hook.build_sdist.side_effect = ['dist.tar.gz', Exception]
+    builder._hook.build_wheel.side_effect = ['dist.whl', Exception]
 
-    builder.build('sdist', tmp_dir)
+    assert builder.build('sdist', tmp_dir) == os.path.join(tmp_dir, 'dist.tar.gz')
     builder._hook.build_sdist.assert_called_with(tmp_dir, {})
     build._working_directory.assert_called_with(test_flit_path)
 
-    builder.build('wheel', tmp_dir)
+    assert builder.build('wheel', tmp_dir) == os.path.join(tmp_dir, 'dist.whl')
     builder._hook.build_wheel.assert_called_with(tmp_dir, {})
     build._working_directory.assert_called_with(test_flit_path)
 
@@ -282,6 +282,7 @@ def test_missing_outdir(mocker, tmp_dir, test_flit_path):
     mocker.patch('pep517.wrappers.Pep517HookCaller', autospec=True)
 
     builder = build.ProjectBuilder(test_flit_path)
+    builder._hook.build_sdist.return_value = 'dist.tar.gz'
     out = os.path.join(tmp_dir, 'out')
 
     builder.build('sdist', out)
@@ -293,6 +294,7 @@ def test_relative_outdir(mocker, tmp_dir, test_flit_path):
     mocker.patch('pep517.wrappers.Pep517HookCaller', autospec=True)
 
     builder = build.ProjectBuilder(test_flit_path)
+    builder._hook.build_sdist.return_value = 'dist.tar.gz'
 
     builder.build('sdist', '.')
 
@@ -303,6 +305,7 @@ def test_not_dir_outdir(mocker, tmp_dir, test_flit_path):
     mocker.patch('pep517.wrappers.Pep517HookCaller', autospec=True)
 
     builder = build.ProjectBuilder(test_flit_path)
+    builder._hook.build_sdist.return_value = 'dist.tar.gz'
     out = os.path.join(tmp_dir, 'out')
 
     open(out, 'a').close()  # create empty file