diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..c876955 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Android binary analysis script +# SPDX-FileCopyrightText: Copyright 2023 LG Electronics Inc. +# SPDX-License-Identifier: Apache-2.0 + +import os +import subprocess +import pytest + + +@pytest.fixture +def android_src_path(): + return os.getenv("ANDROID_SRC_PATH") + + +@pytest.fixture +def android_build_log(): + return os.getenv("ANDROID_BUILD_LOG") + + +@pytest.fixture +def run_command(): + def _run_command(command): + result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + success = result.returncode == 0 + return success, result.stdout.decode('utf-8'), result.stderr.decode('utf-8') + return _run_command diff --git a/test/test_tox.py b/test/test_tox.py new file mode 100644 index 0000000..52821ab --- /dev/null +++ b/test/test_tox.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Android binary analysis script +# SPDX-FileCopyrightText: Copyright 2023 LG Electronics Inc. +# SPDX-License-Identifier: Apache-2.0 + + +import os +import pytest + + +@pytest.mark.run +def test_fosslight_android(run_command, android_src_path, android_build_log): + + # given + assert android_src_path, "android_src_path is not set." + assert android_build_log, "android_build_log is not set." + + # when + command = f"fosslight_android -s {android_src_path} -a {android_build_log} -m" + success, stdout, stderr = run_command(command) + + # then + assert success is True, f"fosslight_android test_run failed. stderr: {stderr}" + + +@pytest.mark.release +def test_release_environment(run_command): + + # given + test_result, _, _ = run_command("rm -rf test_result") + os.makedirs("test_result", exist_ok=True) + + # when + help_result, _, _ = run_command("fosslight_android -h") + ok_result, _, _ = run_command("fosslight_android -b test/binary.txt -n test/NOTICE.html -c ok") + nok_result, _, _ = run_command("fosslight_android -b test/binary.txt -n test/NOTICE.html -c nok") + divide_result, _, _ = run_command("fosslight_android -d test/needtoadd-notice.html") + + # then + assert help_result is True, "Help command failed" + assert ok_result is True, "OK command failed" + assert nok_result is True, "NOK command failed" + assert divide_result is True, "Divide command failed" diff --git a/tox.ini b/tox.ini index a2b692f..7b02dc1 100644 --- a/tox.ini +++ b/tox.ini @@ -21,10 +21,17 @@ ignore = C901 [pytest] filterwarnings = ignore::DeprecationWarning +markers = + run: Test for local environment + release: Test for CI environment [testenv] install_command = pip install {opts} {packages} +setenv = + ANDROID_SRC_PATH = {[main]android_src_path} + ANDROID_BUILD_LOG = {[main]android_build_log} + [testenv:test_run] changedir = test @@ -32,15 +39,11 @@ setenv = PYTHONPATH=. commands = - fosslight_android -s {[main]android_src_path} -a {[main]android_build_log} -m + pytest -m run [testenv:release] deps = -r{toxinidir}/requirements-dev.txt commands = - pytest -v --flake8 --ignore=script - fosslight_android -h - fosslight_android -b test/binary.txt -n test/NOTICE.html -c ok - fosslight_android -b test/binary.txt -n test/NOTICE.html -c nok - fosslight_android -d test/needtoadd-notice.html + pytest -v --flake8 --ignore=script -m release