| 1 |
"""Test cases for MagicLink.""" |
| 2 |
from .. import util |
| 3 |
import markdown |
| 4 |
import warnings |
| 5 |
|
| 6 |
|
| 7 |
class TestMagicLinkShortner(util.MdCase): |
| 8 |
"""Test cases for repo link shortening.""" |
| 9 |
|
| 10 |
extension = [ |
| 11 |
'pymdownx.magiclink', |
| 12 |
] |
| 13 |
|
| 14 |
extension_configs = { |
| 15 |
'pymdownx.magiclink': { |
| 16 |
'repo_url_shortener': True |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
def test_user(self): |
| 21 |
"""Test user shortening.""" |
| 22 |
|
| 23 |
# Test #! original syntax |
| 24 |
self.check_markdown( |
| 25 |
r'https://github.com/facelessuser', |
| 26 |
r'<p><a class="magiclink magiclink-github magiclink-mention" href="https://github.com/facelessuser" title="GitHub User: facelessuser">@facelessuser</a></p>' # noqa: E501 |
| 27 |
) |
| 28 |
|
| 29 |
def test_repo(self): |
| 30 |
"""Test repository shortening.""" |
| 31 |
|
| 32 |
# Test #! original syntax |
| 33 |
self.check_markdown( |
| 34 |
r'https://github.com/facelessuser/pymdown-extensions', |
| 35 |
r'<p><a class="magiclink magiclink-github magiclink-repository" href="https://github.com/facelessuser/pymdown-extensions" title="GitHub Repository: facelessuser/pymdown-extensions">facelessuser/pymdown-extensions</a></p>' # noqa: E501 |
| 36 |
) |
| 37 |
|
| 38 |
def test_no_social(self): |
| 39 |
"""Test that social shortening does not happen.""" |
| 40 |
|
| 41 |
self.check_markdown( |
| 42 |
r'https://x.com/someuser', |
| 43 |
r'<p><a href="https://x.com/someuser">https://x.com/someuser</a></p>' |
| 44 |
) |
| 45 |
|
| 46 |
def test_excluded_user(self): |
| 47 |
"""Test excluded.""" |
| 48 |
|
| 49 |
self.check_markdown( |
| 50 |
r'https://github.com/support', |
| 51 |
r'<p><a href="https://github.com/support">https://github.com/support</a></p>' |
| 52 |
) |
| 53 |
|
| 54 |
def test_excluded_user_repo(self): |
| 55 |
"""Test excluded.""" |
| 56 |
|
| 57 |
self.check_markdown( |
| 58 |
r'https://github.com/support/repo', |
| 59 |
r'<p><a href="https://github.com/support/repo">https://github.com/support/repo</a></p>' |
| 60 |
) |
| 61 |
|
| 62 |
def test_discuss(self): |
| 63 |
"""Test discuss.""" |
| 64 |
|
| 65 |
self.check_markdown( |
| 66 |
r'https://github.com/facelessuser/pymdown-extensions/discussions/1173', |
| 67 |
r'<p><a class="magiclink magiclink-github magiclink-discussion" href="https://github.com/facelessuser/pymdown-extensions/discussions/1173" title="GitHub Discussion: facelessuser/pymdown-extensions #1173">facelessuser/pymdown-extensions?1173</a></p>' # noqa: E501 |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
class TestMagicLinkShorthand(util.MdCase): |
| 72 |
"""Test cases for repo link shortening.""" |
| 73 |
|
| 74 |
extension = [ |
| 75 |
'pymdownx.magiclink', |
| 76 |
] |
| 77 |
|
| 78 |
extension_configs = { |
| 79 |
'pymdownx.magiclink': { |
| 80 |
'repo_url_shorthand': True, |
| 81 |
'user': 'facelessuser', |
| 82 |
'repo': 'pymdown-extensions' |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
def test_discuss(self): |
| 87 |
"""Test discuss.""" |
| 88 |
|
| 89 |
self.check_markdown( |
| 90 |
r'?1173', |
| 91 |
r'<p><a class="magiclink magiclink-github magiclink-discussion" href="https://github.com/facelessuser/pymdown-extensions/discussions/1173" title="GitHub Discussion: facelessuser/pymdown-extensions #1173">?1173</a></p>' # noqa: E501 |
| 92 |
) |
| 93 |
|
| 94 |
def test_bad_discss(self): |
| 95 |
"""Test repo that doesn't support discussions.""" |
| 96 |
|
| 97 |
self.check_markdown( |
| 98 |
r'gitlab:user/repo?1173', |
| 99 |
r'<p>gitlab:user/repo?1173</p>' |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
class TestMagicLinkExternalShorthand(util.MdCase): |
| 104 |
"""Test cases for repo link shortening.""" |
| 105 |
|
| 106 |
extension = [ |
| 107 |
'pymdownx.magiclink', |
| 108 |
] |
| 109 |
|
| 110 |
extension_configs = { |
| 111 |
'pymdownx.magiclink': { |
| 112 |
'repo_url_shorthand': True, |
| 113 |
'user': 'facelessuser', |
| 114 |
'repo': 'pymdown-extensions', |
| 115 |
'provider': 'gitlab' |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
def test_bad_discss(self): |
| 120 |
"""Test repo that doesn't support discussions.""" |
| 121 |
|
| 122 |
self.check_markdown( |
| 123 |
r'?1173', |
| 124 |
r'<p>?1173</p>' |
| 125 |
) |
| 126 |
|
| 127 |
|
| 128 |
class TestMagicLinkShortnerSocial(util.MdCase): |
| 129 |
"""Test cases for social link shortener.""" |
| 130 |
|
| 131 |
extension = [ |
| 132 |
'pymdownx.magiclink' |
| 133 |
] |
| 134 |
|
| 135 |
extension_configs = { |
| 136 |
'pymdownx.magiclink': { |
| 137 |
'social_url_shortener': True |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
def test_user(self): |
| 142 |
"""Test user shortening.""" |
| 143 |
|
| 144 |
# Test #! original syntax |
| 145 |
self.check_markdown( |
| 146 |
r'https://x.com/someuser', |
| 147 |
r'<p><a class="magiclink magiclink-x magiclink-mention" href="https://x.com/someuser" title="X User: someuser">@someuser</a></p>' # noqa: E501 |
| 148 |
) |
| 149 |
|
| 150 |
def test_no_repo(self): |
| 151 |
"""Test that repository shortening does not happen.""" |
| 152 |
|
| 153 |
self.check_markdown( |
| 154 |
r'https://github.com/facelessuser', |
| 155 |
r'<p><a href="https://github.com/facelessuser">https://github.com/facelessuser</a></p>' |
| 156 |
) |
| 157 |
|
| 158 |
def test_excluded(self): |
| 159 |
"""Test excluded user.""" |
| 160 |
|
| 161 |
self.check_markdown( |
| 162 |
r'https://x.com/home', |
| 163 |
r'<p><a href="https://x.com/home">https://x.com/home</a></p>' |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
class TestMagicLinkCustom(util.MdCase): |
| 168 |
"""Test cases for custom provider.""" |
| 169 |
|
| 170 |
extension = [ |
| 171 |
'pymdownx.magiclink', |
| 172 |
'pymdownx.saneheaders' |
| 173 |
] |
| 174 |
|
| 175 |
extension_configs = { |
| 176 |
'pymdownx.magiclink': { |
| 177 |
'repo_url_shorthand': True, |
| 178 |
'repo_url_shortener': True, |
| 179 |
'user': 'facelessuser', |
| 180 |
'repo': 'pymdown-extensions', |
| 181 |
'provider': 'test', |
| 182 |
'custom': { |
| 183 |
'test': { |
| 184 |
'host': 'http://test.com', |
| 185 |
'label': 'Test', |
| 186 |
'type': 'github' |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
def test_user(self): |
| 193 |
"""Test user in custom repo.""" |
| 194 |
|
| 195 |
self.check_markdown( |
| 196 |
'@facelessuser', |
| 197 |
'<p><a class="magiclink magiclink-test magiclink-mention" href="http://test.com/facelessuser" title="Test User: facelessuser">@facelessuser</a></p>' # noqa: E501 |
| 198 |
) |
| 199 |
|
| 200 |
def test_repo(self): |
| 201 |
"""Test repo in custom repo.""" |
| 202 |
|
| 203 |
self.check_markdown( |
| 204 |
'@facelessuser/pymdown-extensions', |
| 205 |
'<p><a class="magiclink magiclink-test magiclink-repository" href="http://test.com/facelessuser/pymdown-extensions" title="Test Repository: facelessuser/pymdown-extensions">facelessuser/pymdown-extensions</a></p>' # noqa: E501 |
| 206 |
) |
| 207 |
|
| 208 |
def test_default_issue(self): |
| 209 |
"""Test default issue case.""" |
| 210 |
|
| 211 |
self.check_markdown( |
| 212 |
'#2', |
| 213 |
'<p><a class="magiclink magiclink-test magiclink-issue" href="http://test.com/facelessuser/pymdown-extensions/issues/2" title="Test Issue: facelessuser/pymdown-extensions #2">#2</a></p>' # noqa: E501 |
| 214 |
) |
| 215 |
|
| 216 |
def test_default_pull(self): |
| 217 |
"""Test default pull case.""" |
| 218 |
|
| 219 |
self.check_markdown( |
| 220 |
'!2', |
| 221 |
'<p><a class="magiclink magiclink-test magiclink-pull" href="http://test.com/facelessuser/pymdown-extensions/pull/2" title="Test Pull Request: facelessuser/pymdown-extensions #2">!2</a></p>' # noqa: E501 |
| 222 |
) |
| 223 |
|
| 224 |
def test_default_discussion(self): |
| 225 |
"""Test default discussion case.""" |
| 226 |
|
| 227 |
self.check_markdown( |
| 228 |
'?2', |
| 229 |
'<p><a class="magiclink magiclink-test magiclink-discussion" href="http://test.com/facelessuser/pymdown-extensions/discussions/2" title="Test Discussion: facelessuser/pymdown-extensions #2">?2</a></p>' # noqa: E501 |
| 230 |
) |
| 231 |
|
| 232 |
def test_default_commit(self): |
| 233 |
"""Test default commit case.""" |
| 234 |
|
| 235 |
self.check_markdown( |
| 236 |
'3f6b07a8eeaa9d606115758d90f55fec565d4e2a', |
| 237 |
'<p><a class="magiclink magiclink-test magiclink-commit" href="http://test.com/facelessuser/pymdown-extensions/commit/3f6b07a8eeaa9d606115758d90f55fec565d4e2a" title="Test Commit: facelessuser/pymdown-extensions@3f6b07a">3f6b07a</a></p>' # noqa: E501 |
| 238 |
) |
| 239 |
|
| 240 |
def test_default_compare(self): |
| 241 |
"""Test default compare case.""" |
| 242 |
|
| 243 |
self.check_markdown( |
| 244 |
'e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac', |
| 245 |
'<p><a class="magiclink magiclink-test magiclink-compare" href="http://test.com/facelessuser/pymdown-extensions/compare/e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac" title="Test Compare: facelessuser/[email protected]">e2ed7e0...90b6fb8</a></p>' # noqa: E501 |
| 246 |
) |
| 247 |
|
| 248 |
def test_user_link(self): |
| 249 |
"""Test user link.""" |
| 250 |
|
| 251 |
self.check_markdown( |
| 252 |
'http://test.com/facelessuser', |
| 253 |
'<p><a class="magiclink magiclink-test magiclink-mention" href="http://test.com/facelessuser" title="Test User: facelessuser">@facelessuser</a></p>' # noqa: E501 |
| 254 |
) |
| 255 |
|
| 256 |
def test_repo_link(self): |
| 257 |
"""Test repository link.""" |
| 258 |
|
| 259 |
self.check_markdown( |
| 260 |
'http://test.com/facelessuser/pymdown-extensions', |
| 261 |
'<p><a class="magiclink magiclink-test magiclink-repository" href="http://test.com/facelessuser/pymdown-extensions" title="Test Repository: facelessuser/pymdown-extensions">facelessuser/pymdown-extensions</a></p>' # noqa: E501 |
| 262 |
) |
| 263 |
|
| 264 |
def test_issue_link(self): |
| 265 |
"""Test issue link.""" |
| 266 |
|
| 267 |
self.check_markdown( |
| 268 |
'http://test.com/facelessuser/pymdown-extensions/issues/2', |
| 269 |
'<p><a class="magiclink magiclink-test magiclink-issue" href="http://test.com/facelessuser/pymdown-extensions/issues/2" title="Test Issue: facelessuser/pymdown-extensions #2">#2</a></p>' # noqa: E501 |
| 270 |
) |
| 271 |
|
| 272 |
def test_pull_link(self): |
| 273 |
"""Test issue link.""" |
| 274 |
|
| 275 |
self.check_markdown( |
| 276 |
'http://test.com/facelessuser/pymdown-extensions/pull/2', |
| 277 |
'<p><a class="magiclink magiclink-test magiclink-pull" href="http://test.com/facelessuser/pymdown-extensions/pull/2" title="Test Pull Request: facelessuser/pymdown-extensions #2">!2</a></p>' # noqa: E501 |
| 278 |
) |
| 279 |
|
| 280 |
def test_discussion_link(self): |
| 281 |
"""Test discussion link.""" |
| 282 |
|
| 283 |
self.check_markdown( |
| 284 |
'http://test.com/facelessuser/pymdown-extensions/discussions/2', |
| 285 |
'<p><a class="magiclink magiclink-test magiclink-discussion" href="http://test.com/facelessuser/pymdown-extensions/discussions/2" title="Test Discussion: facelessuser/pymdown-extensions #2">?2</a></p>' # noqa: E501 |
| 286 |
) |
| 287 |
|
| 288 |
def test_commit_link(self): |
| 289 |
"""Test commit link.""" |
| 290 |
|
| 291 |
self.check_markdown( |
| 292 |
'http://test.com/facelessuser/pymdown-extensions/commit/3f6b07a8eeaa9d606115758d90f55fec565d4e2a', |
| 293 |
'<p><a class="magiclink magiclink-test magiclink-commit" href="http://test.com/facelessuser/pymdown-extensions/commit/3f6b07a8eeaa9d606115758d90f55fec565d4e2a" title="Test Commit: facelessuser/pymdown-extensions@3f6b07a">3f6b07a</a></p>' # noqa: E501 |
| 294 |
) |
| 295 |
|
| 296 |
def test_compare_link(self): |
| 297 |
"""Test compare link.""" |
| 298 |
|
| 299 |
self.check_markdown( |
| 300 |
'http://test.com/facelessuser/pymdown-extensions/compare/e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac', |
| 301 |
'<p><a class="magiclink magiclink-test magiclink-compare" href="http://test.com/facelessuser/pymdown-extensions/compare/e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac" title="Test Compare: facelessuser/[email protected]">e2ed7e0...90b6fb8</a></p>' # noqa: E501 |
| 302 |
) |
| 303 |
|
| 304 |
def test_external_user(self): |
| 305 |
"""Test external user in custom repo.""" |
| 306 |
|
| 307 |
self.check_markdown( |
| 308 |
'@github:facelessuser', |
| 309 |
'<p><a class="magiclink magiclink-github magiclink-mention" href="https://github.com/facelessuser" title="GitHub User: facelessuser">@facelessuser</a></p>' # noqa: E501 |
| 310 |
) |
| 311 |
|
| 312 |
self.check_markdown( |
| 313 |
'@test:facelessuser', |
| 314 |
'<p><a class="magiclink magiclink-test magiclink-mention" href="http://test.com/facelessuser" title="Test User: facelessuser">@facelessuser</a></p>' # noqa: E501 |
| 315 |
) |
| 316 |
|
| 317 |
def test_bad_name(self): |
| 318 |
"""Test bad name.""" |
| 319 |
|
| 320 |
extension = [ |
| 321 |
'pymdownx.magiclink', |
| 322 |
'pymdownx.saneheaders' |
| 323 |
] |
| 324 |
|
| 325 |
extension_configs = { |
| 326 |
'pymdownx.magiclink': { |
| 327 |
'repo_url_shorthand': True, |
| 328 |
'repo_url_shortener': True, |
| 329 |
'user': 'facelessuser', |
| 330 |
'repo': 'pymdown-extensions', |
| 331 |
'provider': 'bad-name', |
| 332 |
'custom': { |
| 333 |
'bad-name': { |
| 334 |
'host': 'http://bad.com', |
| 335 |
'label': 'Bad', |
| 336 |
'type': 'github' |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
with self.assertRaises(ValueError): |
| 343 |
markdown.markdown('', extensions=extension, extension_configs=extension_configs) |
| 344 |
|
| 345 |
class TestMagicLinkWarning(util.MdCase): |
| 346 |
"""Test cases for social link shortener.""" |
| 347 |
|
| 348 |
extension = [ |
| 349 |
'pymdownx.magiclink' |
| 350 |
] |
| 351 |
|
| 352 |
extension_configs = { |
| 353 |
'pymdownx.magiclink': { |
| 354 |
'social_url_shortener': True, |
| 355 |
'social_url_shorthand': True |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
def test_deprecated_twitter(self): |
| 360 |
"""Test deprecation warning.""" |
| 361 |
|
| 362 |
with warnings.catch_warnings(record=True) as w: |
| 363 |
# Cause all warnings to always be triggered. |
| 364 |
|
| 365 |
# Trigger a warning. |
| 366 |
warnings.simplefilter("always") |
| 367 |
self.check_markdown( |
| 368 |
'@twitter:user', |
| 369 |
'<p><a class="magiclink magiclink-twitter magiclink-mention" href="https://twitter.com/user" title="Twitter User: user">@user</a></p>' # noqa: E501 |
| 370 |
) |
| 371 |
target = "The 'twitter' social provider has been deprecated, please use 'x' instead" |
| 372 |
for warn in w: |
| 373 |
if target in str(warn.message): |
| 374 |
found = True |
| 375 |
break |
| 376 |
# Verify some things |
| 377 |
self.assertTrue(len(w) == 1) |
| 378 |
self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) |
| 379 |
self.assertTrue(found) |
| 380 |
|
| 381 |
def test_deprecated_twitter_shortener(self): |
| 382 |
"""Test shortener deprecation warning.""" |
| 383 |
|
| 384 |
with warnings.catch_warnings(record=True) as w: |
| 385 |
# Cause all warnings to always be triggered. |
| 386 |
|
| 387 |
# Trigger a warning. |
| 388 |
warnings.simplefilter("always") |
| 389 |
self.check_markdown( |
| 390 |
'https://twitter.com/user', |
| 391 |
'<p><a class="magiclink magiclink-twitter magiclink-mention" href="https://twitter.com/user" title="Twitter User: user">@user</a></p>' # noqa: E501 |
| 392 |
) |
| 393 |
target = "The 'twitter' social provider has been deprecated, please use 'x' instead" |
| 394 |
for warn in w: |
| 395 |
if target in str(warn.message): |
| 396 |
found = True |
| 397 |
break |
| 398 |
# Verify some things |
| 399 |
self.assertTrue(len(w) == 1) |
| 400 |
self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) |
| 401 |
self.assertTrue(found) |