Skip to content

Commit 91c321d

Browse files
authored
Merge pull request #35 from Holzhaus/unittest
Add unittests and fix path issue on Windows
2 parents cc23360 + 0854eee commit 91c321d

File tree

5 files changed

+204
-71
lines changed

5 files changed

+204
-71
lines changed

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,16 @@ repos:
2626
files: ^(\.pre-commit/version_check\.py|setup\.py|sphinx_multiversion/__init__\.py|docs/conf\.py|docs/changelog\.rst)$
2727
additional_dependencies:
2828
- sphinx
29+
- id: unittest
30+
name: unittest
31+
description: "Run unittests"
32+
entry: python -m unittest discover
33+
pass_filenames: false
34+
language: python
35+
types:
36+
- python
37+
stages:
38+
- commit
39+
- manual
40+
additional_dependencies:
41+
- sphinx

.travis.yml

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ language: python
55
jobs:
66
include:
77
- name: "Ubuntu / Python 3.6"
8+
stage: Testing
89
python: 3.6
910
- name: "Ubuntu / Python 3.7"
11+
stage: Testing
1012
python: 3.7
1113
- name: "Windows / Python 3.6"
14+
stage: Testing
1215
os: windows
1316
language: shell
1417
before_install:
@@ -17,13 +20,61 @@ jobs:
1720
- PATH=/c/Python36:/c/Python36/Scripts:$PATH
1821
- SKIP=check-executables-have-shebangs
1922
- name: "Windows / Python 3.7"
23+
stage: Testing
2024
os: windows
2125
language: shell
2226
before_install:
2327
- choco install python --version 3.7.8
2428
env:
2529
- PATH=/c/Python37:/c/Python37/Scripts:$PATH
2630
- SKIP=check-executables-have-shebangs
31+
- name: "Ubuntu / Python 3.7"
32+
stage: Deployment
33+
if: type != pull_request AND (branch = master OR tag IS present)
34+
python: 3.7
35+
before_deploy:
36+
- touch html/.nojekyll
37+
- cp assets/gh-pages-redirect.html html/index.html
38+
deploy:
39+
# Deploy to PyPI
40+
- provider: pypi
41+
username: "__token__"
42+
password:
43+
secure: "0bFhY80KT5zp5wpsQwmBdHNvrC1aXnCUnL268UBb5bcVrTQ/cVr08CPVEZLefRU96CCqCZHCQPh1iP9DCtPEszB3XI0pVydoNJ9Fei9ccHI6bJBtfpLnTwlyygiBiTT3uNlal+/+videWqhDXAs2EHZAQn4L41Na6h1pHBoTu+V8KIJinNJccgiUXeeHdDc4lWCVKU0GfeKtzvgmFZq9B1w7jo+VnOQFKYH3rS80u1xkTrWNiVb3vdt+36PHssNXv5XI9kULsE+ofSdKIwYd7zGxs3ouw+LYhEM6QpDp1ERITAPWhdrY6DTTVdatnClTlzvxRMiqbeKmx/cYIx1x2aXqGxQo0UIcjK2N647ZG6lQAmJnRBc71CJvzGOFQQ+PNnlRlUcOmhnqr8K4bHDhe6Cf1PAFW6SCZvZ8ruXRV726FhRqMzmE/fJwtOY97F0BHJWwBAjbTdDM0J9Hph68YjkMcB96/bRTvGqkwVZTVqPtGppa3uRX2+ljt9pFY3j/jsk77mTYoY5VJrDB8lQSK7dzn8U/vg6q7lbRA5QfYXVIc77/7w29BFgVKv418LlfEh9zlq2/mB0+EX9BGEtEFNMEDzmXvJpEMQ2isDhfACKLO6B8opkfNEgUKhEpUBOTKf0VNSTVdQy1VL+jnXk7SzHo6ncb7PCMMK/yWmlCsvc="
44+
distributions: "sdist bdist_wheel"
45+
skip_existing: true
46+
on:
47+
branch: master
48+
repo: Holzhaus/sphinx-multiversion
49+
tags: true
50+
# Deploy to Github Releases
51+
- provider: releases
52+
token:
53+
secure: "tAwp3NTqbvig1wU0hwkN7aWOAQYnpS12zsAlBc/ud05cYrjrBIjjFltX/1C0tgxxxJ11zdAhTjgUfYFrWbRXJ5hHptyD8fTa8RXfJZ2KuTx44aEHSyNHtl8YbwlXXr9hGjhrUHUulNghpFCAwNgg6oONbocQ3teW6maO7ByLUmO/RbNnligK1iQW6GPHovOg1iTbsZLVhriP2e2/JFRxq5rdnxArNLzwLhg8L4QAuc6YX3gnI1Z2xuBtpgQhNfL5S+hMuKOwONpdMQDYgXD7QbPA0pqSYQRf81vd+/FtZqNnHHku0POsoLLMkscalXxa1AbznvH9vg9nwO0J20XNZ1Mg6BFLpDwPzqLhCAwmeFcz+k78m+3x9bIhS27ps0XCgg0VxjM4Rvaabfm7J9h1aIteZf4oGS+1Er8fK6ejOeGI+tZu/5YXDhpS6hyK6gXey7Ep3SGF5oqrcu80gBrCGkWXrADMogarL6RbcZV2vrYkm5kBorcUA4s3nU2fV5UUnCg83fdlY70kmrNt3Im83ndSH3jOyTDXITjHCDN6+GFjd1eP0h8tcwiGPtVs2JGolSmqq/AzjDqY2CfgwccBDg6MF4gbcgJQdzZ11j/5cH9SNoHN6/V4869vO28XbwTKqbgsKpuajmbKVEqIlGTi2zylgzNbxtySTL0tnZvyO4w="
54+
name: "$TRAVIS_TAG"
55+
release_notes: "Release $TRAVIS_TAG"
56+
draft: false
57+
skip_cleanup: true
58+
file_glob: true
59+
file: dist/*
60+
overwrite: true
61+
on:
62+
branch: master
63+
repo: Holzhaus/sphinx-multiversion
64+
tags: true
65+
# Deploy documentation
66+
- provider: pages
67+
skip_cleanup: true
68+
keep_history: false
69+
token: $GITHUB_TOKEN
70+
local_dir: html
71+
on:
72+
branch: master
73+
repo: Holzhaus/sphinx-multiversion
74+
75+
stages:
76+
- Testing
77+
- Deployment
2778

2879
cache:
2980
pip: true
@@ -35,57 +86,11 @@ install:
3586

3687
before_script:
3788
- pre-commit run --all-files --show-diff-on-failure
89+
- python -m unittest --verbose
90+
3891
script:
3992
- mkdir html
4093
- git fetch --all
4194
- sphinx-multiversion -W docs html
4295
- python setup.py build sdist bdist_wheel
4396

44-
# Deployment
45-
before_deploy:
46-
- touch html/.nojekyll
47-
- cp assets/gh-pages-redirect.html html/index.html
48-
deploy:
49-
# Deploy documentation
50-
- provider: pages
51-
skip_cleanup: true
52-
keep_history: false
53-
token: $GITHUB_TOKEN
54-
local_dir: html
55-
on:
56-
branch: master
57-
repo: Holzhaus/sphinx-multiversion
58-
python: 3.7
59-
os: linux
60-
61-
# Deploy to PyPI
62-
- deploy:
63-
provider: pypi
64-
username: "__token__"
65-
password:
66-
secure: "0bFhY80KT5zp5wpsQwmBdHNvrC1aXnCUnL268UBb5bcVrTQ/cVr08CPVEZLefRU96CCqCZHCQPh1iP9DCtPEszB3XI0pVydoNJ9Fei9ccHI6bJBtfpLnTwlyygiBiTT3uNlal+/+videWqhDXAs2EHZAQn4L41Na6h1pHBoTu+V8KIJinNJccgiUXeeHdDc4lWCVKU0GfeKtzvgmFZq9B1w7jo+VnOQFKYH3rS80u1xkTrWNiVb3vdt+36PHssNXv5XI9kULsE+ofSdKIwYd7zGxs3ouw+LYhEM6QpDp1ERITAPWhdrY6DTTVdatnClTlzvxRMiqbeKmx/cYIx1x2aXqGxQo0UIcjK2N647ZG6lQAmJnRBc71CJvzGOFQQ+PNnlRlUcOmhnqr8K4bHDhe6Cf1PAFW6SCZvZ8ruXRV726FhRqMzmE/fJwtOY97F0BHJWwBAjbTdDM0J9Hph68YjkMcB96/bRTvGqkwVZTVqPtGppa3uRX2+ljt9pFY3j/jsk77mTYoY5VJrDB8lQSK7dzn8U/vg6q7lbRA5QfYXVIc77/7w29BFgVKv418LlfEh9zlq2/mB0+EX9BGEtEFNMEDzmXvJpEMQ2isDhfACKLO6B8opkfNEgUKhEpUBOTKf0VNSTVdQy1VL+jnXk7SzHo6ncb7PCMMK/yWmlCsvc="
67-
distributions: "sdist bdist_wheel"
68-
skip_existing: true
69-
on:
70-
branch: master
71-
repo: Holzhaus/sphinx-multiversion
72-
tags: true
73-
os: linux
74-
75-
# Deploy to Github Releases
76-
- provider: releases
77-
token:
78-
secure: "tAwp3NTqbvig1wU0hwkN7aWOAQYnpS12zsAlBc/ud05cYrjrBIjjFltX/1C0tgxxxJ11zdAhTjgUfYFrWbRXJ5hHptyD8fTa8RXfJZ2KuTx44aEHSyNHtl8YbwlXXr9hGjhrUHUulNghpFCAwNgg6oONbocQ3teW6maO7ByLUmO/RbNnligK1iQW6GPHovOg1iTbsZLVhriP2e2/JFRxq5rdnxArNLzwLhg8L4QAuc6YX3gnI1Z2xuBtpgQhNfL5S+hMuKOwONpdMQDYgXD7QbPA0pqSYQRf81vd+/FtZqNnHHku0POsoLLMkscalXxa1AbznvH9vg9nwO0J20XNZ1Mg6BFLpDwPzqLhCAwmeFcz+k78m+3x9bIhS27ps0XCgg0VxjM4Rvaabfm7J9h1aIteZf4oGS+1Er8fK6ejOeGI+tZu/5YXDhpS6hyK6gXey7Ep3SGF5oqrcu80gBrCGkWXrADMogarL6RbcZV2vrYkm5kBorcUA4s3nU2fV5UUnCg83fdlY70kmrNt3Im83ndSH3jOyTDXITjHCDN6+GFjd1eP0h8tcwiGPtVs2JGolSmqq/AzjDqY2CfgwccBDg6MF4gbcgJQdzZ11j/5cH9SNoHN6/V4869vO28XbwTKqbgsKpuajmbKVEqIlGTi2zylgzNbxtySTL0tnZvyO4w="
79-
name: "$TRAVIS_TAG"
80-
release_notes: "Release $TRAVIS_TAG"
81-
draft: false
82-
skip_cleanup: true
83-
file_glob: true
84-
file: dist/*
85-
overwrite: true
86-
on:
87-
branch: master
88-
repo: Holzhaus/sphinx-multiversion
89-
tags: true
90-
python: 3.7
91-
os: linux

sphinx_multiversion/sphinx.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import datetime
33
import json
4-
import pathlib
54
import collections
65
import logging
76
import os
@@ -97,39 +96,39 @@ def vpathto(self, other_version_name):
9796
posixpath.split(self.context["pagename"])[-1]
9897
)
9998

100-
# Find output root
99+
# Find relative outputdir paths from common output root
101100
current_version = self.metadata[self.current_version_name]
102101
other_version = self.metadata[other_version_name]
103-
outputroot = os.path.commonpath(
104-
(current_version["outputdir"], other_version["outputdir"])
102+
103+
current_outputroot = os.path.abspath(current_version["outputdir"])
104+
other_outputroot = os.path.abspath(other_version["outputdir"])
105+
outputroot = os.path.commonpath((current_outputroot, other_outputroot))
106+
107+
current_outputroot = os.path.relpath(
108+
current_outputroot, start=outputroot
105109
)
110+
other_outputroot = os.path.relpath(other_outputroot, start=outputroot)
106111

107-
current_outputroot = pathlib.PurePath(
108-
current_version["outputdir"]
109-
).relative_to(outputroot)
110-
other_outputroot = pathlib.PurePath(
111-
other_version["outputdir"]
112-
).relative_to(outputroot)
113-
114-
relative_path_to_outputroot = os.path.join(
115-
*(
116-
".."
117-
for x in current_outputroot.joinpath(
118-
self.context["pagename"]
119-
).parent.parts
112+
# Ensure that we use POSIX separators in the path (for the HTML code)
113+
if os.sep != posixpath.sep:
114+
current_outputroot = posixpath.join(
115+
*os.path.split(current_outputroot)
120116
)
121-
)
117+
other_outputroot = posixpath.join(*os.path.split(other_outputroot))
122118

123-
# Find output dir of other version
124-
outputdir = posixpath.join(
125-
relative_path_to_outputroot, other_outputroot
119+
# Find relative path to root of other_version's outputdir
120+
current_outputdir = posixpath.dirname(
121+
posixpath.join(current_outputroot, self.context["pagename"])
122+
)
123+
other_outputdir = posixpath.relpath(
124+
other_outputroot, start=current_outputdir
126125
)
127126

128127
if not self.vhasdoc(other_version_name):
129-
return posixpath.join(outputdir, "index.html")
128+
return posixpath.join(other_outputdir, "index.html")
130129

131130
return posixpath.join(
132-
outputdir, "{}.html".format(self.context["pagename"])
131+
other_outputdir, "{}.html".format(self.context["pagename"])
133132
)
134133

135134

tests/__init__.py

Whitespace-only changes.

tests/test_sphinx.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import os.path
2+
import posixpath
3+
import tempfile
4+
import unittest
5+
6+
import sphinx_multiversion
7+
8+
9+
class VersionInfoTestCase(unittest.TestCase):
10+
def setUp(self):
11+
root = tempfile.gettempdir()
12+
13+
self.versioninfo = sphinx_multiversion.sphinx.VersionInfo(
14+
app=None,
15+
context={"pagename": "testpage"},
16+
metadata={
17+
"master": {
18+
"name": "master",
19+
"version": "",
20+
"release": "0.2",
21+
"is_released": False,
22+
"source": "heads",
23+
"creatordate": "2020-08-07 07:45:20 -0700",
24+
"basedir": os.path.join(root, "master"),
25+
"sourcedir": os.path.join(root, "master", "docs"),
26+
"outputdir": os.path.join(root, "build", "html", "master"),
27+
"confdir": os.path.join(root, "master", "docs"),
28+
"docnames": ["testpage", "appendix/faq"],
29+
},
30+
"v0.1.0": {
31+
"name": "v0.1.0",
32+
"version": "",
33+
"release": "0.1.0",
34+
"is_released": True,
35+
"source": "tags",
36+
"creatordate": "2020-07-16 08:45:20 -0100",
37+
"basedir": os.path.join(root, "v0.1.0"),
38+
"sourcedir": os.path.join(root, "v0.1.0", "docs"),
39+
"outputdir": os.path.join(root, "build", "html", "v0.1.0"),
40+
"confdir": os.path.join(root, "v0.1.0", "docs"),
41+
"docnames": ["old_testpage", "appendix/faq"],
42+
},
43+
"branch-with/slash": {
44+
"name": "branch-with/slash",
45+
"version": "",
46+
"release": "0.1.1",
47+
"is_released": False,
48+
"source": "heads",
49+
"creatordate": "2020-08-06 11:53:06 -0400",
50+
"basedir": os.path.join(root, "branch-with/slash"),
51+
"sourcedir": os.path.join(
52+
root, "branch-with/slash", "docs"
53+
),
54+
"outputdir": os.path.join(
55+
root, "build", "html", "branch-with/slash"
56+
),
57+
"confdir": os.path.join(root, "branch-with/slash", "docs"),
58+
"docnames": ["testpage"],
59+
},
60+
},
61+
current_version_name="master",
62+
)
63+
64+
def test_tags_property(self):
65+
versions = self.versioninfo.tags
66+
self.assertEqual([version.name for version in versions], ["v0.1.0"])
67+
68+
def test_branches_property(self):
69+
versions = self.versioninfo.branches
70+
self.assertEqual(
71+
[version.name for version in versions],
72+
["master", "branch-with/slash"],
73+
)
74+
75+
def test_releases_property(self):
76+
versions = self.versioninfo.releases
77+
self.assertEqual([version.name for version in versions], ["v0.1.0"])
78+
79+
def test_in_development_property(self):
80+
versions = self.versioninfo.in_development
81+
self.assertEqual(
82+
[version.name for version in versions],
83+
["master", "branch-with/slash"],
84+
)
85+
86+
def test_vhasdoc(self):
87+
self.assertTrue(self.versioninfo.vhasdoc("master"))
88+
self.assertFalse(self.versioninfo.vhasdoc("v0.1.0"))
89+
self.assertTrue(self.versioninfo.vhasdoc("branch-with/slash"))
90+
91+
self.versioninfo.context["pagename"] = "appendix/faq"
92+
self.assertTrue(self.versioninfo.vhasdoc("master"))
93+
self.assertTrue(self.versioninfo.vhasdoc("v0.1.0"))
94+
self.assertFalse(self.versioninfo.vhasdoc("branch-with/slash"))
95+
96+
def test_vpathto(self):
97+
self.assertEqual(self.versioninfo.vpathto("master"), "testpage.html")
98+
self.assertEqual(
99+
self.versioninfo.vpathto("v0.1.0"),
100+
posixpath.join("..", "v0.1.0", "index.html"),
101+
)
102+
self.assertEqual(
103+
self.versioninfo.vpathto("branch-with/slash"),
104+
posixpath.join("..", "branch-with/slash", "testpage.html"),
105+
)
106+
107+
self.versioninfo.context["pagename"] = "appendix/faq"
108+
self.assertEqual(self.versioninfo.vpathto("master"), "faq.html")
109+
self.assertEqual(
110+
self.versioninfo.vpathto("v0.1.0"),
111+
posixpath.join("..", "..", "v0.1.0", "appendix", "faq.html"),
112+
)
113+
self.assertEqual(
114+
self.versioninfo.vpathto("branch-with/slash"),
115+
posixpath.join("..", "..", "branch-with/slash", "index.html"),
116+
)

0 commit comments

Comments
 (0)