
    3jB                    R   d Z 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Zddl	Z	ddl
Z
ddlZddlmZ ddlZddlZddlmZ ddlmZ  ej&                  d      ZddZdd	Zd
 ZddZ G d dej2                  j4                        Z G d d      Zedk(  r ej<                          yy)z$
Things that are common to testing.
    )annotationsN)registerResult)environment)commonztest.commonTestc                   | j                   j                  }|j                         D ]  \  }}d}dD ]'  }|j                  |      s|j	                  |      s&d}) |r7t        |      rt        |t        j                        r]t        |d      r|j                  |k7  ry	  |       }	 t        j                  |       	 t        j                  |        y # t        $ r Y w xY w# t        $ r!}| j                  d| d|        Y d }~Md }~ww xY w# t        $ r"}| j                  d| d|        Y d }~d }~ww xY w)NF)_Test	ExceptionT
__module__zCould not copy obj z: zCould not deepcopy obj )	__class__r   items
startswithendswithcallable
isinstancetypesFunctionTypehasattr	TypeErrorcopyr
   faildeepcopy)	testInstanceglobals_	my_modulepartobjmatchskipinstancees	            D/DATA/.local/lib/python3.12/site-packages/music21/test/commonTest.pytestCopyAllr#   "   s4   &&11I^^%	c.Dt$d(; / }
30B0B CsL)S^^y-H	uH	AIIh	EMM(#/ &  		
  	A 3D6A3?@@	A  	E 7vRsCDD	EsB   !C)C&?D	C#"C#&	D/DD	D>D99D>c                   t         j                  j                  | |      }||j                  t	        d|      | t
        j                  v rt
        j                  |    }nBt         j                  j                  |      }|t	        d|      |t
        j                  | <   |j                  j                  |       |S )z
    Replacement for deprecated imp.load_source()

    Thanks to:
    https://github.com/epfl-scitas/spack for pointing out the
    important missing "spec.loader.exec_module(module)" line.
    zNo such file or directory: )		importlibutilspec_from_file_locationloaderFileNotFoundErrorsysmodulesmodule_from_specexec_module)namepathspecmodules       r"   load_sourcer2   @   s     >>11$=D|t{{*"=dX FGGs{{T"006>#&A$$JKK"DKKF#M    c                     	 ddl } 	 ddlm} ddlm}m} 	  |        y# t        $ r}t        d      |d}~ww xY w# t        $ r}t        d      |d}~ww xY w# |$ r}t        d      |d}~ww xY w)zN
    Test that all optional packages needed for test suites are installed
    r   Nz2pip install scipy : needed for running test suites)StringMatcherz?pip install python-Levenshtein : needed for running test suites)LilypondConverterLilyTranslateExceptionz-lilypond must be installed to run test suites)scipyImportErrorLevenshteinr5   music21.lily.translater6   r7   )r8   r!   r5   r6   r7   s        r"   testImportsr<   W   s    
Wd- QR  WNOUVVW
  d[\bccd " RIJPQQRs<    ; A 	838	AAAA.A))A.c                    t        d      j                  j                         }t        j                  t        j
                  z  }||d}| t        j                  | fi |}|S t        j                  di |}|S )Nmusic21)globsoptionflags )
__import____dict__r   doctestELLIPSISNORMALIZE_WHITESPACEDocTestSuite)r.   r?   docTestOptionskeywordss1s        r"   defaultDoctestSuiterK   l   s|    y!**//1E&&)E)EEN%H
 !!$3(3 I !!-H-Ir3   c                      e Zd Zd Zy)Music21TestRunnerc                   | j                         }t        |       | j                  |_        | j                  |_        t	        j
                         5  t        | d      rU| j                  rIt	        j                  | j                         | j                  dv rt	        j                  dt        d       t        |dd      }| |        	  ||       t        |dd      }| |        	 ddd       |j                          d	x}x}}	 t        t        |j                  |j                  |j                   f      }|\  }}}g }	|j%                         sr| j&                  j)                  d
       t        |j*                        t        |j,                        }}
|
r|	j/                  d|
        |r|	j/                  d|        n	 |r|	j/                  d|        |r|	j/                  d|        |r|	j/                  d|        |	r2dj1                  |	      }| j&                  j3                  d| d       |S 	 |S # t        |dd      }| |        w w xY w# 1 sw Y   rxY w# t"        $ r Y 3w xY w)z8
        Run the given test case or test suite.
        warnings)defaultalwaysr1   zPlease use assert\w+ instead.)categorymessagestartTestRunNstopTestRunr   FAILEDz	failures=zerrors=zskipped=zexpected failures=zunexpected successes=z, z ())_makeResultr   failfastbufferrO   catch_warningsr   simplefilterfilterwarningsDeprecationWarninggetattrprintErrorsmaplenexpectedFailuresunexpectedSuccessesskippedAttributeErrorwasSuccessfulstreamwritefailureserrorsappendjoinwriteln)selftestresultrT   rU   expectedFailsrd   re   resultsinfosfailederroredjoineds                r"   runzMusic21TestRunner.run   sU    !!#v--$$&tZ(T]]%%dmm4 ==$99++H5G4TV #6>4@L'"V%fmTB*M- '2 	8999+g	B# 7 7 & : : & 0 1G ;B7M.##%KKh'!&//2C4FGFy12wwi01LL8G9-.LL-m_=>LL01D0EFGYYu%FKK"VHA/  M &fmTB*M ++ '&@  		s7   A8IH>I	1I' >III$'	I43I4N)__name__r   __qualname__rx   rA   r3   r"   rM   rM   ~   s    Br3   rM   c                  N    e Zd ZdZddZd Zd Zd ZdddZdd	Z	dd
Z
ddZy)ModuleGathera<  
    Utility class for gathering and importing all modules in the music21
    package. Puts them in self.modulePaths.

    >>> from music21.test import commonTest
    >>> mg = commonTest.ModuleGather(useExtended=True)
    >>> #_DOCS_SHOW print(mg.modulePaths[0])
    D:\Web\eclipse\music21base\music21\volume.py
    Fc                   t        t        j                               | _        || _        g | _        g d| _        | j                  g dz   | _        g d| _        g | _	        | j                  g z   | _
        | j                  D cg c]"  }|j                  dt        j                        $ c}| _        | j                  D cg c]"  }|j                  dt        j                        $ c}| _        | j                  D cg c]"  }|j                  dt        j                        $ c}| _	        | j                  D cg c]"  }|j                  dt        j                        $ c}| _
        | j                  D cg c]"  }|j                  dt        j                        $ c}| _        |r| j                          y y c c}w c c}w c c}w c c}w c c}w )N)ztestSingleCoreAll.pyztestExternal.pyztestDefault.pyztestInstallation.pyztestLint.pyztestPerformance.pyztimeGraphs.pyztimeGraphImportStar.pyzmultiprocessTest.pyzsetup.pyzfiguredBass/examples.py)zconfigure.pyztestSerialization.pyzmptCurses.pyzmemoryUsage.pyztest/treeYield.pyztest/toggleDebug.pyzmusicxml/testPrimitive.pyzmusicxml/testFiles.pyz)musedata/testPrimitive/test01/__init__.pyz"musedata/testPrimitive/__init__.pyzmei/test_base.pyzhumdrum/questions.pyzdocumentation/upload.pyzdocumentation/source/conf.pyzdocumentation/extensions.pyzcorpus/testCorpus.pyzaudioSearch/scoreFollower.pyzaudioSearch/repetitionGame.pyzabcFormat/testFiles.py)zmetadata/bundleszmetadata/cachingfeaturesgraphz
graph/plotz
graph/axiszgraph/primitives
freezeThawzfiguredBass/realizerzfeatures/jSymboliczfeatures/nativezfiguredBass/exampleszbraille/testzstream/testszanalysis/windowedzconverter/__init__zmusicxml/test_m21ToXmlzmusicxml/test_xmlToM21zromanText/translatezcorpus/testCorpuszcorpus/corporazaudioSearch/transcriberzaudioSearch/__init__z#alpha/theoryAnalysis/theoryAnalyzer/)strr   getSourceFilePath	dirParentuseExtendedmodulePaths
moduleSkipmoduleSkipExtendedslowModulespathSkippathSkipExtendedreplaceossepwalk)ro   r   autoWalkxs       r"   __init__zModuleGather.__init__   sm   V5578&
" #'// 5
 #
6:  $ 2;???K?a199S"&&1?KCGCZCZ"[CZa199S"&&#9CZ"[9=GA3/GAEAVAV WAVA3!7AV W<@<L<LM<LqAIIc2662<LM IIK  L"[G WMs   'F9?'F><'G9'G6'Gc                    |D ]o  }|j                  d      st        j                  j                  ||      }t        j                  j	                  |      rU| j
                  j                  |       q y)zz
        append all module paths from _walk() to self.modulePaths.
        Utility function called from os.walk()
        pyN)r   r   r/   rm   isdirr   rl   )ro   argsdirnamenamesfileNamefps         r"   
_visitFunczModuleGather._visitFunc,  sT    
 H  &WW\\'84ww}}R($$++B/	 r3   c                V     fd}t        j                   j                        D ]  \  }}} j                  d||        t	        j
                         dkD  r j                  j                  |       n j                  j                           j                  j                          y)zX
        Get all the modules in reverse order, storing them in self.modulePaths
        c                ~    | t        j                        dz   d } | j                  dd      } | j                  v | fS )z
            for many core systems, like the MacPro, running slowest modules first
            helps there be fewer idle cores later
               N.py )rb   r   r   r   )r.   ro   s    r"   manyCoreSortFuncz+ModuleGather.walk.<locals>.manyCoreSortFunc;  sF    
 DNN+a/01D<<r*DD,,,d33r3   N   )key)	r   r   r   r   r   cpusr   sortreverse)ro   r   dirPathunused_dirNames	filenamess   `    r"   r   zModuleGather.walk7  s    	4 46774>>3J/G_iOOD'95 4K ;;=1!!&6!7!!#
 	  "r3   c                   |j                  t        | j                        d      }|j                  t        j
                        r|dd }|j                  t        j
                  d      }|j                  d      r|dd }|S )a#  
        Given full file pathlib.Path, find a name for the module with _ as the separator.

        >>> from music21.test import commonTest
        >>> mg = commonTest.ModuleGather()
        >>> #_DOCS_SHOW mg._getName(r'D:\Web\eclipse\music21base\music21\chord.py')
        'chord'
        r   r   Nr   r   )r   r   r   r   r   r   r   )ro   r   fns      r"   _getNamezModuleGather._getNameR  sh     ZZDNN+R0== ABBZZ$;;uCRB	r3   addM21c               T   |j                  | j                  d      }|j                  t        j                        D cg c]  }|s|	 }}|d   j                  d      r|d   dd |d<   |d   dk(  r|j                          dj                  |      }|r	|rd|z   }|S |rd	}|S c c}w )
aQ  
        Given full file path, find a name for the module with . as the separator.

        >>> from music21.test import commonTest
        >>> mg = commonTest.ModuleGather()
        >>> name = '/Users/cuthbert/git/music21base/music21/features/native.py'
        >>> #_DOCS_SHOW mg._getNamePeriod(name)
        'features.native'
        r   r   Nr   r   .zmusic21.r>   )r   r   splitr   r   r   poprm   )ro   r   r   r   r   partss         r"   _getNamePeriodzModuleGather._getNamePeriodc  s     ZZ+HHRVV,2,q,29e$b	#2E"I9
"IIKXXe_bbB 	 B	 3s   B%B%c                x    g }| j                   D ](  }| j                  ||      }||j                  |       * |S )z
        Return a list of module objects that are not in the skip list.

        N.B. the list is a list of actual module objects not names,
        therefore cannot be pickled.
        )r   	getModulerl   )ro   restoreEnvironmentDefaultsr+   r   moduleObjects        r"   loadzModuleGather.load{  sC     ""B>>".HIL'|, # r3   c                p   d}| j                   }| j                  r| j                  }|D ]  }|j                  |      sd} n |ry| j                  }| j                  r| j
                  }|D ]
  }||v sd} n |ry| j                  |d      }	 t        j                         5  t        ||      }	ddd       |r&t        	d	      r|	j                  j!                          	S # 1 sw Y   3xY w# t        $ r=}
t        j                  d|d|ddt        |
      j                         g       Y d}
~
yd}
~
ww xY w)
z;
        gets one module object from the file path
        FTNr   zfailed import:	
z	EXCEPTION:environLocal)r   r   r   r   r   r   r   rO   r[   r2   r
   r   warnr   stripr   restoreDefaults)ro   r   r   r   msfnSkippsdirSkipr.   modexcps              r"   r   zModuleGather.getModule  s2    __((BF{{6"  ]]&&BG"}  ""2e"4	((*!$+ + &sN+  002
 +* 	/tR-s4y/@B C	s0   C/ $C#1C/ #C,(C/ /	D583D00D5c                   d}| j                   D ]  }|j                  |      sd} n |ry| j                  D ].  }t        j                  |z   t        j                  z   }||v s,d} n |ry| j                  |      }|j                  d      }t        }	|D ]  }
t        |	|
      r3t        j                  |	|
      }	t        |	t        j                        r@ yd|
v rV|	j                  }t        j                   d|
z   |      }t#        |	|
|       t$        j'                  d|
d|	d	|d
|       |}	 y |	}|r&t        |d      r|j$                  j)                          t+        d|z          |S )zM
        gets one module object from the file path without using Imp
        FTr   r   	notInTreerp   zImported thisName=z from currentModule=z, fp=z, packageName=r   z	starting )r   r   r   r   r   r   r   r>   r   object__getattribute__r   r   
ModuleTypery   r%   import_modulesetattrr   
printDebugr   print)ro   r   r   r   r   r   dirSkipSlash
moduleNamemoduleNamescurrentModulethisNamepackageNamenewModr   s                 r"   getModuleWithoutImpz ModuleGather.getModuleWithoutImp  ss    ooF{{6" & }}G66G+bff4Lr!	 %
 ((,
 &&s+ $H}h/ & 7 7x P!-1A1AB&8#+44"00xMx8''){*?0@2%+X !'" $  %sN+  002kJ&'
r3   N)FT)F)ry   r   rz   __doc__r   r   r   r   r   r   r   r   rA   r3   r"   r|   r|      s7    Yv	0#6" ,1 0(T-r3   r|   __main__)r   zunittest.TestCaser   ztyping.Dict[str, typing.Any])r.   r   r/   r   returnztypes.ModuleType)N)r   
__future__r   r   rD   r%   importlib.utilr   r*   typingr   unittest.runnerunittestunittest.signalsr   rO   r>   r   r   Environmentr   r#   r2   r<   rK   runnerTextTestRunnerrM   r|   ry   mainTestrA   r3   r"   <module>r      s    #     	 
    +    &{&&'89E<.R*$C66 CPZ Zz zG r3   