|
| 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 | +import os |
| 7 | +import subprocess |
| 8 | +import shutil |
| 9 | +import pytest |
| 10 | + |
| 11 | +TEST_RESULT_DIR = "test_result" # 테스트 디렉토리 명 |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="function", autouse=True) # 테스트 디렉토리 생성/삭제 로직 |
| 15 | +def setup_test_result_dir_and_teardown(): |
| 16 | + print("==============setup==============") |
| 17 | + os.makedirs(TEST_RESULT_DIR, exist_ok=True) |
| 18 | + |
| 19 | + yield |
| 20 | + |
| 21 | + print("==============tearDown==============") |
| 22 | + shutil.rmtree(TEST_RESULT_DIR) |
| 23 | + |
| 24 | + |
| 25 | +def run_command(*args): # 리눅스 명령어 실행 |
| 26 | + result = subprocess.run(args, capture_output=True, text=True) |
| 27 | + success = result.returncode == 0 |
| 28 | + output = result.stdout if success else result.stderr |
| 29 | + return success, output |
| 30 | + |
| 31 | + |
| 32 | +def test_test_run_environment(): # 테스트 환경 tox.ini |
| 33 | + # When |
| 34 | + csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "csv") |
| 35 | + exclude_success, _ = run_command( |
| 36 | + "fosslight_bin", |
| 37 | + "-p", ".", |
| 38 | + "-e", "test", "commons-logging-1.2.jar", |
| 39 | + "-o", "test_result/exclude_result.csv", |
| 40 | + "-f", "csv" |
| 41 | + ) |
| 42 | + |
| 43 | + files_in_result = run_command("ls", "test_result")[1].split() |
| 44 | + txt_success, txt_output = run_command("find", "test_result", "-type", "f", "-name", "fosslight_log*.txt") |
| 45 | + |
| 46 | + # Then |
| 47 | + csv_files = [f for f in files_in_result if f.endswith('.csv')] |
| 48 | + txt_files = txt_output.split() |
| 49 | + assert csv_success is True, "Test Run Environment: CSV files not properly created (not create)" |
| 50 | + assert len(csv_files) > 0, "Test Run Environment: CSV files not properly created (length)" |
| 51 | + assert exclude_success is True, "Test Run Environment: Exclude feature fail" |
| 52 | + assert len(txt_files) > 0, "Test Run Environment: txt files not properly created" |
| 53 | + |
| 54 | + |
| 55 | +def test_release_environment(): # 릴리즈 환경 tox.ini |
| 56 | + # When |
| 57 | + help_success, _ = run_command("fosslight_bin", "-h") |
| 58 | + csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.csv", "-f", "csv") |
| 59 | + exclude_csv_success, _ = run_command( |
| 60 | + "fosslight_bin", |
| 61 | + "-p", ".", |
| 62 | + "-e", "test", "commons-logging-1.2.jar", |
| 63 | + "-o", "test_result/exclude_result.csv", |
| 64 | + "-f", "csv" |
| 65 | + ) |
| 66 | + |
| 67 | + json_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.json", "-f", "opossum") |
| 68 | + files_in_result = run_command("ls", "test_result")[1].split() |
| 69 | + |
| 70 | + # Then |
| 71 | + required_files = ['result.csv', 'exclude_result.csv', 'result.json'] |
| 72 | + files_exist = all(f in files_in_result for f in required_files) |
| 73 | + assert help_success is True, "Release Run Environment: help method not properly processing" |
| 74 | + assert csv_success is True, "Release Run Environment: CSV files not properly created" |
| 75 | + assert exclude_csv_success is True, "Release Run Environment: Exclude feature fail" |
| 76 | + assert json_success is True, "Release Run Environment: json files not properly created" |
| 77 | + assert files_exist is True, "Release Run Environment: Required files not properly created" |
0 commit comments