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
16testIsActive = exu.sys.get('testIsActive', False)
17
18from pathlib import Path
19import importlib.util
20import sys
21import os
22
23# Set the directory path ('.' for current directory)
24exudynPath = os.path.dirname(exu.__file__)
25directory = Path(exudynPath)
26
27# Get all files ending in .py recursively
28allModules = list(directory.rglob('*.py')) #list of file objects
29
30testSolution = 1
31
32exu.Print('first module location=',str(allModules[0]))
33
34excludeModules = ['__init__.py', #fail
35 '__main__.py', #the command line of the package; importing it RUNS it
36 'rosInterface.py', #rospy usually missing
37 #'artificialIntelligence.py'
38 ]
39
40try:
41 import stable_baselines3
42 #ok
43except:
44 exu.Print('stable-baselines3 not available: skip artificialIntelligence.py')
45 excludeModules.append('artificialIntelligence.py')
46
47#the dialogs need tkinter, which a Python may lack: the Python of the cibuildwheel tests on Windows has none (#2920)
48try:
49 import tkinter
50except ImportError:
51 exu.Print('tkinter not available: skip GUI.py')
52 excludeModules.append('GUI.py')
53
54#test all modules
55for moduleCheck in allModules: #moduleCheck converts to string as path+fileName
56 if moduleCheck.name in excludeModules:
57 continue
58 exu.Print('check module:',moduleCheck.name)
59 try:
60 spec = importlib.util.spec_from_file_location(moduleCheck.stem, moduleCheck)
61
62 testModule = importlib.util.module_from_spec(spec)
63
64 sys.modules[moduleCheck.stem] = testModule #moduleCheck.name is the filename with ending, moduleCheck.stem is without ending
65
66 spec.loader.exec_module(testModule) #now execute module
67
68 except FileNotFoundError:
69 testSolution = 0
70 exu.Print(f"Error: The module {moduleCheck} does not exist.")
71 except SyntaxError as e:
72 testSolution = 0
73 exu.Print(f"Error: module {moduleCheck} has invalid Python syntax: {e}")
74 except Exception as e:
75 testSolution = 0
76 exu.Print(f"Error: Failed to execute {moduleCheck}: {e}")
77
78if testSolution==1:
79 exu.Print('\n*** all imports successful! ***\n')
80#also add test for demos
81try:
82 exu.Print('test demo1')
83 exu.demos.Demo1()
84 exu.Print('test demo2')
85 exu.demos.Demo2(showAll=False)
86except Exception as e:
87 exu.Print(f"Error: Demos failed: {e}")
88
89#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
90exu.Print('\nsolution of allExudynModulesTest (should be 1)=',testSolution)
91exu.sys['testResult'] = testSolution