Skip to content

Commit 756d120

Browse files
authored
Refactor existing tox test to pytest (#225)
Signed-off-by: yongjunhong <[email protected]>
1 parent f703a48 commit 756d120

14 files changed

+306
-51
lines changed

Diff for: requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
flake8==3.9.2
2-
pyinstaller
2+
pyinstaller>=6.10.0
33
tox>=4.18.1
44
pytest
55
pytest-cov

Diff for: requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ requirements-parser
1010
defusedxml
1111
packageurl-python
1212
igraph
13-
matplotlib
13+
matplotlib

Diff for: tests/pytest/conftest.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import shutil
7+
import pytest
8+
9+
set_up_directories = [
10+
"tests/result/android",
11+
"tests/result/cocoapods",
12+
"tests/result/exclude",
13+
"tests/result/gradle",
14+
"tests/result/gradle2",
15+
"tests/result/helm",
16+
"tests/result/maven1",
17+
"tests/result/maven2",
18+
"tests/result/multi_pypi_npm",
19+
"tests/result/npm1",
20+
"tests/result/npm2",
21+
"tests/result/nuget1",
22+
"tests/result/nuget2",
23+
"tests/result/pub",
24+
"tests/result/pypi"
25+
]
26+
27+
remove_directories = set_up_directories
28+
29+
30+
@pytest.fixture(scope="session", autouse=True)
31+
def setup_test_result_dir_and_teardown():
32+
print("==============setup==============")
33+
for directory in set_up_directories:
34+
os.makedirs(directory, exist_ok=True)
35+
36+
yield
37+
38+
print("==============tearDown==============")
39+
for directory in remove_directories:
40+
shutil.rmtree(directory)

Diff for: tests/pytest/package_manager/__init__.py

Whitespace-only changes.

Diff for: tests/pytest/package_manager/test_android.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
10+
11+
12+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
13+
("tests/test_android", "tests/result/android", "-m android")
14+
])
15+
@pytest.mark.ubuntu
16+
def test_ubuntu(input_path, output_path, extra_args):
17+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
18+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
19+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
20+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
21+
22+
23+
@pytest.mark.parametrize("input_path, output_path", [
24+
(os.path.join("tests", "test_android", "sunflower"), os.path.join("tests", "result", "android"))
25+
])
26+
@pytest.mark.windows
27+
def test_windows(input_path, output_path):
28+
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
29+
result = subprocess.run(command, capture_output=True, text=True)
30+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
31+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_cocoapods.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
10+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
11+
("tests/test_cocoapods", "tests/result/cocoapods", "-m cocoapods")
12+
])
13+
@pytest.mark.ubuntu
14+
def test_ubuntu(input_path, output_path, extra_args):
15+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
16+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
17+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
18+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_gradle.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
10+
11+
12+
@pytest.mark.parametrize("input_path, output_path", [
13+
("tests/test_gradle/jib", "tests/result/gradle"),
14+
("tests/test_gradle2", "tests/result/gradle2")
15+
])
16+
@pytest.mark.ubuntu
17+
def test_ubuntu(input_path, output_path):
18+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
19+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
20+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
21+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
22+
23+
24+
@pytest.mark.parametrize("input_path, output_path", [
25+
(os.path.join("tests", "test_gradle", "jib"), os.path.join("tests", "result", "gradle")),
26+
(os.path.join("tests", "test_gradle2"), os.path.join("tests", "result", "gradle2"))
27+
])
28+
@pytest.mark.windows
29+
def test_windows(input_path, output_path):
30+
command = f"{DIST_PATH} -p {input_path} -o {output_path} -m gradle"
31+
result = subprocess.run(command, capture_output=True, text=True)
32+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
33+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_helm.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
10+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
11+
("tests/test_helm", "tests/result/helm", "-m helm")
12+
])
13+
@pytest.mark.ubuntu
14+
def test_ubuntu(input_path, output_path, extra_args):
15+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
16+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
17+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
18+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_maven.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
10+
11+
12+
@pytest.mark.parametrize("input_path, output_path", [
13+
("tests/test_maven1/lombok.maven", "tests/result/maven1"),
14+
("tests/test_maven2", "tests/result/maven2")
15+
])
16+
@pytest.mark.ubuntu
17+
def test_ubuntu(input_path, output_path):
18+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
19+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
20+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
21+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
22+
23+
24+
@pytest.mark.parametrize("input_path, output_path", [
25+
(os.path.join("tests", "test_maven2"), os.path.join("tests", "result", "maven2"))
26+
])
27+
@pytest.mark.windows
28+
def test_windows(input_path, output_path):
29+
command = f"{DIST_PATH} -p {input_path} -o {output_path} -m maven"
30+
result = subprocess.run(command, capture_output=True, text=True)
31+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
32+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_npm.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
10+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
11+
("tests/test_npm1", "tests/result/npm1", ""),
12+
("tests/test_npm2", "tests/result/npm2", "-m npm")
13+
])
14+
@pytest.mark.ubuntu
15+
def test_ubuntu(input_path, output_path, extra_args):
16+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
17+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
18+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
19+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_nuget.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
UBUNTU_COMMANDS = [
10+
"fosslight_dependency -p tests/test_nuget -o tests/result/nuget1",
11+
"fosslight_dependency -p tests/test_nuget2 -o tests/result/nuget2"
12+
]
13+
14+
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
15+
16+
17+
@pytest.mark.parametrize("input_path, output_path", [
18+
("tests/test_nuget", "tests/result/nuget1"),
19+
("tests/test_nuget2", "tests/result/nuget2")
20+
])
21+
@pytest.mark.ubuntu
22+
def test_ubuntu(input_path, output_path):
23+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
24+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
25+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
26+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
27+
28+
29+
@pytest.mark.parametrize("input_path, output_path", [
30+
(os.path.join("tests", "test_nuget"), os.path.join("tests", "result", "nuget1")),
31+
(os.path.join("tests", "test_nuget2"), os.path.join("tests", "result", "nuget2"))
32+
])
33+
@pytest.mark.windows
34+
def test_windows(input_path, output_path):
35+
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
36+
result = subprocess.run(command, capture_output=True, text=True)
37+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
38+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_pub.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
10+
11+
12+
@pytest.mark.parametrize("input_path, output_path", [
13+
("tests/test_pub", "tests/result/pub"),
14+
("tests/test_exclude -e requirements.txt", "tests/result/exclude")
15+
])
16+
@pytest.mark.ubuntu
17+
def test_ubuntu(input_path, output_path):
18+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
19+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
20+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
21+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
22+
23+
24+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
25+
(os.path.join("tests", "test_pub"), os.path.join("tests", "result", "pub"), ""),
26+
(os.path.join("tests", "test_pub"), os.path.join("tests", "result", "pub"), "-f opossum"),
27+
(os.path.join("tests", "test_exclude") + " -e requirements.txt", os.path.join("tests", "result", "exclude"), "")
28+
])
29+
@pytest.mark.windows
30+
def test_windows(input_path, output_path, extra_args):
31+
command = f"{DIST_PATH} -p {input_path} -o {output_path} {extra_args}"
32+
result = subprocess.run(command, capture_output=True, text=True)
33+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
34+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tests/pytest/package_manager/test_pypi.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import pytest
7+
import subprocess
8+
9+
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
10+
11+
12+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
13+
("tests/test_pypi", "tests/result/pypi", ""),
14+
("tests/test_multi_pypi_npm", "tests/result/multi_pypi_npm", ""),
15+
("tests/test_multi_pypi_npm", "tests/result/multi_pypi_npm", "-f opossum")
16+
])
17+
@pytest.mark.ubuntu
18+
def test_ubuntu(input_path, output_path, extra_args):
19+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
20+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
21+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
22+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
23+
24+
25+
@pytest.mark.parametrize("input_path, output_path", [
26+
(os.path.join("tests", "test_pypi"), os.path.join("tests", "result", "pypi"))
27+
])
28+
@pytest.mark.windows
29+
def test_windows(input_path, output_path):
30+
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
31+
result = subprocess.run(command, capture_output=True, text=True)
32+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
33+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

Diff for: tox.ini

+8-49
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ toxworkdir = {toxinidir}/tests/
99
install_command = pip install {opts} {packages}
1010
setenv =
1111
PYTHONPATH=.
12+
TOX_PATH={toxinidir}
1213
allowlist_externals =
1314
{toxinidir}/dist/cli
1415
{toxinidir}\dist\cli.exe
1516

1617
[pytest]
1718
filterwarnings = ignore::DeprecationWarning
19+
markers =
20+
ubuntu: Test for Ubuntu
21+
windows: Test for Windows
1822

1923
[flake8]
2024
max-line-length = 130
@@ -36,58 +40,13 @@ commands =
3640
deps =
3741
-r{toxinidir}/requirements-dev.txt
3842
commands =
39-
# Test for PEP8
40-
pytest -v --flake8
41-
# Test for Pypi
42-
fosslight_dependency -p tests/test_pypi -o tests/result/pypi
43-
# Test for NPM (without optional command)
44-
fosslight_dependency -p tests/test_npm1 -o tests/result/npm1
45-
# Test for NPM (with optional command)
46-
fosslight_dependency -p tests/test_npm2 -o tests/result/npm2 -m npm
47-
# Test for Maven (without optional command)
48-
fosslight_dependency -p tests/test_maven1/lombok.maven -o tests/result/maven1
49-
# Test for Maven (with optional command)
50-
fosslight_dependency -p tests/test_maven2 -o tests/result/maven2
51-
# Test for Gradle
52-
fosslight_dependency -p tests/test_gradle/jib -o tests/result/gradle
53-
# Test for Gradle2
54-
fosslight_dependency -p tests/test_gradle2 -o tests/result/gradle2
55-
# Test for Pub
56-
fosslight_dependency -p tests/test_pub -o tests/result/pub
57-
# Test for multi package manager (npm, pypi)
58-
fosslight_dependency -p tests/test_multi_pypi_npm -o tests/result/multi_pypi_npm
59-
# Test for opossum result
60-
fosslight_dependency -p tests/test_multi_pypi_npm -o tests/result/multi_pypi_npm -f opossum
61-
# Test for nuget (for packageReference)
62-
fosslight_dependency -p tests/test_nuget -o tests/result/nuget1
63-
# Test for nuget2 (for packages.config)
64-
fosslight_dependency -p tests/test_nuget2 -o tests/result/nuget2
65-
# Test for excluding path (scan only pub, exclude pypi)
66-
fosslight_dependency -p tests/test_exclude -e requirements.txt -o tests/result/exclude
43+
pytest -m "ubuntu"
6744

6845
[testenv:run_windows]
6946
deps =
7047
-r{toxinidir}\requirements-dev.txt
7148
commands =
7249
# Test for making excutable file
73-
pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks --add-binary "src\fosslight_dependency\third_party\askalono\askalono.exe;third_party\askalono" --add-binary "LICENSE;LICENSES" --add-binary "LICENSES\LicenseRef-3rd_party_licenses.txt;LICENSES" --collect-datas fosslight_util --hidden-import=_cffi_backend
74-
# Test for Pypi
75-
{toxinidir}\dist\cli.exe -p tests\test_pypi -o tests\result\pypi
76-
# Test for Maven (with optional command)
77-
{toxinidir}\dist\cli.exe -p tests\test_maven2 -o tests\result\maven2 -m maven
78-
# Test for Gradle
79-
{toxinidir}\dist\cli.exe -p tests\test_gradle\jib -o tests\result\gradle -m gradle
80-
# Test for Gradle2
81-
fosslight_dependency -p tests\test_gradle2 -o tests\result\gradle2 -m gradle
82-
# Test for Pub
83-
{toxinidir}\dist\cli.exe -p tests\test_pub -o tests\result\pub
84-
# Test for Android
85-
{toxinidir}\dist\cli.exe -p tests\test_android\sunflower -o tests\result\android
86-
# Test for opossum result
87-
{toxinidir}\dist\cli.exe -p tests\test_pub -o tests\result\pub -f opossum
88-
# Test for nuget (for packageReference)
89-
{toxinidir}\dist\cli.exe -p tests\test_nuget -o tests\result\nuget1
90-
# Test for nuget2 (for packages.config)
91-
{toxinidir}\dist\cli.exe -p tests\test_nuget2 -o tests\result\nuget2
92-
# Test for excluding path (scan only pub, exclude pypi)
93-
{toxinidir}\dist\cli.exe -p tests\test_exclude -e requirements.txt -o tests\result\exclude
50+
pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks --add-binary=src\fosslight_dependency\third_party\askalono\askalono.exe:third_party\askalono --add-binary=LICENSE:LICENSES --add-binary=LICENSES\LicenseRef-3rd_party_licenses.txt:LICENSES --collect-datas fosslight_util --hidden-import=_cffi_backend
51+
pytest -m "windows"
52+
pytest -v --flake8

0 commit comments

Comments
 (0)