• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import pytest
17import os
18import json
19from unittest.mock import patch
20import sys
21
22sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))
23from generate_readme_opensource import (
24    ask_question,
25    ask_for_list,
26    process_license_info,
27    generate_readme_opensource
28)
29
30
31@pytest.fixture
32def temp_output_dir(tmp_path):
33    """创建临时输出目录"""
34    return str(tmp_path)
35
36
37def test_ask_question():
38    """测试ask_question函数"""
39    with patch('builtins.input', return_value='test_value'):
40        assert ask_question("prompt", "default") == "test_value"
41
42    with patch('builtins.input', return_value=''):
43        assert ask_question("prompt", "default") == "default"
44
45
46def test_ask_for_list():
47    """测试ask_for_list函数"""
48    with patch('builtins.input', return_value='item1, item2, item3'):
49        result = ask_for_list("prompt")
50        assert result == ['item1', 'item2', 'item3']
51
52    with patch('builtins.input', return_value=''):
53        result = ask_for_list("prompt")
54        assert result == []
55
56
57def test_process_license_info_single():
58    """测试处理单个许可证信息"""
59    with patch('builtins.input', side_effect=['MIT', 'LICENSE']):
60        licenses, files = process_license_info()
61        assert licenses == ['MIT']
62        assert files == ['LICENSE']
63
64
65def test_process_license_info_multiple():
66    """测试处理多个许可证信息"""
67    with patch('builtins.input', side_effect=['MIT; Apache-2.0', 'LICENSE.mit; LICENSE.apache']):
68        licenses, files = process_license_info()
69        assert licenses == ['MIT', 'Apache-2.0']
70        assert files == ['LICENSE.mit', 'LICENSE.apache']
71
72
73def test_process_license_info_one_license_multiple_files():
74    """测试一个许可证对应多个文件的情况"""
75    with patch('builtins.input', side_effect=['MIT', 'LICENSE.txt; COPYING.txt']):
76        licenses, files = process_license_info()
77        assert licenses == ['MIT']
78        assert files == ['LICENSE.txt', 'COPYING.txt']
79
80
81def test_process_license_info_multiple_licenses_one_file():
82    """测试多个许可证对应一个文件的情况"""
83    with patch('builtins.input', side_effect=['MIT; Apache-2.0', 'LICENSE']):
84        licenses, files = process_license_info()
85        assert licenses == ['MIT', 'Apache-2.0']
86        assert files == ['LICENSE']
87
88
89def test_process_license_info_error():
90    """测试许可证信息不匹配的错误情况"""
91    # 测试三个许可证对应两个文件的情况,这应该会引发错误
92    with patch('builtins.input', side_effect=['MIT; Apache-2.0; GPL', 'LICENSE.mit; LICENSE.apache']):
93        with pytest.raises(ValueError) as exc_info:
94            process_license_info()
95        assert "许可证和许可证文件的数量不匹配" in str(exc_info.value)
96
97
98def test_generate_readme_opensource(temp_output_dir):
99    """测试生成README.OpenSource文件"""
100    # 模拟用户输入
101    input_values = [
102        'TestComponent',  # Name
103        '1.0.0',  # Version
104        'Test Owner',  # Owner
105        'https://example.com',  # URL
106        'Test Description',  # Description
107        'MIT',  # License
108        'LICENSE',  # License File
109        'dep1, dep2',  # Dependencies
110        'n'  # Don't add more components
111    ]
112
113    with patch('builtins.input', side_effect=input_values):
114        generate_readme_opensource(temp_output_dir)
115
116        # 验证文件是否创建
117        readme_path = os.path.join(temp_output_dir, 'README.OpenSource')
118        assert os.path.exists(readme_path)
119
120        # 验证文件内容
121        with open(readme_path, 'r', encoding='utf-8') as f:
122            content = json.load(f)
123            assert len(content) == 1
124            component = content[0]
125            assert component['Name'] == 'TestComponent'
126            assert component['Version Number'] == '1.0.0'
127            assert component['Owner'] == 'Test Owner'
128            assert component['Upstream URL'] == 'https://example.com'
129            assert component['Description'] == 'Test Description'
130            assert component['License'] == 'MIT'
131            assert component['License File'] == 'LICENSE'
132            assert component['Dependencies'] == ['dep1', 'dep2']
133
134
135def test_generate_readme_opensource_multiple_components(temp_output_dir):
136    """测试生成包含多个组件的README.OpenSource文件"""
137    input_values = [
138        # 第一个组件
139        'Component1',
140        '1.0.0',
141        'Owner1',
142        'https://example1.com',
143        'Description1',
144        'MIT',
145        'LICENSE1',
146        '',  # 无依赖项
147        'y',  # 添加另一个组件
148        # 第二个组件
149        'Component2',
150        '2.0.0',
151        'Owner2',
152        'https://example2.com',
153        'Description2',
154        'Apache-2.0',
155        'LICENSE2',
156        'dep1',
157        'n'  # 不再添加组件
158    ]
159
160    with patch('builtins.input', side_effect=input_values):
161        generate_readme_opensource(temp_output_dir)
162
163        # 验证文件是否创建
164        readme_path = os.path.join(temp_output_dir, 'README.OpenSource')
165        assert os.path.exists(readme_path)
166
167        # 验证文件内容
168        with open(readme_path, 'r', encoding='utf-8') as f:
169            content = json.load(f)
170            assert len(content) == 2
171
172            # 验证第一个组件
173            assert content[0]['Name'] == 'Component1'
174            assert content[0]['Version Number'] == '1.0.0'
175            assert 'Dependencies' not in content[0]
176
177            # 验证第二个组件
178            assert content[1]['Name'] == 'Component2'
179            assert content[1]['Version Number'] == '2.0.0'
180            assert content[1]['Dependencies'] == ['dep1']