| 1 |
import pathlib |
| 2 |
import sys |
| 3 |
|
| 4 |
from django.test import SimpleTestCase |
| 5 |
|
| 6 |
|
| 7 |
def last_n_parts(path, n): |
| 8 |
return "/".join(path.parts[-n:]) |
| 9 |
|
| 10 |
|
| 11 |
# The import must happen at the end of setUpClass, so it can't be imported at |
| 12 |
# the top of the file. |
| 13 |
github_links = None |
| 14 |
|
| 15 |
|
| 16 |
class GitHubLinkTests(SimpleTestCase): |
| 17 |
@classmethod |
| 18 |
def setUpClass(cls): |
| 19 |
# The file implementing the code under test is in the docs folder and |
| 20 |
# is not part of the Django package. This means it cannot be imported |
| 21 |
# through standard means. Include its parent in the pythonpath for the |
| 22 |
# duration of the tests to allow the code to be imported. |
| 23 |
cls.ext_path = str((pathlib.Path(__file__).parents[2] / "docs/_ext").resolve()) |
| 24 |
sys.path.insert(0, cls.ext_path) |
| 25 |
cls.addClassCleanup(sys.path.remove, cls.ext_path) |
| 26 |
cls.addClassCleanup(sys.modules.pop, "github_links", None) |
| 27 |
# Linters/IDEs may not be able to detect this as a valid import. |
| 28 |
import github_links as _github_links |
| 29 |
|
| 30 |
global github_links |
| 31 |
github_links = _github_links |
| 32 |
|
| 33 |
def test_code_locator(self): |
| 34 |
locator = github_links.CodeLocator.from_code(""" |
| 35 |
from a import b, c |
| 36 |
from .d import e, f as g |
| 37 |
|
| 38 |
def h(): |
| 39 |
pass |
| 40 |
|
| 41 |
class I: |
| 42 |
def j(self): |
| 43 |
pass""") |
| 44 |
|
| 45 |
self.assertEqual(locator.node_line_numbers, {"h": 5, "I": 8, "I.j": 9}) |
| 46 |
self.assertEqual(locator.import_locations, {"b": "a", "c": "a", "e": ".d"}) |
| 47 |
|
| 48 |
def test_module_name_to_file_path_package(self): |
| 49 |
path = github_links.module_name_to_file_path("django") |
| 50 |
|
| 51 |
self.assertEqual(last_n_parts(path, 2), "django/__init__.py") |
| 52 |
|
| 53 |
def test_module_name_to_file_path_module(self): |
| 54 |
path = github_links.module_name_to_file_path("django.shortcuts") |
| 55 |
|
| 56 |
self.assertEqual(last_n_parts(path, 2), "django/shortcuts.py") |
| 57 |
|
| 58 |
def test_get_path_and_line_class(self): |
| 59 |
path, line = github_links.get_path_and_line( |
| 60 |
module="tests.sphinx.testdata.package.module", fullname="MyClass" |
| 61 |
) |
| 62 |
|
| 63 |
self.assertEqual( |
| 64 |
last_n_parts(path, 5), "tests/sphinx/testdata/package/module.py" |
| 65 |
) |
| 66 |
self.assertEqual(line, 12) |
| 67 |
|
| 68 |
def test_get_path_and_line_func(self): |
| 69 |
path, line = github_links.get_path_and_line( |
| 70 |
module="tests.sphinx.testdata.package.module", fullname="my_function" |
| 71 |
) |
| 72 |
|
| 73 |
self.assertEqual( |
| 74 |
last_n_parts(path, 5), "tests/sphinx/testdata/package/module.py" |
| 75 |
) |
| 76 |
self.assertEqual(line, 24) |
| 77 |
|
| 78 |
def test_get_path_and_line_method(self): |
| 79 |
path, line = github_links.get_path_and_line( |
| 80 |
module="tests.sphinx.testdata.package.module", fullname="MyClass.my_method" |
| 81 |
) |
| 82 |
|
| 83 |
self.assertEqual( |
| 84 |
last_n_parts(path, 5), "tests/sphinx/testdata/package/module.py" |
| 85 |
) |
| 86 |
self.assertEqual(line, 16) |
| 87 |
|
| 88 |
def test_get_path_and_line_cached_property(self): |
| 89 |
path, line = github_links.get_path_and_line( |
| 90 |
module="tests.sphinx.testdata.package.module", |
| 91 |
fullname="MyClass.my_cached_property", |
| 92 |
) |
| 93 |
|
| 94 |
self.assertEqual( |
| 95 |
last_n_parts(path, 5), "tests/sphinx/testdata/package/module.py" |
| 96 |
) |
| 97 |
self.assertEqual(line, 20) |
| 98 |
|
| 99 |
def test_get_path_and_line_forwarded_import(self): |
| 100 |
path, line = github_links.get_path_and_line( |
| 101 |
module="tests.sphinx.testdata.package.module", fullname="MyOtherClass" |
| 102 |
) |
| 103 |
|
| 104 |
self.assertEqual( |
| 105 |
last_n_parts(path, 5), "tests/sphinx/testdata/package/other_module.py" |
| 106 |
) |
| 107 |
self.assertEqual(line, 1) |
| 108 |
|
| 109 |
def test_get_path_and_line_wildcard_import(self): |
| 110 |
path, line = github_links.get_path_and_line( |
| 111 |
module="tests.sphinx.testdata.package.module", fullname="WildcardClass" |
| 112 |
) |
| 113 |
|
| 114 |
self.assertEqual( |
| 115 |
last_n_parts(path, 5), |
| 116 |
"tests/sphinx/testdata/package/wildcard_module.py", |
| 117 |
) |
| 118 |
self.assertEqual(line, 4) |
| 119 |
|
| 120 |
path, line = github_links.get_path_and_line( |
| 121 |
module="tests.sphinx.testdata.package.module", |
| 122 |
fullname="WildcardMixin", |
| 123 |
) |
| 124 |
self.assertEqual( |
| 125 |
last_n_parts(path, 5), |
| 126 |
"tests/sphinx/testdata/package/wildcard_base.py", |
| 127 |
) |
| 128 |
self.assertEqual(line, 1) |
| 129 |
|
| 130 |
def test_get_path_and_line_forwarded_import_module(self): |
| 131 |
path, line = github_links.get_path_and_line( |
| 132 |
module="tests.sphinx.testdata.package.module", |
| 133 |
fullname="other_module.MyOtherClass", |
| 134 |
) |
| 135 |
|
| 136 |
self.assertEqual( |
| 137 |
last_n_parts(path, 5), "tests/sphinx/testdata/package/other_module.py" |
| 138 |
) |
| 139 |
self.assertEqual(line, 1) |
| 140 |
|
| 141 |
def test_get_branch_stable(self): |
| 142 |
branch = github_links.get_branch(version="2.2", next_version="3.2") |
| 143 |
self.assertEqual(branch, "stable/2.2.x") |
| 144 |
|
| 145 |
def test_get_branch_latest(self): |
| 146 |
branch = github_links.get_branch(version="3.2", next_version="3.2") |
| 147 |
self.assertEqual(branch, "main") |
| 148 |
|
| 149 |
def test_github_linkcode_resolve_unspecified_domain(self): |
| 150 |
domain = "unspecified" |
| 151 |
info = {} |
| 152 |
self.assertIsNone( |
| 153 |
github_links.github_linkcode_resolve( |
| 154 |
domain, info, version="3.2", next_version="3.2" |
| 155 |
) |
| 156 |
) |
| 157 |
|
| 158 |
def test_github_linkcode_resolve_unspecified_info(self): |
| 159 |
domain = "py" |
| 160 |
info = {"module": None, "fullname": None} |
| 161 |
self.assertIsNone( |
| 162 |
github_links.github_linkcode_resolve( |
| 163 |
domain, info, version="3.2", next_version="3.2" |
| 164 |
) |
| 165 |
) |
| 166 |
|
| 167 |
def test_github_linkcode_resolve_not_found(self): |
| 168 |
info = { |
| 169 |
"module": "foo.bar.baz.hopefully_non_existant_module", |
| 170 |
"fullname": "MyClass", |
| 171 |
} |
| 172 |
self.assertIsNone( |
| 173 |
github_links.github_linkcode_resolve( |
| 174 |
"py", info, version="3.2", next_version="3.2" |
| 175 |
) |
| 176 |
) |
| 177 |
|
| 178 |
def test_github_linkcode_resolve_link_to_object(self): |
| 179 |
info = { |
| 180 |
"module": "tests.sphinx.testdata.package.module", |
| 181 |
"fullname": "MyClass", |
| 182 |
} |
| 183 |
self.assertEqual( |
| 184 |
github_links.github_linkcode_resolve( |
| 185 |
"py", info, version="3.2", next_version="3.2" |
| 186 |
), |
| 187 |
"https://github.com/django/django/blob/main/tests/sphinx/" |
| 188 |
"testdata/package/module.py#L12", |
| 189 |
) |
| 190 |
|
| 191 |
def test_github_linkcode_resolve_link_to_class_older_version(self): |
| 192 |
info = { |
| 193 |
"module": "tests.sphinx.testdata.package.module", |
| 194 |
"fullname": "MyClass", |
| 195 |
} |
| 196 |
self.assertEqual( |
| 197 |
github_links.github_linkcode_resolve( |
| 198 |
"py", info, version="2.2", next_version="3.2" |
| 199 |
), |
| 200 |
"https://github.com/django/django/blob/stable/2.2.x/tests/sphinx/" |
| 201 |
"testdata/package/module.py#L12", |
| 202 |
) |
| 203 |
|
| 204 |
def test_import_error(self): |
| 205 |
msg = "Could not import '.....test' in 'tests.sphinx.testdata.package'." |
| 206 |
with self.assertRaisesMessage(ImportError, msg): |
| 207 |
github_links.get_path_and_line( |
| 208 |
module="tests.sphinx.testdata.package.import_error", fullname="Test" |
| 209 |
) |