ϪfdZddlZddlZddlmZddlmZddlmZm Z GddZ Gdd e e Z Gd d e e Z Gd d e e Z Gdde e Zy)z- Tests for L{twisted.names} example scripts. N)StringIO)FilePath)SkipTestTestCasec.eZdZdZdZdZdZdZdZy)ExampleTestBasea This is a mixin which adds an example to the path, tests it, and then removes it from the path and unimports the modules which the test loaded. Test cases which test example code and documentation listings should use this. This is done this way so that examples can live in isolated path entries, next to the documentation, replete with their own plugin packages and whatever other metadata they need. Also, example code is a rare instance of it being valid to have multiple versions of the same code in the repository at once, rather than relying on version control, because documentation will often show the progression of a single piece of code as features are added to it, and we want to test each one. c~tjdd|_tjj |_t |_|jtd|jt |_ |jtd|j ttjdjd}|j j#dD]<}|j|}|j%r%td|jd ||_tjj)|j&j+j|j&j-j#d d }t/||_y#t$r tdwxYw) zn Add our example directory to the path and record which modules are currently loaded. Nstderrstdout TOX_INI_DIRdocsz5Examples not found ($TOX_INI_DIR unset) - cannot test/z Examples (z) not found - cannot test.r)syspath originalPathmodulescopyoriginalModulesrfakeErrpatchfakeOutrosenvironchildKeyErrorrexampleRelativePathsplitexists examplePathappendparentbasename __import__example)selfhere childName moduleNames B/usr/lib/python3/dist-packages/twisted/names/test/test_examples.pysetUpzExampleTestBase.setUp!s`  HHQK"{{//1 z  3$,,/z  3$,,/ BJJ}56< !*- # G  s +F''F<ctjjtjj|j|j tj ddy)z Remove the example directory from the path and remove all modules loaded by the test from sys.modules. N)rrclearupdaterrr)r&s r*tearDownzExampleTestBase.tearDownEs?  4//0'' c|jj5}|j|jj ddddy#1swYyxYw)zK The example scripts start with the standard shebang line. s#!/usr/bin/env pythonN)r open assertEqualreadlinerstrip)r&fs r* test_shebangzExampleTestBase.test_shebangNsQ   " " $ N   QZZ\0024L M N N Ns /AAc|jt|jjdd|jj j }|j|djdd|dy)z The example script prints a usage message to stdout if it is passed a --help option and then exits. The first line should contain a USAGE summary, explaining the accepted command arguments. Nz--helprUsage:=Usage message first line should start with "Usage:". Actual: ) assertRaises SystemExitr%mainrgetvalue splitlines assertTrue startswith)r&outs r*test_usageConsistencyz%ExampleTestBase.test_usageConsistencyUsg *dll&7&7xHll##%002  F  h 'F % r0cT|jt|jjdd|jj j }|j|djdd|d|j|djdd|dy) a` The example script prints a usage message to stderr if it is passed unrecognized command line arguments. The first line should contain a USAGE summary, explaining the accepted command arguments. The last line should contain an ERROR summary, explaining that incorrect arguments were supplied. Nz--unexpected_argumentrr9r:zERROR:z;Usage message last line should start with "ERROR:" Actual: ) r;r<r%r=rr>r?r@rA)r&errs r*test_usageConsistencyOnErrorz,ExampleTestBase.test_usageConsistencyOnErrorhs *dll&7&7?VWll##%002  F  h 'F %  G  x (G & r0N) __name__ __module__ __qualname____doc__r+r/r7rCrGr0r*rrs" ".H(N & r0rceZdZdZdZy) TestDnsTestsz- Test the testdns.py example script. znames/examples/testdns.pyNrHrIrJrKrrLr0r*rNrNs6r0rNceZdZdZdZy)GetHostByNameTestsz3 Test the gethostbyname.py example script. znames/examples/gethostbyname.pyNrOrLr0r*rQrQs<r0rQceZdZdZdZy)DnsServiceTestsz1 Test the dns-service.py example script. znames/examples/dns-service.pyNrOrLr0r*rSrSs:r0rSceZdZdZdZy)MultiReverseLookupTestsz: Test the multi_reverse_lookup.py example script. z&names/examples/multi_reverse_lookup.pyNrOrLr0r*rUrUsCr0rU)rKrriortwisted.python.filepathrtwisted.trial.unittestrrrrNrQrSrUrLr0r*rYsb  ,5p p f6?H6<(<:ox:CoxCr0