U
    ©€ljÍ  ã                   @   s\   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZmZ G dd„ de	ƒZdS )	z1Test handling of ``pyproject.toml`` configurationé    N)ÚPath)ÚOptionalÚUnionÚcast)ÚTestCase)Ú_IncrementalConfigÚ_load_pyproject_tomlc                   @   s~   e Zd ZdZddœeeeedf ee dœdd„Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚVerifyPyprojectDotTomlTestsz/Test the `_load_pyproject_toml` helper functionN©Úpath)Útomlr   Úreturnc             
   C   sˆ   |dkr|   ¡ }nt|ƒ}t|dƒ�}| |¡ W 5 Q R X z
t|ƒW S  tk
r‚ } z t|dƒrp| d|› �¡ ‚ W 5 d}~X Y nX dS )zÈ
        Read a TOML snipped from a temporary file with `_load_pyproject_toml`

        @param toml: TOML content of the temporary file

        @param path: Path to which the TOML is written
        NÚwÚadd_notezWhile loading:

)ÚmktempÚstrÚopenÚwriter   Ú	ExceptionÚhasattrr   )Úselfr   r   Zpath_ÚfÚe© r   úD/tmp/pip-unpacked-wheel-1x2dpkdc/incremental/tests/test_pyproject.pyÚ	_loadToml   s    


z%VerifyPyprojectDotTomlTests._loadTomlc                 C   s*   t j tt|  ¡ ƒd¡}|  tt|¡ dS )zf
        An absent ``pyproject.toml`` file produces no result unless
        there is opt-in.
        úpyproject.tomlN)	Úosr   Újoinr   r   r   ÚassertRaisesÚFileNotFoundErrorr   )r   r   r   r   r   Útest_fileNotFound,   s    z-VerifyPyprojectDotTomlTests.test_fileNotFoundc                 C   s   d}|   t| j|¡ dS )zˆ
        Syntactially invalid TOML produces an exception. The specific
        exception varies by the underlying TOML library.
        z[project]
name = "abcN)r   r   r   ©r   r   r   r   r   Útest_brokenToml4   s    z+VerifyPyprojectDotTomlTests.test_brokenTomlc                 C   s   dD ]}|   t| j|¡ qdS )zP
        `ValueError` is raised when we can't extract the project name.
        )Ú
z[tool.notincremental]
z
[project]
z[tool.incremental]
z[project]
[tool.incremental]
N©r   Ú
ValueErrorr   r"   r   r   r   Útest_nameMissing<   s    z,VerifyPyprojectDotTomlTests.test_nameMissingc              
   C   s0   dD ]&}|   td¡� |  |¡ W 5 Q R X qdS )zM
        `TypeError` is raised when the project name isn't a string.
        )z[project]
name = false
z[tool.incremental]
name = -1
z([tool.incremental]
[project]
name = 1.0
z!The project name must be a stringN)ÚassertRaisesRegexÚ	TypeErrorr   r"   r   r   r   Útest_nameInvalidOptInI   s    z1VerifyPyprojectDotTomlTests.test_nameInvalidOptInc                 C   s   dD ]}|   t| j|¡ qdS )zm
        `ValueError` is raised when the ``[tool]`` or ``[tool.incremental]``
        isn't a table.
        )ztool = false
z[tool]
incremental = false
z[tool]
incremental = 123
z[tool]
incremental = null
Nr%   r"   r   r   r   Útest_toolIncrementalInvalidU   s    z7VerifyPyprojectDotTomlTests.test_toolIncrementalInvalidc                 C   s   dD ]}|   t| j|¡ qdS )zx
        Raise `ValueError` when the ``[tool.incremental]`` section contains
        keys other than ``"name"``
        )z[tool.incremental]
foo = false
z-[tool.incremental]
name = "OK"
other = false
Nr%   r"   r   r   r   Ú!test_toolIncrementalUnexpecteKeysb   s    z=VerifyPyprojectDotTomlTests.test_toolIncrementalUnexpecteKeysc              	   C   s^   t |  ¡ ƒ}|d d }|jdd� dD ]0}| j||d d�}|  |tddt|ƒd	�¡ q(d
S )zá
        The package has opted-in to Incremental version management when
        the ``[tool.incremental]`` section is a dict. The project name
        is taken from ``[tool.incremental] name`` or ``[project] name``.
        ÚsrcÚfooT©Úparents)z*[project]
name = "Foo"
[tool.incremental]
z [tool.incremental]
name = "Foo"
r   r
   ÚFoo©Zopt_inÚpackager   N©r   r   Úmkdirr   ÚassertEqualr   r   )r   ÚrootÚpkgr   Úconfigr   r   r   Útest_setuptoolsOptInm   s    ýþz0VerifyPyprojectDotTomlTests.test_setuptoolsOptInc              	   C   sB   t |  ¡ ƒ}| ¡  |  td¡� | jd|d d� W 5 Q R X dS )zM
        Raise `ValueError` when the package root can't be resolved.
        z$Can't find the directory of project z[project]
name = "foo"
r   r
   N)r   r   r5   r(   r&   r   )r   r7   r   r   r   Útest_packagePathRequired†   s    þz4VerifyPyprojectDotTomlTests.test_packagePathRequiredc                 C   sT   t |  ¡ ƒ}|d d }|jdd� | jd|d d�}|  |tdd	t|ƒd
�¡ dS )z„
        The ``[tool.incremental]`` table is not strictly required, but its
        ``opt_in=False`` indicates its absence.
        r-   r.   Tr/   z[project]
name = "Foo"
r   r
   Fr1   r2   Nr4   )r   r7   r8   r9   r   r   r   Útest_noToolIncrementalSection“   s    þýþz9VerifyPyprojectDotTomlTests.test_noToolIncrementalSection)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r!   r#   r'   r*   r+   r,   r:   r;   r<   r   r   r   r   r	      s    ÿ þr	   )r@   r   Úpathlibr   Útypingr   r   r   Ztwisted.trial.unittestr   Úincrementalr   r   r	   r   r   r   r   Ú<module>   s   