aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author简律纯 <i@jyunko.cn>2024-01-17 15:41:11 +0800
committer简律纯 <i@jyunko.cn>2024-01-17 15:41:11 +0800
commita066ba06d05474b054a2c47ddfee09f4a9b3c57f (patch)
treef309aad68775f26991dc702ee2861572c343151f
parent959885e4c13fdd447733046d06916d9061dad402 (diff)
downloadipm-a066ba06d05474b054a2c47ddfee09f4a9b3c57f.tar.gz
ipm-a066ba06d05474b054a2c47ddfee09f4a9b3c57f.zip
feat(ci|test): enhance test and test commit
-rw-r--r--.github/workflows/test.yml23
-rw-r--r--tests/test-api.py62
2 files changed, 85 insertions, 0 deletions
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