diff --git a/requirements-dev.txt b/requirements-dev.txt index 3ab0fa69..49006f4a 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,5 @@ flake8==3.9.2 -pyinstaller +pyinstaller>=6.10.0 tox>=4.18.1 pytest pytest-cov diff --git a/requirements.txt b/requirements.txt index 8b6fec93..a29061c1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,4 @@ requirements-parser defusedxml packageurl-python igraph -matplotlib \ No newline at end of file +matplotlib diff --git a/tests/pytest/conftest.py b/tests/pytest/conftest.py new file mode 100644 index 00000000..0638be86 --- /dev/null +++ b/tests/pytest/conftest.py @@ -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) diff --git a/tests/pytest/package_manager/__init__.py b/tests/pytest/package_manager/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/pytest/package_manager/test_android.py b/tests/pytest/package_manager/test_android.py new file mode 100644 index 00000000..52cd5474 --- /dev/null +++ b/tests/pytest/package_manager/test_android.py @@ -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}" diff --git a/tests/pytest/package_manager/test_cocoapods.py b/tests/pytest/package_manager/test_cocoapods.py new file mode 100644 index 00000000..4e518758 --- /dev/null +++ b/tests/pytest/package_manager/test_cocoapods.py @@ -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}" diff --git a/tests/pytest/package_manager/test_gradle.py b/tests/pytest/package_manager/test_gradle.py new file mode 100644 index 00000000..35e16c11 --- /dev/null +++ b/tests/pytest/package_manager/test_gradle.py @@ -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}" diff --git a/tests/pytest/package_manager/test_helm.py b/tests/pytest/package_manager/test_helm.py new file mode 100644 index 00000000..1a4fd0f7 --- /dev/null +++ b/tests/pytest/package_manager/test_helm.py @@ -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}" diff --git a/tests/pytest/package_manager/test_maven.py b/tests/pytest/package_manager/test_maven.py new file mode 100644 index 00000000..58f26ab3 --- /dev/null +++ b/tests/pytest/package_manager/test_maven.py @@ -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}" diff --git a/tests/pytest/package_manager/test_npm.py b/tests/pytest/package_manager/test_npm.py new file mode 100644 index 00000000..b9914001 --- /dev/null +++ b/tests/pytest/package_manager/test_npm.py @@ -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}" diff --git a/tests/pytest/package_manager/test_nuget.py b/tests/pytest/package_manager/test_nuget.py new file mode 100644 index 00000000..c46f3628 --- /dev/null +++ b/tests/pytest/package_manager/test_nuget.py @@ -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}" diff --git a/tests/pytest/package_manager/test_pub.py b/tests/pytest/package_manager/test_pub.py new file mode 100644 index 00000000..a4a3e900 --- /dev/null +++ b/tests/pytest/package_manager/test_pub.py @@ -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}" diff --git a/tests/pytest/package_manager/test_pypi.py b/tests/pytest/package_manager/test_pypi.py new file mode 100644 index 00000000..2b63fe82 --- /dev/null +++ b/tests/pytest/package_manager/test_pypi.py @@ -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}" diff --git a/tox.ini b/tox.ini index 06a05216..75361b8e 100644 --- a/tox.ini +++ b/tox.ini @@ -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 @@ -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