cli-test-framework 0.2.1__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cli_test_framework-0.2.1/src/cli_test_framework.egg-info → cli_test_framework-0.2.2}/PKG-INFO +1 -1
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/setup.py +47 -47
- cli_test_framework-0.2.2/src/cli_test_framework/__init__.py +21 -0
- cli_test_framework-0.2.2/src/cli_test_framework/cli.py +132 -0
- cli_test_framework-0.2.2/src/cli_test_framework/core/__init__.py +15 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/core/assertions.py +31 -31
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/__init__.py +9 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/base_comparator.py +107 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/binary_comparator.py +242 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/csv_comparator.py +140 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/factory.py +141 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/h5_comparator.py +423 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/json_comparator.py +326 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/result.py +191 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/text_comparator.py +177 -0
- cli_test_framework-0.2.2/src/cli_test_framework/file_comparator/xml_comparator.py +150 -0
- cli_test_framework-0.2.2/src/cli_test_framework/runners/__init__.py +13 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/runners/yaml_runner.py +92 -92
- cli_test_framework-0.2.2/src/cli_test_framework/utils/__init__.py +11 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/utils/report_generator.py +67 -67
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2/src/cli_test_framework.egg-info}/PKG-INFO +1 -1
- cli_test_framework-0.2.2/src/cli_test_framework.egg-info/SOURCES.txt +50 -0
- cli_test_framework-0.2.2/src/cli_test_framework.egg-info/top_level.txt +1 -0
- cli_test_framework-0.2.1/src/cli_test_framework.egg-info/SOURCES.txt +0 -38
- cli_test_framework-0.2.1/src/cli_test_framework.egg-info/top_level.txt +0 -3
- cli_test_framework-0.2.1/src/core/__init__.py +0 -4
- cli_test_framework-0.2.1/src/runners/__init__.py +0 -3
- cli_test_framework-0.2.1/src/utils/__init__.py +0 -3
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/CHANGELOG.md +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/MANIFEST.in +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/README.md +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/docs/user_manual.md +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/pyproject.toml +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/setup.cfg +0 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/core/base_runner.py +0 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/core/parallel_runner.py +0 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/core/process_worker.py +0 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/core/test_case.py +0 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/runners/json_runner.py +0 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/runners/parallel_json_runner.py +0 -0
- {cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/utils/path_resolver.py +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/src/cli_test_framework.egg-info/dependency_links.txt +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/src/cli_test_framework.egg-info/entry_points.txt +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/src/cli_test_framework.egg-info/requires.txt +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/__init__.py +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/__pycache__/__init__.cpython-312.pyc +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/__pycache__/test_parallel_runner.cpython-312-pytest-7.4.4.pyc +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/fixtures/test_cases.json +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/fixtures/test_cases.yaml +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/fixtures/test_cases1.json +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/performance_test.py +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/test1.py +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/test_comprehensive_space.py +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/test_parallel_runner.py +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/test_parallel_space.py +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/test_report.txt +0 -0
- {cli_test_framework-0.2.1 → cli_test_framework-0.2.2}/tests/test_runners.py +0 -0
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
from setuptools import setup, find_packages
|
|
2
|
-
import os
|
|
3
|
-
|
|
4
|
-
# Read the contents of the README file
|
|
5
|
-
this_directory = os.path.abspath(os.path.dirname(__file__))
|
|
6
|
-
with open(os.path.join(this_directory, 'README.md'), encoding='utf-8') as f:
|
|
7
|
-
long_description = f.read()
|
|
8
|
-
|
|
9
|
-
setup(
|
|
10
|
-
name="cli-test-framework",
|
|
11
|
-
version="0.2.
|
|
12
|
-
author="Xiaotong Wang",
|
|
13
|
-
author_email="xiaotongwang98@gmail.com",
|
|
14
|
-
description="A small command line testing framework in Python with file comparison capabilities.",
|
|
15
|
-
long_description=long_description,
|
|
16
|
-
long_description_content_type="text/markdown",
|
|
17
|
-
url="https://github.com/yourusername/cli-test-framework",
|
|
18
|
-
packages=find_packages(where="src"),
|
|
19
|
-
package_dir={"": "src"},
|
|
20
|
-
install_requires=[
|
|
21
|
-
"dukpy==0.5.0",
|
|
22
|
-
"h5py>=3.8.0",
|
|
23
|
-
"numpy>=2.0.1",
|
|
24
|
-
"setuptools>=75.8.0",
|
|
25
|
-
"wheel>=0.45.1"
|
|
26
|
-
],
|
|
27
|
-
entry_points={
|
|
28
|
-
'console_scripts': [
|
|
29
|
-
'cli-test=cli_test_framework.cli:main',
|
|
30
|
-
'compare-files=cli_test_framework.commands.compare:main',
|
|
31
|
-
],
|
|
32
|
-
},
|
|
33
|
-
classifiers=[
|
|
34
|
-
"Programming Language :: Python :: 3",
|
|
35
|
-
"License :: OSI Approved :: MIT License",
|
|
36
|
-
"Operating System :: OS Independent",
|
|
37
|
-
"Development Status :: 4 - Beta",
|
|
38
|
-
"Intended Audience :: Developers",
|
|
39
|
-
"Topic :: Software Development :: Testing",
|
|
40
|
-
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
41
|
-
],
|
|
42
|
-
python_requires='>=3.6',
|
|
43
|
-
project_urls={
|
|
44
|
-
'Documentation': 'https://github.com/yourusername/cli-test-framework/docs/user_manual.md',
|
|
45
|
-
'Source': 'https://github.com/yourusername/cli-test-framework',
|
|
46
|
-
'Tracker': 'https://github.com/yourusername/cli-test-framework/issues',
|
|
47
|
-
},
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
# Read the contents of the README file
|
|
5
|
+
this_directory = os.path.abspath(os.path.dirname(__file__))
|
|
6
|
+
with open(os.path.join(this_directory, 'README.md'), encoding='utf-8') as f:
|
|
7
|
+
long_description = f.read()
|
|
8
|
+
|
|
9
|
+
setup(
|
|
10
|
+
name="cli-test-framework",
|
|
11
|
+
version="0.2.2",
|
|
12
|
+
author="Xiaotong Wang",
|
|
13
|
+
author_email="xiaotongwang98@gmail.com",
|
|
14
|
+
description="A small command line testing framework in Python with file comparison capabilities.",
|
|
15
|
+
long_description=long_description,
|
|
16
|
+
long_description_content_type="text/markdown",
|
|
17
|
+
url="https://github.com/yourusername/cli-test-framework",
|
|
18
|
+
packages=find_packages(where="src"),
|
|
19
|
+
package_dir={"": "src"},
|
|
20
|
+
install_requires=[
|
|
21
|
+
"dukpy==0.5.0",
|
|
22
|
+
"h5py>=3.8.0",
|
|
23
|
+
"numpy>=2.0.1",
|
|
24
|
+
"setuptools>=75.8.0",
|
|
25
|
+
"wheel>=0.45.1"
|
|
26
|
+
],
|
|
27
|
+
entry_points={
|
|
28
|
+
'console_scripts': [
|
|
29
|
+
'cli-test=cli_test_framework.cli:main',
|
|
30
|
+
'compare-files=cli_test_framework.commands.compare:main',
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
classifiers=[
|
|
34
|
+
"Programming Language :: Python :: 3",
|
|
35
|
+
"License :: OSI Approved :: MIT License",
|
|
36
|
+
"Operating System :: OS Independent",
|
|
37
|
+
"Development Status :: 4 - Beta",
|
|
38
|
+
"Intended Audience :: Developers",
|
|
39
|
+
"Topic :: Software Development :: Testing",
|
|
40
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
41
|
+
],
|
|
42
|
+
python_requires='>=3.6',
|
|
43
|
+
project_urls={
|
|
44
|
+
'Documentation': 'https://github.com/yourusername/cli-test-framework/docs/user_manual.md',
|
|
45
|
+
'Source': 'https://github.com/yourusername/cli-test-framework',
|
|
46
|
+
'Tracker': 'https://github.com/yourusername/cli-test-framework/issues',
|
|
47
|
+
},
|
|
48
48
|
)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CLI Test Framework - A powerful command-line testing framework
|
|
3
|
+
|
|
4
|
+
This package provides tools for testing command-line applications and scripts
|
|
5
|
+
with support for parallel execution and advanced file comparison capabilities.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "0.2.2"
|
|
9
|
+
__author__ = "Xiaotong Wang"
|
|
10
|
+
__email__ = "xiaotongwang98@gmail.com"
|
|
11
|
+
|
|
12
|
+
# Import main classes for convenient access
|
|
13
|
+
from .runners.json_runner import JSONRunner
|
|
14
|
+
from .runners.parallel_json_runner import ParallelJSONRunner
|
|
15
|
+
from .runners.yaml_runner import YAMLRunner
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
'JSONRunner',
|
|
19
|
+
'ParallelJSONRunner',
|
|
20
|
+
'YAMLRunner',
|
|
21
|
+
]
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
CLI Test Framework - Command Line Interface
|
|
6
|
+
|
|
7
|
+
This module provides the main command-line interface for the CLI Testing Framework.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import sys
|
|
12
|
+
import os
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from .runners import JSONRunner, ParallelJSONRunner, YAMLRunner
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def create_parser():
|
|
19
|
+
"""Create and configure the argument parser"""
|
|
20
|
+
parser = argparse.ArgumentParser(
|
|
21
|
+
description="CLI Testing Framework - A powerful tool for testing command-line applications",
|
|
22
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
23
|
+
epilog="""
|
|
24
|
+
Examples:
|
|
25
|
+
cli-test run test_cases.json
|
|
26
|
+
cli-test run test_cases.json --parallel --workers 4
|
|
27
|
+
cli-test run test_cases.yaml --workspace /path/to/project
|
|
28
|
+
"""
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
subparsers = parser.add_subparsers(dest='command', help='Available commands')
|
|
32
|
+
|
|
33
|
+
# Run command
|
|
34
|
+
run_parser = subparsers.add_parser('run', help='Run test cases from a configuration file')
|
|
35
|
+
run_parser.add_argument('config_file', help='Path to the test configuration file (JSON or YAML)')
|
|
36
|
+
run_parser.add_argument('--workspace', '-w', help='Working directory for test execution')
|
|
37
|
+
run_parser.add_argument('--parallel', '-p', action='store_true', help='Run tests in parallel')
|
|
38
|
+
run_parser.add_argument('--workers', type=int, help='Number of parallel workers (default: CPU count)')
|
|
39
|
+
run_parser.add_argument('--execution-mode', choices=['thread', 'process'], default='thread',
|
|
40
|
+
help='Parallel execution mode (default: thread)')
|
|
41
|
+
run_parser.add_argument('--output-format', choices=['text', 'json', 'html'], default='text',
|
|
42
|
+
help='Output format for test results')
|
|
43
|
+
run_parser.add_argument('--verbose', '-v', action='store_true', help='Enable verbose output')
|
|
44
|
+
run_parser.add_argument('--debug', action='store_true', help='Enable debug mode')
|
|
45
|
+
|
|
46
|
+
return parser
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def run_tests(args):
|
|
50
|
+
"""Run tests based on command line arguments"""
|
|
51
|
+
config_file = Path(args.config_file)
|
|
52
|
+
|
|
53
|
+
if not config_file.exists():
|
|
54
|
+
print(f"Error: Configuration file not found: {config_file}")
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
# Determine file type
|
|
58
|
+
file_ext = config_file.suffix.lower()
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
if args.parallel:
|
|
62
|
+
# Use parallel runner
|
|
63
|
+
runner = ParallelJSONRunner(
|
|
64
|
+
config_file=str(config_file),
|
|
65
|
+
workspace=args.workspace,
|
|
66
|
+
max_workers=args.workers,
|
|
67
|
+
execution_mode=args.execution_mode
|
|
68
|
+
)
|
|
69
|
+
else:
|
|
70
|
+
# Use appropriate single-threaded runner
|
|
71
|
+
if file_ext in ['.json']:
|
|
72
|
+
runner = JSONRunner(
|
|
73
|
+
config_file=str(config_file),
|
|
74
|
+
workspace=args.workspace
|
|
75
|
+
)
|
|
76
|
+
elif file_ext in ['.yaml', '.yml']:
|
|
77
|
+
runner = YAMLRunner(
|
|
78
|
+
config_file=str(config_file),
|
|
79
|
+
workspace=args.workspace
|
|
80
|
+
)
|
|
81
|
+
else:
|
|
82
|
+
print(f"Error: Unsupported configuration file format: {file_ext}")
|
|
83
|
+
return False
|
|
84
|
+
|
|
85
|
+
# Run tests
|
|
86
|
+
print(f"Running tests from: {config_file}")
|
|
87
|
+
if args.parallel:
|
|
88
|
+
print(f"Parallel mode: {args.execution_mode}, workers: {args.workers or 'auto'}")
|
|
89
|
+
|
|
90
|
+
success = runner.run_tests()
|
|
91
|
+
|
|
92
|
+
# Output results
|
|
93
|
+
if hasattr(runner, 'results'):
|
|
94
|
+
results = runner.results
|
|
95
|
+
print(f"\nTest Results:")
|
|
96
|
+
print(f"Total tests: {results.get('total_tests', 0)}")
|
|
97
|
+
print(f"Passed: {results.get('passed', 0)}")
|
|
98
|
+
print(f"Failed: {results.get('failed', 0)}")
|
|
99
|
+
|
|
100
|
+
if args.verbose and 'details' in results:
|
|
101
|
+
print("\nDetailed Results:")
|
|
102
|
+
for result in results['details']:
|
|
103
|
+
status_symbol = "✓" if result['status'] == 'passed' else "✗"
|
|
104
|
+
print(f" {status_symbol} {result['name']}: {result['status']}")
|
|
105
|
+
if result['status'] == 'failed' and result.get('message'):
|
|
106
|
+
print(f" Error: {result['message']}")
|
|
107
|
+
|
|
108
|
+
return success
|
|
109
|
+
|
|
110
|
+
except Exception as e:
|
|
111
|
+
print(f"Error running tests: {e}")
|
|
112
|
+
if args.debug:
|
|
113
|
+
import traceback
|
|
114
|
+
traceback.print_exc()
|
|
115
|
+
return False
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def main():
|
|
119
|
+
"""Main entry point for the CLI"""
|
|
120
|
+
parser = create_parser()
|
|
121
|
+
args = parser.parse_args()
|
|
122
|
+
|
|
123
|
+
if args.command == 'run':
|
|
124
|
+
success = run_tests(args)
|
|
125
|
+
sys.exit(0 if success else 1)
|
|
126
|
+
else:
|
|
127
|
+
parser.print_help()
|
|
128
|
+
sys.exit(1)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
if __name__ == '__main__':
|
|
132
|
+
main()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Core components for the CLI Testing Framework
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .base_runner import BaseRunner
|
|
6
|
+
from .parallel_runner import ParallelRunner
|
|
7
|
+
from .test_case import TestCase
|
|
8
|
+
from .assertions import Assertions
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
'BaseRunner',
|
|
12
|
+
'ParallelRunner',
|
|
13
|
+
'TestCase',
|
|
14
|
+
'Assertions'
|
|
15
|
+
]
|
{cli_test_framework-0.2.1/src → cli_test_framework-0.2.2/src/cli_test_framework}/core/assertions.py
RENAMED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import re
|
|
2
|
-
from typing import Any, Pattern
|
|
3
|
-
|
|
4
|
-
class Assertions:
|
|
5
|
-
@staticmethod
|
|
6
|
-
def equals(actual: Any, expected: Any, message: str = "") -> bool:
|
|
7
|
-
if actual != expected:
|
|
8
|
-
raise AssertionError(f"{message} Expected: {expected}, but got: {actual}")
|
|
9
|
-
return True
|
|
10
|
-
|
|
11
|
-
@staticmethod
|
|
12
|
-
def contains(container: str, item: str, message: str = "") -> bool:
|
|
13
|
-
"""
|
|
14
|
-
Check if the item is contained within the container string.
|
|
15
|
-
This method returns True if the item is found anywhere within the container,
|
|
16
|
-
even if the container contains other information.
|
|
17
|
-
"""
|
|
18
|
-
if item not in container:
|
|
19
|
-
raise AssertionError(f"{message} Expected to contain: {item}")
|
|
20
|
-
return True
|
|
21
|
-
|
|
22
|
-
@staticmethod
|
|
23
|
-
def matches(text: str, pattern: str, message: str = "") -> bool:
|
|
24
|
-
if not re.search(pattern, text):
|
|
25
|
-
raise AssertionError(f"{message} Text does not match pattern: {pattern}")
|
|
26
|
-
return True
|
|
27
|
-
|
|
28
|
-
@staticmethod
|
|
29
|
-
def return_code_equals(actual: int, expected: int, message: str = "") -> bool:
|
|
30
|
-
if actual != expected:
|
|
31
|
-
raise AssertionError(f"{message} Expected return code: {expected}, got: {actual}")
|
|
1
|
+
import re
|
|
2
|
+
from typing import Any, Pattern
|
|
3
|
+
|
|
4
|
+
class Assertions:
|
|
5
|
+
@staticmethod
|
|
6
|
+
def equals(actual: Any, expected: Any, message: str = "") -> bool:
|
|
7
|
+
if actual != expected:
|
|
8
|
+
raise AssertionError(f"{message} Expected: {expected}, but got: {actual}")
|
|
9
|
+
return True
|
|
10
|
+
|
|
11
|
+
@staticmethod
|
|
12
|
+
def contains(container: str, item: str, message: str = "") -> bool:
|
|
13
|
+
"""
|
|
14
|
+
Check if the item is contained within the container string.
|
|
15
|
+
This method returns True if the item is found anywhere within the container,
|
|
16
|
+
even if the container contains other information.
|
|
17
|
+
"""
|
|
18
|
+
if item not in container:
|
|
19
|
+
raise AssertionError(f"{message} Expected to contain: {item}")
|
|
20
|
+
return True
|
|
21
|
+
|
|
22
|
+
@staticmethod
|
|
23
|
+
def matches(text: str, pattern: str, message: str = "") -> bool:
|
|
24
|
+
if not re.search(pattern, text):
|
|
25
|
+
raise AssertionError(f"{message} Text does not match pattern: {pattern}")
|
|
26
|
+
return True
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def return_code_equals(actual: int, expected: int, message: str = "") -> bool:
|
|
30
|
+
if actual != expected:
|
|
31
|
+
raise AssertionError(f"{message} Expected return code: {expected}, got: {actual}")
|
|
32
32
|
return True
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""
|
|
2
|
+
File comparison module for cli-test-framework.
|
|
3
|
+
This module provides functionality for comparing different types of files.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .factory import ComparatorFactory
|
|
7
|
+
from .result import ComparisonResult
|
|
8
|
+
|
|
9
|
+
__all__ = ['ComparatorFactory', 'ComparisonResult']
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
@file base_comparator.py
|
|
6
|
+
@brief Base abstract class for file comparison operations
|
|
7
|
+
@author Xiaotong Wang
|
|
8
|
+
@date 2025
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from abc import ABC, abstractmethod
|
|
12
|
+
import logging
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from .result import ComparisonResult, Difference
|
|
15
|
+
|
|
16
|
+
class BaseComparator(ABC):
|
|
17
|
+
"""
|
|
18
|
+
@brief Base abstract class for all file comparators
|
|
19
|
+
@details This class defines the interface and common functionality for all file comparators.
|
|
20
|
+
It provides basic file comparison operations and logging capabilities.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, encoding="utf-8", chunk_size=8192, verbose=False):
|
|
24
|
+
"""
|
|
25
|
+
@brief Initialize the base comparator
|
|
26
|
+
@param encoding str: File encoding to use (default: "utf-8")
|
|
27
|
+
@param chunk_size int: Size of chunks for reading large files (default: 8192)
|
|
28
|
+
@param verbose bool: Enable verbose logging (default: False)
|
|
29
|
+
"""
|
|
30
|
+
self.encoding = encoding
|
|
31
|
+
self.chunk_size = chunk_size
|
|
32
|
+
self.logger = logging.getLogger(f"file_comparator.{self.__class__.__name__}")
|
|
33
|
+
if verbose:
|
|
34
|
+
self.logger.setLevel(logging.DEBUG)
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def read_content(self, file_path, start_line=0, end_line=None, start_column=0, end_column=None):
|
|
38
|
+
"""
|
|
39
|
+
@brief Read file content with specified range
|
|
40
|
+
@param file_path Path: Path to the file to read
|
|
41
|
+
@param start_line int: Starting line number (0-based)
|
|
42
|
+
@param end_line int: Ending line number (0-based, None for end of file)
|
|
43
|
+
@param start_column int: Starting column number (0-based)
|
|
44
|
+
@param end_column int: Ending column number (0-based, None for end of line)
|
|
45
|
+
@return object: File content in a format suitable for comparison
|
|
46
|
+
"""
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def compare_content(self, content1, content2):
|
|
51
|
+
"""
|
|
52
|
+
@brief Compare two content objects and return comparison details
|
|
53
|
+
@param content1 object: First content object to compare
|
|
54
|
+
@param content2 object: Second content object to compare
|
|
55
|
+
@return tuple: (bool, list) - (identical, differences)
|
|
56
|
+
"""
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
def compare_files(self, file1, file2, start_line=0, end_line=None, start_column=0, end_column=None):
|
|
60
|
+
"""
|
|
61
|
+
@brief Compare two files with the specified parameters
|
|
62
|
+
@param file1 Path: Path to the first file
|
|
63
|
+
@param file2 Path: Path to the second file
|
|
64
|
+
@param start_line int: Starting line number (0-based)
|
|
65
|
+
@param end_line int: Ending line number (0-based, None for end of file)
|
|
66
|
+
@param start_column int: Starting column number (0-based)
|
|
67
|
+
@param end_column int: Ending column number (0-based, None for end of line)
|
|
68
|
+
@return ComparisonResult: Result object containing comparison details
|
|
69
|
+
"""
|
|
70
|
+
result = ComparisonResult(
|
|
71
|
+
file1=str(file1),
|
|
72
|
+
file2=str(file2),
|
|
73
|
+
start_line=start_line,
|
|
74
|
+
end_line=end_line,
|
|
75
|
+
start_column=start_column,
|
|
76
|
+
end_column=end_column
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
self.logger.info(f"Comparing files: {file1} and {file2}")
|
|
81
|
+
|
|
82
|
+
# Record file metadata
|
|
83
|
+
file1_path = Path(file1)
|
|
84
|
+
file2_path = Path(file2)
|
|
85
|
+
result.file1_size = file1_path.stat().st_size
|
|
86
|
+
result.file2_size = file2_path.stat().st_size
|
|
87
|
+
|
|
88
|
+
# Read content with specified ranges
|
|
89
|
+
self.logger.debug(f"Reading content from files")
|
|
90
|
+
content1 = self.read_content(file1, start_line, end_line, start_column, end_column)
|
|
91
|
+
content2 = self.read_content(file2, start_line, end_line, start_column, end_column)
|
|
92
|
+
|
|
93
|
+
# Compare content
|
|
94
|
+
self.logger.debug(f"Comparing content")
|
|
95
|
+
identical, differences = self.compare_content(content1, content2)
|
|
96
|
+
|
|
97
|
+
# Update result
|
|
98
|
+
result.identical = identical
|
|
99
|
+
result.differences = differences
|
|
100
|
+
|
|
101
|
+
return result
|
|
102
|
+
|
|
103
|
+
except Exception as e:
|
|
104
|
+
self.logger.error(f"Error during comparison: {str(e)}")
|
|
105
|
+
result.error = str(e)
|
|
106
|
+
result.identical = False
|
|
107
|
+
return result
|