Skip to content

Commit ad2dec2

Browse files
authored
Tox to pytest (#188)
Signed-off-by: hkkim <[email protected]>
1 parent d7986e5 commit ad2dec2

File tree

3 files changed

+96
-15
lines changed

3 files changed

+96
-15
lines changed

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pytest-flake8
55
flake8==3.9.2
66
dataclasses
77
scanoss
8-
importlib-metadata==4.12.0
8+
importlib-metadata==4.12.0
9+
pytest-xdist

tests/test_tox.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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"

tox.ini

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ allowlist_externals =
1010
cat
1111
rm
1212
ls
13+
pytest
1314
setenv =
1415
PYTHONPATH=.
1516

@@ -25,29 +26,21 @@ deps =
2526
-r{toxinidir}/requirements-dev.txt
2627

2728
commands =
28-
rm -rf test_scan
29-
fosslight_source -p tests/test_files -j -m -o test_scan
30-
fosslight_source -p tests -e test_files/test cli_test.py -j -m -o test_scan2
29+
pytest tests/test_tox.py::test_run --maxfail=1 --disable-warnings --cache-clear
3130

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

3635
commands =
37-
fosslight_source -h
38-
39-
fosslight_source -p tests/test_files -o test_scan/scan_result.csv
40-
cat test_scan/scan_result.csv
41-
42-
fosslight_source -p tests -e test_files/test cli_test.py -j -m -o test_scan2/scan_exclude_result.csv
43-
cat test_scan2/scan_exclude_result.csv
44-
45-
fosslight_source -p tests/test_files -m -j -o test_scan3/
46-
ls test_scan3/
36+
pytest tests/test_tox.py::test_help_command tests/test_tox.py::test_scan_command \
37+
tests/test_tox.py::test_exclude_command tests/test_tox.py::test_json_command \
38+
--maxfail=1 --disable-warnings
4739

4840
python tests/cli_test.py
4941
pytest -v --flake8
5042

5143
[testenv:flake8]
5244
deps = flake8
53-
commands = flake8
45+
commands =
46+
pytest tests/test_tox.py::test_flake8

0 commit comments

Comments
 (0)