o
    qoi                     @   s  d Z ddlZddlZddlmZmZm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 eZee Ze	ddd	Ze	d
ddZddded
edefddZdejded
edejfddZdedefddZdejdejdee dejfddZejZdS )a,  Dynamically generated test suites to verify fiddle configs build.

Software engineering can be described as programming integrated over time. (See:
https://www.oreilly.com/library/view/software-engineering-at/9781492082781/ch01.html)
Because Fiddle is engineered for the entire project lifecycle, Fiddle includes
testing utilities to ensure Fiddle configurations stay valid.

This file is a collection of reusable utilities to generate your own customized
tests for advanced users, as well as the business logic to power the
`fiddle_autotest` bazel macro.
    N)AnyOptionalType)flags)absltest)building)module_reflectionfiddle_config_modulezVThe name of the Python module containing the configurations and fiddlers to be tested.skip_buildingFz,Skips calling `fdl.build` in the test cases.r
   modulereturnc                   s   i }t | D ]} fdd}t|| |}d| |_|||j< qt | D ]&}t | D ]} fdd}t|| ||}d| d| |_|||j< q.q't | s`t | r`dd }	|	|d	< t| d
rk| jd }
nd}
t|
tj	f|S )zHReturns a `absltest.TestCase` subclass appropriate for testing `module`.c                    s6   |  t|| t||}| } st|}d S d S N
assertTruehasattrgetattrr   build)selfr   fn_namefncfg_r    P/home/ubuntu/.local/lib/python3.10/site-packages/fiddle/_src/testing/autotest.pytest_base_config?   s   
z4make_test_class_for_module.<locals>.test_base_configtest_c                    sX   |  t|| |  t|| t||}t||}| }||  s*t|}d S d S r   r   )r   r   	base_namefiddler_namebase_fn
fiddler_fnr   r   r   r   r   test_base_and_fiddlerN   s   

z9make_test_class_for_module.<locals>.test_base_and_fiddler_and_c                 S   s   |  d d S )Nz`Found a fiddler, but found no base configurations, so auto-fiddle tests cannot test the fiddler!)fail)r   r   r   r    test_no_base_configuration_sorry`   s   zDmake_test_class_for_module.<locals>.test_no_base_configuration_sorryr$   __name__
FiddleTestFiddleAutoTest)
r   find_base_config_like_things	functoolspartialmethodr%   find_fiddler_like_thingsr   typer   TestCase)r   r
   test_functionsnamer   r   r   r   r!   r$   test_class_namer   r   r   make_test_class_for_module5   s6   



r1   loaderc                C   s   t ||d}| |S )Nr   )r1   loadTestsFromTestCase)r2   r   r
   
test_classr   r   r   load_tests_from_moduleo   s   
r5   module_namec                 C   s>   d| v r
|  dd} | dr| dtd  } t| }|S )zAImports and returns the module given a path to the module's file./.z.pyN)replaceendswithlen	importlibimport_module)r6   r   r   r   r   load_module_from_pathu   s   

r>   testspatternc                 C   s"   ~~t tj}t| |tjd}|S )a  An implementation of the load_tests module protocol for dynamic tests.

  For additional context, see the documentation on unittest's load_tests
  protocol, which describes how modules can control test suite discovery.

  Args:
    loader: See unittest's load_tests protocol.
    tests: See unittest's load_tests protocol.
    pattern: See unittest's load_tests protocol.

  Returns:
    A test suite.
  r   )r>   _FLAG_FIDDLE_CONFIG_MODULEvaluer5   _FLAG_SKIP_BUILDING)r2   r?   r@   r   suiter   r   r   
load_tests   s   
rE   )__doc__r)   r<   typingr   r   r   unittestabslr   absl.testingr   fiddler   fiddle._srcr   ModuleCustomTestClassDEFINE_stringrA   DEFINE_booleanrC   boolr1   
TestLoader	TestSuiter5   strr>   rE   mainr   r   r   r   <module>   sH   

:



