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 --- .github/workflows/test.yml | 23 +++++++++++++++++ tests/test-api.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 tests/test-api.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..8d90f73 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +on: + push: + workflow_dispatch: + +jobs: + test-api: + name: test commit + runs-on: ubuntu-latest + permissions: + id-token: write + contents: write + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.9" + + - run: | + pwd + cd tests + python3 -m test-api.py \ No newline at end of file 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(+) 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 15ed1d1fd6e2f26193d5e27b46bc337bf876ffe8 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:26:58 +0800 Subject: chore(deps): add pytest in group test --- pdm.lock | 18 +++++++++--------- pyproject.toml | 5 +++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pdm.lock b/pdm.lock index b123817..3f90529 100644 --- a/pdm.lock +++ b/pdm.lock @@ -2,10 +2,10 @@ # It is not intended for manual editing. [metadata] -groups = ["default"] +groups = ["default", "test"] strategy = ["cross_platform", "inherit_metadata"] lock_version = "4.4.1" -content_hash = "sha256:fd0a3ff5fbf43c9b6344f7072de1848ea7b263df6a1042f3dbfaefd12df45d92" +content_hash = "sha256:7e92b84c2b52abef8bf2cca785657bbd0686221a6d3174500294332114b6df90" [[package]] name = "certifi" @@ -79,7 +79,7 @@ name = "colorama" version = "0.4.6" requires_python = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" summary = "Cross-platform colored terminal text." -groups = ["default"] +groups = ["default", "test"] marker = "sys_platform == \"win32\"" files = [ {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, @@ -91,7 +91,7 @@ name = "exceptiongroup" version = "1.2.0" requires_python = ">=3.7" summary = "Backport of PEP 654 (exception groups)" -groups = ["default"] +groups = ["default", "test"] marker = "python_version < \"3.11\"" files = [ {file = "exceptiongroup-1.2.0-py3-none-any.whl", hash = "sha256:4bfd3996ac73b41e9b9628b04e079f193850720ea5945fc96a08633c66912f14"}, @@ -114,7 +114,7 @@ name = "iniconfig" version = "2.0.0" requires_python = ">=3.7" summary = "brain-dead simple config-ini parsing" -groups = ["default"] +groups = ["default", "test"] files = [ {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, @@ -154,7 +154,7 @@ name = "packaging" version = "23.2" requires_python = ">=3.7" summary = "Core utilities for Python packages" -groups = ["default"] +groups = ["default", "test"] files = [ {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"}, {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, @@ -165,7 +165,7 @@ name = "pluggy" version = "1.3.0" requires_python = ">=3.8" summary = "plugin and hook calling mechanisms for python" -groups = ["default"] +groups = ["default", "test"] files = [ {file = "pluggy-1.3.0-py3-none-any.whl", hash = "sha256:d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7"}, {file = "pluggy-1.3.0.tar.gz", hash = "sha256:cf61ae8f126ac6f7c451172cf30e3e43d3ca77615509771b3a984a0730651e12"}, @@ -176,7 +176,7 @@ name = "pytest" version = "7.4.4" requires_python = ">=3.7" summary = "pytest: simple powerful testing with Python" -groups = ["default"] +groups = ["default", "test"] dependencies = [ "colorama; sys_platform == \"win32\"", "exceptiongroup>=1.0.0rc8; python_version < \"3.11\"", @@ -223,7 +223,7 @@ name = "tomli" version = "2.0.1" requires_python = ">=3.7" summary = "A lil' TOML parser" -groups = ["default"] +groups = ["default", "test"] marker = "python_version < \"3.11\"" files = [ {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, diff --git a/pyproject.toml b/pyproject.toml index c9e9641..fc41f6c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,3 +25,8 @@ ipm = "ipm.__main__:main" [tool.pdm] package-type = "library" + +[tool.pdm.dev-dependencies] +test = [ + "pytest>=7.4.4", +] -- cgit v1.2.3-70-g09d2 From bf445b19e830acce98d6e57068df43dcee5fbd2d Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:29:09 +0800 Subject: chore(ci): add section --- .github/workflows/test.yml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8d90f73..f1648eb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,14 @@ jobs: with: python-version: "3.9" - - run: | - pwd - cd tests - python3 -m test-api.py \ No newline at end of file + - name: set up pdm + run: pip install pdm + + - name: install test deps + run: | + pdm install -dG test + + - name: test api + run: | + cd test + python -m test-api.py \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 81a4d9fa63c4378e0afee4d9f8d1bef9528736f5 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:30:22 +0800 Subject: fix(ci): test -> tests --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f1648eb..f4c789f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,5 +26,5 @@ jobs: - name: test api run: | - cd test + cd tests python -m test-api.py \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 7d496726ad6b8ebcd7fa0aa6453f6abf5b97669e Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:32:28 +0800 Subject: chore(ci): add pip install --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f4c789f..f816148 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,12 +17,13 @@ jobs: with: python-version: "3.9" - - name: set up pdm + - name: setup pdm run: pip install pdm - name: install test deps run: | pdm install -dG test + pip install pytest - name: test api run: | -- cgit v1.2.3-70-g09d2 From 89029672d27d7150fde35d2df50b316aeee31d89 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:33:54 +0800 Subject: fix(ci): install self --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f816148..07ae015 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,8 @@ jobs: - name: setup pdm run: pip install pdm + + - run: pdm install - name: install test deps run: | -- cgit v1.2.3-70-g09d2 From c91e570d4f28eeb0a352f5f27b9611377b66da1c Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:36:45 +0800 Subject: --- .github/workflows/test.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 07ae015..0a6958f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,9 +25,10 @@ jobs: - name: install test deps run: | pdm install -dG test - pip install pytest + # pip install pytest - name: test api run: | cd tests - python -m test-api.py \ No newline at end of file + pdm venv activate in-project + pdm run test-api.py \ 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(-) 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(-) 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 f9527f433eb2c1dba8c527098efb085688df8a0f Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:46:32 +0800 Subject: refactor(api): Remove unnecessary else after guard condition (remove-unnecessary-else) --- src/ipm/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ipm/api.py b/src/ipm/api.py index 30f7040..3f2f847 100644 --- a/src/ipm/api.py +++ b/src/ipm/api.py @@ -26,7 +26,7 @@ def install(uri: str, index: str = "", echo: bool = False) -> None: info("正在初始化 IPM 环境...", echo) HOME.mkdir(parents=True, exist_ok=True) - index = index if index else INDEX + index = index or INDEX if os.path.isabs(uri): info(f"检定给定的 URI 地址[{uri}]为本地路径.", echo) -- cgit v1.2.3-70-g09d2 From 5ad6f36c39d14c99c6d9f8f0d7db6653bd7d6e76 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 17 Jan 2024 16:46:51 +0800 Subject: refactor(api): Remove unnecessary else after guard condition (remove-unnecessary-else) --- src/ipm/api.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ipm/api.py b/src/ipm/api.py index 3f2f847..faf7e0b 100644 --- a/src/ipm/api.py +++ b/src/ipm/api.py @@ -30,11 +30,10 @@ def install(uri: str, index: str = "", echo: bool = False) -> None: if os.path.isabs(uri): info(f"检定给定的 URI 地址[{uri}]为本地路径.", echo) - if uri.endswith(".ipk"): - info("安装中...", echo) - ipk = extract(Path(uri).resolve(), HOME, echo) - else: + if not uri.endswith(".ipk"): raise FileTypeMismatch("文件类型与预期[.ipk]不匹配.") + info("安装中...", echo) + ipk = extract(Path(uri).resolve(), HOME, echo) elif urlparser.is_valid_url(uri): filename = uri.rstrip("/").split("/")[-1] ipk = loader.load( -- 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 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