first commit for chrg

This commit is contained in:
wmano
2025-08-16 22:58:22 +08:00
commit 52a3ed5862
2306 changed files with 1021208 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
# 测试用例目录
本目录包含 RT-Thread 工具的测试脚本。
## 测试脚本
### test_preprocessor.py
SCons PreProcessor 补丁功能测试脚本。测试与 building.py 的集成,验证预处理器补丁是否正常工作。
### test_refactor.py
验证目标模块重构是否成功的测试脚本。测试内容包括:
- 目标模块导入
- Building.py 导入
- 目标函数调用
### mock_rtconfig.py
用于测试的模拟 rtconfig 模块。在实际 rtconfig 不可用的测试场景中提供模拟的 rtconfig 模块。
## 使用方法
要运行测试,请导航到此目录并执行:
```bash
python test_preprocessor.py
python test_refactor.py
```
## 说明
- 这些测试脚本用于验证 RT-Thread 工具的功能
- 可以独立运行或作为测试套件的一部分
- mock_rtconfig.py 文件被其他测试脚本用来模拟 rtconfig 模块
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
#
# Mock rtconfig module for testing purposes
#
# Mock configuration variables
CROSS_TOOL = 'gcc'
PLATFORM = 'gcc'
CC = 'gcc'
CXX = 'g++'
AS = 'as'
AR = 'ar'
LINK = 'gcc'
EXEC_PATH = '/usr/bin'
# Mock functions
def GetDepend(depend):
return True
# Mock environment
class MockEnv:
def __init__(self):
self.CPPPATH = []
self.CPPDEFINES = []
self.LIBS = []
self.LIBPATH = []
self.CFLAGS = []
self.CXXFLAGS = []
self.LINKFLAGS = []
self.ASFLAGS = []
# Global variables
Env = MockEnv()
Rtt_Root = '/mock/rt-thread'
Projects = []
@@ -0,0 +1,116 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# File : test_preprocessor_patch.py
# This file is part of RT-Thread RTOS
# COPYRIGHT (C) 2006 - 2025, RT-Thread Development Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Change Logs:
# Date Author Notes
# 2025-01-05 Assistant Test file for SCons PreProcessor patch
import sys
import os
# Add current directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_preprocessor_patch():
"""Test the SCons PreProcessor patch functionality"""
try:
from scons_preprocessor_patch import SConsPreProcessorPatch, create_preprocessor_instance
print("Testing SCons PreProcessor patch...")
# Test creating patch instance
patch = SConsPreProcessorPatch()
print("✓ SConsPreProcessorPatch instance created successfully")
# Test getting patched preprocessor
patched_class = patch.get_patched_preprocessor()
print("✓ Patched PreProcessor class retrieved successfully")
# Test creating preprocessor instance
preprocessor = create_preprocessor_instance()
print("✓ PreProcessor instance created successfully")
# Test basic functionality
test_content = """
#define TEST_MACRO 1
#ifdef TEST_MACRO
#define ENABLED_FEATURE 1
#else
#define DISABLED_FEATURE 1
#endif
"""
preprocessor.process_contents(test_content)
namespace = preprocessor.cpp_namespace
print("✓ PreProcessor processed test content successfully")
print(f" - TEST_MACRO: {namespace.get('TEST_MACRO', 'Not found')}")
print(f" - ENABLED_FEATURE: {namespace.get('ENABLED_FEATURE', 'Not found')}")
print(f" - DISABLED_FEATURE: {namespace.get('DISABLED_FEATURE', 'Not found')}")
print("\n✓ All tests passed! SCons PreProcessor patch is working correctly.")
return True
except ImportError as e:
print(f"✗ Import error: {e}")
print("Make sure SCons is available in the environment")
return False
except Exception as e:
print(f"✗ Test failed: {e}")
return False
def test_building_integration():
"""Test integration with building.py"""
try:
# Test that the function is available from the patch module
from scons_preprocessor_patch import create_preprocessor_instance
print("\nTesting scons_preprocessor_patch integration...")
# Test that the function is available
preprocessor = create_preprocessor_instance()
print("✓ create_preprocessor_instance function works from scons_preprocessor_patch")
# Test basic processing
test_content = "#define BUILD_TEST 1"
preprocessor.process_contents(test_content)
namespace = preprocessor.cpp_namespace
print(f"✓ Integration test passed: BUILD_TEST = {namespace.get('BUILD_TEST', 'Not found')}")
return True
except Exception as e:
print(f"✗ Integration test failed: {e}")
return False
if __name__ == "__main__":
print("SCons PreProcessor Patch Test Suite")
print("=" * 40)
success1 = test_preprocessor_patch()
success2 = test_building_integration()
if success1 and success2:
print("\n🎉 All tests passed! The refactoring was successful.")
sys.exit(0)
else:
print("\n❌ Some tests failed. Please check the implementation.")
sys.exit(1)
+121
View File
@@ -0,0 +1,121 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Test script to verify the refactoring is successful
import sys
import os
# Add current directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Mock rtconfig module for testing
import mock_rtconfig
sys.modules['rtconfig'] = mock_rtconfig
def test_targets_import():
"""Test if all target modules can be imported successfully"""
print("Testing targets module imports...")
try:
# Test importing targets module
import targets
print("✓ targets module imported successfully")
# Test importing individual target modules
target_modules = [
'keil', 'iar', 'vs', 'vs2012', 'codeblocks', 'ua',
'vsc', 'cdk', 'ses', 'eclipse', 'codelite',
'cmake', 'xmake', 'esp_idf', 'zigbuild', 'makefile', 'rt_studio'
]
for module_name in target_modules:
try:
module = getattr(targets, module_name)
print(f"{module_name} module imported successfully")
except AttributeError as e:
print(f"✗ Failed to import {module_name}: {e}")
return False
return True
except ImportError as e:
print(f"✗ Failed to import targets module: {e}")
return False
def test_building_import():
"""Test if building.py can import target modules"""
print("\nTesting building.py imports...")
try:
# Test importing building module
import building
print("✓ building module imported successfully")
# Test if GenTargetProject function exists
if hasattr(building, 'GenTargetProject'):
print("✓ GenTargetProject function found")
else:
print("✗ GenTargetProject function not found")
return False
return True
except ImportError as e:
print(f"✗ Failed to import building module: {e}")
return False
def test_target_functions():
"""Test if target functions can be called"""
print("\nTesting target function calls...")
try:
# Test importing specific target functions
from targets.keil import MDK4Project, MDK5Project
print("✓ Keil target functions imported successfully")
from targets.iar import IARProject
print("✓ IAR target functions imported successfully")
from targets.eclipse import TargetEclipse
print("✓ Eclipse target functions imported successfully")
from targets.cmake import CMakeProject
print("✓ CMake target functions imported successfully")
import targets.rt_studio
print("✓ RT-Studio target functions imported successfully")
return True
except ImportError as e:
print(f"✗ Failed to import target functions: {e}")
return False
def main():
"""Main test function"""
print("RT-Thread Tools Refactoring Test")
print("=" * 40)
success = True
# Run all tests
if not test_targets_import():
success = False
if not test_building_import():
success = False
if not test_target_functions():
success = False
print("\n" + "=" * 40)
if success:
print("✓ All tests passed! Refactoring is successful.")
return 0
else:
print("✗ Some tests failed. Please check the errors above.")
return 1
if __name__ == '__main__':
sys.exit(main())