|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (c) 2020 LG Electronics Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import pytest |
| 8 | +import shutil |
| 9 | + |
| 10 | +remove_directories = ["test_scan", "test_scan2", "test_scan3"] |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="module", autouse=True) |
| 14 | +def setup_test_result_dir(): |
| 15 | + print("==============setup==============") |
| 16 | + for dir in remove_directories: |
| 17 | + if os.path.exists(dir): |
| 18 | + shutil.rmtree(dir) |
| 19 | + |
| 20 | + yield |
| 21 | + |
| 22 | + |
| 23 | +def run_command(command): |
| 24 | + process = subprocess.run(command, shell=True, capture_output=True, text=True) |
| 25 | + success = (process.returncode == 0) |
| 26 | + return success, process.stdout if success else process.stderr |
| 27 | + |
| 28 | + |
| 29 | +def test_run(): |
| 30 | + scan_success, _ = run_command("fosslight_source -p tests/test_files -j -m -o test_scan") |
| 31 | + scan_exclude_success, _ = run_command("fosslight_source -p tests -e test_files/test cli_test.py -j -m -o test_scan2") |
| 32 | + scan_files = os.listdir("test_scan") |
| 33 | + scan2_files = os.listdir('test_scan2') |
| 34 | + |
| 35 | + assert scan_success is True, "Test Run: Scan command failed" |
| 36 | + assert scan_exclude_success is True, "Test Run: Exclude command failed" |
| 37 | + assert len(scan_files) > 0, "Test Run: No scan files created in test_scan directory" |
| 38 | + assert len(scan2_files) > 0, "Test Run: No scan files created in test_scan2 directory" |
| 39 | + |
| 40 | + |
| 41 | +def test_help_command(): |
| 42 | + success, _ = run_command("fosslight_source -h") |
| 43 | + assert success is True, "Test Release: Help command failed " |
| 44 | + |
| 45 | + |
| 46 | +def test_scan_command(): |
| 47 | + success, _ = run_command("fosslight_source -p tests/test_files -o test_scan/scan_result.csv") |
| 48 | + assert success is True, "Test Release: Failed to generate scan result CSV file" |
| 49 | + |
| 50 | + assert os.path.exists("test_scan/scan_result.csv"), "Test Release: scan_result.csv file not generated" |
| 51 | + |
| 52 | + with open("test_scan/scan_result.csv", 'r') as file: |
| 53 | + content = file.read() |
| 54 | + |
| 55 | + assert len(content) > 0, "Test Release: scan_result.csv is empty" |
| 56 | + print(f"Content of scan_result.csv:\n{content}") |
| 57 | + |
| 58 | + |
| 59 | +def test_exclude_command(): |
| 60 | + success, _ = run_command( |
| 61 | + "fosslight_source -p tests -e test_files/test cli_test.py -j -m -o test_scan2/scan_exclude_result.csv" |
| 62 | + ) |
| 63 | + assert success is True, "Test release: Exclude scan failded" |
| 64 | + |
| 65 | + assert os.path.exists("test_scan2/scan_exclude_result.csv"), "Test Release: scan_exclude_result.csv file not generated" |
| 66 | + |
| 67 | + with open("test_scan2/scan_exclude_result.csv", 'r') as file: |
| 68 | + content = file.read() |
| 69 | + |
| 70 | + assert len(content) > 0, "Test Release: scan_exclude_result.csv is empty" |
| 71 | + print(f"Content of scan_exclude_result.csv:\n{content}") |
| 72 | + |
| 73 | + |
| 74 | +def test_json_command(): |
| 75 | + success, _ = run_command("fosslight_source -p tests/test_files -m -j -o test_scan3/") |
| 76 | + assert success is True, "Test release: Failed to generate JSON files" |
| 77 | + |
| 78 | + |
| 79 | +def test_ls_test_scan3_command(): |
| 80 | + files_in_test_scan3 = os.listdir("test_scan3") |
| 81 | + assert len(files_in_test_scan3) > 0, "Test Release: test_scan3 is empty" |
| 82 | + print(f"Files in test_scan3: {files_in_test_scan3}") |
| 83 | + |
| 84 | + |
| 85 | +def test_flake8(): |
| 86 | + success, _ = run_command("flake8 -j 4") |
| 87 | + assert success is True, "Flake8: Style check failed" |
0 commit comments