1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Test script to verify the refactoring is successful
5
6import sys
7import os
8
9# Add current directory to path
10sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
11
12# Mock rtconfig module for testing
13import mock_rtconfig
14sys.modules['rtconfig'] = mock_rtconfig
15
16def test_targets_import():
17    """Test if all target modules can be imported successfully"""
18    print("Testing targets module imports...")
19
20    try:
21        # Test importing targets module
22        import targets
23        print("✓ targets module imported successfully")
24
25        # Test importing individual target modules
26        target_modules = [
27            'keil', 'iar', 'vs', 'vs2012', 'codeblocks', 'ua',
28            'vsc', 'cdk', 'ses', 'eclipse', 'codelite',
29            'cmake', 'xmake', 'esp_idf', 'zigbuild', 'makefile', 'rt_studio'
30        ]
31
32        for module_name in target_modules:
33            try:
34                module = getattr(targets, module_name)
35                print(f"✓ {module_name} module imported successfully")
36            except AttributeError as e:
37                print(f"✗ Failed to import {module_name}: {e}")
38                return False
39
40        return True
41
42    except ImportError as e:
43        print(f"✗ Failed to import targets module: {e}")
44        return False
45
46def test_building_import():
47    """Test if building.py can import target modules"""
48    print("\nTesting building.py imports...")
49
50    try:
51        # Test importing building module
52        import building
53        print("✓ building module imported successfully")
54
55        # Test if GenTargetProject function exists
56        if hasattr(building, 'GenTargetProject'):
57            print("✓ GenTargetProject function found")
58        else:
59            print("✗ GenTargetProject function not found")
60            return False
61
62        return True
63
64    except ImportError as e:
65        print(f"✗ Failed to import building module: {e}")
66        return False
67
68def test_target_functions():
69    """Test if target functions can be called"""
70    print("\nTesting target function calls...")
71
72    try:
73        # Test importing specific target functions
74        from targets.keil import MDK4Project, MDK5Project
75        print("✓ Keil target functions imported successfully")
76
77        from targets.iar import IARProject
78        print("✓ IAR target functions imported successfully")
79
80        from targets.eclipse import TargetEclipse
81        print("✓ Eclipse target functions imported successfully")
82
83        from targets.cmake import CMakeProject
84        print("✓ CMake target functions imported successfully")
85
86        import targets.rt_studio
87        print("✓ RT-Studio target functions imported successfully")
88
89        return True
90
91    except ImportError as e:
92        print(f"✗ Failed to import target functions: {e}")
93        return False
94
95def main():
96    """Main test function"""
97    print("RT-Thread Tools Refactoring Test")
98    print("=" * 40)
99
100    success = True
101
102    # Run all tests
103    if not test_targets_import():
104        success = False
105
106    if not test_building_import():
107        success = False
108
109    if not test_target_functions():
110        success = False
111
112    print("\n" + "=" * 40)
113    if success:
114        print("✓ All tests passed! Refactoring is successful.")
115        return 0
116    else:
117        print("✗ Some tests failed. Please check the errors above.")
118        return 1
119
120if __name__ == '__main__':
121    sys.exit(main())