From e82cfb122fb36d23cdb10396a5c5bb5c9fbfc5f3 Mon Sep 17 00:00:00 2001 From: "Hendry, Adam" Date: Mon, 17 Oct 2022 08:29:19 -0700 Subject: [PATCH 01/21] fix(pythonpackage.yml): use `bash` Specify `shell` as `bash` in `Run tests and linters` step. Fixes: Issue #604 --- .github/workflows/pythonpackage.yml | 51 +++++++++++++++-------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index 90ccbaaa30..aa87888b16 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -10,28 +10,29 @@ jobs: platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install -U pip poetry - poetry --version - poetry install - - name: Run tests and linters - run: | - git config --global user.email "action@github.com" - git config --global user.name "GitHub Action" - ./scripts/test - - name: Upload coverage to Codecov - if: runner.os == 'Linux' - uses: codecov/codecov-action@v1.0.3 - with: - token: ${{secrets.CODECOV_TOKEN}} - file: ./coverage.xml - flags: unittests - name: codecov-umbrella + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install -U pip poetry + poetry --version + poetry install + - name: Run tests and linters + run: | + git config --global user.email "action@github.com" + git config --global user.name "GitHub Action" + ./scripts/test + shell: bash + - name: Upload coverage to Codecov + if: runner.os == 'Linux' + uses: codecov/codecov-action@v1.0.3 + with: + token: ${{secrets.CODECOV_TOKEN}} + file: ./coverage.xml + flags: unittests + name: codecov-umbrella From 906cf00b2785af0c6f4e39f98bfdc7d82e99da4d Mon Sep 17 00:00:00 2001 From: "Hendry, Adam" Date: Wed, 19 Oct 2022 11:38:33 -0700 Subject: [PATCH 02/21] ci: rerun workflow From d858b96411e8a8a60bda843689e7535d4002e88a Mon Sep 17 00:00:00 2001 From: "Hendry, Adam" Date: Wed, 19 Oct 2022 11:44:44 -0700 Subject: [PATCH 03/21] fix(pythonpackage.yml): undo indent reformatting --- .github/workflows/pythonpackage.yml | 52 ++++++++++++++--------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index aa87888b16..d377023813 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -10,29 +10,29 @@ jobs: platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install -U pip poetry - poetry --version - poetry install - - name: Run tests and linters - run: | - git config --global user.email "action@github.com" - git config --global user.name "GitHub Action" - ./scripts/test - shell: bash - - name: Upload coverage to Codecov - if: runner.os == 'Linux' - uses: codecov/codecov-action@v1.0.3 - with: - token: ${{secrets.CODECOV_TOKEN}} - file: ./coverage.xml - flags: unittests - name: codecov-umbrella + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install -U pip poetry + poetry --version + poetry install + - name: Run tests and linters + run: | + git config --global user.email "action@github.com" + git config --global user.name "GitHub Action" + ./scripts/test + shell: bash + - name: Upload coverage to Codecov + if: runner.os == 'Linux' + uses: codecov/codecov-action@v1.0.3 + with: + token: ${{secrets.CODECOV_TOKEN}} + file: ./coverage.xml + flags: unittests + name: codecov-umbrella From c3a446b8801ecb4c5695907bfb55a99b6b22c681 Mon Sep 17 00:00:00 2001 From: "Hendry, Adam" Date: Wed, 19 Oct 2022 12:20:33 -0700 Subject: [PATCH 04/21] fix(scripts): use portable shebang Switch `#!/bin/sh` to `#!/usr/bin/env sh` so scripts work on Windows as well as Linux and MacOS. --- scripts/format | 2 +- scripts/test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/format b/scripts/format index 47b2b90f44..37586c644e 100755 --- a/scripts/format +++ b/scripts/format @@ -1,4 +1,4 @@ -#!/bin/sh -e +#!/usr/bin/env sh -e export PREFIX="poetry run python -m " if [ -d 'venv' ] ; then diff --git a/scripts/test b/scripts/test index 11131cd209..4613b5c16e 100755 --- a/scripts/test +++ b/scripts/test @@ -1,4 +1,4 @@ -#!/bin/sh -e +#!/usr/bin/env sh -e export PREFIX="poetry run python -m " if [ -d 'venv' ] ; then From 8557a3be80040f9f1c37449f434307df5704fa3c Mon Sep 17 00:00:00 2001 From: "Hendry, Adam" Date: Wed, 19 Oct 2022 12:48:50 -0700 Subject: [PATCH 05/21] fix(scripts): use cross-platform POSIX - Change shebang from `#!/bin/sh` to `#!/usr/bin/env sh` - Set `$PREFIX` depending on OS --- scripts/format | 16 ++++++++++++++-- scripts/test | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/scripts/format b/scripts/format index 37586c644e..b9826cbfd3 100755 --- a/scripts/format +++ b/scripts/format @@ -1,8 +1,20 @@ -#!/usr/bin/env sh -e +#!/usr/bin/env sh +set -e export PREFIX="poetry run python -m " + if [ -d 'venv' ] ; then - export PREFIX="venv/bin/" + case $(uname -s) in + Linux|FreeBSD|OpenBSD|NetBSD|Darwin) + export PREFIX="venv/bin/" + ;; + *MINGW*) + export PREFIX="venv/Scripts/" + ;; + *) + exit 1 + ;; + esac fi set -x diff --git a/scripts/test b/scripts/test index 4613b5c16e..1053854cce 100755 --- a/scripts/test +++ b/scripts/test @@ -1,8 +1,20 @@ -#!/usr/bin/env sh -e +#!/usr/bin/env sh +set -e export PREFIX="poetry run python -m " + if [ -d 'venv' ] ; then - export PREFIX="venv/bin/" + case $(uname -s) in + Linux|FreeBSD|OpenBSD|NetBSD|Darwin) + export PREFIX="venv/bin/" + ;; + *MINGW*) + export PREFIX="venv/Scripts/" + ;; + *) + exit 1 + ;; + esac fi ${PREFIX}pytest -n 3 --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen tests/ From 8822bf32b378417dc4f834c1265b12fc48eb304b Mon Sep 17 00:00:00 2001 From: "Hendry, Adam" Date: Wed, 19 Oct 2022 11:38:33 -0700 Subject: [PATCH 06/21] ci: rerun workflow From 8176121782d931708bf126d015e628415058f14c Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Wed, 19 Oct 2022 16:25:01 -0700 Subject: [PATCH 07/21] test(conf.py): don't set `GNUPGHOME` on Windows --- tests/conftest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 64a5652f1a..26c0904c6d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,7 +42,8 @@ def tmp_commitizen_project_with_gpg(tmp_commitizen_project): # create a temporary GPGHOME to store a temporary keyring. # Home path must be less than 104 characters gpg_home = tempfile.TemporaryDirectory(suffix="_cz") - os.environ["GNUPGHOME"] = gpg_home.name # tempdir = temp keyring + # os.environ["GNUPGHOME"] = gpg_home.name # tempdir = temp keyring + # os.environ["HOMEDIR"] = gpg_home.name # create a key (a keyring will be generated within GPUPGHOME) c = cmd.run( From 6d59a5b8ec4260fa2c7056bf0f4248faae0f68c6 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Wed, 19 Oct 2022 16:42:42 -0700 Subject: [PATCH 08/21] test(conf.py): remove `os.linesep` This should be used when parsing binary files, not text streams. The regular expression fails on Windows because the stream contains `\n`, but `os.linesep` is `\r\n`. For reference, see https://stackoverflow.com/questions/38074811/what-is-os-linesep-for --- tests/conftest.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 26c0904c6d..bb18531699 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -28,7 +28,7 @@ def tmp_commitizen_project(tmp_git_project): def _get_gpg_keyid(signer_mail): _new_key = cmd.run(f"gpg --list-secret-keys {signer_mail}") _m = re.search( - rf"[a-zA-Z0-9 \[\]-_]*{os.linesep}[ ]*([0-9A-Za-z]*){os.linesep}[{os.linesep}a-zA-Z0-9 \[\]-_<>@]*", + r"[a-zA-Z0-9 \[\]-_]*\n[ ]*([0-9A-Za-z]*)\n[\na-zA-Z0-9 \[\]-_<>@]*", _new_key.out, ) return _m.group(1) if _m else None @@ -42,8 +42,8 @@ def tmp_commitizen_project_with_gpg(tmp_commitizen_project): # create a temporary GPGHOME to store a temporary keyring. # Home path must be less than 104 characters gpg_home = tempfile.TemporaryDirectory(suffix="_cz") - # os.environ["GNUPGHOME"] = gpg_home.name # tempdir = temp keyring - # os.environ["HOMEDIR"] = gpg_home.name + if os.name != "nt": + os.environ["GNUPGHOME"] = gpg_home.name # tempdir = temp keyring # create a key (a keyring will be generated within GPUPGHOME) c = cmd.run( From 7cd675fa23fc23022e642bab7ccbae32de4e675a Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Wed, 19 Oct 2022 16:46:58 -0700 Subject: [PATCH 09/21] test(test_init_command.py): use `os.sep` Use `os.sep` for file paths. On Windows, this is `\`. On Linux and MacOS, this is `/`. --- tests/commands/test_init_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commands/test_init_command.py b/tests/commands/test_init_command.py index 131aaaad44..6fa8d5008a 100644 --- a/tests/commands/test_init_command.py +++ b/tests/commands/test_init_command.py @@ -65,7 +65,7 @@ def test_init_without_setup_pre_commit_hook(tmpdir, mocker, config): def test_init_when_config_already_exists(config, capsys): # Set config path - path = "tests/pyproject.toml" + path = os.sep.join(["tests", "pyproject.toml"]) config.add_path(path) commands.Init(config)() From dc071206a56a35eaf2aeb65364008255990a63df Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Wed, 19 Oct 2022 17:22:55 -0700 Subject: [PATCH 10/21] test(bump): handle Windows path separator Windows uses `\` for path separation, causing python to see the `\U` in `C:\Users` as a Unicode string. Replace `\\` with `/` in file path strings. Also, the colon in drive paths on Windows causes the `partition` logic in `bump.py` and `commands/bump.py` to fail. Perform the logic on the path without the drive portion. --- commitizen/bump.py | 5 ++++- commitizen/commands/bump.py | 9 ++++++++- tests/test_bump_update_version_in_files.py | 6 +++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/commitizen/bump.py b/commitizen/bump.py index f571e6a04b..7b621a6e1c 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -1,3 +1,4 @@ +import os import re from collections import OrderedDict from itertools import zip_longest @@ -155,7 +156,9 @@ def update_version_in_files( """ # TODO: separate check step and write step for location in files: - filepath, _, regex = location.partition(":") + drive, tail = os.path.splitdrive(location) + path, _, regex = tail.partition(":") + filepath = drive + path if not regex: regex = _version_to_regex(current_version) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index bc43032036..06dc908dcc 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -1,3 +1,4 @@ +import os from logging import getLogger from typing import List, Optional @@ -230,9 +231,15 @@ def __call__(self): # noqa: C901 }, ) changelog_cmd() + file_names = [] + for file_name in version_files: + drive, tail = os.path.splitdrive(file_name) + path, _, regex = tail.partition(":") + path = drive + path if path != "" else drive + regex + file_names.append(path) git_add_changelog_and_version_files_command = ( f"git add {changelog_cmd.file_name} " - f"{' '.join(file_name.partition(':')[0] for file_name in version_files)}" + f"{' '.join(name for name in file_names)}" ) c = cmd.run(git_add_changelog_and_version_files_command) diff --git a/tests/test_bump_update_version_in_files.py b/tests/test_bump_update_version_in_files.py index 3113638003..0c37bf538c 100644 --- a/tests/test_bump_update_version_in_files.py +++ b/tests/test_bump_update_version_in_files.py @@ -15,9 +15,9 @@ def _copy_sample_file_to_tmpdir( tmpdir: LocalPath, source_filename: str, dest_filename: str ) -> str: - tmp_file = tmpdir.join(dest_filename) - copyfile(f"{TESTING_FILE_PREFIX}/{source_filename}", str(tmp_file)) - return str(tmp_file) + tmp_file = str(tmpdir.join(dest_filename)).replace("\\", "/") + copyfile(f"{TESTING_FILE_PREFIX}/{source_filename}", tmp_file) + return tmp_file @pytest.fixture(scope="function") From 500cf71c88cbc1e65502cea89c6cff2e0ab8ba4f Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Wed, 19 Oct 2022 19:36:13 -0700 Subject: [PATCH 11/21] test(cli): skip argcomplete activation on Windows `argcomplete` does not support Git Bash on Windows out of the box. For details, see https://kislyuk.github.io/argcomplete/#git-bash-support. --- tests/test_cli.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 383d7a18cf..54a2dcd775 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,4 @@ +import os import subprocess import sys @@ -84,6 +85,10 @@ def test_commitizen_debug_excepthook(capsys): assert "NotAGitProjectError" in str(excinfo.traceback[0]) +@pytest.mark.skipif( + os.name == "nt", + reason="`argcomplete` does not support Git Bash on Windows.", +) def test_argcomplete_activation(): """ This function is testing the one-time activation of argcomplete for @@ -92,7 +97,7 @@ def test_argcomplete_activation(): Equivalent to run: $ eval "$(register-python-argcomplete pytest)" """ - output = subprocess.run(["register-python-argcomplete", "cz"]) + output = subprocess.run(["py", "-m", "register-python-argcomplete", "cz"]) assert output.returncode == 0 From 9d40918d2652b9db42c0ebda0bd6f463fa429f8a Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 11:12:36 -0700 Subject: [PATCH 12/21] test(commit): use double-quotes On Windows, `Popen` with `shell=True` reads the `COMSPEC` environment variable to determine the shell to run, which defaults to Command Prompt (see https://docs.python.org/3/library/subprocess.html#subprocess.run). For backwards-compatibility for older OS utility scripts, `COMSPEC` should not be changed on Windows. Command Prompt requires double-quotes instead of single-quotes. See - https://stackoverflow.com/questions/25534995/escape-double-quotes-in-git-config-from-cmd) - https://docs.python.org/3/library/subprocess.html#converting-an-argument-sequence-to-a-string-on-windows Note that although the commitizen shell `scripts` shebangs have been changed to a portable variety (i.e. `#!/usr/bin/env sh`), which runs Git Bash on Windows when Git is installed (i.e. the `sh.exe` executable that ships with Git for Windows), `subprocess.Popen()` opens a child process from the active running shell (i.e. via `CreateProcess()`). Hence, it will still read `COMSPEC` and run a Command Prompt shell. --- tests/test_bump_create_commit_message.py | 11 +++++++++-- tests/test_git.py | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test_bump_create_commit_message.py b/tests/test_bump_create_commit_message.py index ee1f1f768c..b4bcf9631a 100644 --- a/tests/test_bump_create_commit_message.py +++ b/tests/test_bump_create_commit_message.py @@ -1,3 +1,4 @@ +import os import sys from pathlib import Path from textwrap import dedent @@ -55,7 +56,10 @@ def test_bump_pre_commit_changelog(tmp_commitizen_project, mocker, freezer, retr """ ) cmd.run("git add -A") - cmd.run("git commit -m 'fix: _test'") + if os.name == "nt": + cmd.run('git commit -m "fix: _test"') + else: + cmd.run("git commit -m 'fix: _test'") cmd.run("pre-commit install") cli.main() # Pre-commit fixed last line adding extra indent and "\" char @@ -93,7 +97,10 @@ def test_bump_pre_commit_changelog_fails_always( """ ) cmd.run("git add -A") - cmd.run("git commit -m 'feat: forbid changelogs'") + if os.name == "nt": + cmd.run('git commit -m "feat: forbid changelogs"') + else: + cmd.run("git commit -m 'feat: forbid changelogs'") cmd.run("pre-commit install") with pytest.raises(exceptions.BumpCommitFailedError): cli.main() diff --git a/tests/test_git.py b/tests/test_git.py index ca51c8f2cd..cc1f4cf3c9 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -206,7 +206,10 @@ def test_is_staging_clean_when_updating_file(tmp_commitizen_project): cmd.run("touch test_file") cmd.run("git add test_file") - cmd.run("git commit -m 'add test_file'") + if os.name == "nt": + cmd.run('git commit -m "add test_file"') + else: + cmd.run("git commit -m 'add test_file'") cmd.run("echo 'test' > test_file") assert git.is_staging_clean() is True From d1a2919e07cb7425e2e0ff3d874611b0cc27cac8 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 11:20:29 -0700 Subject: [PATCH 13/21] test(bump_command): use `/` path separator The Windows path separator is the backslash (i.e. `\\`). This causes problems when python reads files from the `Users` folder since `\\U` is the prefix for a Unicode string in python. Since `os.path` can properly parse forward slashes on all OS's, we substitute `\\` for `/` wherever `os.path` methods return backslashes. --- tests/commands/test_bump_command.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 7374e5aa52..81b4f9d1b4 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -302,9 +302,10 @@ def test_bump_when_version_inconsistent_in_version_files( tmp_version_file = tmp_commitizen_project.join("__version__.py") tmp_version_file.write("100.999.10000") tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file_string = str(tmp_version_file).replace("\\", "/") tmp_commitizen_cfg_file.write( f"{tmp_commitizen_cfg_file.read()}\n" - f'version_files = ["{str(tmp_version_file)}"]' + f'version_files = ["{tmp_version_file_string}"]' ) create_file_and_commit("feat: new file") @@ -323,9 +324,10 @@ def test_bump_files_only(mocker, tmp_commitizen_project): tmp_version_file = tmp_commitizen_project.join("__version__.py") tmp_version_file.write("0.1.0") tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file_string = str(tmp_version_file).replace("\\", "/") tmp_commitizen_cfg_file.write( f"{tmp_commitizen_cfg_file.read()}\n" - f'version_files = ["{str(tmp_version_file)}"]' + f'version_files = ["{tmp_version_file_string}"]' ) create_file_and_commit("feat: new user interface") @@ -355,10 +357,11 @@ def test_bump_local_version(mocker, tmp_commitizen_project): tmp_version_file = tmp_commitizen_project.join("__version__.py") tmp_version_file.write("4.5.1+0.1.0") tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") + tmp_version_file_string = str(tmp_version_file).replace("\\", "/") tmp_commitizen_cfg_file.write( f"[tool.commitizen]\n" 'version="4.5.1+0.1.0"\n' - f'version_files = ["{str(tmp_version_file)}"]' + f'version_files = ["{tmp_version_file_string}"]' ) create_file_and_commit("feat: new user interface") From 6a963a852d1a3cd030d5b3cd41f366a3e6ec0b7b Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 11:24:44 -0700 Subject: [PATCH 14/21] fix(bump.py): use `sys.stdin.isatty()` For some reason, `pytest-mock` does not affect `os.isatty` when `sys.stdin` is mocked to use `StringIO` (i.e. a file-like object). Checking `sys.stdin.isatty()` works and is equivalent to checking `os.isatty(0)` (`stdin` is file descriptor `0`). --- commitizen/commands/check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index 58f6814ba3..2ce8ed4d64 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -40,7 +40,7 @@ def _valid_command_argument(self): arg is not None for arg in (self.commit_msg_file, self.commit_msg, self.rev_range) ) - if num_exclusive_args_provided == 0 and not os.isatty(0): + if num_exclusive_args_provided == 0 and not sys.stdin.isatty(): self.commit_msg: Optional[str] = sys.stdin.read() elif num_exclusive_args_provided != 1: raise InvalidCommandArgumentError( From d936262efa3fbf5178f11474fcc89bff93a9bed6 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 12:01:51 -0700 Subject: [PATCH 15/21] fix(scripts): pydocstyle and cz For `pydocstyle`, use `--match-dir` switch to prevent traversal into `venv/` folder. For `commitizen`, use `cz` instead of `commitizen` as `cz` is present on all OS's, but `commitizen` is not present in `venv/Scripts` on Windows. --- scripts/test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/test b/scripts/test index 1053854cce..742b48dbc7 100755 --- a/scripts/test +++ b/scripts/test @@ -22,5 +22,5 @@ ${PREFIX}black commitizen tests --check ${PREFIX}isort --check-only commitizen tests ${PREFIX}flake8 commitizen/ tests/ ${PREFIX}mypy commitizen/ tests/ -${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 -${PREFIX}commitizen check --rev-range origin/master.. +${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 --match-dir='^(?![.]|venv).*' +${PREFIX}cz check --rev-range origin/master.. From 53ac7c9f2f097abfa7a9c184ef36ab89c1018e61 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 12:09:05 -0700 Subject: [PATCH 16/21] feat(scripts): add error message to `test` Add error message if `test` script encounters unsupported OS. --- scripts/test | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/test b/scripts/test index 742b48dbc7..91e8f18d81 100755 --- a/scripts/test +++ b/scripts/test @@ -12,6 +12,7 @@ if [ -d 'venv' ] ; then export PREFIX="venv/Scripts/" ;; *) + echo "ERROR: Script 'test' encountered unsupported operating system $(uname -s)" exit 1 ;; esac From 4536f9348a93e120ab0f4c97656b6d1fdc03ef12 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 12:18:24 -0700 Subject: [PATCH 17/21] test(argcomplete): fix command `py -m` was added to command in `test_argcomplete_activation` during experimentation and was forgotten to be removed. --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 54a2dcd775..68a9c04a23 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -97,7 +97,7 @@ def test_argcomplete_activation(): Equivalent to run: $ eval "$(register-python-argcomplete pytest)" """ - output = subprocess.run(["py", "-m", "register-python-argcomplete", "cz"]) + output = subprocess.run(["register-python-argcomplete", "cz"]) assert output.returncode == 0 From 24adbcdc0c88c1fe640c6d20c39afd0150dbb878 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 13:57:36 -0700 Subject: [PATCH 18/21] feat(scripts): remove `venv/bin/` Commit `d936262e` switched the `test` script to use `cz` instead of `commitizen`, but forgot the prefix to the command is `poetry run python -m`, which looks for a python module. `cz` is not a module: it is an entry point script (specified in the `pyproject.toml` table `[tool.poetry.scripts]`). Switching `$PREFIX` to `venv/bin/` creates two problems: - The subdirectory `venv` creates differs depending on OS. On Linux/MacOS, the subdirectory containing copies/symlinks to Python binaries is `bin`, but on Windows it is `Scripts`. Similiarly, the `site-packages` directory on Linux/MacOS is `lib/pythonX.Y/site-packages`, but on Windows it is `Lib\site-packages`. See: https://docs.python.org/3/library/venv.html#module-venv - Since `commitizen` isn't the entry-point script, but rather `cz`, the command `venv/bin/commitizen` will fail on Linux/MacOS and Windows. Poetry will check to see if it is currently running inside a virtual environment, and if it is, it will use it directly without creating a new one. If it isn't, it will use one that it has already created or create a brand new one for you. See: https://python-poetry.org/docs/managing-environments/ A better solution than switching the prefix would be to activate the virtual environment. However, users can create virtual environments with any name they choose (`venv/`, `.venv/`, or otherwise). In addition, they can specify whether or not `poetry` should create a virtual environment in the project folder `virtualenvs.in-project`), or at all (`virtualenvs.create`). Since switching to `venv/bin` only affects developers running scripts locally (the GitHub Actions CI scripts run `poetry install` and let it manage the virtual environment), it's best to remove changing the `$PREFIX` and instead add instructions to `README`/`CONTRIBUTING`/etc. to tell users to configure `poetry` and activate their virtual environment appropriately if running these scripts locally. --- scripts/format | 14 -------------- scripts/test | 17 +---------------- 2 files changed, 1 insertion(+), 30 deletions(-) diff --git a/scripts/format b/scripts/format index b9826cbfd3..9d300e964b 100755 --- a/scripts/format +++ b/scripts/format @@ -3,20 +3,6 @@ set -e export PREFIX="poetry run python -m " -if [ -d 'venv' ] ; then - case $(uname -s) in - Linux|FreeBSD|OpenBSD|NetBSD|Darwin) - export PREFIX="venv/bin/" - ;; - *MINGW*) - export PREFIX="venv/Scripts/" - ;; - *) - exit 1 - ;; - esac -fi - set -x ${PREFIX}isort commitizen tests diff --git a/scripts/test b/scripts/test index 91e8f18d81..d180c571e1 100755 --- a/scripts/test +++ b/scripts/test @@ -3,25 +3,10 @@ set -e export PREFIX="poetry run python -m " -if [ -d 'venv' ] ; then - case $(uname -s) in - Linux|FreeBSD|OpenBSD|NetBSD|Darwin) - export PREFIX="venv/bin/" - ;; - *MINGW*) - export PREFIX="venv/Scripts/" - ;; - *) - echo "ERROR: Script 'test' encountered unsupported operating system $(uname -s)" - exit 1 - ;; - esac -fi - ${PREFIX}pytest -n 3 --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen tests/ ${PREFIX}black commitizen tests --check ${PREFIX}isort --check-only commitizen tests ${PREFIX}flake8 commitizen/ tests/ ${PREFIX}mypy commitizen/ tests/ ${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 --match-dir='^(?![.]|venv).*' -${PREFIX}cz check --rev-range origin/master.. +${PREFIX}commitizen check --rev-range origin/master.. From db5131e3f01ce7c4304eae2b1f28c934be0cdd42 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 14:10:58 -0700 Subject: [PATCH 19/21] fix(scripts/test): use double-quotes Command Prompt on Windows requires double-quotes. Otherwise, the command `${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 --match-dir="^(?![.]|venv).*"` will fail with 'venv).*' is not recognized as an internal or external command this will not affect the command in other Linux-based/bash shells. --- scripts/test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/test b/scripts/test index d180c571e1..81fb980811 100755 --- a/scripts/test +++ b/scripts/test @@ -8,5 +8,5 @@ ${PREFIX}black commitizen tests --check ${PREFIX}isort --check-only commitizen tests ${PREFIX}flake8 commitizen/ tests/ ${PREFIX}mypy commitizen/ tests/ -${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 --match-dir='^(?![.]|venv).*' +${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 --match-dir="^(?![.]|venv).*" ${PREFIX}commitizen check --rev-range origin/master.. From 5d1eaba136cc0705485a87a9fde73589168eeca6 Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 14:18:46 -0700 Subject: [PATCH 20/21] ci(rerun): random `poetry` exception Last CI run ended with a random `poetry` exception on Windows, which happens from time-to-time. From a079fe43d0e61b2330324ce1f6beafcc9e9cc04c Mon Sep 17 00:00:00 2001 From: Adam Hendry Date: Thu, 20 Oct 2022 16:00:03 -0700 Subject: [PATCH 21/21] fix(scripts/test): MinGW64 workaround Pipe appears broken in certain instances within the version of MinGW64 that ships as Git Bash in Git for Windows. See: - https://sourceforge.net/p/mingw/bugs/2303/ - https://github.com/msys2/MINGW-packages/issues/13662 --- scripts/test | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/test b/scripts/test index 81fb980811..ea5a318988 100755 --- a/scripts/test +++ b/scripts/test @@ -1,12 +1,13 @@ #!/usr/bin/env sh set -e -export PREFIX="poetry run python -m " +export PREFIX='poetry run python -m ' +export REGEX='^(?![.]|venv).*' ${PREFIX}pytest -n 3 --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen tests/ ${PREFIX}black commitizen tests --check ${PREFIX}isort --check-only commitizen tests ${PREFIX}flake8 commitizen/ tests/ ${PREFIX}mypy commitizen/ tests/ -${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 --match-dir="^(?![.]|venv).*" +${PREFIX}pydocstyle --convention=google --add-ignore=D1,D415 --match-dir='"${REGEX}"' ${PREFIX}commitizen check --rev-range origin/master..