From 02b4f7b38df44be4dddb68fac5595f3abdafa313 Mon Sep 17 00:00:00 2001 From: Samuel Dowling Date: Sat, 5 Jun 2021 13:13:37 +0930 Subject: [PATCH] Add support to locate artefacts from nested paths Trying to access a download artefact from a page other than the root would fail to resolve the correct artefact path. This change adds support to resolve the correct path from any page the user is located on. Add unit tests to confirm this feature works as expected, and also to ensure artefacts are found correctly when the project name contains spaces and variable casing. --- sphinx_multiversion/sphinx.py | 12 +++++++--- tests/test_sphinx.py | 45 ++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/sphinx_multiversion/sphinx.py b/sphinx_multiversion/sphinx.py index ad14f0c4..610f25f1 100644 --- a/sphinx_multiversion/sphinx.py +++ b/sphinx_multiversion/sphinx.py @@ -157,7 +157,12 @@ def apathto(self, build_target_name, build_target): """Find the path to the artefact identified by build_target_name and build_target. """ - artefact_dir = "artefacts" + current_version = self.metadata[self.current_version_name] + current_outputroot = os.path.abspath(current_version["outputdir"]) + artefact_dir = posixpath.join(current_outputroot, "artefacts") + current_outputdir = posixpath.dirname( + posixpath.join(current_outputroot, self.context["pagename"]) + ) filename = "{project}_docs-{version}".format( project=self.app.config.project.replace(" ", ""), @@ -175,8 +180,9 @@ def apathto(self, build_target_name, build_target): f=filename, extension=build_target["download_format"], ) - artefact_path = posixpath.join(artefact_dir, filename) - + artefact_path = posixpath.relpath( + posixpath.join(artefact_dir, filename), start=current_outputdir + ) return artefact_path diff --git a/tests/test_sphinx.py b/tests/test_sphinx.py index 04024018..84f3618c 100644 --- a/tests/test_sphinx.py +++ b/tests/test_sphinx.py @@ -160,15 +160,48 @@ def test_apathto(self): ) self.assertEqual( self.versioninfo.apathto("PDF", build_targets["PDF"]), - "artefacts/example_docs-master.pdf", + posixpath.join("artefacts", "example_docs-master.pdf"), ) - mock_versioninfo = self.versioninfo - mock_versioninfo.current_version_name = "branch-with/slash" + self.versioninfo.context["pagename"] = "appendix/faq" + self.assertEqual( + self.versioninfo.apathto("PDF", build_targets["PDF"]), + posixpath.join("..", "artefacts", "example_docs-master.pdf"), + ) - self.versioninfo = Mock() - self.versioninfo = mock_versioninfo + self.versioninfo.context["pagename"] = "testpage" + self.versioninfo.current_version_name = "branch-with/slash" + # mock_versioninfo = self.versioninfo + # mock_versioninfo.current_version_name = "branch-with/slash" + # + # self.versioninfo = Mock() + # self.versioninfo = mock_versioninfo + self.assertEqual( + self.versioninfo.apathto("PDF", build_targets["PDF"]), + posixpath.join("artefacts", "example_docs-branch-with-slash.pdf"), + ) + self.assertEqual( + self.versioninfo.apathto("HTML", build_targets["HTML"]), + posixpath.join( + "artefacts", "example_docs-branch-with-slash-HTML.zip" + ), + ) + + self.versioninfo.app.config.project = ( + "Project Name with Spaces and VaRiAbLe case" + ) + self.versioninfo.current_version_name = "master" + self.assertEqual( + self.versioninfo.apathto("HTML", build_targets["HTML"]), + posixpath.join( + "artefacts", + "ProjectNamewithSpacesandVaRiAbLecase_docs-master-HTML.zip", + ), + ) self.assertEqual( self.versioninfo.apathto("PDF", build_targets["PDF"]), - "artefacts/example_docs-branch-with-slash.pdf", + posixpath.join( + "artefacts", + "ProjectNamewithSpacesandVaRiAbLecase_docs-master.pdf", + ), )