فهرست منبع

Merge pull request #35 from Holzhaus/unittest

Add unittests and fix path issue on Windows
Jan Holthuis 4 سال پیش
والد
کامیت
91c321d69c
5فایلهای تغییر یافته به همراه204 افزوده شده و 71 حذف شده
  1. 13 0
      .pre-commit-config.yaml
  2. 53 48
      .travis.yml
  3. 22 23
      sphinx_multiversion/sphinx.py
  4. 0 0
      tests/__init__.py
  5. 116 0
      tests/test_sphinx.py

+ 13 - 0
.pre-commit-config.yaml

@@ -26,3 +26,16 @@ repos:
     files: ^(\.pre-commit/version_check\.py|setup\.py|sphinx_multiversion/__init__\.py|docs/conf\.py|docs/changelog\.rst)$
     additional_dependencies:
     - sphinx
+  - id: unittest
+    name: unittest
+    description: "Run unittests"
+    entry: python -m unittest discover
+    pass_filenames: false
+    language: python
+    types:
+    - python
+    stages:
+    - commit
+    - manual
+    additional_dependencies:
+    - sphinx

+ 53 - 48
.travis.yml

@@ -5,10 +5,13 @@ language: python
 jobs:
   include:
     - name: "Ubuntu / Python 3.6"
+      stage: Testing
       python: 3.6
     - name: "Ubuntu / Python 3.7"
+      stage: Testing
       python: 3.7
     - name: "Windows / Python 3.6"
+      stage: Testing
       os: windows
       language: shell
       before_install:
@@ -17,6 +20,7 @@ jobs:
       - PATH=/c/Python36:/c/Python36/Scripts:$PATH
       - SKIP=check-executables-have-shebangs
     - name: "Windows / Python 3.7"
+      stage: Testing
       os: windows
       language: shell
       before_install:
@@ -24,6 +28,53 @@ jobs:
       env:
       - PATH=/c/Python37:/c/Python37/Scripts:$PATH
       - SKIP=check-executables-have-shebangs
+    - name: "Ubuntu / Python 3.7"
+      stage: Deployment
+      if: type != pull_request AND (branch = master OR tag IS present)
+      python: 3.7
+      before_deploy:
+      - touch html/.nojekyll
+      - cp assets/gh-pages-redirect.html html/index.html
+      deploy:
+        # Deploy to PyPI
+      - provider: pypi
+        username: "__token__"
+        password:
+          secure: "0bFhY80KT5zp5wpsQwmBdHNvrC1aXnCUnL268UBb5bcVrTQ/cVr08CPVEZLefRU96CCqCZHCQPh1iP9DCtPEszB3XI0pVydoNJ9Fei9ccHI6bJBtfpLnTwlyygiBiTT3uNlal+/+videWqhDXAs2EHZAQn4L41Na6h1pHBoTu+V8KIJinNJccgiUXeeHdDc4lWCVKU0GfeKtzvgmFZq9B1w7jo+VnOQFKYH3rS80u1xkTrWNiVb3vdt+36PHssNXv5XI9kULsE+ofSdKIwYd7zGxs3ouw+LYhEM6QpDp1ERITAPWhdrY6DTTVdatnClTlzvxRMiqbeKmx/cYIx1x2aXqGxQo0UIcjK2N647ZG6lQAmJnRBc71CJvzGOFQQ+PNnlRlUcOmhnqr8K4bHDhe6Cf1PAFW6SCZvZ8ruXRV726FhRqMzmE/fJwtOY97F0BHJWwBAjbTdDM0J9Hph68YjkMcB96/bRTvGqkwVZTVqPtGppa3uRX2+ljt9pFY3j/jsk77mTYoY5VJrDB8lQSK7dzn8U/vg6q7lbRA5QfYXVIc77/7w29BFgVKv418LlfEh9zlq2/mB0+EX9BGEtEFNMEDzmXvJpEMQ2isDhfACKLO6B8opkfNEgUKhEpUBOTKf0VNSTVdQy1VL+jnXk7SzHo6ncb7PCMMK/yWmlCsvc="
+        distributions: "sdist bdist_wheel"
+        skip_existing: true
+        on:
+          branch: master
+          repo: Holzhaus/sphinx-multiversion
+          tags: true
+        # Deploy to Github Releases
+      - provider: releases
+        token:
+          secure: "tAwp3NTqbvig1wU0hwkN7aWOAQYnpS12zsAlBc/ud05cYrjrBIjjFltX/1C0tgxxxJ11zdAhTjgUfYFrWbRXJ5hHptyD8fTa8RXfJZ2KuTx44aEHSyNHtl8YbwlXXr9hGjhrUHUulNghpFCAwNgg6oONbocQ3teW6maO7ByLUmO/RbNnligK1iQW6GPHovOg1iTbsZLVhriP2e2/JFRxq5rdnxArNLzwLhg8L4QAuc6YX3gnI1Z2xuBtpgQhNfL5S+hMuKOwONpdMQDYgXD7QbPA0pqSYQRf81vd+/FtZqNnHHku0POsoLLMkscalXxa1AbznvH9vg9nwO0J20XNZ1Mg6BFLpDwPzqLhCAwmeFcz+k78m+3x9bIhS27ps0XCgg0VxjM4Rvaabfm7J9h1aIteZf4oGS+1Er8fK6ejOeGI+tZu/5YXDhpS6hyK6gXey7Ep3SGF5oqrcu80gBrCGkWXrADMogarL6RbcZV2vrYkm5kBorcUA4s3nU2fV5UUnCg83fdlY70kmrNt3Im83ndSH3jOyTDXITjHCDN6+GFjd1eP0h8tcwiGPtVs2JGolSmqq/AzjDqY2CfgwccBDg6MF4gbcgJQdzZ11j/5cH9SNoHN6/V4869vO28XbwTKqbgsKpuajmbKVEqIlGTi2zylgzNbxtySTL0tnZvyO4w="
+        name: "$TRAVIS_TAG"
+        release_notes: "Release $TRAVIS_TAG"
+        draft: false
+        skip_cleanup: true
+        file_glob: true
+        file: dist/*
+        overwrite: true
+        on:
+          branch: master
+          repo: Holzhaus/sphinx-multiversion
+          tags: true
+        # Deploy documentation
+      - provider: pages
+        skip_cleanup: true
+        keep_history: false
+        token: $GITHUB_TOKEN
+        local_dir: html
+        on:
+          branch: master
+          repo: Holzhaus/sphinx-multiversion
+
+stages:
+  - Testing
+  - Deployment
 
 cache:
   pip: true
@@ -35,57 +86,11 @@ install:
 
 before_script:
 - pre-commit run --all-files --show-diff-on-failure
+- python -m unittest --verbose
+
 script:
 - mkdir html
 - git fetch --all
 - sphinx-multiversion -W docs html
 - python setup.py build sdist bdist_wheel
 
-# Deployment
-before_deploy:
-- touch html/.nojekyll
-- cp assets/gh-pages-redirect.html html/index.html
-deploy:
-  # Deploy documentation
-- provider: pages
-  skip_cleanup: true
-  keep_history: false
-  token: $GITHUB_TOKEN
-  local_dir: html
-  on:
-    branch: master
-    repo: Holzhaus/sphinx-multiversion
-    python: 3.7
-    os: linux
-
-  # Deploy to PyPI
-- deploy:
-  provider: pypi
-  username: "__token__"
-  password:
-    secure: "0bFhY80KT5zp5wpsQwmBdHNvrC1aXnCUnL268UBb5bcVrTQ/cVr08CPVEZLefRU96CCqCZHCQPh1iP9DCtPEszB3XI0pVydoNJ9Fei9ccHI6bJBtfpLnTwlyygiBiTT3uNlal+/+videWqhDXAs2EHZAQn4L41Na6h1pHBoTu+V8KIJinNJccgiUXeeHdDc4lWCVKU0GfeKtzvgmFZq9B1w7jo+VnOQFKYH3rS80u1xkTrWNiVb3vdt+36PHssNXv5XI9kULsE+ofSdKIwYd7zGxs3ouw+LYhEM6QpDp1ERITAPWhdrY6DTTVdatnClTlzvxRMiqbeKmx/cYIx1x2aXqGxQo0UIcjK2N647ZG6lQAmJnRBc71CJvzGOFQQ+PNnlRlUcOmhnqr8K4bHDhe6Cf1PAFW6SCZvZ8ruXRV726FhRqMzmE/fJwtOY97F0BHJWwBAjbTdDM0J9Hph68YjkMcB96/bRTvGqkwVZTVqPtGppa3uRX2+ljt9pFY3j/jsk77mTYoY5VJrDB8lQSK7dzn8U/vg6q7lbRA5QfYXVIc77/7w29BFgVKv418LlfEh9zlq2/mB0+EX9BGEtEFNMEDzmXvJpEMQ2isDhfACKLO6B8opkfNEgUKhEpUBOTKf0VNSTVdQy1VL+jnXk7SzHo6ncb7PCMMK/yWmlCsvc="
-  distributions: "sdist bdist_wheel"
-  skip_existing: true
-  on:
-    branch: master
-    repo: Holzhaus/sphinx-multiversion
-    tags: true
-    os: linux
-
-  # Deploy to Github Releases
-- provider: releases
-  token:
-    secure: "tAwp3NTqbvig1wU0hwkN7aWOAQYnpS12zsAlBc/ud05cYrjrBIjjFltX/1C0tgxxxJ11zdAhTjgUfYFrWbRXJ5hHptyD8fTa8RXfJZ2KuTx44aEHSyNHtl8YbwlXXr9hGjhrUHUulNghpFCAwNgg6oONbocQ3teW6maO7ByLUmO/RbNnligK1iQW6GPHovOg1iTbsZLVhriP2e2/JFRxq5rdnxArNLzwLhg8L4QAuc6YX3gnI1Z2xuBtpgQhNfL5S+hMuKOwONpdMQDYgXD7QbPA0pqSYQRf81vd+/FtZqNnHHku0POsoLLMkscalXxa1AbznvH9vg9nwO0J20XNZ1Mg6BFLpDwPzqLhCAwmeFcz+k78m+3x9bIhS27ps0XCgg0VxjM4Rvaabfm7J9h1aIteZf4oGS+1Er8fK6ejOeGI+tZu/5YXDhpS6hyK6gXey7Ep3SGF5oqrcu80gBrCGkWXrADMogarL6RbcZV2vrYkm5kBorcUA4s3nU2fV5UUnCg83fdlY70kmrNt3Im83ndSH3jOyTDXITjHCDN6+GFjd1eP0h8tcwiGPtVs2JGolSmqq/AzjDqY2CfgwccBDg6MF4gbcgJQdzZ11j/5cH9SNoHN6/V4869vO28XbwTKqbgsKpuajmbKVEqIlGTi2zylgzNbxtySTL0tnZvyO4w="
-  name: "$TRAVIS_TAG"
-  release_notes: "Release $TRAVIS_TAG"
-  draft: false
-  skip_cleanup: true
-  file_glob: true
-  file: dist/*
-  overwrite: true
-  on:
-    branch: master
-    repo: Holzhaus/sphinx-multiversion
-    tags: true
-    python: 3.7
-    os: linux

+ 22 - 23
sphinx_multiversion/sphinx.py

@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 import datetime
 import json
-import pathlib
 import collections
 import logging
 import os
@@ -97,39 +96,39 @@ class VersionInfo:
                 posixpath.split(self.context["pagename"])[-1]
             )
 
-        # Find output root
+        # Find relative outputdir paths from common output root
         current_version = self.metadata[self.current_version_name]
         other_version = self.metadata[other_version_name]
-        outputroot = os.path.commonpath(
-            (current_version["outputdir"], other_version["outputdir"])
+
+        current_outputroot = os.path.abspath(current_version["outputdir"])
+        other_outputroot = os.path.abspath(other_version["outputdir"])
+        outputroot = os.path.commonpath((current_outputroot, other_outputroot))
+
+        current_outputroot = os.path.relpath(
+            current_outputroot, start=outputroot
         )
+        other_outputroot = os.path.relpath(other_outputroot, start=outputroot)
 
-        current_outputroot = pathlib.PurePath(
-            current_version["outputdir"]
-        ).relative_to(outputroot)
-        other_outputroot = pathlib.PurePath(
-            other_version["outputdir"]
-        ).relative_to(outputroot)
-
-        relative_path_to_outputroot = os.path.join(
-            *(
-                ".."
-                for x in current_outputroot.joinpath(
-                    self.context["pagename"]
-                ).parent.parts
+        # Ensure that we use POSIX separators in the path (for the HTML code)
+        if os.sep != posixpath.sep:
+            current_outputroot = posixpath.join(
+                *os.path.split(current_outputroot)
             )
-        )
+            other_outputroot = posixpath.join(*os.path.split(other_outputroot))
 
-        # Find output dir of other version
-        outputdir = posixpath.join(
-            relative_path_to_outputroot, other_outputroot
+        # Find relative path to root of other_version's outputdir
+        current_outputdir = posixpath.dirname(
+            posixpath.join(current_outputroot, self.context["pagename"])
+        )
+        other_outputdir = posixpath.relpath(
+            other_outputroot, start=current_outputdir
         )
 
         if not self.vhasdoc(other_version_name):
-            return posixpath.join(outputdir, "index.html")
+            return posixpath.join(other_outputdir, "index.html")
 
         return posixpath.join(
-            outputdir, "{}.html".format(self.context["pagename"])
+            other_outputdir, "{}.html".format(self.context["pagename"])
         )
 
 

+ 0 - 0
tests/__init__.py


+ 116 - 0
tests/test_sphinx.py

@@ -0,0 +1,116 @@
+import os.path
+import posixpath
+import tempfile
+import unittest
+
+import sphinx_multiversion
+
+
+class VersionInfoTestCase(unittest.TestCase):
+    def setUp(self):
+        root = tempfile.gettempdir()
+
+        self.versioninfo = sphinx_multiversion.sphinx.VersionInfo(
+            app=None,
+            context={"pagename": "testpage"},
+            metadata={
+                "master": {
+                    "name": "master",
+                    "version": "",
+                    "release": "0.2",
+                    "is_released": False,
+                    "source": "heads",
+                    "creatordate": "2020-08-07 07:45:20 -0700",
+                    "basedir": os.path.join(root, "master"),
+                    "sourcedir": os.path.join(root, "master", "docs"),
+                    "outputdir": os.path.join(root, "build", "html", "master"),
+                    "confdir": os.path.join(root, "master", "docs"),
+                    "docnames": ["testpage", "appendix/faq"],
+                },
+                "v0.1.0": {
+                    "name": "v0.1.0",
+                    "version": "",
+                    "release": "0.1.0",
+                    "is_released": True,
+                    "source": "tags",
+                    "creatordate": "2020-07-16 08:45:20 -0100",
+                    "basedir": os.path.join(root, "v0.1.0"),
+                    "sourcedir": os.path.join(root, "v0.1.0", "docs"),
+                    "outputdir": os.path.join(root, "build", "html", "v0.1.0"),
+                    "confdir": os.path.join(root, "v0.1.0", "docs"),
+                    "docnames": ["old_testpage", "appendix/faq"],
+                },
+                "branch-with/slash": {
+                    "name": "branch-with/slash",
+                    "version": "",
+                    "release": "0.1.1",
+                    "is_released": False,
+                    "source": "heads",
+                    "creatordate": "2020-08-06 11:53:06 -0400",
+                    "basedir": os.path.join(root, "branch-with/slash"),
+                    "sourcedir": os.path.join(
+                        root, "branch-with/slash", "docs"
+                    ),
+                    "outputdir": os.path.join(
+                        root, "build", "html", "branch-with/slash"
+                    ),
+                    "confdir": os.path.join(root, "branch-with/slash", "docs"),
+                    "docnames": ["testpage"],
+                },
+            },
+            current_version_name="master",
+        )
+
+    def test_tags_property(self):
+        versions = self.versioninfo.tags
+        self.assertEqual([version.name for version in versions], ["v0.1.0"])
+
+    def test_branches_property(self):
+        versions = self.versioninfo.branches
+        self.assertEqual(
+            [version.name for version in versions],
+            ["master", "branch-with/slash"],
+        )
+
+    def test_releases_property(self):
+        versions = self.versioninfo.releases
+        self.assertEqual([version.name for version in versions], ["v0.1.0"])
+
+    def test_in_development_property(self):
+        versions = self.versioninfo.in_development
+        self.assertEqual(
+            [version.name for version in versions],
+            ["master", "branch-with/slash"],
+        )
+
+    def test_vhasdoc(self):
+        self.assertTrue(self.versioninfo.vhasdoc("master"))
+        self.assertFalse(self.versioninfo.vhasdoc("v0.1.0"))
+        self.assertTrue(self.versioninfo.vhasdoc("branch-with/slash"))
+
+        self.versioninfo.context["pagename"] = "appendix/faq"
+        self.assertTrue(self.versioninfo.vhasdoc("master"))
+        self.assertTrue(self.versioninfo.vhasdoc("v0.1.0"))
+        self.assertFalse(self.versioninfo.vhasdoc("branch-with/slash"))
+
+    def test_vpathto(self):
+        self.assertEqual(self.versioninfo.vpathto("master"), "testpage.html")
+        self.assertEqual(
+            self.versioninfo.vpathto("v0.1.0"),
+            posixpath.join("..", "v0.1.0", "index.html"),
+        )
+        self.assertEqual(
+            self.versioninfo.vpathto("branch-with/slash"),
+            posixpath.join("..", "branch-with/slash", "testpage.html"),
+        )
+
+        self.versioninfo.context["pagename"] = "appendix/faq"
+        self.assertEqual(self.versioninfo.vpathto("master"), "faq.html")
+        self.assertEqual(
+            self.versioninfo.vpathto("v0.1.0"),
+            posixpath.join("..", "..", "v0.1.0", "appendix", "faq.html"),
+        )
+        self.assertEqual(
+            self.versioninfo.vpathto("branch-with/slash"),
+            posixpath.join("..", "..", "branch-with/slash", "index.html"),
+        )