From a066ba06d05474b054a2c47ddfee09f4a9b3c57f Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 15:41:11 +0800 Subject: feat(ci|test): enhance test and test commit --- tests/test-api.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test-api.py (limited to 'tests') diff --git a/tests/test-api.py b/tests/test-api.py new file mode 100644 index 0000000..3687632 --- /dev/null +++ b/tests/test-api.py @@ -0,0 +1,62 @@ +import pytest +from ipm.__main__ import main +from unittest.mock import patch, MagicMock + +# Test IDs for parametrization +HAPPY_PATH_INSTALL = "happy_install" +HAPPY_PATH_EXTRACT = "happy_extract" +HAPPY_PATH_BUILD = "happy_build" +EDGE_CASE_NO_ARGS = "edge_no_args" +ERROR_CASE_UNKNOWN_COMMAND = "error_unknown_command" + +# Mock the sys.argv to simulate command line arguments +@pytest.fixture +def mock_sys_argv(monkeypatch): + def _mock_sys_argv(args): + monkeypatch.setattr("sys.argv", ["ipm"] + args) + return _mock_sys_argv + +# Mock the api functions to prevent actual execution +@pytest.fixture +def mock_api_functions(monkeypatch): + install_mock = MagicMock() + extract_mock = MagicMock() + build_mock = MagicMock() + monkeypatch.setattr("ipm.__main__.install", install_mock) + monkeypatch.setattr("ipm.__main__.extract", extract_mock) + monkeypatch.setattr("ipm.__main__.build", build_mock) + return install_mock, extract_mock, build_mock + +@pytest.mark.parametrize("test_id, args, expected_call", [ + # Happy path tests + (HAPPY_PATH_INSTALL, ["install", "http://ipm.hydroroll.team/package.ipm"], ("install", ["http://ipm.hydroroll.team/package.ipm", None])), + (HAPPY_PATH_EXTRACT, ["extract", "package.ipm", "--dist", "dist_folder"], ("extract", ["package.ipm", "dist_folder"])), + (HAPPY_PATH_BUILD, ["build", "source_folder"], ("build", ["source_folder"])), + + # Edge case tests + (EDGE_CASE_NO_ARGS, [], ("help", [])), + + # Error case tests + (ERROR_CASE_UNKNOWN_COMMAND, ["unknown", "arg"], ("error", ["unknown"])), +]) +def test_main_commands(test_id, args, expected_call, mock_sys_argv, mock_api_functions, capsys): + mock_sys_argv(args) + install_mock, extract_mock, build_mock = mock_api_functions + + # Act + with pytest.raises(SystemExit): # argparse exits the program when -h is called or on error + main() + + # Assert + if expected_call[0] == "install": + install_mock.assert_called_once_with(*expected_call[1], echo=True) + elif expected_call[0] == "extract": + extract_mock.assert_called_once_with(*expected_call[1]) + elif expected_call[0] == "build": + build_mock.assert_called_once_with(*expected_call[1]) + elif expected_call[0] == "help": + captured = capsys.readouterr() + assert "Infini 包管理器" in captured.out + elif expected_call[0] == "error": + captured = capsys.readouterr() + assert "error: unrecognized arguments" in captured.err -- cgit v1.2.3-70-g09d2 From bb078f0215891cc825699e14892f1625df3d2312 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:24:57 +0800 Subject: fix(test): add main() --- tests/test-api.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/test-api.py b/tests/test-api.py index 3687632..4332886 100644 --- a/tests/test-api.py +++ b/tests/test-api.py @@ -60,3 +60,5 @@ def test_main_commands(test_id, args, expected_call, mock_sys_argv, mock_api_fun elif expected_call[0] == "error": captured = capsys.readouterr() assert "error: unrecognized arguments" in captured.err + +pytest.main() \ No newline at end of file -- cgit v1.2.3-70-g09d2 From dcb735da6c276e4363796df0266bd3ee145868ac Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:39:43 +0800 Subject: chore(test): update code --- tests/test-api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test-api.py b/tests/test-api.py index 4332886..841aec5 100644 --- a/tests/test-api.py +++ b/tests/test-api.py @@ -29,7 +29,7 @@ def mock_api_functions(monkeypatch): @pytest.mark.parametrize("test_id, args, expected_call", [ # Happy path tests - (HAPPY_PATH_INSTALL, ["install", "http://ipm.hydroroll.team/package.ipm"], ("install", ["http://ipm.hydroroll.team/package.ipm", None])), + (HAPPY_PATH_INSTALL, ["install", "http://ipm.hydroroll.team/package.ipk"], ("install", ["http://ipm.hydroroll.team/package.ipk", None])), (HAPPY_PATH_EXTRACT, ["extract", "package.ipm", "--dist", "dist_folder"], ("extract", ["package.ipm", "dist_folder"])), (HAPPY_PATH_BUILD, ["build", "source_folder"], ("build", ["source_folder"])), -- cgit v1.2.3-70-g09d2 From d60e2193cc829148eba244f241b03b0462b3ba1e Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:41:19 +0800 Subject: chore(test): update code --- tests/test-api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test-api.py b/tests/test-api.py index 841aec5..9afe40b 100644 --- a/tests/test-api.py +++ b/tests/test-api.py @@ -30,7 +30,7 @@ def mock_api_functions(monkeypatch): @pytest.mark.parametrize("test_id, args, expected_call", [ # Happy path tests (HAPPY_PATH_INSTALL, ["install", "http://ipm.hydroroll.team/package.ipk"], ("install", ["http://ipm.hydroroll.team/package.ipk", None])), - (HAPPY_PATH_EXTRACT, ["extract", "package.ipm", "--dist", "dist_folder"], ("extract", ["package.ipm", "dist_folder"])), + (HAPPY_PATH_EXTRACT, ["extract", "package.ipk", "--dist", "dist_folder"], ("extract", ["package.ipk", "dist_folder"])), (HAPPY_PATH_BUILD, ["build", "source_folder"], ("build", ["source_folder"])), # Edge case tests -- cgit v1.2.3-70-g09d2 From 55623b0cb2e0cb49f5ff4f456121b07c2376659c Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 21:01:18 +0800 Subject: Delete tests/test_api.py --- tests/test_api.py | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 tests/test_api.py (limited to 'tests') diff --git a/tests/test_api.py b/tests/test_api.py deleted file mode 100644 index e45bb77..0000000 --- a/tests/test_api.py +++ /dev/null @@ -1,15 +0,0 @@ -from ipm.api import build, extract, install - - -def test_build(): - build("C:\\Users\\fu050\\Desktop\\coc") - - -def test_extract(): - build("C:\\Users\\fu050\\Desktop\\coc") - extract("C:\\Users\\fu050\\Desktop\\coc\\dist\\coc-0.1.0-alpha.1.ipk") - - -def test_install(): - build("C:\\Users\\fu050\\Desktop\\coc") - install("C:\\Users\\fu050\\Desktop\\coc\\dist\\coc-0.1.0-alpha.1.ipk") -- cgit v1.2.3-70-g09d2