|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2020 LG Electronics Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | + |
| 7 | +import pytest |
| 8 | +from fosslight_scanner._run_compare import write_result_json_yaml, parse_result_for_table, get_sample_html, \ |
| 9 | + write_result_html, write_result_xlsx, write_compared_result, get_comparison_result_filename, \ |
| 10 | + count_compared_result, run_compare, \ |
| 11 | + ADD, DELETE, CHANGE, XLSX_EXT, HTML_EXT, YAML_EXT, JSON_EXT |
| 12 | +import logging |
| 13 | +import json |
| 14 | +import yaml |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize("ext, expected_content", [ |
| 18 | + # Test for JSON and YAML extensions |
| 19 | + (".json", {"key": "value"}), |
| 20 | + (".yaml", {"key": "value"}), |
| 21 | +
|
| 22 | + # Test for TXT extension (failure) |
| 23 | + (".txt", {"key": "value"}), |
| 24 | +]) |
| 25 | +def test_write_result_json_yaml(tmp_path, ext, expected_content): |
| 26 | + # given |
| 27 | + output_file = tmp_path / f"result{ext}" |
| 28 | + compared_result = expected_content |
| 29 | + |
| 30 | + # when |
| 31 | + success = write_result_json_yaml(output_file, compared_result, ext) |
| 32 | + |
| 33 | + # then |
| 34 | + assert success is True, f"Failed to write file with extension {ext}" |
| 35 | + |
| 36 | + # Verify content based on extension |
| 37 | + if ext == ".json": |
| 38 | + with open(output_file, 'r', encoding='utf-8') as f: |
| 39 | + result_content = json.load(f) |
| 40 | + assert result_content == compared_result, "The content of the JSON file does not match the expected content." |
| 41 | + |
| 42 | + elif ext == ".yaml": |
| 43 | + with open(output_file, 'r', encoding='utf-8') as f: |
| 44 | + result_content = yaml.safe_load(f) |
| 45 | + assert result_content == compared_result, "The content of the YAML file does not match the expected content." |
| 46 | + |
| 47 | + elif ext == ".txt": |
| 48 | + with open(output_file, 'r', encoding='utf-8') as f: |
| 49 | + result_lines = f.readlines() |
| 50 | + result_content = ''.join(result_lines) |
| 51 | + assert result_content != compared_result, "The content of the TXT file does not match the expected string representation." |
| 52 | + |
| 53 | + |
| 54 | +def test_parse_result_for_table(): |
| 55 | + # given |
| 56 | + add_expected = [ADD, '', '', 'test(1.0)', 'MIT'] |
| 57 | + oi = {"name": "test", "version": "1.0", "license": ["MIT"]} |
| 58 | + |
| 59 | + # when |
| 60 | + add_result = parse_result_for_table(oi, ADD) |
| 61 | + |
| 62 | + # then |
| 63 | + assert add_result == add_expected |
| 64 | + |
| 65 | + |
| 66 | +def test_get_sample_html(): |
| 67 | + # then |
| 68 | + assert get_sample_html() != '' |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("compared_result, expected_before, expected_after", [ |
| 72 | + # Case with empty add, delete, change |
| 73 | + ({ADD: [], DELETE: [], CHANGE: []}, "before.yaml", "after.yaml"), |
| 74 | +
|
| 75 | + # Case with one entry in add and no deletes or changes |
| 76 | + ({ADD: [{"name": "test", "version": "1.0", "license": ["MIT"]}], DELETE: [], CHANGE: []}, |
| 77 | + "before.yaml", "after.yaml") |
| 78 | +]) |
| 79 | +def test_write_result_html(tmp_path, compared_result, expected_before, expected_after): |
| 80 | + # given |
| 81 | + output_file = tmp_path / "result.html" |
| 82 | + |
| 83 | + # when |
| 84 | + success = write_result_html(output_file, compared_result, expected_before, expected_after) |
| 85 | + |
| 86 | + # then |
| 87 | + assert success is True, "Failed to write the HTML file." |
| 88 | + assert output_file.exists(), "The HTML file was not created." |
| 89 | + with open(output_file, 'r', encoding='utf-8') as f: |
| 90 | + content = f.read() |
| 91 | + assert content, "The HTML file is empty." |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize("compared_result", [ |
| 95 | + # Case with empty add, delete, change |
| 96 | + {ADD: [], DELETE: [], CHANGE: []}, |
| 97 | +
|
| 98 | + # Case with one entry in add and no deletes or changes |
| 99 | + {ADD: [{"name": "test", "version": "1.0", "license": ["MIT"]}], DELETE: [], CHANGE: []} |
| 100 | +]) |
| 101 | +def test_write_result_xlsx(tmp_path, compared_result): |
| 102 | + # given |
| 103 | + output_file = tmp_path / "result.xlsx" |
| 104 | + |
| 105 | + # when |
| 106 | + success = write_result_xlsx(output_file, compared_result) |
| 107 | + |
| 108 | + # then |
| 109 | + assert success is True, "Failed to write the XLSX file." |
| 110 | + assert output_file.exists(), "The XLSX file was not created." |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("ext, expected_output", [ |
| 114 | + (XLSX_EXT, "xlsx"), |
| 115 | + (HTML_EXT, "html"), |
| 116 | + (JSON_EXT, "json"), |
| 117 | + (YAML_EXT, "yaml"), |
| 118 | +]) |
| 119 | +def test_write_compared_result(tmp_path, ext, expected_output): |
| 120 | + # given |
| 121 | + output_file = tmp_path / "result" |
| 122 | + compared_result = {ADD: [], DELETE: [], CHANGE: []} |
| 123 | + |
| 124 | + # when |
| 125 | + success, result_file = write_compared_result(output_file, compared_result, ext) |
| 126 | + |
| 127 | + # then |
| 128 | + assert success is True, f"Failed to write the compared result for extension {ext}" |
| 129 | + if ext == XLSX_EXT: |
| 130 | + assert str(result_file) == str(output_file), "The XLSX result file path does not match the expected output path." |
| 131 | + elif ext == HTML_EXT: |
| 132 | + expected_result_file = f"{str(output_file) + XLSX_EXT}, {str(output_file)}" |
| 133 | + assert result_file == expected_result_file, "HTML file creation failed." |
| 134 | + elif ext == JSON_EXT: |
| 135 | + assert str(result_file) == str(output_file), "The JSON result file path does not match the expected output path." |
| 136 | + else: |
| 137 | + assert str(result_file) == str(output_file), "The YAML result file path does not match the expected output path." |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.parametrize("path, file_name, ext, time_suffix, expected_output", [ |
| 141 | + # Case when file name is provided |
| 142 | + ("/path", "file", XLSX_EXT, "time", "/path/file.xlsx"), |
| 143 | +
|
| 144 | + # Case when file name is empty, with different extensions |
| 145 | + ("/path", "", XLSX_EXT, "time", "/path/fosslight_compare_time.xlsx"), |
| 146 | + ("/path", "", HTML_EXT, "time", "/path/fosslight_compare_time.html"), |
| 147 | + ("/path", "", YAML_EXT, "time", "/path/fosslight_compare_time.yaml"), |
| 148 | + ("/path", "", JSON_EXT, "time", "/path/fosslight_compare_time.json"), |
| 149 | +]) |
| 150 | +def test_get_comparison_result_filename(path, file_name, ext, time_suffix, expected_output): |
| 151 | + # when |
| 152 | + result = get_comparison_result_filename(path, file_name, ext, time_suffix) |
| 153 | + |
| 154 | + # then |
| 155 | + assert result == expected_output, f"Expected {expected_output} but got {result}" |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.parametrize("compared_result, expected_log", [ |
| 159 | + ({ADD: [], DELETE: [], CHANGE: []}, "all oss lists are the same."), |
| 160 | + ({ADD: [{"name": "test"}], DELETE: [], CHANGE: []}, "total 1 oss updated (add: 1, delete: 0, change: 0)") |
| 161 | +]) |
| 162 | +def test_count_compared_result(compared_result, expected_log, caplog): |
| 163 | + # when |
| 164 | + with caplog.at_level(logging.INFO): |
| 165 | + count_compared_result(compared_result) |
| 166 | + # then |
| 167 | + assert expected_log in caplog.text |
| 168 | + |
| 169 | + |
| 170 | +def test_run_compare_different_extension(tmp_path): |
| 171 | + # given |
| 172 | + before_f = tmp_path / "before.yaml" |
| 173 | + after_f = tmp_path / "after.xlsx" |
| 174 | + output_path = tmp_path |
| 175 | + output_file = "result" |
| 176 | + file_ext = ".yaml" |
| 177 | + _start_time = "time" |
| 178 | + _output_dir = tmp_path |
| 179 | + |
| 180 | + # Write example content to before_f and after_f |
| 181 | + before_content = { |
| 182 | + "oss_list": [ |
| 183 | + {"name": "test", "version": "1.0", "license": "MIT"} |
| 184 | + ] |
| 185 | + } |
| 186 | + |
| 187 | + # Write these contents to the files |
| 188 | + with open(before_f, "w") as bf: |
| 189 | + yaml.dump(before_content, bf) |
| 190 | + |
| 191 | + # Create an empty xlsx file for after_f |
| 192 | + with open(after_f, "w") as af: |
| 193 | + af.write("") |
| 194 | + |
| 195 | + # when |
| 196 | + comparison_result = run_compare(before_f, after_f, output_path, output_file, file_ext, _start_time, _output_dir) |
| 197 | + |
| 198 | + # then |
| 199 | + assert comparison_result is False |
0 commit comments