Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/sas/system/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import webbrowser
from pathlib import Path

from packaging.version import parse

logger = logging.getLogger(__name__)


Expand All @@ -10,12 +12,23 @@ def _release_version(version_string: str) -> str:

Strips dev/pre-release suffixes so the version matches deployed documentation URLs.
"""
from packaging.version import parse

version = parse(version_string)
return f"{version.major}.{version.minor}.{version.micro}"


def _online_doc_base(version_string: str) -> str:
"""Return the best online documentation base URL for a version string.

Released documentation is archived under ``docs/old_docs/X.Y.Z``.
Development and pre-release builds should point at the current docs root.
"""
version = parse(version_string)
if version.is_devrelease or version.is_prerelease:
return "https://www.sasview.org/docs"

return f"https://www.sasview.org/docs/old_docs/{_release_version(version_string)}"


class _HelpSystem:
"""Extensible storage for help-system-related paths and configuration"""

Expand Down Expand Up @@ -75,8 +88,7 @@ def _online_url(self, relative_path: Path, fragment: str = "") -> str:
"""Construct the online documentation URL for the current version."""
from sas.system.version import __version__

version = _release_version(__version__)
base = f"https://www.sasview.org/docs/v{version}"
base = _online_doc_base(__version__)
url = f"{base}/{relative_path.as_posix()}"
if fragment:
url += "#" + fragment
Expand Down
31 changes: 24 additions & 7 deletions test/system/utest_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path, PurePosixPath
from unittest.mock import patch

from sas.system._help import _HelpSystem, _release_version
from sas.system._help import _HelpSystem, _online_doc_base, _release_version


class PosixTestPath(PurePosixPath):
Expand All @@ -29,6 +29,22 @@ def test_post_release_stripped(self):
assert _release_version("6.1.2.post1") == "6.1.2"


class TestOnlineDocBase:
"""Tests for online documentation base URL selection."""

def test_release_uses_archived_docs(self):
assert _online_doc_base("6.1.2") == "https://www.sasview.org/docs/old_docs/6.1.2"

def test_post_release_uses_archived_docs(self):
assert _online_doc_base("6.1.2.post1") == "https://www.sasview.org/docs/old_docs/6.1.2"

def test_dev_release_uses_current_docs(self):
assert _online_doc_base("6.1.2.dev159+g77be83657") == "https://www.sasview.org/docs"

def test_pre_release_uses_current_docs(self):
assert _online_doc_base("6.2.0a1") == "https://www.sasview.org/docs"


class TestHelpSystemOnlineUrl:
"""Tests for _HelpSystem._online_url()"""

Expand All @@ -43,7 +59,7 @@ def test_online_url_basic(self, _mock):
Path("user/qtgui/Perspectives/Fitting/fitting_help.html")
)
assert url == (
"https://www.sasview.org/docs/v6.1.2"
"https://www.sasview.org/docs/old_docs/6.1.2"
"/user/qtgui/Perspectives/Fitting/fitting_help.html"
)

Expand All @@ -52,7 +68,8 @@ def test_online_url_dev_version(self):
url = self.hs._online_url(
Path("user/qtgui/Perspectives/Fitting/fitting_help.html")
)
assert url.startswith("https://www.sasview.org/docs/v6.1.2/")
assert url.startswith("https://www.sasview.org/docs/")
assert "/old_docs/" not in url
assert "+g77be83657" not in url
assert ".dev159" not in url

Expand Down Expand Up @@ -115,7 +132,7 @@ def test_online_fallback_when_local_missing(self, mock_wb, tmp_path):

opened_url = mock_wb.open.call_args[0][0]
assert opened_url == (
"https://www.sasview.org/docs/v6.1.2/user/fitting.html"
"https://www.sasview.org/docs/old_docs/6.1.2/user/fitting.html"
)

@patch("sas.system._help.webbrowser")
Expand All @@ -138,7 +155,7 @@ def test_online_fallback_when_path_is_none(self, mock_wb):
self.hs.show_help("user/fitting.html")

opened_url = mock_wb.open.call_args[0][0]
assert opened_url.startswith("https://www.sasview.org/docs/v6.1.2/")
assert opened_url.startswith("https://www.sasview.org/docs/old_docs/6.1.2/")

@patch("sas.system._help.webbrowser")
def test_absolute_path_stripped_for_online(self, mock_wb, tmp_path):
Expand All @@ -151,7 +168,7 @@ def test_absolute_path_stripped_for_online(self, mock_wb, tmp_path):

opened_url = mock_wb.open.call_args[0][0]
assert opened_url == (
"https://www.sasview.org/docs/v6.1.2/user/fitting.html"
"https://www.sasview.org/docs/old_docs/6.1.2/user/fitting.html"
)

@patch("sas.system._help.webbrowser")
Expand All @@ -166,7 +183,7 @@ def test_posix_absolute_path_stripped_for_online(self, mock_wb):

opened_url = mock_wb.open.call_args[0][0]
assert opened_url == (
"https://www.sasview.org/docs/v6.1.2/user/fitting.html"
"https://www.sasview.org/docs/old_docs/6.1.2/user/fitting.html"
)

@patch("sas.system._help.webbrowser")
Expand Down
Loading