Skip to content

Commit

Permalink
Refactor existing tox test to pytest (#225)
Browse files Browse the repository at this point in the history
Signed-off-by: yongjunhong <[email protected]>
  • Loading branch information
YongGoose authored Oct 3, 2024
1 parent f703a48 commit 756d120
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 51 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
flake8==3.9.2
pyinstaller
pyinstaller>=6.10.0
tox>=4.18.1
pytest
pytest-cov
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ requirements-parser
defusedxml
packageurl-python
igraph
matplotlib
matplotlib
40 changes: 40 additions & 0 deletions tests/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import shutil
import pytest

set_up_directories = [
"tests/result/android",
"tests/result/cocoapods",
"tests/result/exclude",
"tests/result/gradle",
"tests/result/gradle2",
"tests/result/helm",
"tests/result/maven1",
"tests/result/maven2",
"tests/result/multi_pypi_npm",
"tests/result/npm1",
"tests/result/npm2",
"tests/result/nuget1",
"tests/result/nuget2",
"tests/result/pub",
"tests/result/pypi"
]

remove_directories = set_up_directories


@pytest.fixture(scope="session", autouse=True)
def setup_test_result_dir_and_teardown():
print("==============setup==============")
for directory in set_up_directories:
os.makedirs(directory, exist_ok=True)

yield

print("==============tearDown==============")
for directory in remove_directories:
shutil.rmtree(directory)
Empty file.
31 changes: 31 additions & 0 deletions tests/pytest/package_manager/test_android.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess

DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")


@pytest.mark.parametrize("input_path, output_path, extra_args", [
("tests/test_android", "tests/result/android", "-m android")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path, extra_args):
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"


@pytest.mark.parametrize("input_path, output_path", [
(os.path.join("tests", "test_android", "sunflower"), os.path.join("tests", "result", "android"))
])
@pytest.mark.windows
def test_windows(input_path, output_path):
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
result = subprocess.run(command, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
18 changes: 18 additions & 0 deletions tests/pytest/package_manager/test_cocoapods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess


@pytest.mark.parametrize("input_path, output_path, extra_args", [
("tests/test_cocoapods", "tests/result/cocoapods", "-m cocoapods")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path, extra_args):
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
33 changes: 33 additions & 0 deletions tests/pytest/package_manager/test_gradle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess

DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")


@pytest.mark.parametrize("input_path, output_path", [
("tests/test_gradle/jib", "tests/result/gradle"),
("tests/test_gradle2", "tests/result/gradle2")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path):
command = f"fosslight_dependency -p {input_path} -o {output_path}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"


@pytest.mark.parametrize("input_path, output_path", [
(os.path.join("tests", "test_gradle", "jib"), os.path.join("tests", "result", "gradle")),
(os.path.join("tests", "test_gradle2"), os.path.join("tests", "result", "gradle2"))
])
@pytest.mark.windows
def test_windows(input_path, output_path):
command = f"{DIST_PATH} -p {input_path} -o {output_path} -m gradle"
result = subprocess.run(command, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
18 changes: 18 additions & 0 deletions tests/pytest/package_manager/test_helm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess


@pytest.mark.parametrize("input_path, output_path, extra_args", [
("tests/test_helm", "tests/result/helm", "-m helm")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path, extra_args):
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
32 changes: 32 additions & 0 deletions tests/pytest/package_manager/test_maven.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess

DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")


@pytest.mark.parametrize("input_path, output_path", [
("tests/test_maven1/lombok.maven", "tests/result/maven1"),
("tests/test_maven2", "tests/result/maven2")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path):
command = f"fosslight_dependency -p {input_path} -o {output_path}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"


@pytest.mark.parametrize("input_path, output_path", [
(os.path.join("tests", "test_maven2"), os.path.join("tests", "result", "maven2"))
])
@pytest.mark.windows
def test_windows(input_path, output_path):
command = f"{DIST_PATH} -p {input_path} -o {output_path} -m maven"
result = subprocess.run(command, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
19 changes: 19 additions & 0 deletions tests/pytest/package_manager/test_npm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess


@pytest.mark.parametrize("input_path, output_path, extra_args", [
("tests/test_npm1", "tests/result/npm1", ""),
("tests/test_npm2", "tests/result/npm2", "-m npm")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path, extra_args):
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
38 changes: 38 additions & 0 deletions tests/pytest/package_manager/test_nuget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess

UBUNTU_COMMANDS = [
"fosslight_dependency -p tests/test_nuget -o tests/result/nuget1",
"fosslight_dependency -p tests/test_nuget2 -o tests/result/nuget2"
]

DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")


@pytest.mark.parametrize("input_path, output_path", [
("tests/test_nuget", "tests/result/nuget1"),
("tests/test_nuget2", "tests/result/nuget2")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path):
command = f"fosslight_dependency -p {input_path} -o {output_path}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"


@pytest.mark.parametrize("input_path, output_path", [
(os.path.join("tests", "test_nuget"), os.path.join("tests", "result", "nuget1")),
(os.path.join("tests", "test_nuget2"), os.path.join("tests", "result", "nuget2"))
])
@pytest.mark.windows
def test_windows(input_path, output_path):
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
result = subprocess.run(command, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
34 changes: 34 additions & 0 deletions tests/pytest/package_manager/test_pub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess

DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")


@pytest.mark.parametrize("input_path, output_path", [
("tests/test_pub", "tests/result/pub"),
("tests/test_exclude -e requirements.txt", "tests/result/exclude")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path):
command = f"fosslight_dependency -p {input_path} -o {output_path}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"


@pytest.mark.parametrize("input_path, output_path, extra_args", [
(os.path.join("tests", "test_pub"), os.path.join("tests", "result", "pub"), ""),
(os.path.join("tests", "test_pub"), os.path.join("tests", "result", "pub"), "-f opossum"),
(os.path.join("tests", "test_exclude") + " -e requirements.txt", os.path.join("tests", "result", "exclude"), "")
])
@pytest.mark.windows
def test_windows(input_path, output_path, extra_args):
command = f"{DIST_PATH} -p {input_path} -o {output_path} {extra_args}"
result = subprocess.run(command, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
33 changes: 33 additions & 0 deletions tests/pytest/package_manager/test_pypi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
import subprocess

DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")


@pytest.mark.parametrize("input_path, output_path, extra_args", [
("tests/test_pypi", "tests/result/pypi", ""),
("tests/test_multi_pypi_npm", "tests/result/multi_pypi_npm", ""),
("tests/test_multi_pypi_npm", "tests/result/multi_pypi_npm", "-f opossum")
])
@pytest.mark.ubuntu
def test_ubuntu(input_path, output_path, extra_args):
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"


@pytest.mark.parametrize("input_path, output_path", [
(os.path.join("tests", "test_pypi"), os.path.join("tests", "result", "pypi"))
])
@pytest.mark.windows
def test_windows(input_path, output_path):
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
result = subprocess.run(command, capture_output=True, text=True)
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
57 changes: 8 additions & 49 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ toxworkdir = {toxinidir}/tests/
install_command = pip install {opts} {packages}
setenv =
PYTHONPATH=.
TOX_PATH={toxinidir}
allowlist_externals =
{toxinidir}/dist/cli
{toxinidir}\dist\cli.exe

[pytest]
filterwarnings = ignore::DeprecationWarning
markers =
ubuntu: Test for Ubuntu
windows: Test for Windows

[flake8]
max-line-length = 130
Expand All @@ -36,58 +40,13 @@ commands =
deps =
-r{toxinidir}/requirements-dev.txt
commands =
# Test for PEP8
pytest -v --flake8
# Test for Pypi
fosslight_dependency -p tests/test_pypi -o tests/result/pypi
# Test for NPM (without optional command)
fosslight_dependency -p tests/test_npm1 -o tests/result/npm1
# Test for NPM (with optional command)
fosslight_dependency -p tests/test_npm2 -o tests/result/npm2 -m npm
# Test for Maven (without optional command)
fosslight_dependency -p tests/test_maven1/lombok.maven -o tests/result/maven1
# Test for Maven (with optional command)
fosslight_dependency -p tests/test_maven2 -o tests/result/maven2
# Test for Gradle
fosslight_dependency -p tests/test_gradle/jib -o tests/result/gradle
# Test for Gradle2
fosslight_dependency -p tests/test_gradle2 -o tests/result/gradle2
# Test for Pub
fosslight_dependency -p tests/test_pub -o tests/result/pub
# Test for multi package manager (npm, pypi)
fosslight_dependency -p tests/test_multi_pypi_npm -o tests/result/multi_pypi_npm
# Test for opossum result
fosslight_dependency -p tests/test_multi_pypi_npm -o tests/result/multi_pypi_npm -f opossum
# Test for nuget (for packageReference)
fosslight_dependency -p tests/test_nuget -o tests/result/nuget1
# Test for nuget2 (for packages.config)
fosslight_dependency -p tests/test_nuget2 -o tests/result/nuget2
# Test for excluding path (scan only pub, exclude pypi)
fosslight_dependency -p tests/test_exclude -e requirements.txt -o tests/result/exclude
pytest -m "ubuntu"

[testenv:run_windows]
deps =
-r{toxinidir}\requirements-dev.txt
commands =
# Test for making excutable file
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
# Test for Pypi
{toxinidir}\dist\cli.exe -p tests\test_pypi -o tests\result\pypi
# Test for Maven (with optional command)
{toxinidir}\dist\cli.exe -p tests\test_maven2 -o tests\result\maven2 -m maven
# Test for Gradle
{toxinidir}\dist\cli.exe -p tests\test_gradle\jib -o tests\result\gradle -m gradle
# Test for Gradle2
fosslight_dependency -p tests\test_gradle2 -o tests\result\gradle2 -m gradle
# Test for Pub
{toxinidir}\dist\cli.exe -p tests\test_pub -o tests\result\pub
# Test for Android
{toxinidir}\dist\cli.exe -p tests\test_android\sunflower -o tests\result\android
# Test for opossum result
{toxinidir}\dist\cli.exe -p tests\test_pub -o tests\result\pub -f opossum
# Test for nuget (for packageReference)
{toxinidir}\dist\cli.exe -p tests\test_nuget -o tests\result\nuget1
# Test for nuget2 (for packages.config)
{toxinidir}\dist\cli.exe -p tests\test_nuget2 -o tests\result\nuget2
# Test for excluding path (scan only pub, exclude pypi)
{toxinidir}\dist\cli.exe -p tests\test_exclude -e requirements.txt -o tests\result\exclude
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
pytest -m "windows"
pytest -v --flake8

0 comments on commit 756d120

Please sign in to comment.