allExudynModulesTest.py

You can view and download this file on Github: allExudynModulesTest.py

 1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  Test for import of all exudyn modules, like utilities, etc.;
 5#           done to ensure that they do not contain any major error due to docstring conversion
 6#
 7# Author:   Johannes Gerstmayr
 8# Date:     2025-06-14
 9#
10# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
11#
12#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
14import exudyn as exu
15
16useGraphics = True #without test
17#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
18#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
19try: #only if called from test suite
20    from modelUnitTests import exudynTestGlobals #for globally storing test results
21    useGraphics = exudynTestGlobals.useGraphics
22except:
23    class ExudynTestGlobals:
24        pass
25    exudynTestGlobals = ExudynTestGlobals()
26#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
28from pathlib import Path
29import importlib.util
30import sys
31import os
32
33# Set the directory path ('.' for current directory)
34exudynPath = os.path.dirname(exu.__file__)
35directory = Path(exudynPath)
36
37# Get all files ending in .py recursively
38allModules = list(directory.rglob('*.py')) #list of file objects
39
40testSolution = 1
41
42exu.Print('first module location=',str(allModules[0]))
43
44excludeModules = ['__init__.py', #fail
45                  'resultsMonitor.py', #can only be called from command line with args
46                  'rosInterface.py',   #rospy usually missing
47                  #'artificialIntelligence.py'
48                  ]
49
50try:
51    import stable_baselines3
52    #ok
53except:
54    exu.Print('stable-baselines3 not available: skip artificialIntelligence.py')
55    excludeModules.append('artificialIntelligence.py')
56
57#test all modules
58for moduleCheck in allModules: #moduleCheck converts to string as path+fileName
59    if moduleCheck.name in excludeModules:
60        continue
61    exu.Print('check module:',moduleCheck.name)
62    try:
63        spec = importlib.util.spec_from_file_location(moduleCheck.stem, moduleCheck)
64
65        testModule = importlib.util.module_from_spec(spec)
66
67        sys.modules[moduleCheck.stem] = testModule #moduleCheck.name is the filename with ending, moduleCheck.stem is without ending
68
69        spec.loader.exec_module(testModule) #now execute module
70
71    except FileNotFoundError:
72        testSolution = 0
73        exu.Print(f"Error: The module {moduleCheck} does not exist.")
74    except SyntaxError as e:
75        testSolution = 0
76        exu.Print(f"Error: module {moduleCheck} has invalid Python syntax: {e}")
77    except Exception as e:
78        testSolution = 0
79        exu.Print(f"Error: Failed to execute {moduleCheck}: {e}")
80
81if testSolution==1:
82    exu.Print('\n*** all imports successful! ***\n')
83#also add test for demos
84try:
85    exu.Print('test demo1')
86    exu.demos.Demo1()
87    exu.Print('test demo2')
88    exu.demos.Demo2(showAll=False)
89except Exception as e:
90    exu.Print(f"Error: Demos failed: {e}")
91
92#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
93exu.Print('\nsolution of allExudynModulesTest (should be 1)=',testSolution)
94exudynTestGlobals.testResult = testSolution