
    -Ph]Q                         d dl Z d dlZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ dd	lmZmZmZ 	 ddZd ZddZ G d de
          ZdS )    N)Pathcall)	CliRunner)TestCase)check)_main   )setup_simple_projectwith_isolated_runnerwritepyproject.tomlmain c                     t          | |           t          d                              d           t          |           t	          g d           dS )z
    Create the project files in the main branch that already has a
    news-fragment and then switch to a new in-work branch.
    )pyproject_pathextra_configfoo/newsfragments/123.featureAdds levitationbranchgitcheckout-botherbranchN)r   r   
write_textinitial_commitr   )r   main_branchr   s      Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/towncrier/test/test_check.pycreate_projectr!      sa     \RRRR	())445FGGG+&&&&	1	1	122222    c                 N    t          g d           t          ddd| g           dS )zStage and commit the repo in the current working directory

    There must be uncommitted changes otherwise git will complain:
    "nothing to commit, working tree clean"
    )r   add.r   commit-mNr   )messages    r    r&   r&   "   s6     				%4	)*****r"   c                     t          ddd|  g           t          g d           t          g d           t          d           dS )z
    Create a git repo, configure it and make an initial commit

    There must be uncommitted changes otherwise git will complain:
    "nothing to commit, working tree clean"
    r   initz--initial-branch=)r   configz	user.nameuser)r   r+   z
user.emailzuser@example.comzInitial CommitN)r   r&   r   s    r    r   r   ,   sc     	%5V55	6777 		/	/	/000	<	<	<===
r"   c                      e Zd ZdZd Zd Zd Zd Zed             Z	ed             Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zed             Zed             Zed             Zed             Zed             Zed             ZdS )TestCheckerNc                 @   t                      }|                                5  t          d           |                    t          ddg          }|                     d|j                   |                     d|j                   ddd           dS # 1 swxY w Y   dS )zz
        If git fails to report a comparison, git's output is reported to aid in
        debugging the situation.
        r   --compare-withhblaughz!git produced output while failingN)r   isolated_filesystemr!   invoketowncrier_checkassertInoutputselfrunnerresults      r    test_git_failszTestChecker.test_git_fails@   s    
 '')) 	4 	4+,,,]]?5Ey4QRRFMM=v}MMMMM)V]333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   A#BBBc                 4    |                      dd            d S )Nr   c                 .    |                      ||          S )Nr3   )r9   r   argvs      r    <lambda>z2TestChecker.test_no_changes_made.<locals>.<lambda>O   s    tT9R9R r"   _test_no_changes_made)r8   s    r    test_no_changes_madez TestChecker.test_no_changes_madeM   s-    ""RR	
 	
 	
 	
 	
r"   c                 >    d|                      fd           d S )Nznot-pyproject.tomlc                 :    |                      ||dgz             S )N--configr>   )r9   r   r?   	pyprojects      r    r@   z>TestChecker.test_no_changes_made_config_path.<locals>.<lambda>V   s$    v}}dj)44( ( r"   rA   )r8   rG   s    @r     test_no_changes_made_config_pathz,TestChecker.test_no_changes_made_config_pathR   sA    (	""   	
 	
 	
 	
 	
r"   c                 >   t                      }|                                5  t          |d            ||t          ddg          }|                     d|j        |j                   |                     d|j                   ddd           dS # 1 swxY w Y   dS )zT
        When no changes are made on a new branch, no checks are performed.
        masterr   r0   r   z<On master branch, or no diffs, so no newsfragment required.
N)r   r2   r!   r4   assertEqual	exit_coder6   )r8   r   r3   r9   r:   s        r    rB   z!TestChecker._test_no_changes_made[   s     '')) 		 		>x@@@@VFO6F5QRRFQ 0&-@@@O  		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s   A"BBBc                    t          d           t          dd           t          d           t          d                                          }t          |d           t          d           |                    t          dd	g          }|                     |j        	                    d
t          |          z   dz             |j        t          |          f           |                     d|j        |j                   d S )Nr   foo/somefile.py	import os
add a filefoo/newsfragments/1234.featureAdds gravity backadd a newsfragmentr0   r   
Found:
1. 
r   r!   r   r&   r   absoluter3   r4   
assertTruer6   endswithstrrL   rM   r8   r9   fragment_pathr:   s       r    test_fragment_existsz TestChecker.test_fragment_existsl   s    '(((---|=>>GGIIm0111#$$$1A60JKKM""=3}3E3E#E#LMM]C../	
 	
 	
 	F,fm<<<<<r"   c                    t          dd           t          dd           t          d           t          d                                          }t          |d           t          d	           |                    t          d
dg          }|                     |j        	                    dt          |          z   dz             |j        t          |          f           |                     d|j        |j                   dS )zY
        Location of fragments can be configured using tool.towncrier.directory.
        r   zdirectory = 'deep/fragz'
r   zfoo/bar/somefile.pyrP   rQ   zdeep/fragz/1234.featurerS   rT   r0   r   rU   rV   r   NrW   r\   s       r    test_fragment_exists_hiddenz'TestChecker.test_fragment_exists_hidden   s    
 	'6RSSSS#[111|677@@BBm0111#$$$1A60JKKM""=3}3E3E#E#LMM]C../	
 	
 	
 	F,fm<<<<<r"   c                 *   t                      }|                                5  t          dd           d}t          |d          5 }|                    d           d d d            n# 1 swxY w Y   t          g d           t          g d           |                    t          d	dg          }|                     d
|j	                   | 
                    |j                            d                     d d d            d S # 1 swxY w Y   d S )Nr   rJ   rK   rO   wrP   )r   r$   rO   )r   r&   r'   rQ   r0   r
   +No new newsfragments found on this branch.
)r   r2   r!   openr   r   r3   r4   rL   rM   rY   r6   rZ   )r8   r9   	file_pathfr:   s        r    test_fragment_missingz!TestChecker.test_fragment_missing   s   '')) 	 	+BBBB)Ii%% %$$$% % % % % % % % % % % % % % % 222333666777]]?5Ex4PQQFQ 0111OO&&'UVV  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s6   $DA)D)A-	-D0A-	1B
DDDc           	      ^   t                      }|                                5  t          ddd           d}t          |d           t	          d                                          }t          |d           t          d	           |                    t          d
dg          }| 	                    d|j
                   |                     |j                            dt          |          z   dz             |j        t          |          f           ddd           dS # 1 swxY w Y   dS )zLA fragment that exists but is marked as check=False is ignored by the check.r   rJ   [[tool.towncrier.type]]
directory = "feature"
name = "Features"
showcontent = true
[[tool.towncrier.type]]
directory = "sut"
name = "System Under Test"
showcontent = true
check=false
r   r   rO   rP   zfoo/newsfragments/1234.sutrS   add a file and a newsfragmentr0   r
   z9Found newsfragments of unchecked types in the branch:
1. rV   Nr   r2   r!   r   r   rX   r&   r3   r4   rL   rM   rY   r6   rZ   r[   r8   r9   rf   r]   r:   s        r    %test_fragment_exists_but_not_in_checkz1TestChecker.test_fragment_exists_but_not_in_check   s   ''))  	  	 $     *I)[))) !=>>GGIIM-!45552333]]?5Ex4PQQFQ 0111OO&&P-(() 
 M 2 23  3 	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	   C2D""D&)D&c           	      ^   t                      }|                                5  t          ddd           d}t          |d           t	          d                                          }t          |d           t          d	           |                    t          d
dg          }| 	                    d|j
                   |                     |j                            dt          |          z   dz             |j        t          |          f           ddd           dS # 1 swxY w Y   dS )z
        A fragment that exists but is not marked as check=False is
        not ignored by the check, even if other categories are marked as check=False.
        r   rJ   rj   rk   rO   rP   rR   rS   rl   r0   r   rU   rV   Nrm   rn   s        r    !test_fragment_exists_and_in_checkz-TestChecker.test_fragment_exists_and_in_check   su   
 '')) 	 	 $     *I)[))) !ABBKKMMM-!45552333]]?5Ex4PQQFQ 0111OO&&}s=7I7I'ID'PQQM 2 23  3	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	rp   c                 @   t                      }|                                5  t          dd           d}t          |d          5 }|                    d           ddd           n# 1 swxY w Y   t          dd	|g           t          g d
           t          d          }|                    t          ddg          }ddd           n# 1 swxY w Y   |                     d|j	                   |                     dt          |j                             dS )zS
        No failure when output is piped causing None encoding for stdout.
        r   rJ   rK   rR   rc   rS   Nr   r$   )r   r&   r'   rT   F)
mix_stderrr0   r   )r   r2   r!   re   r   r   r3   r4   rL   rM   lenstderr)r8   r9   r]   rg   r:   s        r    test_none_stdout_encoding_worksz+TestChecker.test_none_stdout_encoding_works   s    '')) 	R 	R+BBBB<MmS)) -Q+,,,- - - - - - - - - - - - - - - %.///>>>???%000F]]?5Ex4PQQF	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	F,---C../////s6   $CA)C)A-	-C0A-	1ACCCc                 F   t                      }|                                5  t                       |                     dt	          j        d                     |                    t          g d           t          d           | 	                    dt	          j        d                     |                    t          ddg          }|                     d|j        ||j        f           | 	                    d|j                   d	d	d	           d	S # 1 swxY w Y   d	S )
z
        The checks should be skipped on a branch that creates the news file.

        If the checks are not skipped in this case, towncrier check would fail
        for the first release that has a changelog.
        NEWS.rstr%   --yes	--versionz1.0zPrepare a releaser0   r   r   *Checks SKIPPED: news file changes detectedN)r   r2   r!   assertNotInoslistdirr3   towncrier_buildr&   r5   r4   rL   rM   r6   r7   s      r    test_first_releasezTestChecker.test_first_release
  s_    '')) 	W 	WZC999MM/+H+H+HIII&''' MM*bjoo666 ]]?5Ev4NOOF Q 066=2IJJJMMFVVV#	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	Ws   C&DDDc                 X   t                      }|                                5  t                       |                    t          g d           t          d           |                     dt          j        d                     t          g d           t          g d           t          g d           t          dd	           t          d
           t          g d           t          g d           t          g d           |                    t          g d           t          d           |                    t          ddg          }|                     d|j        ||j        f           |                     d|j                   ddd           dS # 1 swxY w Y   dS )z
        The checks for missing news fragments are skipped on a branch that
        modifies the news file.
        This is a hint that we are on a release branch
        and at release time is expected no not have news-fragment files.
        rz   zFirst releasery   r%   )r   r   r   )r   merger   r'   zSync release in main branch.)r   r   r   new-feature-branchzfoo/newsfragments/456.featurezFoo the barz A feature in the second release.)r   r   r   r'   zMerge new-feature-branch.)r   r   r   znext-release)r{   r|   z2.0zSecond releaser0   r   r   r}   N)r   r2   r!   r3   r   r&   r5   r   r   r   r   r4   rL   rM   r6   r7   s      r    test_release_branchzTestChecker.test_release_branch&  s    '')) '	W '	W MM/+H+H+HIII?###MM*bjoo666,,,---VVVWWW @@@AAA1=AAA5666,,,---     :::;;;MM/+H+H+HIII#$$$ ]]?5Ev4NOOF Q 066=2IJJJMMFVVVO'	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	W '	Ws   E/FF#&F#c                 6   t                      }|                                5  t                       |                    t                    }ddd           n# 1 swxY w Y   |                     d|j                   |                     d|j                   dS )zM
        If there's no recognized remote origin, exit with an error.
        Nr
   z+Could not detect default branch. Aborting.
)r   r2   r!   r3   r4   rL   rM   r6   r7   s      r    'test_get_default_compare_branch_missingz3TestChecker.test_get_default_compare_branch_missingX  s     '')) 	4 	4]]?33F	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4
 	F,---GWWWWWs   )AAAc                 ^    t          j        ddg          }|                     d|           dS )zY
        If there's a remote branch origin/main, prefer it over everything else.
        origin/masterzorigin/mainN)r   _get_default_compare_branchrL   )r8   r   s     r    $test_get_default_compare_branch_mainz0TestChecker.test_get_default_compare_branch_mainf  s5     2O]3STT/////r"   c                 4   t          j        d          5 }t          j        ddg          }ddd           n# 1 swxY w Y   |                     d|           |                     |d         j        j        d                             d                     dS )zQ
        If there's origin/master and no main, use it and warn about it.
        T)recordr   z
origin/fooNr   zUsing "origin/master)	warningscatch_warningsr   r   rL   rY   r(   args
startswith)r8   rc   r   s      r    (test_get_default_compare_branch_fallbackz4TestChecker.test_get_default_compare_branch_fallbackn  s     $D111 	XQ67VWWF	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	&111!)!,778NOOPPPPPs   9= =c           	         d}t          d                              d           t          d          }|dz                      d           |dz                      d           |d	z                      d           |d
z                      d           t          |           t	          g d           t          |dz  d           t          d           |                    t          dddt          |          ddf          }| 
                    d|j                   |                     |j                            d                     |dz                                  }t          |d           t          d           |                    t          d          }| 
                    d|j        |j                   |                     |j                            dt          |          z   dz             |j        t          |          f           t          d          }|dz                      d           |d	z                      d           t          |dz  d           t          d           |                    t          ddd|ddf          }| 
                    d|j        |j                   |                     |j                            dt          |          z   dz             |j        t          |          f           |                    t          ddd|ddf          }| 
                    d|j                   |                     |j                            d                     dS ) z
        It can check the fragments located in a sub-directory
        that is specified using the `--dir` CLI argument.
        r   r   z+[tool.towncrier]
directory = "changelog.d"
fooT)parentszfoo/__init__.pyr   zchangelog.dzchangelog.d/123.featurer   r   r   rO   rP   rQ   rF   --dirr0   r
   rd   zchangelog.d/124.featurerS   rT   )rF   r   r   r   r0   r   r   rU   rV   barzbar/somefile.pyN)r   r   mkdirr   r   r   r&   r3   r4   r[   rL   rM   rY   r6   rZ   rX   )r8   r9   r   subproject1r:   r]   subproject2s          r    =test_in_different_dir_with_nondefault_newsfragments_directoryzITestChecker.test_in_different_dir_with_nondefault_newsfragments_directoryx  s    )),	
 	
 	
 5kk	u	##D#111	(	(44R888	}	$++D+999	0	0<<=NOOOk****555666 	k--{;;;| K   

 

 	F,---M""#QRR	
 	
 	

 %'@@JJLLm0111#$$$T
 

 	F,fm<<<M""=3}3E3E#E#LMM]C../	
 	
 	
 5kk	u	##D#111	}	$++D+999k--{;;;|  

 

 	F,fm<<<M""=3}3E3E#E#LMM]C../	
 	
 	
   

 

 	F,---M""#QRR	
 	
 	
 	
 	
r"   c                 B   t          dd           t          dd           t          dd           t          dd	           t          d
d           t          d           |                    t          ddg          }|                     d|j        |j                   dS )z
        When `ignore` is set in config, files with those names are ignored.
        Configuration supports wildcard matching with `fnmatch`.
        r   z-ignore = ["template.jinja", "star_wildcard*"]r`   foo/newsfragments/124.feature+This fragment has valid name (control case)z foo/newsfragments/template.jinjazThis is manually ignoredzfoo/newsfragments/.gitignorez"gitignore is automatically ignoredz#foo/newsfragments/star_wildcard_fooz Manually ignored with * wildcard	add stuffr0   r   r   Nr!   r   r&   r3   r4   rL   rM   r6   r7   s      r    test_ignored_fileszTestChecker.test_ignored_files  s     	H	
 	
 	
 	

 	+9	
 	
 	
 	02LMMM,.RSSS35WXXX{1A60JKKF,fm<<<<<r"   c                 T   t          d           t          dd           t          dd           t          dd           t          d           |                    t          d	d
g          }|                     d|j        |j                   |                     d|j                   dS )zr
        Fails if a news fragment has an invalid name, even if `ignore` is not set in
        the config.
        r   r   r   zfoo/newsfragments/feature.125z+This has issue and category wrong way roundry   z@Modification of news file should not skip check of invalid namesr   r0   r   r
   z'Invalid news fragment name: feature.125N	r!   r   r&   r3   r4   rL   rM   r6   r5   r7   s      r    test_invalid_fragment_namez&TestChecker.test_invalid_fragment_name  s     	'(((+9	
 	
 	
 	+9	
 	
 	
 	N	
 	
 	
 	{1A60JKKF,fm<<<?OOOOOr"   c                    t          dd           t          dd           t          dd           t          d           |                    t          d	d
g          }|                     d|j        |j                   dS )M
        Fails if an issue name goes against the configured pattern.
        r   issue_pattern = "\\d+"r`   r   zThis fragment has a valid namez"foo/newsfragments/+abcdefg.featurez0This fragment has a valid name (orphan fragment)r   r0   r   r   Nr   r7   s      r    test_issue_patternzTestChecker.test_issue_pattern  s    
 	3	
 	
 	
 	
 	+,	
 	
 	
 	0>	
 	
 	
 	{1A60JKKF,fm<<<<<r"   c                    t          dd           t          dd           t          d           |                    t          ddg          }|                     d	|j        |j                   |                     d
|j                   dS )r   r   r   r`   z$foo/newsfragments/AAA.BBB.feature.md9This fragment has an invalid name (should be digits only)r   r0   r   r
   HError: Issue name 'AAA.BBB' does not match the configured pattern, '\d+'Nr   r7   s      r    &test_issue_pattern_invalid_with_suffixz2TestChecker.test_issue_pattern_invalid_with_suffix!  s    
 	3	
 	
 	
 	
 	2G	
 	
 	
 	{1A60JKKF,fm<<<WM	
 	
 	
 	
 	
r"   c                    t          dd           t          dd           t          d           |                    t          ddg          }|                     d	|j        |j                   |                     d
|j                   dS )r   r   r   r`   z!foo/newsfragments/AAA.BBB.featurer   r   r0   r   r
   r   Nr   r7   s      r    test_issue_pattern_invalidz&TestChecker.test_issue_pattern_invalid7  s    
 	3	
 	
 	
 	
 	/G	
 	
 	
 	{1A60JKKF,fm<<<WM	
 	
 	
 	
 	
r"   )__name__
__module____qualname__maxDiffr;   rC   rH   rB   r   r^   ra   rh   ro   rr   rw   r   r   r   r   r   r   r   r   r   r   r    r"   r    r.   r.   =   s       G4 4 4
 
 


 
 
  " = = =$ = = =*  ($ $ $L# # #J0 0 0,W W W80W 0W 0WdX X X0 0 0Q Q Q _
 _
 _
B = = =, P P P2 = = =* 
 
 
* 
 
 
 
 
r"   r.   )r   r   r   )r   )r   os.pathr   pathlibr   
subprocessr   click.testingr   twisted.trial.unittestr   	towncrierr   towncrier.buildr	   r   towncrier.checkr4   helpersr   r   r   r!   r&   r   r.   r   r"   r    <module>r      s:   
			               # # # # # # + + + + + +       4 4 4 4 4 4 4 4 4 4 4 4 F F F F F F F F F F GI
3 
3 
3 
3+ + +   "N
 N
 N
 N
 N
( N
 N
 N
 N
 N
r"   