From 51702bb3ecb077e275e1a1fee81b1d8c0f0f4cb4 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sat, 29 Apr 2023 00:11:54 +0900 Subject: [PATCH 01/11] Add --google-java-formatter-jar parameter --- language-formatters-pre-commit-hooks.iml | 12 ++++++++++++ .../pretty_format_java.py | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 language-formatters-pre-commit-hooks.iml diff --git a/language-formatters-pre-commit-hooks.iml b/language-formatters-pre-commit-hooks.iml new file mode 100644 index 0000000..1b1adea --- /dev/null +++ b/language-formatters-pre-commit-hooks.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/language_formatters_pre_commit_hooks/pretty_format_java.py b/language_formatters_pre_commit_hooks/pretty_format_java.py index efcec20..a3ab076 100644 --- a/language_formatters_pre_commit_hooks/pretty_format_java.py +++ b/language_formatters_pre_commit_hooks/pretty_format_java.py @@ -70,6 +70,12 @@ def pretty_format_java(argv: typing.Optional[typing.List[str]] = None) -> int: dest="aosp", help="Formats Java code into AOSP format", ) + parser.add_argument( + "--google-java-formatter-jar", + dest="google_java_formatter_jar", + default=None, + help="Path to Google Java Formatter jar file. Will be downloaded in case of None (default: %(default)).", + ) parser.add_argument("filenames", nargs="*", help="Filenames to fix") args = parser.parse_args(argv) @@ -80,9 +86,12 @@ def pretty_format_java(argv: typing.Optional[typing.List[str]] = None) -> int: if Version(args.google_java_formatter_version) <= Version("1.9"): assert_max_jdk_version(Version("16.0"), inclusive=False) # pragma: no cover - google_java_formatter_jar = _download_google_java_formatter_jar( - args.google_java_formatter_version, - ) + if args.google_java_formatter_jar is None: + google_java_formatter_jar = _download_google_java_formatter_jar( + args.google_java_formatter_version, + ) + else: + google_java_formatter_jar = args.google_java_formatter_jar cmd_args = [ "java", From 0256ad66e1ddb6510a833f6ca2ea6621b0b6ce88 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sat, 29 Apr 2023 16:27:11 +0900 Subject: [PATCH 02/11] Add tests --- .github/workflows/build.yaml | 4 ++- language-formatters-pre-commit-hooks.iml | 12 --------- .../pretty_format_java.py | 25 ++++++++++--------- tests/__init__.py | 2 +- tests/pretty_format_java_test.py | 11 ++++++++ 5 files changed, 28 insertions(+), 26 deletions(-) delete mode 100644 language-formatters-pre-commit-hooks.iml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index dbc14d6..5ca172e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -48,7 +48,7 @@ jobs: - name: Setup Java uses: actions/setup-java@v3 with: - distribution: adopt + distribution: temurin java-version: ${{ matrix.java_version }} - name: Set up Go uses: actions/setup-go@v3 @@ -56,6 +56,8 @@ jobs: go-version: ${{ matrix.golang_version }} - name: Install Python dependencies run: pip install codecov tox tox-gh-actions + - name: Download google-java-format.jar + run: wget -O test-data/pretty_format_java/google-java-format-1.16.0-all-deps.jar -q https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar - name: Run Tox run: tox - name: Upload coverage to Codecov diff --git a/language-formatters-pre-commit-hooks.iml b/language-formatters-pre-commit-hooks.iml deleted file mode 100644 index 1b1adea..0000000 --- a/language-formatters-pre-commit-hooks.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/language_formatters_pre_commit_hooks/pretty_format_java.py b/language_formatters_pre_commit_hooks/pretty_format_java.py index a3ab076..2772b53 100644 --- a/language_formatters_pre_commit_hooks/pretty_format_java.py +++ b/language_formatters_pre_commit_hooks/pretty_format_java.py @@ -52,30 +52,31 @@ def get_urls(_version: str) -> typing.List[str]: @java_required def pretty_format_java(argv: typing.Optional[typing.List[str]] = None) -> int: parser = argparse.ArgumentParser() + group = parser.add_mutually_exclusive_group() + group.add_argument( + "--google-java-formatter-version", + dest="google_java_formatter_version", + default=_get_default_version("google_java_formatter"), + help="Google Java Formatter version to use (default %(default)s)", + ) + group.add_argument( + "--google-java-formatter-jar", + dest="google_java_formatter_jar", + default=None, + help="Path to Google Java Formatter jar file. Will be downloaded if not defined. Note that --google-java-formatter-version will be ignored if this parameter is defined (default: %(default)).", + ) parser.add_argument( "--autofix", action="store_true", dest="autofix", help="Automatically fixes encountered not-pretty-formatted files", ) - parser.add_argument( - "--google-java-formatter-version", - dest="google_java_formatter_version", - default=_get_default_version("google_java_formatter"), - help="Google Java Formatter version to use (default %(default)s)", - ) parser.add_argument( "--aosp", action="store_true", dest="aosp", help="Formats Java code into AOSP format", ) - parser.add_argument( - "--google-java-formatter-jar", - dest="google_java_formatter_jar", - default=None, - help="Path to Google Java Formatter jar file. Will be downloaded in case of None (default: %(default)).", - ) parser.add_argument("filenames", nargs="*", help="Filenames to fix") args = parser.parse_args(argv) diff --git a/tests/__init__.py b/tests/__init__.py index aa5c11a..824db48 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -14,7 +14,7 @@ def change_dir_context(directory: str) -> typing.Generator[None, None, None]: working_directory = os.getcwd() try: - os.chdir(directory) + os.chdir(os.path.join(os.path.dirname(working_directory), directory)) yield finally: os.chdir(working_directory) diff --git a/tests/pretty_format_java_test.py b/tests/pretty_format_java_test.py index c4425de..e19d52b 100644 --- a/tests/pretty_format_java_test.py +++ b/tests/pretty_format_java_test.py @@ -67,3 +67,14 @@ def test_pretty_format_java_up_to_1_9_is_allowed_on_jdk_before_16(undecorate_met def test_pretty_format_java_autofix(tmpdir, undecorate_method): run_autofix_test(tmpdir, undecorate_method, "not-pretty-formatted.java", "not-pretty-formatted_fixed.java") + + +@pytest.mark.parametrize( + ("cli_arg", "expected_retval"), + ( + ("--google-java-formatter-jar=google-java-format-1.16.0-all-deps.jar", 0), + ("", 0), + ), +) +def test_pretty_format_java_jar(undecorate_method, cli_arg, expected_retval): + assert undecorate_method([cli_arg, "pretty-formatted.java"]) == expected_retval From ca5eb3da34122d6d6089e433d92020f3e385ffe6 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sat, 29 Apr 2023 16:30:46 +0900 Subject: [PATCH 03/11] Rollback java dist --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5ca172e..b599988 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -48,7 +48,7 @@ jobs: - name: Setup Java uses: actions/setup-java@v3 with: - distribution: temurin + distribution: adopt java-version: ${{ matrix.java_version }} - name: Set up Go uses: actions/setup-go@v3 From fa0dafd257a31a25c724723b4e469a4356d4ab29 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sun, 30 Apr 2023 00:07:36 +0900 Subject: [PATCH 04/11] Rollback __init__.py change --- tests/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/__init__.py b/tests/__init__.py index 824db48..aa5c11a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -14,7 +14,7 @@ def change_dir_context(directory: str) -> typing.Generator[None, None, None]: working_directory = os.getcwd() try: - os.chdir(os.path.join(os.path.dirname(working_directory), directory)) + os.chdir(directory) yield finally: os.chdir(working_directory) From 9e6ab68e57f896d1e416dce215d660a8ef7986d9 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Tue, 2 May 2023 19:54:56 +0900 Subject: [PATCH 05/11] Replace wget with curl --- .github/workflows/build.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index b599988..38b018e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -57,7 +57,12 @@ jobs: - name: Install Python dependencies run: pip install codecov tox tox-gh-actions - name: Download google-java-format.jar - run: wget -O test-data/pretty_format_java/google-java-format-1.16.0-all-deps.jar -q https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar + run: curl https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar --output test-data/pretty_format_java/google-java-format-1.16.0-all-deps.jar --silent + shell: sh +# - name: Download google-java-format.jar (Windows) +# if: ${{ runner.os == 'Windows' }} +# run: Invoke-WebRequest -URI https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar -OutFile $Env:BIN +# shell: pwsh - name: Run Tox run: tox - name: Upload coverage to Codecov From 452d8bbc4922b956eb1f06e59767b6f921eb1ce0 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Tue, 2 May 2023 20:07:31 +0900 Subject: [PATCH 06/11] Fix CI failure --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 38b018e..8efb94e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -57,7 +57,7 @@ jobs: - name: Install Python dependencies run: pip install codecov tox tox-gh-actions - name: Download google-java-format.jar - run: curl https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar --output test-data/pretty_format_java/google-java-format-1.16.0-all-deps.jar --silent + run: curl --location https://github.com/google/google-java-format/releases/download/v1.17.0/google-java-format-1.17.0-all-deps.jar --output test-data/pretty_format_java/google-java-format-1.16.0-all-deps.jar --silent shell: sh # - name: Download google-java-format.jar (Windows) # if: ${{ runner.os == 'Windows' }} From a874df8ae09002a3325bbfd4b7db4f598902ce56 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sun, 7 May 2023 23:10:02 +0900 Subject: [PATCH 07/11] Mock run_command --- .github/workflows/build.yaml | 7 ------- tests/pretty_format_java_test.py | 8 +++++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 8efb94e..dbc14d6 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -56,13 +56,6 @@ jobs: go-version: ${{ matrix.golang_version }} - name: Install Python dependencies run: pip install codecov tox tox-gh-actions - - name: Download google-java-format.jar - run: curl --location https://github.com/google/google-java-format/releases/download/v1.17.0/google-java-format-1.17.0-all-deps.jar --output test-data/pretty_format_java/google-java-format-1.16.0-all-deps.jar --silent - shell: sh -# - name: Download google-java-format.jar (Windows) -# if: ${{ runner.os == 'Windows' }} -# run: Invoke-WebRequest -URI https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar -OutFile $Env:BIN -# shell: pwsh - name: Run Tox run: tox - name: Upload coverage to Codecov diff --git a/tests/pretty_format_java_test.py b/tests/pretty_format_java_test.py index e19d52b..bd337b9 100644 --- a/tests/pretty_format_java_test.py +++ b/tests/pretty_format_java_test.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from unittest.mock import patch + import pytest from packaging.version import Version @@ -76,5 +78,9 @@ def test_pretty_format_java_autofix(tmpdir, undecorate_method): ("", 0), ), ) -def test_pretty_format_java_jar(undecorate_method, cli_arg, expected_retval): +@patch("language_formatters_pre_commit_hooks.pretty_format_java.run_command", autospec=True) +def test_pretty_format_java_jar(mock_run_command, undecorate_method, cli_arg, expected_retval): + mock_run_command.return_value = (0, "") assert undecorate_method([cli_arg, "pretty-formatted.java"]) == expected_retval + args = mock_run_command.call_args.args + assert cli_arg in args From 36f75ec3fff10e563fbb0b61721265dd8bb09f15 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sun, 7 May 2023 23:18:54 +0900 Subject: [PATCH 08/11] Fix test --- tests/pretty_format_java_test.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/pretty_format_java_test.py b/tests/pretty_format_java_test.py index bd337b9..b086eb8 100644 --- a/tests/pretty_format_java_test.py +++ b/tests/pretty_format_java_test.py @@ -82,5 +82,8 @@ def test_pretty_format_java_autofix(tmpdir, undecorate_method): def test_pretty_format_java_jar(mock_run_command, undecorate_method, cli_arg, expected_retval): mock_run_command.return_value = (0, "") assert undecorate_method([cli_arg, "pretty-formatted.java"]) == expected_retval - args = mock_run_command.call_args.args - assert cli_arg in args + in_args = cli_arg in mock_run_command.call_args.args + if cli_arg == "": + assert not in_args + else: + assert in_args From 88559ecadb410d37afb38488e7d20928cd77db7d Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sun, 7 May 2023 23:23:31 +0900 Subject: [PATCH 09/11] Fix CI --- .github/workflows/build.yaml | 2 +- tests/pretty_format_java_test.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index dbc14d6..25d817c 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -12,7 +12,7 @@ on: jobs: build: strategy: - fail-fast: false + fail-fast: true matrix: os: [macos-latest, ubuntu-latest, windows-latest] diff --git a/tests/pretty_format_java_test.py b/tests/pretty_format_java_test.py index b086eb8..dd91cc7 100644 --- a/tests/pretty_format_java_test.py +++ b/tests/pretty_format_java_test.py @@ -84,6 +84,6 @@ def test_pretty_format_java_jar(mock_run_command, undecorate_method, cli_arg, ex assert undecorate_method([cli_arg, "pretty-formatted.java"]) == expected_retval in_args = cli_arg in mock_run_command.call_args.args if cli_arg == "": - assert not in_args - else: assert in_args + else: + assert not in_args From 1695ce06ebbbdbbe298868da95f5baf84775c439 Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Sun, 7 May 2023 23:30:39 +0900 Subject: [PATCH 10/11] Rollback fail-fast param --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 25d817c..dbc14d6 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -12,7 +12,7 @@ on: jobs: build: strategy: - fail-fast: true + fail-fast: false matrix: os: [macos-latest, ubuntu-latest, windows-latest] From 68c3859bd6a866b9794030450228ca03f11aea4e Mon Sep 17 00:00:00 2001 From: Yevhen Fabizhevskyi Date: Tue, 9 May 2023 09:32:05 +0900 Subject: [PATCH 11/11] Add new parameter to README.md --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 05abc4e..da8c6ad 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,18 @@ To work-around such scenario you have 2 approaches available: Side note: We're not embedding the Dockerfile in the repository as this is more a workaround to support whom cannot of installing a more recent Java version on the library-user system and as such we are not planning to fully support this other than giving possible solutions (Java 11+ was released in September, 2018). +### How to use pre-downloaded [google-java-format jar](https://github.com/google/google-java-format) file? + +You can pass the jar file path to `--google-java-formatter-jar` argument: + +```yaml + - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks + rev: ... + hooks: + - id: pretty-format-java + args: [--google-java-formatter-jar=/usr/bin/google-java-format-1.17.0-all-deps.jar] +``` + ## License `language-formatters-pre-commit-hooks` is licensed with [`Apache License version 2.0`](http://www.apache.org/licenses/LICENSE-2.0.html).