Skip to content

Commit 02b4f7b

Browse files
committed
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.
1 parent 3c9dcbc commit 02b4f7b

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

sphinx_multiversion/sphinx.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ def apathto(self, build_target_name, build_target):
157157
"""Find the path to the artefact identified by build_target_name
158158
and build_target.
159159
"""
160-
artefact_dir = "artefacts"
160+
current_version = self.metadata[self.current_version_name]
161+
current_outputroot = os.path.abspath(current_version["outputdir"])
162+
artefact_dir = posixpath.join(current_outputroot, "artefacts")
163+
current_outputdir = posixpath.dirname(
164+
posixpath.join(current_outputroot, self.context["pagename"])
165+
)
161166

162167
filename = "{project}_docs-{version}".format(
163168
project=self.app.config.project.replace(" ", ""),
@@ -175,8 +180,9 @@ def apathto(self, build_target_name, build_target):
175180
f=filename,
176181
extension=build_target["download_format"],
177182
)
178-
artefact_path = posixpath.join(artefact_dir, filename)
179-
183+
artefact_path = posixpath.relpath(
184+
posixpath.join(artefact_dir, filename), start=current_outputdir
185+
)
180186
return artefact_path
181187

182188

tests/test_sphinx.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,48 @@ def test_apathto(self):
160160
)
161161
self.assertEqual(
162162
self.versioninfo.apathto("PDF", build_targets["PDF"]),
163-
"artefacts/example_docs-master.pdf",
163+
posixpath.join("artefacts", "example_docs-master.pdf"),
164164
)
165165

166-
mock_versioninfo = self.versioninfo
167-
mock_versioninfo.current_version_name = "branch-with/slash"
166+
self.versioninfo.context["pagename"] = "appendix/faq"
167+
self.assertEqual(
168+
self.versioninfo.apathto("PDF", build_targets["PDF"]),
169+
posixpath.join("..", "artefacts", "example_docs-master.pdf"),
170+
)
168171

169-
self.versioninfo = Mock()
170-
self.versioninfo = mock_versioninfo
172+
self.versioninfo.context["pagename"] = "testpage"
173+
self.versioninfo.current_version_name = "branch-with/slash"
174+
# mock_versioninfo = self.versioninfo
175+
# mock_versioninfo.current_version_name = "branch-with/slash"
176+
#
177+
# self.versioninfo = Mock()
178+
# self.versioninfo = mock_versioninfo
179+
self.assertEqual(
180+
self.versioninfo.apathto("PDF", build_targets["PDF"]),
181+
posixpath.join("artefacts", "example_docs-branch-with-slash.pdf"),
182+
)
183+
self.assertEqual(
184+
self.versioninfo.apathto("HTML", build_targets["HTML"]),
185+
posixpath.join(
186+
"artefacts", "example_docs-branch-with-slash-HTML.zip"
187+
),
188+
)
189+
190+
self.versioninfo.app.config.project = (
191+
"Project Name with Spaces and VaRiAbLe case"
192+
)
193+
self.versioninfo.current_version_name = "master"
194+
self.assertEqual(
195+
self.versioninfo.apathto("HTML", build_targets["HTML"]),
196+
posixpath.join(
197+
"artefacts",
198+
"ProjectNamewithSpacesandVaRiAbLecase_docs-master-HTML.zip",
199+
),
200+
)
171201
self.assertEqual(
172202
self.versioninfo.apathto("PDF", build_targets["PDF"]),
173-
"artefacts/example_docs-branch-with-slash.pdf",
203+
posixpath.join(
204+
"artefacts",
205+
"ProjectNamewithSpacesandVaRiAbLecase_docs-master.pdf",
206+
),
174207
)

0 commit comments

Comments
 (0)