From 164ff68507a9ad1974c5e7705b85b11b9f1be8a4 Mon Sep 17 00:00:00 2001 From: svtter Date: Fri, 15 Nov 2024 17:32:41 +0800 Subject: [PATCH 1/3] fix checker function --- pdm.lock | 99 ++++++++++++++++++++++++++++++++++++++ pyproject.toml | 4 ++ src/easybuilder/checker.py | 8 +-- tests/test_checker.py | 25 ++++++++++ 4 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 pdm.lock create mode 100644 tests/test_checker.py diff --git a/pdm.lock b/pdm.lock new file mode 100644 index 0000000..6128797 --- /dev/null +++ b/pdm.lock @@ -0,0 +1,99 @@ +# This file is @generated by PDM. +# It is not intended for manual editing. + +[metadata] +groups = ["default", "dev"] +strategy = ["inherit_metadata"] +lock_version = "4.5.0" +content_hash = "sha256:1b330fcac28c2ae2aa2241fb0e4793ef3dd8da5c21aaeb65748e977b856cec79" + +[[metadata.targets]] +requires_python = ">=3.9" + +[[package]] +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 = ["dev"] +marker = "sys_platform == \"win32\"" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "exceptiongroup" +version = "1.2.2" +requires_python = ">=3.7" +summary = "Backport of PEP 654 (exception groups)" +groups = ["dev"] +marker = "python_version < \"3.11\"" +files = [ + {file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"}, + {file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"}, +] + +[[package]] +name = "iniconfig" +version = "2.0.0" +requires_python = ">=3.7" +summary = "brain-dead simple config-ini parsing" +groups = ["dev"] +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + +[[package]] +name = "packaging" +version = "24.2" +requires_python = ">=3.8" +summary = "Core utilities for Python packages" +groups = ["dev"] +files = [ + {file = "packaging-24.2-py3-none-any.whl", hash = "sha256:09abb1bccd265c01f4a3aa3f7a7db064b36514d2cba19a2f694fe6150451a759"}, + {file = "packaging-24.2.tar.gz", hash = "sha256:c228a6dc5e932d346bc5739379109d49e8853dd8223571c7c5b55260edc0b97f"}, +] + +[[package]] +name = "pluggy" +version = "1.5.0" +requires_python = ">=3.8" +summary = "plugin and hook calling mechanisms for python" +groups = ["dev"] +files = [ + {file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"}, + {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"}, +] + +[[package]] +name = "pytest" +version = "8.3.3" +requires_python = ">=3.8" +summary = "pytest: simple powerful testing with Python" +groups = ["dev"] +dependencies = [ + "colorama; sys_platform == \"win32\"", + "exceptiongroup>=1.0.0rc8; python_version < \"3.11\"", + "iniconfig", + "packaging", + "pluggy<2,>=1.5", + "tomli>=1; python_version < \"3.11\"", +] +files = [ + {file = "pytest-8.3.3-py3-none-any.whl", hash = "sha256:a6853c7375b2663155079443d2e45de913a911a11d669df02a50814944db57b2"}, + {file = "pytest-8.3.3.tar.gz", hash = "sha256:70b98107bd648308a7952b06e6ca9a50bc660be218d53c257cc1fc94fda10181"}, +] + +[[package]] +name = "tomli" +version = "2.1.0" +requires_python = ">=3.8" +summary = "A lil' TOML parser" +groups = ["dev"] +marker = "python_version < \"3.11\"" +files = [ + {file = "tomli-2.1.0-py3-none-any.whl", hash = "sha256:a5c57c3d1c56f5ccdf89f6523458f60ef716e210fc47c4cfb188c5ba473e0391"}, + {file = "tomli-2.1.0.tar.gz", hash = "sha256:3f646cae2aec94e17d04973e4249548320197cfabdf130015d023de4b74d8ab8"}, +] diff --git a/pyproject.toml b/pyproject.toml index 4323dd9..6bf731e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,3 +18,7 @@ distribution = true requires = ["pdm-backend"] build-backend = "pdm.backend" +[dependency-groups] +dev = [ + "pytest>=8.3.3", +] diff --git a/src/easybuilder/checker.py b/src/easybuilder/checker.py index 590d5e9..31a215d 100644 --- a/src/easybuilder/checker.py +++ b/src/easybuilder/checker.py @@ -3,11 +3,13 @@ def check_clean(): # 检查git仓库是否有未提交的更改 - result = os.system("git status --porcelain") - if result != 0: - print("错误: 无法获取git状态") + result = os.popen("git status --porcelain").read() + if not os.path.exists(".git"): + print("错误: 当前目录不是git仓库") exit(1) if result: print("错误: git仓库不干净,请先提交所有更改") + print("未提交的更改:") + print(result) exit(1) print("git仓库状态检查通过") diff --git a/tests/test_checker.py b/tests/test_checker.py new file mode 100644 index 0000000..a384144 --- /dev/null +++ b/tests/test_checker.py @@ -0,0 +1,25 @@ +import os +import random + +import pytest + +from easybuilder.checker import check_clean + + +@pytest.fixture +def create_tmp(): + random_num = random.randint(0, 100) + if random_num > 50: + with open("tmp.txt", "w") as f: + f.write("test") + yield True + os.remove("tmp.txt") + yield False + + +def test_check_clean(create_tmp): + if create_tmp: + with pytest.raises(SystemExit): + check_clean() + else: + check_clean() From 951dc0fdb33aeba18a50094ab4911fec7591ae5b Mon Sep 17 00:00:00 2001 From: svtter Date: Fri, 15 Nov 2024 17:42:14 +0800 Subject: [PATCH 2/3] Fix checker problem, and add test for checker. Feat. --- src/easybuilder/checker.py | 3 ++- src/easybuilder/temputils.py | 14 +++++++++++++ tests/test_checker.py | 40 +++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 src/easybuilder/temputils.py diff --git a/src/easybuilder/checker.py b/src/easybuilder/checker.py index 31a215d..9e29cdf 100644 --- a/src/easybuilder/checker.py +++ b/src/easybuilder/checker.py @@ -1,8 +1,9 @@ import os -def check_clean(): +def check_clean(folder: str = "."): # 检查git仓库是否有未提交的更改 + os.chdir(folder) result = os.popen("git status --porcelain").read() if not os.path.exists(".git"): print("错误: 当前目录不是git仓库") diff --git a/src/easybuilder/temputils.py b/src/easybuilder/temputils.py new file mode 100644 index 0000000..0cbbc0f --- /dev/null +++ b/src/easybuilder/temputils.py @@ -0,0 +1,14 @@ +import shutil +import tempfile + + +class TempFolder: + def __init__(self): + self.folder = tempfile.mkdtemp() + + def __enter__(self): + return self.folder + + def __exit__(self, exc_type, exc_value, traceback): + # shutil.rmtree(self.folder) + pass diff --git a/tests/test_checker.py b/tests/test_checker.py index a384144..90569df 100644 --- a/tests/test_checker.py +++ b/tests/test_checker.py @@ -1,25 +1,37 @@ import os -import random +import time import pytest from easybuilder.checker import check_clean +from easybuilder.temputils import TempFolder @pytest.fixture -def create_tmp(): - random_num = random.randint(0, 100) - if random_num > 50: - with open("tmp.txt", "w") as f: +def create_dirty_temp_repo(): + with TempFolder() as tmp_folder: + os.chdir(tmp_folder) + os.system("git init") + with open("README.md", "w") as f: f.write("test") - yield True - os.remove("tmp.txt") - yield False + # not clean + yield tmp_folder -def test_check_clean(create_tmp): - if create_tmp: - with pytest.raises(SystemExit): - check_clean() - else: - check_clean() + +@pytest.fixture +def create_clean_temp_repo(): + with TempFolder() as tmp_folder: + os.chdir(tmp_folder) + os.system("git init") + yield tmp_folder + + +def test_no_git(create_dirty_temp_repo): + # we should create a new directory and check it + with pytest.raises(SystemExit): + check_clean(create_dirty_temp_repo) + + +def test_clean(create_clean_temp_repo): + check_clean(create_clean_temp_repo) From 1f1374bc80dd6d31301eadc8bfcef408ad05340e Mon Sep 17 00:00:00 2001 From: svtter Date: Fri, 15 Nov 2024 17:45:39 +0800 Subject: [PATCH 3/3] install itself --- .github/workflows/python-app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 1168bd9..dcd662b 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -26,7 +26,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install flake8 pytest + pip install flake8 pytest . if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with flake8 run: |