Skip to content
Merged
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ tox
pytest
pytest-cov
pytest-flake8
pytest-xdist
flake8==3.9.2
45 changes: 45 additions & 0 deletions tests/file_format_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0

import os
import subprocess


def run_command(*args): # 리눅스 명령어 실행
result = subprocess.run(args, capture_output=True, text=True)
success = result.returncode == 0
output = result.stdout if success else result.stderr
return success, output


def test_output_file_format(): # 테스트 환경 tox.ini
# Given
run_command("rm", "-rf", "test_result")
os.makedirs("test_result", exist_ok=True)

# When
excel_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "excel")
csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "csv")
opossum_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "opossum")
yaml_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "yaml")
files_in_result = os.listdir("test_result")

# Then
excel_files = [f for f in files_in_result if f.endswith('.xlsx')] # Assuming Excel files end with .xlsx
csv_files = [f for f in files_in_result if f.endswith('.csv')]
opossum_files = [f for f in files_in_result if f.endswith('.json')] # Assuming opossum files are in JSON format
yaml_files = [f for f in files_in_result if f.endswith('.yaml') or f.endswith('.yml')]

assert excel_success is True, "Test Output File : Excel files not properly created (not create)"
assert len(excel_files) > 0, "Test Output File : Excel files not properly created (length)"

assert csv_success is True, "Test Output File : CSV files not properly created (not create)"
assert len(csv_files) > 0, "Test Output File : CSV files not properly created (length)"

assert opossum_success is True, "Test Output File : JSON files not properly created (not create)"
assert len(opossum_files) > 0, "Test Output File : JSON files not properly created (length)"

assert yaml_success is True, "Test Output File : Yaml files not properly created (not create)"
assert len(yaml_files) > 0, "Test Output File : Yaml files not properly created (length)"
70 changes: 70 additions & 0 deletions tests/initial_tox_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0

import os
import subprocess


def run_command(*args): # 리눅스 명령어 실행
result = subprocess.run(args, capture_output=True, text=True)
success = result.returncode == 0
output = result.stdout if success else result.stderr
return success, output


def test_test_run_environment(): # 테스트 환경 tox.ini
# Given
run_command("rm", "-rf", "test_result")
os.makedirs("test_result", exist_ok=True)

# When
csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result", "-f", "csv")
exclude_success, _ = run_command(
"fosslight_bin",
"-p", ".",
"-e", "test", "commons-logging-1.2.jar",
"-o", "test_result/exclude_result.csv",
"-f", "csv"
)

files_in_result = run_command("ls", "test_result")[1].split()
txt_success, txt_output = run_command("find", "test_result", "-type", "f", "-name", "fosslight_log*.txt")

# Then
csv_files = [f for f in files_in_result if f.endswith('.csv')]
txt_files = txt_output.split()
assert csv_success is True, "Test Run Environment: CSV files not properly created (not create)"
assert len(csv_files) > 0, "Test Run Environment: CSV files not properly created (length)"
assert exclude_success is True, "Test Run Environment: Exclude feature fail"
assert len(txt_files) > 0, "Test Run Environment: txt files not properly created"


def test_release_environment(): # 릴리즈 환경 tox.ini
# Given
run_command("rm", "-rf", "test_result")
os.makedirs("test_result", exist_ok=True)

# When
help_success, _ = run_command("fosslight_bin", "-h")
csv_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.csv", "-f", "csv")
exclude_csv_success, _ = run_command(
"fosslight_bin",
"-p", ".",
"-e", "test", "commons-logging-1.2.jar",
"-o", "test_result/exclude_result.csv",
"-f", "csv"
)

json_success, _ = run_command("fosslight_bin", "-p", ".", "-o", "test_result/result.json", "-f", "opossum")
files_in_result = run_command("ls", "test_result")[1].split()

# Then
required_files = ['result.csv', 'exclude_result.csv', 'result.json']
files_exist = all(f in files_in_result for f in required_files)
assert help_success is True, "Release Run Environment: help method not properly processing"
assert csv_success is True, "Release Run Environment: CSV files not properly created"
assert exclude_csv_success is True, "Release Run Environment: Exclude feature fail"
assert json_success is True, "Release Run Environment: json files not properly created"
assert files_exist is True, "Release Run Environment: Required files not properly created"
23 changes: 9 additions & 14 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ allowlist_externals =
ls
rm
cat
pytest
{toxinidir}/dist/cli
setenv =
PYTHONPATH=.
Expand All @@ -24,28 +25,22 @@ ignore = E722, W503
filterwarnings = ignore::DeprecationWarning

[testenv:test_run]
changedir = tests
commands =
rm -rf test_result
fosslight_bin -p tests -o test_result -f csv
fosslight_bin -p tests -e test commons-logging-1.2.jar -o test_result/exclude_result.csv -f csv
ls test_result/
bash -c 'find test_result -type f -name "fosslight_report*.csv" | xargs cat'
bash -c 'find test_result -type f -name "fosslight_binary*.txt" | xargs cat'
pytest -n 4 initial_tox_test.py::test_test_run_environment

[testenv:format_test]
changedir = tests
commands =
pytest -n 4 file_format_test.py::test_output_file_format

[testenv:release]
deps =
-r{toxinidir}/requirements-dev.txt

commands =
fosslight_bin -h
fosslight_bin -p tests -o test_result/result.csv -f csv
fosslight_bin -p tests -e test commons-logging-1.2.jar -o test_result/exclude_result.csv -f csv
ls test_result/
cat test_result/result.csv
cat test_result/exclude_result.csv
fosslight_bin -p tests -o test_result/result.json -f opossum
pytest -n 5 tests/initial_tox_test.py::test_release_environment
pytest -v --flake8
pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks --hidden-import=pkg_resources.extern
{toxinidir}/dist/cli -p tests -o test_result_cli
; py.test --cov-report term-missing --cov={envsitepackagesdir}/fosslight_binary