Skip to content

Commit

Permalink
Add support to locate artefacts from nested paths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
samuel-emrys committed Jun 5, 2021
1 parent 3c9dcbc commit 02b4f7b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
12 changes: 9 additions & 3 deletions sphinx_multiversion/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ", ""),
Expand All @@ -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


Expand Down
45 changes: 39 additions & 6 deletions tests/test_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
)

0 comments on commit 02b4f7b

Please sign in to comment.