o nnhb@sdZddlmZddlZddlmZddlZddlZddl Z ddl Z ddlZddl Z ddl mZmZddlmZddlmZmZmZmZddlmZmZd ZdfddZdgdhddZ   didjd d!Z " "dkdld&d'Zdmd)d*Zdmd+d,Z dndod.d/Z!dndod0d1Z"dmd2d3Z#dgdpd6d7Z$dgdpd8d9Z%dgdpd:d;Z&dqdTest target identification, iteration and inclusion/exclusion.) annotationsN)to_bytesto_text)read_text_file)ApplicationErrordisplayread_lines_without_comments is_subdir) data_contextcontent_plugins).py.ps1 target_func,c.Callable[[], c.Iterable[CompletionTarget]]prefixstrshortboolreturn list[str]c CsLz|}tt|||}|WSty%}z d|gWYd}~Sd}~ww)zUReturn a list of targets from the given target function which match the given prefix.z%sN)listwalk_completion_targets Exception)rrrtargetsmatchesexrH/usr/local/lib/python3.10/dist-packages/ansible_test/_internal/target.pyfind_target_completion%srFrc.Iterable[CompletionTarget]tuple[str, ...]cstdd|D}dr|vr|fdd|D}|rAttjrAd7fdd|D}t|dkrA|}tt|S)zVReturn a tuple of targets from the given target iterable which match the given prefix.css |] }|jD]}|VqqdSN)aliases).0targetaliasrrr 1z*walk_completion_targets../cs.g|]}|rd|tdvr|qS)r)) startswithlen)r$r&rrr 6s.z+walk_completion_targets..rcs$g|]}t|kr|dqSr")r,)r$match)offsetrrr.<$) setendswithremover,ospathdirnametuplesorted)rrrr#rrelative_matchesr)r0rrr/s   rc.Iterable[TCompletionTarget]includest.Optional[list[str]]excludesrequirestuple[TCompletionTarget, ...]cst|}tt||ddd}|r!tt||fdd|D}|r,tt||ddtt||ddd}tt|d ddS) z.Return a tuple of matching completion targets.cS|jSr"name)include_targetrrrLz'walk_internal_targets..)keycsg|]}|vr|qSrr)r$require_targetrequire_targetsrrr.Pz)walk_internal_targets..F)include)errorsrLcSrAr"rB) sort_targetrrrrEVrF)r8r9filter_targetsr2r)rr<r>r?include_targetsinternal_targetsrrIrwalk_internal_targetsCsrRTpatternsrLrMc csLt|pd}|rtdd|Dnd}|D]}t}d}|rb|jD]>} |D]9} || | r_d}z|| Wn tyAYnw| dr_|jrZt|jt| krZ| |jq&| | q&q"n/|rd}|jsn| d|jD]} | dr|jrt|jt| kr| |jqq| | qq||krq|Vq|r|rt |dSdS) zOIterate over the given targets and filter them based on the supplied arguments.rcss"|] }|td|fVqdS)z^%s$N)recompiler$prrrr'as z!filter_targets..NFTr).) r2dictr#r/r4KeyErrorr3 base_pathr,addTargetPatternsNotMatched) rrSrLrM unmatchedcompiled_patternsr%matched_directoriesr/r&patternrrrrOYsR         rOc.Iterable[TestTarget]ccs4ttjjtjjtdD] }|jsq|VqdS)z(Iterate through the module test targets.)r6 module_path extensionsN)walk_test_targetsr contentrcMODULE_EXTENSIONSmodule)r%rrrwalk_module_targetss ricCsttjjtjjdddS)z$Return an iterable of units targets.r test_)r6rcrdr)rer rf unit_pathunit_module_pathrrrrwalk_units_targetssrninclude_symlinkscCsttjjdd|dS)z&Return an iterable of compile targets.rj)bin)rcrd extra_dirsrorer rfrcrorrrwalk_compile_targetssrtcCsttjjd|dS)z)Return an iterable of PowerShell targets.)rz.psm1)rcrdrorrrsrrrwalk_powershell_targetsrucCsttjjdddS)z%Return an iterable of sanity targets.T)rcroinclude_symlinked_directoriesrrrrrrwalk_sanity_targetsrvrxinclude_hiddenc.Iterable[IntegrationTarget]cc0tD]}d|jvs|rd|jvr|VqdS)z0Return an iterable of POSIX integration targets.zposix/z hidden/posix/Nwalk_integration_targetsr#ryr%rrrwalk_posix_integration_targets  rccr{)z2Return an iterable of network integration targets.network/zhidden/network/Nr|r~rrr walk_network_integration_targetsrrccr{)z2Return an iterable of windows integration targets.zwindows/zhidden/windows/Nr|r~rrr walk_windows_integration_targetsrrc #s$tjj}tddtD}tj|}t}t|t j j }d}d}g|D]}|D]} t j t j j ||q/q+tdd|D} | fdd|D|| vr^| |d} g}t| D]} | ru| | dt| kruqf| } | t j j | qf|D] }tt|||VqdS)z*Return an iterable of integration targets.css|]}|jVqdSr")rhr$r%rrrr'sz+walk_integration_targets..)defaultsfileshandlersmetatasks templatesvars)zmain.ymlz main.yamlcss*|]}ttj|tjjVqdSr")r8r5r6r7splitseprVrrrr's"c3sH|]tfddDrttjtjtjjVqdS)c3s|]}|VqdSr")r3)r$ entry_pointrWrrr'z5walk_integration_targets...N)anyr8r5r6r7rr)r$ entry_pointsrrr's,N)r rfintegration_targets_path frozensetri walk_filesload_integration_prefixesr8rr5r6rappendjoinr2updater4r9r,IntegrationTargetr) r6modulespathsprefixestargets_path_tuple entry_dirs entry_files entry_dir entry_file path_tuplesprevious_path_tuple path_tuplerrrr}s>    r}dict[str, str]csttjj}tddtj|D}i}|D] }tj|ddd|t fddt | Dq|S)z.Load and return the integration test prefixes.css0|]}tjtj|ddkr|VqdS)rztarget-prefixesN)r5r6splitextbasename)r$frrrr's.z,load_integration_prefixes..rNc3s|]}|fVqdSr"r)r$kr-rrr's) r rfintegration_pathr9 get_filesr5r6rrrYr splitlines)r6 file_pathsr file_pathrr-rrs &rr6t.Optional[str]rcrdt.Optional[tuple[str, ...]]rqrwc cs|r tjj||d}ntjj|d}|D]:}tjtj|\} } |r,| |vr,q|r4| |s4qtj t | tjj } | rF|sFqt t||||| Vqg}|rj|D]} tj| D]}||qaqX|D]}tj t | tjj } | r|sqlt ||||| VqldS)z$Iterate over available test targets.)rwN)r rfr all_filesr5r6rrr+islinkrrstripr TestTargetrrr) r6rcrdrrqrorwrrrCextsymlink extra_dirrrrres4   reintegration_targetslist[IntegrationTarget]dict[str, set[str]]c Cstjtjjd}dd|D}tdd|D}tt}|D]}|j |j D] }|| |j q*q"|D]}|j D] }|| |j q=q8|D]B}tj|jD]7}tjt|tjjseqUtj|} | |sqqU| t|ddd} | |j krqU||  |j qUqK|D]W}tj|jd} tj| sqtj| } | D]<} tj| rzt| }Wn tyYqw|D]}td |rq|sq|D]}||vr|| |j qqqq d}| D]'\}}t!|D]}|"|}|r|D]}||vr| ||d 7}qqq|snqt#|D]&}||}|s+q t$j%d |d dt#|D] }t$j%d|d dq8q |S)zAnalyze the given list of integration test targets and return a dictionary expressing target names and the target names which depend on them.r)cSsg|] }|jdkr|qS)role)typerrrrr.6sz;analyze_integration_target_dependencies..css |] }d|jvr|jVqdS)hidden/N)r#rCrrrrr'7r(z:analyze_integration_target_dependencies..Nrrz^ *#.*$Trz%s:) verbosityz %s)&r5r6realpathr rfrr2 collections defaultdict setup_always setup_oncer\rC needs_targetrrrrrr+r,rrisdirrexistsrrUnicodeDecodeErrorrTsearchstripitemsrgetr9rinfo)rreal_target_root role_targetshidden_role_target_names dependenciesr%setup_target_name need_targetr6real_link_path link_targetmeta_dir meta_paths meta_path meta_lines meta_linehidden_target_namechangesdummydependent_target_namesdependent_target_namenew_target_namesnew_target_name target_name consumersconsumerrrr'analyze_integration_target_dependencies2s                rc@sJeZdZdZdddZddZdd Zd d Zd d ZddZ ddZ dS)CompletionTargetz3Command-line argument completion target base class.rNonecCs&d|_d|_d|_t|_t|_dS)N)rCr6r[r8rr#selfrrr__init__s  zCompletionTarget.__init__cCst|tr ||kSdS)NF) isinstancer__repr__rotherrrr__eq__s zCompletionTarget.__eq__cCs || Sr")rrrrr__ne__ zCompletionTarget.__ne__cC|j|jSr")rC__lt__rrrrrzCompletionTarget.__lt__cCrr")rC__gt__rrrrrrzCompletionTarget.__gt__cCs t|Sr")hashrrrrr__hash__rzCompletionTarget.__hash__cCs"|jrd|jd|jfS|jS)Nz%s (%s)z, )rrCrrrrrrszCompletionTarget.__repr__N)rr) __name__ __module__ __qualname____doc__rrrrrrrrrrrrs  r) metaclasscs&eZdZdZ ddfd d ZZS)rzGeneric test target.Nr6rrcr module_prefixr[rt.Optional[bool]rrc s*t|durtjt|tjj}||_||_|r"|dnd|_ ||_ tj tj |j\}}|rXt ||rX|dkrX|tvrX|t|pIddd|_|jf|_nd|_t|_|j|jg}|jd} tdt| D]} dd| d| } || qrdd|D}tt||_dS) Nr)rr_r%s/cSsg|]}|r|qSrrr$arrrr.z'TestTarget.__init__..)superrr5r6rrrrrCr[rrrr rgr,lstriprhrr8rrangerrr9r#) rr6rcrr[rrCrr#partsir& __class__rrrs(     zTestTarget.__init__r") r6rrcrrrr[rrrrrrrrrr __classcell__rrr rrsrc@s0eZdZdZeZeZeZeZ dS)IntegrationTargetTypez Type of integration test target.N) rrrrenumauto CONTROLLERTARGETUNKNOWNCONFLICTrrrrrs  rrCr#list[tuple[str, str]]c Cst}g}|g|D]F}d}|}|||ivr|||f|d}dD])}t||krOd|d|}d||d}|||ivrO|||fq&q |S)z_Return a list of plugin references found in the given integration test target name and aliases.rr)rN)r rrrr,r) rCr#pluginsfoundr& plugin_type plugin_namer  type_lengthrrrextract_plugin_referencess"  r force_target3tuple[IntegrationTargetType, IntegrationTargetType]c Csdtjj|v}dtjj|vp|}d}tjj}|r'|r'tj}n5|r/|s/tj}n-|r7|s7tj}n%ddt ||D}t |dkrL| }n|sYtj }|rU|ntj}ntj}||p`|fS)zfReturn the integration test target types (used and actual) based on the given target name and aliases.context/NcSs$h|]\}}|dvr tjntjqS))r module_utils)rrr)r$rrrrr sz.categorize_integration_test..r) rrrClowerrr rf is_ansiblerrr,popr) rCr#rcontext_controllercontext_target actual_type strict_mode target_type target_typesrrrcategorize_integration_tests(    r-cs:eZdZdZedZeeedBZdfd d ZZS)rzIntegration test target.)networkwindows)posixrhneedsskipr6rrfrozenset[str]rrrrc s^ttj|tjj_j tjj d_ |_tj |}tj |d}||vr6d_|_nd_d_tj |d}||vrPtt|dd}nt}j vr\j }nj drsj d dvrsj d d}nd}ttfd d |t|gD_jg} | fd d |D7} | dd jD7} tjjrj dksj dr| djs| dd| vr| dd| vr| d|D]\} } j | dsq| | krd| | f} | | qԈj dr| dj dr | dj dsj dr| djdvr'| dtjjtdd | D_tttfd d | D_d!d | D} | rT| d"nd_t| d"t| D]$} d#| vr| d#} t d$t| D]}| d# | d|qsq`t!fd%d | Ds| d&t!fd'd | D}t"j t#||\}}| $d(d(|j %g||kr| $d(d(|j %g|_&|_'j gd)d | Dfd*d | D}d+|vrd+gd,d |D}ttt|_(d-j(v_)tttd.d | D_*tttd/d | D_+tttd0d | D_,dS)1NrXzrunme.shscriptrr#T)remove_blank_lineswin_rc3s|] }|vr|VqdSr"rrrrrr'Qsz-IntegrationTarget.__init__..csg|]}|vr|qSrrrr7rrr.VrKz.IntegrationTarget.__init__..cSg|]}d|qS)z module/%sr)r$mrrrr.Wrz ansible-testz ansible-test- non_module destructivenon_destructivezneeds/httptesterzcloud/httptesterr%s/%sr/ connection_ connectionsetup_prepare_hidden)r4rcss|] }|dr|VqdS)zskip/Nr+r$grrrr'}sc3s@|]}|dr|dsd|dddVqdS)z needs/file/zneeds/file/%s/r)rN)r+rrrD)targets_relative_pathrrr's  "cSs$g|]}|dr|ddqS)rr)rr+rrDrrrr.r1rr)rc3|]}|jvVqdSr" non_posixrDrrrr'rr0c3rHr"rI)r$grouprrrr'rr!cSr8)rrrDrrrr.rcs$g|]}|jvrd|jfqS)r=) categoriesrCrDrrrr.r1rcSsg|] }|dsd|qS)rz hidden/%srCrrrrr.sz retry/never/cs(|]}|dr|ddVqdS)z setup/once/r)rNrGrDrrrr'&csrM)z setup/always/r)rNrGrDrrrr'rNcsrM)z needs/target/r)rNrGrDrrrr'rN)-rrr5r6relpathr rfr relative_pathreplacerrCrrr script_pathr8r r+r9rr%rrskipsr2 needs_filenetwork_platform itertoolsislicer,rr rr-rextendr$r+r)r# retry_neverrrr)rr6rrr runme_path aliases_pathstatic_aliases module_namegroupsrrKnetworksr r rr+r)r#r )rrrFrr,s  &                       zIntegrationTarget.__init__)r6rrr3rrrr) rrrrrrJrLrrrrr rrs rcs"eZdZdZdfdd ZZS) r]z?One or more targets were not matched when a match was required.rSset[str]rrcsFt||_t|dkrdd|j}nd|jd}t|dS)NrzTarget patterns not matched: %s zTarget pattern not matched: %sr)r9rSr,rrr)rrSmessager rrrs  z!TargetPatternsNotMatched.__init__)rSr`rrrrrr rr]sr]TCompletionTarget)boundTIntegrationTarget)rrrrrrrr)F)rr rrrrrr!)NNN) rr;r<r=r>r=r?r=rr@)TT) rr;rSrrLrrMrrr;)rrb)T)rorrrb)ryrrrz)rrz)rr)NNNNNFF)r6rrcrrdrrrrqrrorrwrrrb)rrrr)rCrr#rrr)rCrr#rrrrr )7r __future__rrcollections.abcabccrr5rTrVtypingtencodingrriorutilrrr r datar r rgrrrRrOrirnrtrurxrrrr}rrerABCMetarrEnumrrr-rr]TypeVarrcrerrrrsj       3        8 /a#*