o
    b                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 G dd dZ
G dd	 d	e
e	ZG d
d de
e	ZG dd de
e	ZG dd de
e	ZdS )z-
Tests for L{twisted.names} example scripts.
    N)StringIO)FilePath)SkipTestTestCasec                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )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                 C   s   t jdd | _t j | _t | _| t d| j t | _	| t d| j	 zt
tjd d}W n ty=   tdw | jdD ]}||}| sXtd|j d	qD|| _t j| j j | j d
d }t|| _dS )zn
        Add our example directory to the path and record which modules are
        currently loaded.
        NstderrstdoutTOX_INI_DIRdocsz5Examples not found ($TOX_INI_DIR unset) - cannot test/z
Examples (z) not found - cannot test.r   )syspathoriginalPathmodulescopyoriginalModulesr   fakeErrpatchfakeOutr   osenvironchildKeyErrorr   exampleRelativePathsplitexistsexamplePathappendparentbasename
__import__example)selfhere	childName
moduleName r'   B/usr/lib/python3/dist-packages/twisted/names/test/test_examples.pysetUp!   s,   
zExampleTestBase.setUpc                 C   s,   t j  t j| j | jt jdd< dS )z
        Remove the example directory from the path and remove all
        modules loaded by the test from sys.modules.
        N)r   r   clearupdater   r   r   )r#   r'   r'   r(   tearDownE   s   
zExampleTestBase.tearDownc                 C   sD   | j  }| |  d W d   dS 1 sw   Y  dS )zK
        The example scripts start with the standard shebang line.
        s   #!/usr/bin/env pythonN)r   openassertEqualreadlinerstrip)r#   fr'   r'   r(   test_shebangN   s   "zExampleTestBase.test_shebangc                 C   sF   |  t| jjdd | j  }| |d dd|d f  dS )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--helpr   Usage:?Usage message first line should start with "Usage:". Actual: %r)	assertRaises
SystemExitr"   mainr   getvalue
splitlines
assertTrue
startswith)r#   outr'   r'   r(   test_usageConsistencyU   s   
z%ExampleTestBase.test_usageConsistencyc                 C   sf   |  t| jjdd | j  }| |d dd|d f  | |d dd|d f  dS )	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_argumentr   r3   r4   zERROR:z=Usage message last line should start with "ERROR:" Actual: %r)	r5   r6   r"   r7   r   r8   r9   r:   r;   )r#   errr'   r'   r(   test_usageConsistencyOnErrorh   s   z,ExampleTestBase.test_usageConsistencyOnErrorN)	__name__
__module____qualname____doc__r)   r,   r2   r=   r@   r'   r'   r'   r(   r      s    $	r   c                   @      e Zd ZdZdZdS )TestDnsTestsz-
    Test the testdns.py example script.
    znames/examples/testdns.pyNrA   rB   rC   rD   r   r'   r'   r'   r(   rF          rF   c                   @   rE   )GetHostByNameTestsz3
    Test the gethostbyname.py example script.
    znames/examples/gethostbyname.pyNrG   r'   r'   r'   r(   rI      rH   rI   c                   @   rE   )DnsServiceTestsz1
    Test the dns-service.py example script.
    znames/examples/dns-service.pyNrG   r'   r'   r'   r(   rJ      rH   rJ   c                   @   rE   )MultiReverseLookupTestsz:
    Test the multi_reverse_lookup.py example script.
    z&names/examples/multi_reverse_lookup.pyNrG   r'   r'   r'   r(   rK      rH   rK   )rD   r   r   ior   twisted.python.filepathr   twisted.trial.unittestr   r   r   rF   rI   rJ   rK   r'   r'   r'   r(   <module>   s   s