How to use linkcheck method in Behave

Best Python code snippet using behave

test_url.py

Source:test_url.py Github

copy

Full Screen

1# -*- coding: iso-8859-1 -*-2# Copyright (C) 2004-2014 Bastian Kleineidam3#4# This program is free software; you can redistribute it and/or modify5# it under the terms of the GNU General Public License as published by6# the Free Software Foundation; either version 2 of the License, or7# (at your option) any later version.8#9# This program is distributed in the hope that it will be useful,10# but WITHOUT ANY WARRANTY; without even the implied warranty of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12# GNU General Public License for more details.13#14# You should have received a copy of the GNU General Public License along15# with this program; if not, write to the Free Software Foundation, Inc.,16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.17"""18Test url routines.19"""20from . import need_network, need_posix, need_windows21import unittest22import os23import re24import linkcheck.url25# 'ftp://user:pass@ftp.foo.net/foo/bar':26# 'ftp://user:pass@ftp.foo.net/foo/bar',27# 'http://USER:pass@www.Example.COM/foo/bar':28# 'http://USER:pass@www.example.com/foo/bar',29# '-': '-',30# All portions of the URI must be utf-8 encoded NFC form Unicode strings31#valid: http://example.com/?q=%C3%87 (C-cedilla U+00C7)32#valid: http://example.com/?q=%E2%85%A0 (Roman numeral one U+2160)33#invalid: http://example.com/?q=%C7 (C-cedilla ISO-8859-1)34#invalid: http://example.com/?q=C%CC%A735# (Latin capital letter C + Combining cedilla U+0327)36def url_norm (url, encoding=None):37 return linkcheck.url.url_norm(url, encoding=encoding)[0]38class TestUrl (unittest.TestCase):39 """Test url norming and quoting."""40 def urlnormtest (self, url, nurl, encoding=None):41 self.assertFalse(linkcheck.url.url_needs_quoting(nurl),42 "Result URL %r must not need quoting" % nurl)43 nurl1 = url_norm(url, encoding=encoding)44 self.assertFalse(linkcheck.url.url_needs_quoting(nurl1),45 "Normed URL %r needs quoting" % nurl)46 self.assertEqual(nurl1, nurl)47 def test_pathattack (self):48 # Windows winamp path attack prevention.49 url = "http://server/..%5c..%5c..%5c..%5c..%5c..%5c..%5c.."\50 "%5ccskin.zip"51 nurl = "http://server/cskin.zip"52 self.assertEqual(linkcheck.url.url_quote(url_norm(url)), nurl)53 def test_safe_patterns (self):54 is_safe_host = linkcheck.url.is_safe_host55 safe_host_pattern = linkcheck.url.safe_host_pattern56 self.assertTrue(is_safe_host("example.org"))57 self.assertTrue(is_safe_host("example.org:80"))58 self.assertTrue(not is_safe_host("example.org:21"))59 pat = safe_host_pattern("example.org")60 ro = re.compile(pat)61 self.assertTrue(ro.match("http://example.org:80/"))62 def test_url_quote (self):63 url_quote = linkcheck.url.url_quote64 url = "http://a:80/bcd"65 self.assertEqual(url_quote(url), url)66 url = "http://a:80/bcd?"67 url2 = "http://a:80/bcd"68 self.assertEqual(url_quote(url), url2)69 url = "http://a:80/bcd?a=b"70 url2 = "http://a:80/bcd?a=b"71 self.assertEqual(url_quote(url), url2)72 url = "a/b"73 self.assertEqual(url_quote(url), url)74 url = "bcd?"75 url2 = "bcd"76 self.assertEqual(url_quote(url), url2)77 url = "bcd?a=b"78 url2 = "bcd?a=b"79 self.assertEqual(url_quote(url), url2)80 def test_norm_quote (self):81 # Test url norm quoting.82 url = "http://groups.google.com/groups?hl=en&lr&ie=UTF-8&"\83 "threadm=3845B54D.E546F9BD%40monmouth.com&rnum=2&"\84 "prev=/groups%3Fq%3Dlogitech%2Bwingman%2Bextreme%2Bdigital"\85 "%2B3d%26hl%3Den%26lr%3D%26ie%3DUTF-8%26selm%3D3845B54D.E5"\86 "46F9BD%2540monmouth.com%26rnum%3D2"87 self.urlnormtest(url, url)88 url = "http://redirect.alexa.com/redirect?"\89 "http://www.offeroptimizer.com"90 self.urlnormtest(url, url)91 url = "http://www.lesgensducinema.com/photo/Philippe%20Nahon.jpg"92 self.urlnormtest(url, url)93 # Only perform percent-encoding where it is essential.94 url = "http://example.com/%7Ejane"95 nurl = "http://example.com/~jane"96 self.urlnormtest(url, nurl)97 url = "http://example.com/%7ejane"98 self.urlnormtest(url, nurl)99 # Always use uppercase A-through-F characters when percent-encoding.100 url = "http://example.com/?q=1%2a2"101 nurl = "http://example.com/?q=1%2A2"102 self.urlnormtest(url, nurl)103 # the no-quote chars104 url = "http://example.com/a*+-();b"105 self.urlnormtest(url, url)106 url = "http://linkchecker.git.sourceforge.net/git/gitweb.cgi?p=linkchecker/linkchecker;a=blob;f=doc/changelog.txt;hb=HEAD"107 self.urlnormtest(url, url)108 url = "http://www.company.com/path/doc.html?url=/path2/doc2.html?foo=bar"109 self.urlnormtest(url, url)110 url = "http://example.com/#a b"111 nurl = "http://example.com/#a%20b"112 self.urlnormtest(url, nurl)113 url = "http://example.com/?u=http://example2.com?b=c "114 nurl = "http://example.com/?u=http://example2.com?b=c%20"115 self.urlnormtest(url, nurl)116 url = "http://example.com/?u=http://example2.com?b="117 nurl = "http://example.com/?u=http://example2.com?b="118 self.urlnormtest(url, nurl)119 url = "http://localhost:8001/?quoted=ü"120 nurl = "http://localhost:8001/?quoted=%FC"121 self.urlnormtest(url, nurl, encoding="iso-8859-1")122 url = "http://host/?a=b/c+d="123 nurl = "http://host/?a=b/c%20d%3D"124 self.urlnormtest(url, nurl)125 def test_norm_case_sensitivity (self):126 # Test url norm case sensitivity.127 # Always provide the URI scheme in lowercase characters.128 url = "HTTP://example.com/"129 nurl = "http://example.com/"130 self.urlnormtest(url, nurl)131 # Always provide the host, if any, in lowercase characters.132 url = "http://EXAMPLE.COM/"133 nurl = "http://example.com/"134 self.urlnormtest(url, nurl)135 url = "http://EXAMPLE.COM:55/"136 nurl = "http://example.com:55/"137 self.urlnormtest(url, nurl)138 def test_norm_defaultport (self):139 # Test url norm default port recognition.140 # For schemes that define a port, use an empty port if the default141 # is desired142 url = "http://example.com:80/"143 nurl = "http://example.com/"144 self.urlnormtest(url, nurl)145 url = "http://example.com:8080/"146 nurl = "http://example.com:8080/"147 self.urlnormtest(url, nurl)148 def test_norm_host_dot (self):149 # Test url norm host dot removal.150 url = "http://example.com./"151 nurl = "http://example.com/"152 self.urlnormtest(url, nurl)153 url = "http://example.com.:81/"154 nurl = "http://example.com:81/"155 self.urlnormtest(url, nurl)156 def test_norm_fragment (self):157 # Test url norm fragment preserving.158 # Empty fragment identifiers must be preserved:159 url = "http://www.w3.org/2000/01/rdf-schema#"160 nurl = url161 self.urlnormtest(url, nurl)162 url = "http://example.org/foo/ #a=1,2,3"163 nurl = "http://example.org/foo/%20#a%3D1%2C2%2C3"164 self.urlnormtest(url, nurl)165 def test_norm_empty_path (self):166 # Test url norm empty path handling.167 # For schemes that define an empty path to be equivalent to a168 # path of "/", use "/".169 url = "http://example.com"170 nurl = "http://example.com"171 self.urlnormtest(url, nurl)172 url = "http://example.com?a=b"173 nurl = "http://example.com/?a=b"174 self.urlnormtest(url, nurl)175 url = "http://example.com#foo"176 nurl = "http://example.com/#foo"177 self.urlnormtest(url, nurl)178 def test_norm_path_backslashes (self):179 # Test url norm backslash path handling.180 # note: this is not RFC conform (see url.py for more info)181 url = r"http://example.com\test.html"182 nurl = "http://example.com/test.html"183 self.urlnormtest(url, nurl)184 url = r"http://example.com/a\test.html"185 nurl = "http://example.com/a/test.html"186 self.urlnormtest(url, nurl)187 url = r"http://example.com\a\test.html"188 nurl = "http://example.com/a/test.html"189 self.urlnormtest(url, nurl)190 url = r"http://example.com\a/test.html"191 nurl = "http://example.com/a/test.html"192 self.urlnormtest(url, nurl)193 def test_norm_path_slashes (self):194 # Test url norm slashes in path handling.195 # reduce duplicate slashes196 url = "http://example.com//a/test.html"197 nurl = "http://example.com/a/test.html"198 self.urlnormtest(url, nurl)199 url = "http://example.com//a/b/"200 nurl = "http://example.com/a/b/"201 self.urlnormtest(url, nurl)202 def test_norm_path_dots (self):203 # Test url norm dots in path handling.204 # Prevent dot-segments appearing in non-relative URI paths.205 url = "http://example.com/a/./b"206 nurl = "http://example.com/a/b"207 self.urlnormtest(url, nurl)208 url = "http://example.com/a/../a/b"209 self.urlnormtest(url, nurl)210 url = "http://example.com/../a/b"211 self.urlnormtest(url, nurl)212 def test_norm_path_relative_dots (self):213 # Test url norm relative path handling with dots.214 # normalize redundant path segments215 url = '/foo/bar/.'216 nurl = '/foo/bar/'217 self.urlnormtest(url, nurl)218 url = '/foo/bar/./'219 nurl = '/foo/bar/'220 self.urlnormtest(url, nurl)221 url = '/foo/bar/..'222 nurl = '/foo/'223 self.urlnormtest(url, nurl)224 url = '/foo/bar/../'225 nurl = '/foo/'226 self.urlnormtest(url, nurl)227 url = '/foo/bar/../baz'228 nurl = '/foo/baz'229 self.urlnormtest(url, nurl)230 url = '/foo/bar/../..'231 nurl = '/'232 self.urlnormtest(url, nurl)233 url = '/foo/bar/../../'234 nurl = '/'235 self.urlnormtest(url, nurl)236 url = '/foo/bar/../../baz'237 nurl = '/baz'238 self.urlnormtest(url, nurl)239 url = '/foo/bar/../../../baz'240 nurl = '/baz'241 self.urlnormtest(url, nurl)242 url = '/foo/bar/../../../../baz'243 nurl = '/baz'244 self.urlnormtest(url, nurl)245 url = '/./foo'246 nurl = '/foo'247 self.urlnormtest(url, nurl)248 url = '/../foo'249 nurl = '/foo'250 self.urlnormtest(url, nurl)251 url = '/foo.'252 nurl = url253 self.urlnormtest(url, nurl)254 url = '/.foo'255 nurl = url256 self.urlnormtest(url, nurl)257 url = '/foo..'258 nurl = url259 self.urlnormtest(url, nurl)260 url = '/..foo'261 nurl = url262 self.urlnormtest(url, nurl)263 url = '/./../foo'264 nurl = '/foo'265 self.urlnormtest(url, nurl)266 url = '/./foo/.'267 nurl = '/foo/'268 self.urlnormtest(url, nurl)269 url = '/foo/./bar'270 nurl = '/foo/bar'271 self.urlnormtest(url, nurl)272 url = '/foo/../bar'273 nurl = '/bar'274 self.urlnormtest(url, nurl)275 url = '../../../images/miniXmlButton.gif'276 nurl = url277 self.urlnormtest(url, nurl)278 url = '/a..b/../images/miniXmlButton.gif'279 nurl = '/images/miniXmlButton.gif'280 self.urlnormtest(url, nurl)281 url = '/.a.b/../foo/'282 nurl = '/foo/'283 self.urlnormtest(url, nurl)284 url = '/..a.b/../foo/'285 nurl = '/foo/'286 self.urlnormtest(url, nurl)287 url = 'b/../../foo/'288 nurl = '../foo/'289 self.urlnormtest(url, nurl)290 url = './foo'291 nurl = 'foo'292 self.urlnormtest(url, nurl)293 def test_norm_path_relative_slashes (self):294 # Test url norm relative path handling with slashes.295 url = '/foo//'296 nurl = '/foo/'297 self.urlnormtest(url, nurl)298 url = '/foo///bar//'299 nurl = '/foo/bar/'300 self.urlnormtest(url, nurl)301 def test_mail_url (self):302 # Test mailto URLs.303 # no netloc and no path304 url = 'mailto:'305 nurl = url306 self.urlnormtest(url, nurl)307 # standard email308 url = 'mailto:user@www.example.org'309 nurl = url310 self.urlnormtest(url, nurl)311 # emails with subject312 url = 'mailto:user@www.example.org?subject=a_b'313 nurl = url314 self.urlnormtest(url, nurl)315 url = 'mailto:business.inquiries@designingpatterns.com?subject=Business%20Inquiry'316 nurl = url317 self.urlnormtest(url, nurl)318 def test_norm_other (self):319 # Test norming of other schemes.320 url = 'news:'321 nurl = 'news:'322 self.urlnormtest(url, nurl)323 url = 'snews:'324 nurl = 'snews://'325 self.urlnormtest(url, nurl)326 url = 'nntp:'327 nurl = 'nntp://'328 self.urlnormtest(url, nurl)329 url = "news:!$%&/()="330 nurl = 'news:!%24%25%26/()='331 self.urlnormtest(url, nurl)332 url = "news:comp.infosystems.www.servers.unix"333 nurl = url334 self.urlnormtest(url, nurl)335 # javascript url336 url = "javascript:loadthis()"337 nurl = url338 self.urlnormtest(url, nurl)339 # ldap url # XXX failing on Travis build340 #url = "ldap://[2001:db8::7]/c=GB?objectClass?one"341 #nurl = "ldap://%5B2001:db8::7%5D/c=GB?objectClass?one"342 #self.urlnormtest(url, nurl)343 url = "tel:+1-816-555-1212"344 nurl = url345 self.urlnormtest(url, nurl)346 url = "urn:oasis:names:specification:docbook:dtd:xml:4.1.2"347 nurl = "urn:oasis%3Anames%3Aspecification%3Adocbook%3Adtd%3Axml%3A4.1.2"348 self.urlnormtest(url, nurl)349 def test_norm_with_auth (self):350 # Test norming of URLs with authentication tokens.351 url = "telnet://User@www.example.org"352 nurl = url353 self.urlnormtest(url, nurl)354 url = "telnet://User:Pass@www.example.org"355 nurl = url356 self.urlnormtest(url, nurl)357 url = "http://User:Pass@www.example.org/"358 nurl = url359 self.urlnormtest(url, nurl)360 @need_posix361 def test_norm_file1 (self):362 url = "file:///a/b.txt"363 nurl = url364 self.urlnormtest(url, nurl)365 @need_windows366 def test_norm_file2 (self):367 url = "file:///C|/a/b.txt"368 nurl = url369 self.urlnormtest(url, nurl)370 @need_posix371 def test_norm_file_unicode (self):372 url = u"file:///a/b.txt"373 nurl = url374 self.urlnormtest(url, nurl)375 url = u"file:///a/ä.txt"376 nurl = u"file:///a/%E4.txt"377 self.urlnormtest(url, nurl, encoding="iso-8859-1")378 #url = u"file:///\u041c\u043e\u0448\u043a\u043e\u0432\u0430.bin"379 #nurl = u"file:///a.bin" # XXX380 #self.urlnormtest(url, nurl)381 def test_norm_invalid (self):382 url = u"äöü?:"383 nurl = u"%E4%F6%FC?:"384 self.urlnormtest(url, nurl, encoding="iso-8859-1")385 def test_fixing (self):386 # Test url fix method.387 url = "http//www.example.org"388 nurl = "http://www.example.org"389 self.assertEqual(linkcheck.url.url_fix_common_typos(url), nurl)390 url = u"http//www.example.org"391 nurl = u"http://www.example.org"392 self.assertEqual(linkcheck.url.url_fix_common_typos(url), nurl)393 url = u"https//www.example.org"394 nurl = u"https://www.example.org"395 self.assertEqual(linkcheck.url.url_fix_common_typos(url), nurl)396 def test_valid (self):397 # Test url validity functions.398 u = "http://www.example.com"399 self.assertTrue(linkcheck.url.is_safe_url(u), u)400 u = "http://www.example.com/"401 self.assertTrue(linkcheck.url.is_safe_url(u), u)402 u = "http://www.example.com/~calvin"403 self.assertTrue(linkcheck.url.is_safe_url(u), u)404 u = "http://www.example.com/a,b"405 self.assertTrue(linkcheck.url.is_safe_url(u), u)406 u = "http://www.example.com#anchor55"407 self.assertTrue(linkcheck.url.is_safe_url(u), u)408 def test_needs_quoting (self):409 # Test url quoting necessity.410 url = "mailto:<calvin@example.org>?subject=Halli Hallo"411 self.assertTrue(linkcheck.url.url_needs_quoting(url), repr(url))412 url = " http://www.example.com/"413 self.assertTrue(linkcheck.url.url_needs_quoting(url), repr(url))414 url = "http://www.example.com/ "415 self.assertTrue(linkcheck.url.url_needs_quoting(url), repr(url))416 url = "http://www.example.com/\n"417 self.assertTrue(linkcheck.url.url_needs_quoting(url), repr(url))418 url = "\nhttp://www.example.com/"419 self.assertTrue(linkcheck.url.url_needs_quoting(url), repr(url))420 url = "http://www.example.com/#a!"421 self.assertTrue(not linkcheck.url.url_needs_quoting(url), repr(url))422 url = "http://www.example.com/#a b"423 self.assertTrue(linkcheck.url.url_needs_quoting(url), repr(url))424 def test_absolute_url (self):425 url = "hutzli:"426 self.assertTrue(linkcheck.url.url_is_absolute(url), repr(url))427 url = "file:/"428 self.assertTrue(linkcheck.url.url_is_absolute(url), repr(url))429 url = ":"430 self.assertTrue(not linkcheck.url.url_is_absolute(url), repr(url))431 url = "/a/b?http://"432 self.assertTrue(not linkcheck.url.url_is_absolute(url), repr(url))433 def test_nopathquote_chars (self):434 if os.name == 'nt':435 url = "file:///c|/msys/"436 nurl = url437 self.assertEqual(url_norm(url), nurl)438 self.assertTrue(not linkcheck.url.url_needs_quoting(url))439 url = "http://hulla/a/b/!?c=d"440 nurl = url441 self.assertEqual(url_norm(url), nurl)442 def test_idn_encoding (self):443 # Test idna encoding.444 url = u'www.öko.de'445 idna_encode = linkcheck.url.idna_encode446 encurl, is_idn = idna_encode(url)447 self.assertTrue(is_idn)448 self.assertTrue(encurl)449 url = u''450 encurl, is_idn = idna_encode(url)451 self.assertFalse(is_idn)452 self.assertFalse(encurl)453 url = u"ä.."454 self.assertRaises(UnicodeError, idna_encode, url)455 def test_match_host (self):456 # Test host matching.457 match_host = linkcheck.url.match_host458 match_url = linkcheck.url.match_url459 self.assertTrue(not match_host("", []))460 self.assertTrue(not match_host("", [".localhost"]))461 self.assertTrue(not match_host("localhost", []))462 self.assertTrue(not match_host("localhost", [".localhost"]))463 self.assertTrue(match_host("a.localhost", [".localhost"]))464 self.assertTrue(match_host("localhost", ["localhost"]))465 self.assertTrue(not match_url("", []))466 self.assertTrue(not match_url("a", []))467 self.assertTrue(match_url("http://example.org/hulla", ["example.org"]))468 def test_splitparam (self):469 # Path parameter split test.470 p = [471 ("", ("", "")),472 ("/", ("/", "")),473 ("a", ("a", "")),474 ("a;", ("a", "")),475 ("a/b;c/d;e", ("a/b;c/d", "e")),476 ]477 for x in p:478 self._splitparam(x)479 def _splitparam (self, x):480 self.assertEqual(linkcheck.url.splitparams(x[0]), (x[1][0], x[1][1]))481 def test_cgi_split (self):482 # Test cgi parameter splitting.483 u = "scid=kb;en-us;Q248840"484 self.assertEqual(linkcheck.url.url_parse_query(u), u)485 u = "scid=kb;en-us;Q248840&b=c;hulla=bulla"486 self.assertEqual(linkcheck.url.url_parse_query(u), u)487 def test_long_cgi (self):488 u = "/test%s;" % ("?a="*1000)489 self.assertEqual(linkcheck.url.url_parse_query(u), u)490 def test_port (self):491 is_numeric_port = linkcheck.url.is_numeric_port492 self.assertTrue(is_numeric_port("80"))493 self.assertTrue(is_numeric_port("1"))494 self.assertFalse(is_numeric_port("0"))495 self.assertFalse(is_numeric_port("66000"))496 self.assertFalse(is_numeric_port("-1"))497 self.assertFalse(is_numeric_port("a"))498 def test_split (self):499 url_split = linkcheck.url.url_split500 url_unsplit = linkcheck.url.url_unsplit501 url = "http://example.org/whoops"502 self.assertEqual(url_unsplit(url_split(url)), url)503 url = "http://example.org:123/whoops"504 self.assertEqual(url_unsplit(url_split(url)), url)505 def test_safe_domain (self):506 is_safe_domain = linkcheck.url.is_safe_domain507 self.assertFalse(is_safe_domain(u"a..example.com"))508 self.assertFalse(is_safe_domain(u"a_b.example.com"))509 self.assertTrue(is_safe_domain(u"a-b.example.com"))510 self.assertTrue(is_safe_domain(u"x1.example.com"))511 @need_network512 def test_get_content (self):513 linkcheck.url.get_content('http://www.debian.org/')514 def test_duplicate_urls(self):515 is_dup = linkcheck.url.is_duplicate_content_url516 self.assertTrue(is_dup("http://example.org", "http://example.org"))517 self.assertTrue(is_dup("http://example.org/", "http://example.org"))518 self.assertTrue(is_dup("http://example.org", "http://example.org/"))519 self.assertTrue(is_dup("http://example.org/index.html", "http://example.org"))520 self.assertTrue(is_dup("http://example.org", "http://example.org/index.html"))521 self.assertTrue(is_dup("http://example.org/index.htm", "http://example.org"))522 self.assertTrue(is_dup("http://example.org", "http://example.org/index.htm"))523 def test_splitport(self):524 splitport = linkcheck.url.splitport525 netloc = "hostname"526 host, port = splitport(netloc, 99)527 self.assertEqual(host, netloc)528 self.assertEqual(port, 99)529 netloc = "hostname:"530 host, port = splitport(netloc, 99)531 self.assertEqual(host, "hostname")532 self.assertEqual(port, 99)533 netloc = "hostname:42"534 host, port = splitport(netloc, 99)535 self.assertEqual(host, "hostname")536 self.assertEqual(port, 42)537 netloc = "hostname:foo"538 host, port = splitport(netloc, 99)539 self.assertEqual(host, netloc)...

Full Screen

Full Screen

test_build_linkcheck.py

Source:test_build_linkcheck.py Github

copy

Full Screen

1"""2 test_build_linkcheck3 ~~~~~~~~~~~~~~~~~~~~4 Test the build process with manpage builder with the test root.5 :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.6 :license: BSD, see LICENSE for details.7"""8import http.server9import json10import re11import textwrap12import time13import wsgiref.handlers14from datetime import datetime15from queue import Queue16from typing import Dict17from unittest import mock18import pytest19import requests20from sphinx.builders.linkcheck import HyperlinkAvailabilityCheckWorker, RateLimit21from sphinx.testing.util import strip_escseq22from sphinx.util.console import strip_colors23from .utils import CERT_FILE, http_server, https_server24ts_re = re.compile(r".*\[(?P<ts>.*)\].*")25@pytest.mark.sphinx('linkcheck', testroot='linkcheck', freshenv=True)26def test_defaults(app):27 app.build()28 assert (app.outdir / 'output.txt').exists()29 content = (app.outdir / 'output.txt').read_text()30 print(content)31 # looking for '#top' and '#does-not-exist' not found should fail32 assert "Anchor 'top' not found" in content33 assert "Anchor 'does-not-exist' not found" in content34 # looking for non-existent URL should fail35 assert " Max retries exceeded with url: /doesnotexist" in content36 # images should fail37 assert "Not Found for url: https://www.google.com/image.png" in content38 assert "Not Found for url: https://www.google.com/image2.png" in content39 # looking for local file should fail40 assert "[broken] path/to/notfound" in content41 assert len(content.splitlines()) == 642@pytest.mark.sphinx('linkcheck', testroot='linkcheck', freshenv=True)43def test_defaults_json(app):44 app.build()45 assert (app.outdir / 'output.json').exists()46 content = (app.outdir / 'output.json').read_text()47 print(content)48 rows = [json.loads(x) for x in content.splitlines()]49 row = rows[0]50 for attr in ["filename", "lineno", "status", "code", "uri",51 "info"]:52 assert attr in row53 assert len(content.splitlines()) == 1154 assert len(rows) == 1155 # the output order of the rows is not stable56 # due to possible variance in network latency57 rowsby = {row["uri"]: row for row in rows}58 assert rowsby["https://www.google.com#!bar"] == {59 'filename': 'links.txt',60 'lineno': 10,61 'status': 'working',62 'code': 0,63 'uri': 'https://www.google.com#!bar',64 'info': ''65 }66 # looking for non-existent URL should fail67 dnerow = rowsby['https://localhost:7777/doesnotexist']68 assert dnerow['filename'] == 'links.txt'69 assert dnerow['lineno'] == 1370 assert dnerow['status'] == 'broken'71 assert dnerow['code'] == 072 assert dnerow['uri'] == 'https://localhost:7777/doesnotexist'73 assert rowsby['https://www.google.com/image2.png'] == {74 'filename': 'links.txt',75 'lineno': 19,76 'status': 'broken',77 'code': 0,78 'uri': 'https://www.google.com/image2.png',79 'info': '404 Client Error: Not Found for url: https://www.google.com/image2.png'80 }81 # looking for '#top' and '#does-not-exist' not found should fail82 assert "Anchor 'top' not found" == \83 rowsby["https://www.google.com/#top"]["info"]84 assert "Anchor 'does-not-exist' not found" == \85 rowsby["http://www.sphinx-doc.org/en/master/index.html#does-not-exist"]["info"]86 # images should fail87 assert "Not Found for url: https://www.google.com/image.png" in \88 rowsby["https://www.google.com/image.png"]["info"]89@pytest.mark.sphinx(90 'linkcheck', testroot='linkcheck', freshenv=True,91 confoverrides={'linkcheck_anchors_ignore': ["^!", "^top$"],92 'linkcheck_ignore': [93 'https://localhost:7777/doesnotexist',94 'http://www.sphinx-doc.org/en/master/index.html#',95 'https://www.google.com/image.png',96 'https://www.google.com/image2.png',97 'path/to/notfound']98 })99def test_anchors_ignored(app):100 app.build()101 assert (app.outdir / 'output.txt').exists()102 content = (app.outdir / 'output.txt').read_text()103 # expect all ok when excluding #top104 assert not content105@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-anchor', freshenv=True)106def test_raises_for_invalid_status(app):107 class InternalServerErrorHandler(http.server.BaseHTTPRequestHandler):108 def do_GET(self):109 self.send_error(500, "Internal Server Error")110 with http_server(InternalServerErrorHandler):111 app.build()112 content = (app.outdir / 'output.txt').read_text()113 assert content == (114 "index.rst:1: [broken] http://localhost:7777/#anchor: "115 "500 Server Error: Internal Server Error "116 "for url: http://localhost:7777/\n"117 )118class HeadersDumperHandler(http.server.BaseHTTPRequestHandler):119 def do_HEAD(self):120 self.do_GET()121 def do_GET(self):122 self.send_response(200, "OK")123 self.end_headers()124 print(self.headers.as_string())125@pytest.mark.sphinx(126 'linkcheck', testroot='linkcheck-localserver', freshenv=True,127 confoverrides={'linkcheck_auth': [128 (r'^$', ('no', 'match')),129 (r'^http://localhost:7777/$', ('user1', 'password')),130 (r'.*local.*', ('user2', 'hunter2')),131 ]})132def test_auth_header_uses_first_match(app, capsys):133 with http_server(HeadersDumperHandler):134 app.build()135 stdout, stderr = capsys.readouterr()136 auth = requests.auth._basic_auth_str('user1', 'password')137 assert "Authorization: %s\n" % auth in stdout138@pytest.mark.sphinx(139 'linkcheck', testroot='linkcheck-localserver', freshenv=True,140 confoverrides={'linkcheck_auth': [(r'^$', ('user1', 'password'))]})141def test_auth_header_no_match(app, capsys):142 with http_server(HeadersDumperHandler):143 app.build()144 stdout, stderr = capsys.readouterr()145 assert "Authorization" not in stdout146@pytest.mark.sphinx(147 'linkcheck', testroot='linkcheck-localserver', freshenv=True,148 confoverrides={'linkcheck_request_headers': {149 "http://localhost:7777/": {150 "Accept": "text/html",151 },152 "*": {153 "X-Secret": "open sesami",154 }155 }})156def test_linkcheck_request_headers(app, capsys):157 with http_server(HeadersDumperHandler):158 app.build()159 stdout, _stderr = capsys.readouterr()160 assert "Accept: text/html\n" in stdout161 assert "X-Secret" not in stdout162 assert "sesami" not in stdout163@pytest.mark.sphinx(164 'linkcheck', testroot='linkcheck-localserver', freshenv=True,165 confoverrides={'linkcheck_request_headers': {166 "http://localhost:7777": {"Accept": "application/json"},167 "*": {"X-Secret": "open sesami"}168 }})169def test_linkcheck_request_headers_no_slash(app, capsys):170 with http_server(HeadersDumperHandler):171 app.build()172 stdout, _stderr = capsys.readouterr()173 assert "Accept: application/json\n" in stdout174 assert "X-Secret" not in stdout175 assert "sesami" not in stdout176@pytest.mark.sphinx(177 'linkcheck', testroot='linkcheck-localserver', freshenv=True,178 confoverrides={'linkcheck_request_headers': {179 "http://do.not.match.org": {"Accept": "application/json"},180 "*": {"X-Secret": "open sesami"}181 }})182def test_linkcheck_request_headers_default(app, capsys):183 with http_server(HeadersDumperHandler):184 app.build()185 stdout, _stderr = capsys.readouterr()186 assert "Accepts: application/json\n" not in stdout187 assert "X-Secret: open sesami\n" in stdout188def make_redirect_handler(*, support_head):189 class RedirectOnceHandler(http.server.BaseHTTPRequestHandler):190 def do_HEAD(self):191 if support_head:192 self.do_GET()193 else:194 self.send_response(405, "Method Not Allowed")195 self.end_headers()196 def do_GET(self):197 if self.path == "/?redirected=1":198 self.send_response(204, "No content")199 else:200 self.send_response(302, "Found")201 self.send_header("Location", "http://localhost:7777/?redirected=1")202 self.end_headers()203 def log_date_time_string(self):204 """Strip date and time from logged messages for assertions."""205 return ""206 return RedirectOnceHandler207@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)208def test_follows_redirects_on_HEAD(app, capsys, warning):209 with http_server(make_redirect_handler(support_head=True)):210 app.build()211 stdout, stderr = capsys.readouterr()212 content = (app.outdir / 'output.txt').read_text()213 assert content == (214 "index.rst:1: [redirected with Found] "215 "http://localhost:7777/ to http://localhost:7777/?redirected=1\n"216 )217 assert stderr == textwrap.dedent(218 """\219 127.0.0.1 - - [] "HEAD / HTTP/1.1" 302 -220 127.0.0.1 - - [] "HEAD /?redirected=1 HTTP/1.1" 204 -221 """222 )223 assert warning.getvalue() == ''224@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)225def test_follows_redirects_on_GET(app, capsys, warning):226 with http_server(make_redirect_handler(support_head=False)):227 app.build()228 stdout, stderr = capsys.readouterr()229 content = (app.outdir / 'output.txt').read_text()230 assert content == (231 "index.rst:1: [redirected with Found] "232 "http://localhost:7777/ to http://localhost:7777/?redirected=1\n"233 )234 assert stderr == textwrap.dedent(235 """\236 127.0.0.1 - - [] "HEAD / HTTP/1.1" 405 -237 127.0.0.1 - - [] "GET / HTTP/1.1" 302 -238 127.0.0.1 - - [] "GET /?redirected=1 HTTP/1.1" 204 -239 """240 )241 assert warning.getvalue() == ''242@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-warn-redirects',243 freshenv=True, confoverrides={244 'linkcheck_allowed_redirects': {'http://localhost:7777/.*1': '.*'}245 })246def test_linkcheck_allowed_redirects(app, warning):247 with http_server(make_redirect_handler(support_head=False)):248 app.build()249 with open(app.outdir / 'output.json') as fp:250 records = [json.loads(l) for l in fp.readlines()]251 assert len(records) == 2252 result = {r["uri"]: r["status"] for r in records}253 assert result["http://localhost:7777/path1"] == "working"254 assert result["http://localhost:7777/path2"] == "redirected"255 assert ("index.rst:1: WARNING: redirect http://localhost:7777/path2 - with Found to "256 "http://localhost:7777/?redirected=1\n" in strip_escseq(warning.getvalue()))257 assert len(warning.getvalue().splitlines()) == 1258class OKHandler(http.server.BaseHTTPRequestHandler):259 def do_HEAD(self):260 self.send_response(200, "OK")261 self.end_headers()262 def do_GET(self):263 self.do_HEAD()264 self.wfile.write(b"ok\n")265@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True)266def test_invalid_ssl(app):267 # Link indicates SSL should be used (https) but the server does not handle it.268 with http_server(OKHandler):269 app.build()270 with open(app.outdir / 'output.json') as fp:271 content = json.load(fp)272 assert content["status"] == "broken"273 assert content["filename"] == "index.rst"274 assert content["lineno"] == 1275 assert content["uri"] == "https://localhost:7777/"276 assert "SSLError" in content["info"]277@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True)278def test_connect_to_selfsigned_fails(app):279 with https_server(OKHandler):280 app.build()281 with open(app.outdir / 'output.json') as fp:282 content = json.load(fp)283 assert content["status"] == "broken"284 assert content["filename"] == "index.rst"285 assert content["lineno"] == 1286 assert content["uri"] == "https://localhost:7777/"287 assert "[SSL: CERTIFICATE_VERIFY_FAILED]" in content["info"]288@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True)289def test_connect_to_selfsigned_with_tls_verify_false(app):290 app.config.tls_verify = False291 with https_server(OKHandler):292 app.build()293 with open(app.outdir / 'output.json') as fp:294 content = json.load(fp)295 assert content == {296 "code": 0,297 "status": "working",298 "filename": "index.rst",299 "lineno": 1,300 "uri": "https://localhost:7777/",301 "info": "",302 }303@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True)304def test_connect_to_selfsigned_with_tls_cacerts(app):305 app.config.tls_cacerts = CERT_FILE306 with https_server(OKHandler):307 app.build()308 with open(app.outdir / 'output.json') as fp:309 content = json.load(fp)310 assert content == {311 "code": 0,312 "status": "working",313 "filename": "index.rst",314 "lineno": 1,315 "uri": "https://localhost:7777/",316 "info": "",317 }318@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True)319def test_connect_to_selfsigned_with_requests_env_var(monkeypatch, app):320 monkeypatch.setenv("REQUESTS_CA_BUNDLE", CERT_FILE)321 with https_server(OKHandler):322 app.build()323 with open(app.outdir / 'output.json') as fp:324 content = json.load(fp)325 assert content == {326 "code": 0,327 "status": "working",328 "filename": "index.rst",329 "lineno": 1,330 "uri": "https://localhost:7777/",331 "info": "",332 }333@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver-https', freshenv=True)334def test_connect_to_selfsigned_nonexistent_cert_file(app):335 app.config.tls_cacerts = "does/not/exist"336 with https_server(OKHandler):337 app.build()338 with open(app.outdir / 'output.json') as fp:339 content = json.load(fp)340 assert content == {341 "code": 0,342 "status": "broken",343 "filename": "index.rst",344 "lineno": 1,345 "uri": "https://localhost:7777/",346 "info": "Could not find a suitable TLS CA certificate bundle, invalid path: does/not/exist",347 }348@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)349def test_TooManyRedirects_on_HEAD(app):350 class InfiniteRedirectOnHeadHandler(http.server.BaseHTTPRequestHandler):351 def do_HEAD(self):352 self.send_response(302, "Found")353 self.send_header("Location", "http://localhost:7777/")354 self.end_headers()355 def do_GET(self):356 self.send_response(200, "OK")357 self.end_headers()358 self.wfile.write(b"ok\n")359 with http_server(InfiniteRedirectOnHeadHandler):360 app.build()361 with open(app.outdir / 'output.json') as fp:362 content = json.load(fp)363 assert content == {364 "code": 0,365 "status": "working",366 "filename": "index.rst",367 "lineno": 1,368 "uri": "http://localhost:7777/",369 "info": "",370 }371def make_retry_after_handler(responses):372 class RetryAfterHandler(http.server.BaseHTTPRequestHandler):373 def do_HEAD(self):374 status, retry_after = responses.pop(0)375 self.send_response(status)376 if retry_after:377 self.send_header('Retry-After', retry_after)378 self.end_headers()379 def log_date_time_string(self):380 """Strip date and time from logged messages for assertions."""381 return ""382 return RetryAfterHandler383@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)384def test_too_many_requests_retry_after_int_delay(app, capsys, status):385 with http_server(make_retry_after_handler([(429, "0"), (200, None)])), \386 mock.patch("sphinx.builders.linkcheck.DEFAULT_DELAY", 0), \387 mock.patch("sphinx.builders.linkcheck.QUEUE_POLL_SECS", 0.01):388 app.build()389 content = (app.outdir / 'output.json').read_text()390 assert json.loads(content) == {391 "filename": "index.rst",392 "lineno": 1,393 "status": "working",394 "code": 0,395 "uri": "http://localhost:7777/",396 "info": "",397 }398 rate_limit_log = "-rate limited- http://localhost:7777/ | sleeping...\n"399 assert rate_limit_log in strip_colors(status.getvalue())400 _stdout, stderr = capsys.readouterr()401 assert stderr == textwrap.dedent(402 """\403 127.0.0.1 - - [] "HEAD / HTTP/1.1" 429 -404 127.0.0.1 - - [] "HEAD / HTTP/1.1" 200 -405 """406 )407@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)408def test_too_many_requests_retry_after_HTTP_date(app, capsys):409 now = datetime.now().timetuple()410 retry_after = wsgiref.handlers.format_date_time(time.mktime(now))411 with http_server(make_retry_after_handler([(429, retry_after), (200, None)])):412 app.build()413 content = (app.outdir / 'output.json').read_text()414 assert json.loads(content) == {415 "filename": "index.rst",416 "lineno": 1,417 "status": "working",418 "code": 0,419 "uri": "http://localhost:7777/",420 "info": "",421 }422 _stdout, stderr = capsys.readouterr()423 assert stderr == textwrap.dedent(424 """\425 127.0.0.1 - - [] "HEAD / HTTP/1.1" 429 -426 127.0.0.1 - - [] "HEAD / HTTP/1.1" 200 -427 """428 )429@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)430def test_too_many_requests_retry_after_without_header(app, capsys):431 with http_server(make_retry_after_handler([(429, None), (200, None)])),\432 mock.patch("sphinx.builders.linkcheck.DEFAULT_DELAY", 0):433 app.build()434 content = (app.outdir / 'output.json').read_text()435 assert json.loads(content) == {436 "filename": "index.rst",437 "lineno": 1,438 "status": "working",439 "code": 0,440 "uri": "http://localhost:7777/",441 "info": "",442 }443 _stdout, stderr = capsys.readouterr()444 assert stderr == textwrap.dedent(445 """\446 127.0.0.1 - - [] "HEAD / HTTP/1.1" 429 -447 127.0.0.1 - - [] "HEAD / HTTP/1.1" 200 -448 """449 )450@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)451def test_too_many_requests_user_timeout(app, capsys):452 app.config.linkcheck_rate_limit_timeout = 0.0453 with http_server(make_retry_after_handler([(429, None)])):454 app.build()455 content = (app.outdir / 'output.json').read_text()456 assert json.loads(content) == {457 "filename": "index.rst",458 "lineno": 1,459 "status": "broken",460 "code": 0,461 "uri": "http://localhost:7777/",462 "info": "429 Client Error: Too Many Requests for url: http://localhost:7777/",463 }464class FakeResponse:465 headers = {} # type: Dict[str, str]466 url = "http://localhost/"467def test_limit_rate_default_sleep(app):468 worker = HyperlinkAvailabilityCheckWorker(app.env, app.config, Queue(), Queue(), {})469 with mock.patch('time.time', return_value=0.0):470 next_check = worker.limit_rate(FakeResponse())471 assert next_check == 60.0472def test_limit_rate_user_max_delay(app):473 app.config.linkcheck_rate_limit_timeout = 0.0474 worker = HyperlinkAvailabilityCheckWorker(app.env, app.config, Queue(), Queue(), {})475 next_check = worker.limit_rate(FakeResponse())476 assert next_check is None477def test_limit_rate_doubles_previous_wait_time(app):478 rate_limits = {"localhost": RateLimit(60.0, 0.0)}479 worker = HyperlinkAvailabilityCheckWorker(app.env, app.config, Queue(), Queue(),480 rate_limits)481 with mock.patch('time.time', return_value=0.0):482 next_check = worker.limit_rate(FakeResponse())483 assert next_check == 120.0484def test_limit_rate_clips_wait_time_to_max_time(app):485 app.config.linkcheck_rate_limit_timeout = 90.0486 rate_limits = {"localhost": RateLimit(60.0, 0.0)}487 worker = HyperlinkAvailabilityCheckWorker(app.env, app.config, Queue(), Queue(),488 rate_limits)489 with mock.patch('time.time', return_value=0.0):490 next_check = worker.limit_rate(FakeResponse())491 assert next_check == 90.0492def test_limit_rate_bails_out_after_waiting_max_time(app):493 app.config.linkcheck_rate_limit_timeout = 90.0494 rate_limits = {"localhost": RateLimit(90.0, 0.0)}495 worker = HyperlinkAvailabilityCheckWorker(app.env, app.config, Queue(), Queue(),496 rate_limits)497 next_check = worker.limit_rate(FakeResponse())498 assert next_check is None499class ConnectionResetHandler(http.server.BaseHTTPRequestHandler):500 def do_HEAD(self):501 self.connection.close()502 def do_GET(self):503 self.send_response(200, "OK")504 self.end_headers()505@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)506def test_get_after_head_raises_connection_error(app):507 with http_server(ConnectionResetHandler):508 app.build()509 content = (app.outdir / 'output.txt').read_text()510 assert not content511 content = (app.outdir / 'output.json').read_text()512 assert json.loads(content) == {513 "filename": "index.rst",514 "lineno": 1,515 "status": "working",516 "code": 0,517 "uri": "http://localhost:7777/",518 "info": "",...

Full Screen

Full Screen

fabfile.py

Source:fabfile.py Github

copy

Full Screen

1"""Tasks to build and deploy the Django Workshop.2The build commmands can be used to build the documentation in different3formats.4Use the make_messages commmand to create or update the message catalog5for translations.6"""7import os8import sys9import webbrowser10from fabric.api import *11from fabric.contrib.console import confirm12from fabric.tasks import Task13class DjangoWorkshopBaseTask(Task):14 def __init__(self, *args, **kwargs):15 super(DjangoWorkshopBaseTask, self).__init__(*args, **kwargs)16 self.prj_root = os.path.realpath(os.path.dirname(__file__))17 self.docs = os.path.join(self.prj_root, 'docs')18 def get_release(self):19 """Returns the release number."""20 sys.path.append(self.docs)21 import conf22 return conf.release23 @classmethod24 def string_to_bool(cls, value):25 """Converts a string to a boolean."""26 if not isinstance(value, bool):27 value = value.strip().lower()28 if value in ('true', '1'):29 value = True30 elif value in ('false', '0'):31 value = False32 return value33 @staticmethod34 def _msgfmt(language):35 """Compiles a message object."""36 context = {37 'locale_dir': os.path.join('locale', language, 'LC_MESSAGES')38 }39 for filename in os.listdir(context['locale_dir']):40 if not filename.endswith('.po'):41 continue42 context['po_file'] = filename43 context['mo_file'] = os.path.splitext(filename)[0] + '.mo'44 cmd = 'msgfmt --check-format -D %(locale_dir)s %(po_file)s '45 cmd += '-o %(locale_dir)s/%(mo_file)s'46 local(cmd % context)47 def run(self, builder=None, language=None, linkcheck=False):48 linkcheck = self.string_to_bool(linkcheck)49 if not builder:50 abort('Missing argument builder.')51 if language:52 if len(language) != 2:53 abort('language must be in ll format.')54 with lcd(self.docs):55 local('make clean-migrations clean')56 if language:57 self._msgfmt(language)58 local('make SPHINXOPTS="-Dlanguage=%s" %s' % (language, builder))59 else:60 local('make %s' % builder)61 if linkcheck:62 fastprint('\n')63 local('make linkcheck')64 fastprint('\nLink check report:\n\n')65 local('cat _build/linkcheck/output.txt')66 fastprint('\n')67class BuildHtmlTask(DjangoWorkshopBaseTask):68 """Builds the Sphinx documentation as HTML.69 The documentation is opened in the default browser after the build.70 Use the language argument to build not the default language.71 Set linkcheck to True to perform a linkcheck after the build.72 """73 name = 'build'74 def run(self, language=None, linkcheck=False, openbrowser=True):75 linkcheck = self.string_to_bool(linkcheck)76 openbrowser = self.string_to_bool(openbrowser)77 with hide('running'):78 super(BuildHtmlTask, self).run('html', language, linkcheck)79 if openbrowser:80 path = os.path.join(self.docs, '_build/html/index.html')81 webbrowser.open('file://%s' % path)82class ServeHtmlTask(BuildHtmlTask):83 """Builds and serves the Sphinx documentation as HTML.84 This is useful for testing Disqus integration and search functionality.85 The documentation is opened in the default browser after the build.86 Use the language argument to build not the default language.87 Set linkcheck to True to perform a linkcheck after the build.88 """89 name = 'serve'90 port = 900091 def run(self, language=None, linkcheck=False):92 linkcheck = self.string_to_bool(linkcheck)93 super(ServeHtmlTask, self).run(language, linkcheck, False)94 webbrowser.open('http://127.0.0.1:%d' % self.port)95 with hide('running'):96 with lcd(os.path.join(self.docs, '_build/html')):97 local('python -m SimpleHTTPServer %d' % self.port)98class BuildPdfTask(DjangoWorkshopBaseTask):99 """Builds the Sphinx documentation as PDF.100 The PDF is opened after the build with the default application.101 Use the language argument to build not the default language.102 Set linkcheck to True to perform a linkcheck after the build.103 """104 name = 'build_pdf'105 def run(self, language=None, linkcheck=False):106 linkcheck = self.string_to_bool(linkcheck)107 with hide('running'):108 super(BuildPdfTask, self).run('latexpdf', language, linkcheck)109 pdfpath = os.path.join(self.docs, '_build/latex/DjangoWorkshop.pdf')110 try:111 os.startfile(pdfpath)112 except AttributeError:113 with hide('running'):114 local('open %s' % pdfpath)115class MakeMessageCatalogTask(DjangoWorkshopBaseTask):116 """Creates .po files in locale directory.117 The language argument is required and defines the language of the118 translation. """119 name = 'make_messages'120 def _msginit(self, pot_file, po_file):121 """Creates .po file from .pot file."""122 cmd = 'msginit --no-translator -l %(lang)s '123 cmd += '-i _build/locale/%(pot_file)s '124 cmd += '-o locale/%(lang_short)s/LC_MESSAGES/%(po_file)s'125 context = {126 'lang': self.lang,127 'lang_short': self.lang_short,128 'pot_file': pot_file,129 'po_file': po_file130 }131 local(cmd % context)132 def _msgfilter(self, po_file):133 """Filters the .po file and removes all msgstr strings."""134 po_file = 'locale/%s/LC_MESSAGES/%s' % (self.lang_short, po_file)135 cmd = 'msgfilter -i %(po_file)s --keep-header -o %(po_file)s echo -n'136 local(cmd % {'po_file': po_file})137 def _msgmerge(self, pot_file, po_file):138 """Updates .po file from .pot file."""139 po_file = 'locale/%s/LC_MESSAGES/%s' % (self.lang_short, po_file)140 cmd = 'msgmerge -U %(po_file)s _build/locale/%(pot_file)s'141 context = {142 'lang_short': self.lang_short,143 'pot_file': pot_file,144 'po_file': po_file145 }146 fastprint('Updating %s' % po_file)147 local(cmd % context)148 def run(self, language=None):149 if not language:150 abort('Missing argument language.')151 if len(language) != 5:152 abort('language must be in ll_CC format.')153 self.lang = language154 self.lang_short = language[:2]155 with lcd(self.docs):156 new_catalog = False157 locale_dir = os.path.join('locale', self.lang_short, 'LC_MESSAGES')158 with hide('running'):159 if not os.path.exists(locale_dir):160 local('mkdir -p %s' % locale_dir)161 new_catalog = True162 local('make gettext')163 fastprint('\n')164 for filename in os.listdir('_build/locale'):165 if not filename.endswith('.pot'):166 continue167 if new_catalog:168 self._msginit(filename, filename[:-1])169 self._msgfilter(filename[:-1])170 else:171 self._msgmerge(filename, filename[:-1])172class DeployTask(DjangoWorkshopBaseTask):173 """Builds and deploys the Sphinx documentation as HTML.174 Performs an automatic linkcheck after the build before the deployment.175 """176 name = 'deploy'177 def run(self):178 with hide('running'):179 super(DeployTask, self).run('html', linkcheck=True)180 msg = 'Do you wish to deploy build %s?' % self.get_release()181 if not confirm(msg, default=False):182 abort('Deployment cancelled.')183 with cd('doms/django-workshop.de/subs'):184 run('rm -rf www')185 run('mkdir www')186 put('docs/_build/html/*', 'www')187 put('docs/.htaccess', 'www')188class DiffSourceTask(DjangoWorkshopBaseTask):189 """Shows a recursive diff of the src directory against another directory."""190 name = 'diff'191 diff_options = '-r'192 diff_exclude = '-x \'*.pyc\' -x \'*.DS_Store\''193 def run(self, target):194 context = {195 'options': self.diff_options,196 'exclude': self.diff_exclude,197 'source': os.path.join(self.prj_root, 'src', 'cookbook'),198 'target': target,199 }200 with settings(hide('warnings', 'stderr'), warn_only=True):201 local('diff %(options)s %(exclude)s %(source)s %(target)s' % context)202class DiffSourceBriefTask(DiffSourceTask):203 """Shows a brief, recursive diff of the src directory against another directory."""204 name = 'diff_brief'205 diff_options = '-rq'206env.user = 'zed00-keimlink'207env.hosts = ['zed00.hostsharing.net']208build = BuildHtmlTask()209build_pdf = BuildPdfTask()210deploy = DeployTask()211diff = DiffSourceTask()212diff_brief = DiffSourceBriefTask()213make_messages = MakeMessageCatalogTask()...

Full Screen

Full Screen

test_strformat.py

Source:test_strformat.py Github

copy

Full Screen

1# -*- coding: iso-8859-1 -*-2# Copyright (C) 2004-2014 Bastian Kleineidam3#4# This program is free software; you can redistribute it and/or modify5# it under the terms of the GNU General Public License as published by6# the Free Software Foundation; either version 2 of the License, or7# (at your option) any later version.8#9# This program is distributed in the hope that it will be useful,10# but WITHOUT ANY WARRANTY; without even the implied warranty of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12# GNU General Public License for more details.13#14# You should have received a copy of the GNU General Public License along15# with this program; if not, write to the Free Software Foundation, Inc.,16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.17"""18Test string formatting operations.19"""20import unittest21import os22import time23import linkcheck.strformat24class TestStrFormat (unittest.TestCase):25 """26 Test string formatting routines.27 """28 def test_unquote (self):29 # Test quote stripping.30 u = linkcheck.strformat.unquote31 self.assertEqual(u(""), "")32 self.assertEqual(u(None), None)33 self.assertEqual(u("'"), "'")34 self.assertEqual(u("\""), "\"")35 self.assertEqual(u("\"\""), "")36 self.assertEqual(u("''"), "")37 self.assertEqual(u("'a'"), "a")38 self.assertEqual(u("'a\"'"), "a\"")39 self.assertEqual(u("'\"a'"), "\"a")40 self.assertEqual(u('"a\'"'), 'a\'')41 self.assertEqual(u('"\'a"'), '\'a')42 self.assertEqual(u("'a'", matching=True), "a")43 self.assertEqual(u('"a"', matching=True), "a")44 # even mis-matching quotes should be removed...45 self.assertEqual(u("'a\""), "a")46 self.assertEqual(u("\"a'"), "a")47 # ...but not when matching is True48 self.assertEqual(u("'a\"", matching=True), "'a\"")49 self.assertEqual(u("\"a'", matching=True), "\"a'")50 def test_wrap (self):51 # Test line wrapping.52 wrap = linkcheck.strformat.wrap53 s = "11%(sep)s22%(sep)s33%(sep)s44%(sep)s55" % {'sep': os.linesep}54 # testing width <= 055 self.assertEqual(wrap(s, -1), s)56 self.assertEqual(wrap(s, 0), s)57 l = len(os.linesep)58 gap = " "59 s2 = "11%(gap)s22%(sep)s33%(gap)s44%(sep)s55" % \60 {'sep': os.linesep, 'gap': gap}61 # splitting lines62 self.assertEqual(wrap(s2, 2), s)63 # combining lines64 self.assertEqual(wrap(s, 4+l), s2)65 # misc66 self.assertEqual(wrap(s, -1), s)67 self.assertEqual(wrap(s, 0), s)68 self.assertEqual(wrap(None, 10), None)69 self.assertFalse(linkcheck.strformat.get_paragraphs(None))70 def test_remove_markup (self):71 # Test markup removing.72 self.assertEqual(linkcheck.strformat.remove_markup("<a>"), "")73 self.assertEqual(linkcheck.strformat.remove_markup("<>"), "")74 self.assertEqual(linkcheck.strformat.remove_markup("<<>"), "")75 self.assertEqual(linkcheck.strformat.remove_markup("a < b"), "a < b")76 def test_strsize (self):77 # Test byte size strings.78 self.assertRaises(ValueError, linkcheck.strformat.strsize, -1)79 self.assertEqual(linkcheck.strformat.strsize(0), "0B")80 self.assertEqual(linkcheck.strformat.strsize(1), "1B")81 self.assertEqual(linkcheck.strformat.strsize(2), "2B")82 self.assertEqual(linkcheck.strformat.strsize(1023, grouping=False), "1023B")83 self.assertEqual(linkcheck.strformat.strsize(1024), "1KB")84 self.assertEqual(linkcheck.strformat.strsize(1024*25), "25.00KB")85 self.assertEqual(linkcheck.strformat.strsize(1024*1024), "1.00MB")86 self.assertEqual(linkcheck.strformat.strsize(1024*1024*11), "11.0MB")87 self.assertEqual(linkcheck.strformat.strsize(1024*1024*1024),88 "1.00GB")89 self.assertEqual(linkcheck.strformat.strsize(1024*1024*1024*14),90 "14.0GB")91 def test_is_ascii (self):92 self.assertTrue(linkcheck.strformat.is_ascii("abcd./"))93 self.assertTrue(not linkcheck.strformat.is_ascii("ä"))94 self.assertTrue(not linkcheck.strformat.is_ascii(u"ä"))95 def test_indent (self):96 s = "bla"97 self.assertEqual(linkcheck.strformat.indent(s, ""), s)98 self.assertEqual(linkcheck.strformat.indent(s, " "), " "+s)99 def test_stripurl (self):100 self.assertEqual(linkcheck.strformat.stripurl(u"a\tb"), u"a\tb")101 self.assertEqual(linkcheck.strformat.stripurl(u" a\t b"), u"a\t b")102 self.assertEqual(linkcheck.strformat.stripurl(u" ab\t\ra\nb"), u"ab")103 self.assertEqual(linkcheck.strformat.stripurl(None), None)104 self.assertEqual(linkcheck.strformat.stripurl(u""), u"")105 def test_limit (self):106 self.assertEqual(linkcheck.strformat.limit("", 0), "")107 self.assertEqual(linkcheck.strformat.limit("a", 0), "")108 self.assertEqual(linkcheck.strformat.limit("1", 1), "1")109 self.assertEqual(linkcheck.strformat.limit("11", 1), "1...")110 def test_strtime (self):111 zone = linkcheck.strformat.strtimezone()112 t = linkcheck.strformat.strtime(0, func=time.gmtime)113 self.assertEqual(t, "1970-01-01 00:00:00"+zone)114 def test_duration (self):115 duration = linkcheck.strformat.strduration116 self.assertEqual(duration(-0.5), "-00:01")117 self.assertEqual(duration(0), "00:00")118 self.assertEqual(duration(0.9), "00:01")119 self.assertEqual(duration(1), "00:01")120 self.assertEqual(duration(2), "00:02")121 self.assertEqual(duration(60), "01:00")122 self.assertEqual(duration(120), "02:00")123 self.assertEqual(duration(60*60), "01:00:00")124 self.assertEqual(duration(60*60*24), "24:00:00")125 def test_duration_long (self):126 duration = lambda s: linkcheck.strformat.strduration_long(s, do_translate=False)127 self.assertEqual(duration(-0.5), "-0.50 seconds")128 self.assertEqual(duration(0), "0.00 seconds")129 self.assertEqual(duration(0.9), "0.90 seconds")130 self.assertEqual(duration(1), "1 second")131 self.assertEqual(duration(2), "2 seconds")132 self.assertEqual(duration(60), "1 minute")133 self.assertEqual(duration(120), "2 minutes")134 self.assertEqual(duration(60*60), "1 hour")135 self.assertEqual(duration(60*60*24), "1 day")136 self.assertEqual(duration(60*60*24*365), "1 year")137 self.assertEqual(duration(60*60*24*365 + 60*60*24 + 2),138 "1 year, 1 day")139 def test_linenumber (self):140 get_line_number = linkcheck.strformat.get_line_number141 self.assertEqual(get_line_number("a", -5), 0)142 self.assertEqual(get_line_number("a", 0), 1)143 self.assertEqual(get_line_number("a\nb", 2), 2)144 def test_encoding (self):145 is_encoding = linkcheck.strformat.is_encoding146 self.assertTrue(is_encoding('ascii'))147 self.assertFalse(is_encoding('hulla'))148 def test_unicode_safe (self):149 unicode_safe = linkcheck.strformat.unicode_safe150 self.assertEqual(unicode_safe("a"), u"a")151 self.assertEqual(unicode_safe(u"a"), u"a")152 def test_ascii_safe (self):153 ascii_safe = linkcheck.strformat.ascii_safe154 self.assertEqual(ascii_safe("a"), "a")155 self.assertEqual(ascii_safe(u"a"), "a")156 self.assertEqual(ascii_safe(u"ä"), "")157 def test_strip_control_chars(self):158 strip = linkcheck.strformat.strip_control_chars159 self.assertEqual(strip(""), "")160 self.assertEqual(strip(u""), u"")161 self.assertEqual(strip("a"), "a")162 self.assertEqual(strip(u"a"), u"a")163 self.assertEqual(strip("ä"), "ä")164 self.assertEqual(strip(u"ä"), u"ä")165 self.assertEqual(strip("\x01"), "")...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Behave automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful