Add initial code
[ta/build-tools.git] / tools / script / process_rpmdata_test.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16
17 from tools.convert import CsvConverter
18 from tools.script.create_rpm_data_test_data import expected_output
19 from tools.script.process_rpmdata import main
20
21
22 def gen_input(tmpdir, rpmdata):
23     p = tmpdir.join('rpmdata.json')
24     p.write(rpmdata)
25     return str(p)
26
27
28 def test_no_output(tmpdir):
29     main(['--rpmdata-path', gen_input(tmpdir, {})])
30
31
32 def test_components(tmpdir):
33     input_content = [expected_output[0],
34                      expected_output[3]]
35     input_ = json.dumps(input_content)
36     output_json = tmpdir.join('components.json')
37     output_csv = tmpdir.join('components.csv')
38     main(['--rpmdata-path', gen_input(tmpdir, input_),
39           '--output-components', str(output_json),
40           '--output-components-csv', str(output_csv)])
41     additions = [{'FOSS': u'yes',
42                   'Source RPM': 'internal-pkg-4-4.src.rpm',
43                   'Name': '@types/d3-axis',
44                   'Source URL': 'http://some.url/1',
45                   'Version': '1.0.10',
46                   'Crypto capable': True},
47                  {'FOSS': u'Yes',
48                   'Source RPM': 'internal-pkg-4-4.src.rpm',
49                   'Name': '@types/d3-array@*',
50                   'Source URL': 'http://some.url/2',
51                   'Version': '1.2.1'}]
52     assert json.loads(output_json.read()) == input_content + additions
53     assert output_csv.read() == _gen_components_csv(input_content + additions)
54
55
56 def _gen_components_csv(_list):
57     csv = CsvConverter(_list, preferred_field_order=['Name', 'Version', 'Release', 'Source RPM'])
58     return csv.convert_to_ms_excel(text_fields=['Version'])