
    wBf,                       d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ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 d dlZd
dlmZ d
dlmZ d
dlmZ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, erd dlm-Z- d dlm.Z.m/Z/m0Z0 d dl1m2Z2  ee3      Z4 e(ddd      d        Z5d Z6e G d d             Z7ejp                  d.d        Z9e G d! d"             Z:ejp                  d/d#       Z;e G d$ d%             Z<ejp                  	 	 	 	 	 	 d0d&       Z=e G d' d(             Z>ejp                  	 	 	 	 	 	 d1d)       Z? ejp                  d*+      d2d,       Z@ejp                  d3d-       ZAy)4    )annotationsN)contextmanagernullcontext)	dataclass)	getLogger)join)Path)copyfile)TYPE_CHECKINGoverload   )EntityEncoder)PACKAGE_CACHE_MAGIC_FILE)contextreset_context)main_subshell)on_win)path_to_url)PackageCacheData)
deprecated)CondaExitZero)PackageRecord)Iterator)CaptureFixtureExceptionInfoMonkeyPatch)MockerFixturez24.9z25.3z=It don't matter which environment the test suite is run from.)addendumc                 |   dt         j                  v rt        t         j                  d         j                         t        t        j
                        j                         k7  rWt        j                  dt         j                  d    dt        j
                          t        rt	        j                  d       y y y y )NCONDA_PYTHON_EXEzERROR :: Running tests from a non-base Python interpreter.  Tests requires installing menuinst and that causes stderr  output when activated.
- CONDA_PYTHON_EXE=z
- sys.executable=)
osenvironr	   resolvesys
executablewarningswarnr   exit     6lib/python3.12/site-packages/conda/testing/__init__.py*conda_ensure_sys_python_is_base_env_pythonr-   4   s     RZZ'./088:CNN#++-. MM& ')jj1C&D%E F$$'NN#3	5  . (r+   c                    dt         j                  v rsddlm} m} t         j
                  dk(  r| }n|} |       }d }|j                  t         j                  d         }g }d}|D ]  }|j                  d      rq|rd}t        t        j                  d      |k7  r=t        t        j                  d      }t        d| d	|        |j                  |       s|j                  |       |j                  |        t         j                  j                  |      t         j                  d
<    |       }|j                  t         j                  d         }t         j                  j                  |      t         j                  d
<   y y )NCONDA_PREFIXr   )CmdExeActivatorPosixActivatorntFcondabinTzIncorrect condabin, swapping z to PATH)r"   r#   activater0   r1   name_remove_prefix_from_pathendswithr   r%   prefixprintappendpathsep_add_prefix_to_path)	r0   r1   activator_cls	activatorpnew_pfound_condabinpecondabin_paths	            r,   conda_move_to_front_of_PATHrE   S   s9   #>77d?+M*M!O	  ..rzz./IJ  	!B{{:&%%)NCJJ
3r9(,SZZ(D =bTm_UV]3R(R 	!  ZZ__U3

6!O	))"**^*DEZZ__Q/

6W $r+   c                  `    e Zd ZU ded<   e	 	 	 	 	 	 dd       Zed	d       Zdd	 	 	 	 	 d
dZy)CondaCLIFixturer   capsysc                    y Nr*   )selfraisesargvs      r,   __call__zCondaCLIFixture.__call__   s    
 *-r+   c                     y rJ   r*   )rK   rM   s     r,   rN   zCondaCLIFixture.__call__   s    QTr+   N)rL   c               J   | j                   j                          t        t        t        |            }d}|rt        j                  |      n	t               5 }t        | }ddd       | j                   j                         \  }}t                |||rfS |fS # 1 sw Y   :xY w)a  Test conda CLI. Mimic what is done in `conda.cli.main.main`.

        `conda ...` == `conda_cli(...)`

        :param argv: Arguments to parse.
        :param raises: Expected exception to intercept. If provided, the raised exception
            will be returned instead of exit code (see pytest.raises and pytest.ExceptionInfo).
        :return: Command results (stdout, stderr, exit code or pytest.ExceptionInfo).
        N)
rH   
readouterrtuplemapstrpytestrL   r   r   r   )rK   rL   rM   code	exceptionouterrs          r,   rN   zCondaCLIFixture.__call__   s     	  Sd^$ &,V]]6"+- 	(9 $'D	( ;;))+S 	Cf66$66	( 	(s   	BB")rM   str | os.PathLike | PathrL   z-type[Exception] | tuple[type[Exception], ...]returnztuple[str, str, ExceptionInfo])rM   rZ   r[   ztuple[str, str, int])rM   rZ   rL   z4type[Exception] | tuple[type[Exception], ...] | Noner[   z$tuple[str, str, int | ExceptionInfo])__name__
__module____qualname____annotations__r   rN   r*   r+   r,   rG   rG      so    -'- >- 
(	- - T T
 HL7'7 E7 
.	7r+   rG   c              #  &   K   t        |        yw)z+Fixture returning CondaCLIFixture instance.N)rG   )rH   s    r,   	conda_clira      s      &
!!   c                  6    e Zd ZU ded<   	 	 	 d	 	 	 	 	 	 	 ddZy)PathFactoryFixturer	   tmp_pathNc                    |xs d}|xs t        j                         j                  }|xs d}| j                  ||z   |z   z  S )a  Unique, non-existent path factory.

        Extends pytest's `tmp_path` fixture with a new unique, non-existent path for usage in cases
        where we need a temporary path that doesn't exist yet.

        :param name: Path name to append to `tmp_path`
        :param prefix: Prefix to prepend to unique name generated
        :param suffix: Suffix to append to unique name generated
        :return: A new unique path
         )uuiduuid4hexre   )rK   r6   r9   suffixs       r,   rN   zPathFactoryFixture.__call__   sD      2'tzz|''2}} 677r+   )NNN)r6   
str | Noner9   rl   rk   rl   r[   r	   )r\   r]   r^   r_   rN   r*   r+   r,   rd   rd      s=    N  !!	88 8 	8
 
8r+   rd   c              #  &   K   t        |        yw)z.Fixture returning PathFactoryFixture instance.N)rd   )re   s    r,   path_factoryrn      s      X
&&rb   c                  D    e Zd ZU ded<   ded<   edd	 	 	 	 	 dd       Zy)	TmpEnvFixturerd   rn   rG   ra   N)r9   c             '     K   t        |xs | j                               } | j                  dd|g|dd  | yw)a   Generate a conda environment with the provided packages.

        :param packages: The packages to install into environment
        :param prefix: The prefix at which to install the conda environment
        :return: The conda environment's prefix
        createz--prefix--yes--quietN)r	   rn   ra   )rK   r9   packagess      r,   rN   zTmpEnvFixture.__call__   sF      f3 1 1 34xVShSSSs   <>)ru   rT   r9   zstr | os.PathLike | Noner[   zIterator[Path]r\   r]   r^   r_   r   rN   r*   r+   r,   rp   rp      sA    $$ ,0 ) 
	 r+   rp   c              #  (   K   t        | |       yw)z)Fixture returning TmpEnvFixture instance.N)rp   rn   ra   s     r,   tmp_envry      s      i
00   c                  4    e Zd ZU ded<   ded<   edd       Zy)TmpChannelFixturerd   rn   rG   ra   c              '    K    | j                   dd| j                          g|ddddt        i t        t	        j
                         j                        }t	        |      }| j                         }|t        j                  z  }|j                  d       |d	z  }|j                  d       i i d
}|D ]t  }|j                  |      D ]^  }	|	d   }
t        ||
z  ||
z         t        di |	j                         j                         D ci c]  \  }}|dvr|| c}}|d   |
<   ` v |dz  j                  t!        j"                  |t$                     |dz  j                  t!        j"                  i t$                     |D ]"  }t'        t	        j(                  |            r"J  |t+        t-        |            f y c c}}w w)Nrr   z	--prefix=rs   rt   z--download-onlyrL   Tparentsnoarch)inforu   fn)urlchannelschannelru   zrepodata.json)clsr*   )ra   rn   r   r	   r   first_writablepkgs_dirr   subdirmkdirqueryr
   r   dumpitems
write_textjsondumpsr   any	query_allr   rT   )rK   ru   r   
pkgs_cacher   r   r   repodatapackagepkg_datafnamefieldvalues                r,   rN   zTmpChannelFixture.__call__   s     	))+,-	
 	
 		

 	
 	
 !	
 (779BBC%h/
##%7>>)T"8#T"B/ 	G&,,W5  E)6E>:.; / -5MMO,A,A,C(E5 (FF u/$U+	 
/	!--djj}.UV	/	!--djj.OP 	<G'11':;;;	< {3w<000s   DGG!BG/"GN)ru   rT   r[   zIterator[tuple[Path, str]]rv   r*   r+   r,   r|   r|      s     $$*1 *1r+   r|   c              #  (   K   t        | |       yw)z-Fixture returning TmpChannelFixture instance.N)r|   rx   s     r,   tmp_channelr   ,  s      L)
44rz   monkeypatch)r6   c              #  6  K   |  | j                   D cg c]+  \  }}}|t        j                  u r|j                  d      r|- c}}}x}rCt        j                  ddj                  |              | j                          t        g        yyc c}}}w w)z9A monkeypatch fixture that resets context after each testCONDA_z+monkeypatch cleanup: undo & reset context: z, N)	_setitemr"   r#   
startswithlogdebugr   undor   )r   objr6   _
conda_varss        r,   context_aware_monkeypatchr   5  s      
 (00 Cq"**!: 	 z 
 			?		*@U?VWXb s   B0BABc              #  :  K    |        dz  }|j                  d       |t        z  j                          |j                  d|j                  t        |      x}f       t        j                  |fk(  sJ | t        j                  j                  |d        y w)NpkgsTr~   z$conda.base.context.Context.pkgs_dirs)new_callablereturn_value)r   r   touchpatchPropertyMockrT   r   	pkgs_dirsr   _cache_pop)rn   mockerr   pkgs_dir_strs       r,   tmp_pkgs_dirr   F  s     ~&HNN4N ((//1
LL.((&)(m3l5  
 ///
N  t4s   BB)rH   r   r[   rG   )re   r	   r[   rd   )rn   rd   ra   rG   r[   rp   )rn   rd   ra   rG   r[   r|   )r   r   r[   r   )rn   rd   r   r   r[   r	   )B
__future__r   r   r"   r%   rh   r'   
contextlibr   r   dataclassesr   loggingr   os.pathr   pathlibr	   shutilr
   typingr   r   rU   auxlib.entityr   base.constantsr   base.contextr   r   cli.mainr   common.compatr   
common.urlr   core.package_cache_datar   deprecationsr   
exceptionsr   models.recordsr   r   r   r   r   pytest_mockr   r\   r   r-   rE   rG   fixturera   rd   rn   rp   ry   r|   r   r   r   r*   r+   r,   <module>r      s   #  	 
   2 !     *  ) 5 1 $ " $ 6 % & *AA) 

L

4,0^ +7 +7 +7\ " "
 8 8 82 ' '
   0 1$11 1 1 /1 /1 /1d 5$55 5 5 ]# $  5 5r+   