diff --git a/end-to-end-tests/features/from_arguments_conflicts.feature b/end-to-end-tests/features/from_arguments_conflicts.feature deleted file mode 100644 index 33c667d..0000000 --- a/end-to-end-tests/features/from_arguments_conflicts.feature +++ /dev/null @@ -1,37 +0,0 @@ -Feature: The from arguments conflict with one another and can not be provided at the same time. - - - Scenario Outline: You can not provide both a reference and a commit hash. - Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". - And the argument --from-commit-hash is provided as "". - Then their is a conflicting from arguments error. - - - Examples: - | repository | checkout_commit | from_reference | from_commit_hash | - | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | v15.4.0 | 028b50d5ced3b41a8dccf74107dbfc7065052a5d | - - - Scenario Outline: You can not provide both a reference and a commit message. - Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". - And the flag --from-stdin is set and the standard input is "". - Then their is a conflicting from arguments error. - - - Examples: - | repository | checkout_commit | from_reference | standard_input | - | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | v15.4.0 | "setup of typescript and jest" | - - - Scenario Outline: You can not provide both a commit hash and a commit message. - Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". - And the flag --from-stdin is set and the standard input is "". - Then their is a conflicting from arguments error. - - - Examples: - | repository | checkout_commit | from_commit_hash | standard_input | - | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | 028b50d5ced3b41a8dccf74107dbfc7065052a5d | "setup of typescript and jest" | diff --git a/end-to-end-tests/features/from_commit_hash.feature b/end-to-end-tests/features/from_commit_hash.feature index 0416f2f..963af33 100644 --- a/end-to-end-tests/features/from_commit_hash.feature +++ b/end-to-end-tests/features/from_commit_hash.feature @@ -3,21 +3,21 @@ Feature: A Git commit hash can be provided as an argument to indicate where to s Scenario Outline: Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". + When linting from the "". Then the linting passes. Examples: - | repository | checkout_commit | from_commit_hash | + | repository | checkout_commit | commit_hash | | https://github.com/danielduarte/diffparse.git | df6be23b79af66d3684fb27719020e1ce587f4b8 | 4f6bf53139fe66f61bd05893bcc9de6e96400c5c | Scenario Outline: When you provide an invalid commit hash a relevant error message is returned. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". - Then their is a could not find commit hash "" error. + When linting from the "". + Then their is a could not find commit hash "" error. Examples: - | repository | checkout_commit | from_commit_hash | + | repository | checkout_commit | commit_hash | | https://github.com/SergioBenitez/Rocket.git | 549c9241c41320fc5af76b53c2ffc3bd8db88f8c | ecfc2c474575c6cdbc6d273c94c13181bd1dbaa6 | diff --git a/end-to-end-tests/features/from_reference.feature b/end-to-end-tests/features/from_reference.feature index e9ba3f7..7d213bc 100644 --- a/end-to-end-tests/features/from_reference.feature +++ b/end-to-end-tests/features/from_reference.feature @@ -3,38 +3,38 @@ Feature: A Git reference can be provided as an argument to indicate where to sta Scenario Outline: Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". + When linting from the "". Then the linting passes. Examples: - | repository | checkout_commit | from_reference | - | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | v15.4.0 | + | repository | checkout_commit | reference | + | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | v15.4.0 | Scenario Outline: You can also provide the long name and partial names not just the short name. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". + When linting from the "". Then the linting passes. Given the arguments are reset. - When the argument --from-reference is provided as "". + When linting from the "". Then the linting passes. Given the arguments are reset. - When the argument --from-reference is provided as "". + When linting from the "". Then the linting passes. Examples: - | repository | checkout_commit | from_full_reference | from_partial_reference | from_short_reference | - | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | refs/tags/v15.4.0 | tags/v15.4.0 | v15.4.0 | + | repository | checkout_commit | full_reference | partial_reference | short_reference | + | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | refs/tags/v15.4.0 | tags/v15.4.0 | v15.4.0 | Scenario Outline: When you provide an invalid reference a relevant error message is returned. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". - Then their is a could not find reference "" error. + When linting from the "". + Then their is a could not find reference "" error. Examples: - | repository | checkout_commit | from_reference | - | https://gitlab.com/Commit451/LabCoat | dcbed0075a41c221b37a3551db51572088fa6e2e | v2.7.4 | + | repository | checkout_commit | reference | + | https://gitlab.com/Commit451/LabCoat | dcbed0075a41c221b37a3551db51572088fa6e2e | v2.7.4 | diff --git a/end-to-end-tests/features/from_shortened_commit_hash.feature b/end-to-end-tests/features/from_shortened_commit_hash.feature index 87733c8..086720a 100644 --- a/end-to-end-tests/features/from_shortened_commit_hash.feature +++ b/end-to-end-tests/features/from_shortened_commit_hash.feature @@ -3,35 +3,35 @@ Feature: A shortened Git commit hash can be provided as an argument to indicate Scenario Outline: A shortened and full Git commit hash can be used interchangeably. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". + When linting from the "". Then the linting passes. Given the arguments are reset. - When the argument --from-commit-hash is provided as "". + When linting from the "". Then the linting passes. Examples: - | repository | checkout_commit | from_commit_hash | shortened_from_commit_hash | - | https://github.com/danielduarte/diffparse.git | df6be23b79af66d3684fb27719020e1ce587f4b8 | 4f6bf53139fe66f61bd05893bcc9de6e96400c5c | 4f6bf53 | + | repository | checkout_commit | commit_hash | shortened_commit_hash | + | https://github.com/danielduarte/diffparse.git | df6be23b79af66d3684fb27719020e1ce587f4b8 | 4f6bf53139fe66f61bd05893bcc9de6e96400c5c | 4f6bf53 | Scenario Outline: The shortened Git commit hash has no matches, so an error is returned. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". - Then their is a could not find shortened commit hash "" error. + When linting from the "". + Then their is a could not find shortened commit hash "" error. Examples: - | repository | checkout_commit | from_shortened_commit_hash | - | https://gitlab.com/dmfay/massive-js.git | 482c364acf5505b81c55245fac0472890d351662 | 3f235ee | + | repository | checkout_commit | shortened_commit_hash | + | https://gitlab.com/dmfay/massive-js.git | 482c364acf5505b81c55245fac0472890d351662 | 3f235ee | Scenario Outline: The shortened Git commit hash is ambiguous as multiple commit hashes match it, so an error is returned. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". - Then their is a ambiguous shortened commit hash "" error. + When linting from the "". + Then their is a ambiguous shortened commit hash "" error. Examples: - | repository | checkout_commit | from_shortened_commit_hash | - | https://github.com/yargs/yargs.git | 089417550ef5a5b8ce3578dd2a989191300b64cd | 3f6 | + | repository | checkout_commit | shortened_commit_hash | + | https://github.com/yargs/yargs.git | 089417550ef5a5b8ce3578dd2a989191300b64cd | 3f6 | diff --git a/end-to-end-tests/features/from_stdin.feature b/end-to-end-tests/features/from_stdin.feature index d35ac61..892eac1 100644 --- a/end-to-end-tests/features/from_stdin.feature +++ b/end-to-end-tests/features/from_stdin.feature @@ -3,12 +3,12 @@ Feature: A commit message can be provided by standard input rather than from a r Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". Then the linting passes. Examples: - | standard_input | + | commit_message | | "test: adding stdin scenario variations" | | "docs: correct spelling of CHANGELOG" | | "fix: minor typos in code" | diff --git a/end-to-end-tests/features/git_environment_variables.feature b/end-to-end-tests/features/git_environment_variables.feature index 0e38206..bdbbc7a 100644 --- a/end-to-end-tests/features/git_environment_variables.feature +++ b/end-to-end-tests/features/git_environment_variables.feature @@ -3,65 +3,65 @@ Feature: Git environment variables are respected and used instead of using the c Scenario Outline: Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". + When linting from the "". Then the linting passes. Given the GIT_DIR environment variable is set to the cloned repository. Then the linting passes. Examples: - | repository | checkout_commit | from_commit_hash | + | repository | checkout_commit | commit_hash | | https://github.com/danielduarte/diffparse.git | df6be23b79af66d3684fb27719020e1ce587f4b8 | 4f6bf53139fe66f61bd05893bcc9de6e96400c5c | Scenario Outline: When you provide an invalid commit hash a relevant error message is returned. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". - Then their is a could not find commit hash "" error. + When linting from the "". + Then their is a could not find commit hash "" error. Given the GIT_DIR environment variable is set to the cloned repository. - Then their is a could not find commit hash "" error. + Then their is a could not find commit hash "" error. Examples: - | repository | checkout_commit | from_commit_hash | + | repository | checkout_commit | commit_hash | | https://github.com/SergioBenitez/Rocket.git | 549c9241c41320fc5af76b53c2ffc3bd8db88f8c | ecfc2c474575c6cdbc6d273c94c13181bd1dbaa6 | Scenario Outline: Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". + When linting from the "". Then the linting passes. Given the GIT_DIR environment variable is set to the cloned repository. Then the linting passes. Examples: - | repository | checkout_commit | from_reference | - | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | v15.4.0 | + | repository | checkout_commit | reference | + | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | v15.4.0 | Scenario Outline: You can also provide the long name and partial names not just the short name. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". + When linting from the "". Then the linting passes. Given the GIT_DIR environment variable is set to the cloned repository. Then the linting passes. Examples: - | repository | checkout_commit | from_reference | + | repository | checkout_commit | reference | | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | tags/v15.4.0 | | https://github.com/yargs/yargs.git | 0f810245494ccf13a35b7786d021b30fc95ecad5 | refs/tags/v15.4.0 | Scenario Outline: When you provide an invalid reference a relevant error message is returned. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-reference is provided as "". - Then their is a could not find reference "" error. + When linting from the "". + Then their is a could not find reference "" error. Given the GIT_DIR environment variable is set to the cloned repository. - Then their is a could not find reference "" error. + Then their is a could not find reference "" error. Examples: - | repository | checkout_commit | from_reference | - | https://gitlab.com/Commit451/LabCoat | dcbed0075a41c221b37a3551db51572088fa6e2e | v2.7.4 | + | repository | checkout_commit | reference | + | https://gitlab.com/Commit451/LabCoat | dcbed0075a41c221b37a3551db51572088fa6e2e | v2.7.4 | diff --git a/end-to-end-tests/features/git_history_first_parent_mode.feature b/end-to-end-tests/features/git_history_first_parent_mode.feature index 9efe9d2..2e3c3e4 100644 --- a/end-to-end-tests/features/git_history_first_parent_mode.feature +++ b/end-to-end-tests/features/git_history_first_parent_mode.feature @@ -3,11 +3,11 @@ Feature: Only the first parent of merge commit's are parsed for their Git commit Scenario Outline: Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". + When linting from the "". And the argument --output is set as "JSON". Then their are "" commits failing linting. Examples: - | repository | checkout_commit | from_commit_hash | number_of_commits | + | repository | checkout_commit | commit_hash | number_of_commits | | https://github.com/dcyou/resume | bbf42bd631b3ee85de650f46891619833c920bef | 0a74203f382e06b6f39f5143da1eee0be77d3acb | 4 | diff --git a/end-to-end-tests/features/history_mode.feature b/end-to-end-tests/features/history_mode.feature index 98361fb..261d1d1 100644 --- a/end-to-end-tests/features/history_mode.feature +++ b/end-to-end-tests/features/history_mode.feature @@ -3,7 +3,7 @@ Feature: Specifies how commits are parsed, acceptable values are 'first' to pars Scenario Outline: Only the first parent of merge commit's are parsed for their Git commit messages. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". + When linting from the "". And the argument --output is set as "JSON". Then their are "" commits failing linting. When the argument --history-mode is provided as "all". @@ -11,30 +11,30 @@ Feature: Specifies how commits are parsed, acceptable values are 'first' to pars Examples: - | repository | checkout_commit | from_commit_hash | default_number_of_commits | all_parent_number_of_commits | + | repository | checkout_commit | commit_hash | default_number_of_commits | all_parent_number_of_commits | | https://github.com/lambci/awslambda.nim | 0fa0f62bec56eb7e09d8fd3e2a4c31798baba89f | 9c9583952ce709fbc6af3b85200c78bf417e5a11 | 5 | 6 | Scenario Outline: Only the first parent of merge commit's are parsed for their Git commit messages. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". + When linting from the "". And the argument --output is set as "JSON". And the argument --history-mode is provided as "first". Then their are "" commits failing linting. Examples: - | repository | checkout_commit | from_commit_hash | number_of_commits | + | repository | checkout_commit | commit_hash | number_of_commits | | https://github.com/dcyou/resume | bbf42bd631b3ee85de650f46891619833c920bef | 0a74203f382e06b6f39f5143da1eee0be77d3acb | 4 | Scenario Outline: Only the first parent of merge commit's are parsed for their Git commit messages, by default. Given the repository "" is cloned and checked out at the commit "". - When the argument --from-commit-hash is provided as "". + When linting from the "". And the argument --output is set as "JSON". Then their are "" commits failing linting. Examples: - | repository | checkout_commit | from_commit_hash | number_of_commits | + | repository | checkout_commit | commit_hash | number_of_commits | | https://github.com/dcyou/resume | bbf42bd631b3ee85de650f46891619833c920bef | 0a74203f382e06b6f39f5143da1eee0be77d3acb | 4 | diff --git a/end-to-end-tests/features/json.feature b/end-to-end-tests/features/json.feature index f41cc32..eb858d5 100644 --- a/end-to-end-tests/features/json.feature +++ b/end-to-end-tests/features/json.feature @@ -2,7 +2,7 @@ Feature: With JSON enabled when linting errors are encountered they are printed Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". Then the linting fails. And standard output is not valid JSON. When the argument --output is set as "JSON". @@ -11,7 +11,7 @@ Feature: With JSON enabled when linting errors are encountered they are printed Examples: - | standard_input | + | commit_message | | "chore:🌐 fix chinese translations" | | "fix(deps updated): update dependency gitmojis to v3" | | "test making test assertion stricter\n\nCo-authored-by: Renovate Bot \n\n" | diff --git a/end-to-end-tests/features/output_argument_ignores_casing.feature b/end-to-end-tests/features/output_argument_ignores_casing.feature index 4352040..7bab75a 100644 --- a/end-to-end-tests/features/output_argument_ignores_casing.feature +++ b/end-to-end-tests/features/output_argument_ignores_casing.feature @@ -3,7 +3,7 @@ Feature: The output argument can be provided in any casing. Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is ""fix(deps updated): update dependency gitmojis to v3"". + When linting the ""fix(deps updated): update dependency gitmojis to v3"". And the argument --output is set as "". Then the linting fails. And standard output is valid JSON. @@ -18,7 +18,7 @@ Feature: The output argument can be provided in any casing. Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is ""fix(deps updated): update dependency gitmojis to v3"". + When linting the ""fix(deps updated): update dependency gitmojis to v3"". And the argument --output is set as "". Then the linting fails. And standard output is empty. @@ -33,7 +33,7 @@ Feature: The output argument can be provided in any casing. Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is ""fix(deps updated): update dependency gitmojis to v3"". + When linting the ""fix(deps updated): update dependency gitmojis to v3"". And the argument --output is set as "". Then the linting fails. And standard output is not empty. diff --git a/end-to-end-tests/features/quiet.feature b/end-to-end-tests/features/quiet.feature index f9dbfba..798373b 100644 --- a/end-to-end-tests/features/quiet.feature +++ b/end-to-end-tests/features/quiet.feature @@ -3,7 +3,7 @@ Feature: With quiet enabled when linting errors are encountered no output is pri Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". Then the linting fails. And standard output is not empty. When the argument --output is set as "Quiet". @@ -12,7 +12,7 @@ Feature: With quiet enabled when linting errors are encountered no output is pri Examples: - | standard_input | + | commit_message | | "chore:🌐 fix chinese translations" | | "fix(deps updated): update dependency gitmojis to v3" | | "test making test assertion stricter\n\nCo-authored-by: Renovate Bot \n\n" | diff --git a/end-to-end-tests/features/steps/given.py b/end-to-end-tests/features/steps/given.py index 18aec36..30adad9 100644 --- a/end-to-end-tests/features/steps/given.py +++ b/end-to-end-tests/features/steps/given.py @@ -9,6 +9,7 @@ @given('the arguments are reset.') def reset_arguments(context): context.arguments = "" + context.from_ref = "" @given('the context and environment are reset.') diff --git a/end-to-end-tests/features/steps/then.py b/end-to-end-tests/features/steps/then.py index cc84648..495b821 100644 --- a/end-to-end-tests/features/steps/then.py +++ b/end-to-end-tests/features/steps/then.py @@ -53,7 +53,7 @@ def assert_could_not_find_commit_hash_error(context, commit_hash): @then('their is a missing from argument error.') def assert_missing_from_argument_error(context): # Given - missing_from_argument_error = "error: the following required arguments were not provided:\n <--from-stdin|--from-reference |--from-commit-hash >\n\nUsage: conventional_commits_linter <--from-stdin|--from-reference |--from-commit-hash >\n\nFor more information, try '--help'.\n" + missing_from_argument_error = "error: the following required arguments were not provided:\n \n\nUsage: conventional_commits_linter \n\nFor more information, try '--help'.\n" # When/Then result = assert_linting_fails(context) @@ -62,23 +62,6 @@ def assert_missing_from_argument_error(context): assert_error_equals(result, missing_from_argument_error) -@then('their is a conflicting from arguments error.') -def assert_conflicting_from_arguments_error(context): - # Given - conflicting_from_commit_hash_and_from_stdin_error = "error: the argument '--from-commit-hash ' cannot be used with '--from-stdin'\n\nUsage: conventional_commits_linter <--from-stdin|--from-reference |--from-commit-hash >\n\nFor more information, try '--help'.\n" - conflicting_from_reference_and_from_commit_hash_error = "error: the argument '--from-reference ' cannot be used with '--from-commit-hash '\n\nUsage: conventional_commits_linter <--from-stdin|--from-reference |--from-commit-hash >\n\nFor more information, try '--help'.\n" - conflicting_from_reference_and_from_stdin_error = "error: the argument '--from-reference ' cannot be used with '--from-stdin'\n\nUsage: conventional_commits_linter <--from-stdin|--from-reference |--from-commit-hash >\n\nFor more information, try '--help'.\n" - - # When/Then - result = assert_linting_fails(context) - - # Then - assert_error_is_one_of(result, - [conflicting_from_commit_hash_and_from_stdin_error, - conflicting_from_reference_and_from_commit_hash_error, - conflicting_from_reference_and_from_stdin_error]) - - @then('standard output is not empty.') def assert_standard_output_not_empty(context): # When/Then @@ -132,7 +115,7 @@ def assert_could_not_find_shortened_commit_hash_error(context, shortened_commit_ 'their is a ambiguous shortened commit hash "{shortened_commit_hash}" error.') def assert_ambiguous_shortened_commit_hash_error(context, shortened_commit_hash): # Given - ambiguous_shortened_commit_hash_error = re.compile(f"^ ERROR conventional_commits_linter > Ambiguous short commit hash, the commit hashes [[]({shortened_commit_hash}[a-f0-9]*(, )?)*[]] all start with the provided short commit hash \"{shortened_commit_hash}\".\n$") # fmt: off + ambiguous_shortened_commit_hash_error = re.compile(f"^ ERROR conventional_commits_linter > Could not find a reference with the name \"{shortened_commit_hash}\".\n\nCaused by:\n Ambiguous short commit hash, the commit hashes [[]({shortened_commit_hash}[a-f0-9]*(, )?)*[]] all start with the provided short commit hash \"{shortened_commit_hash}\".\n$") # fmt: off # When/Then result = assert_linting_fails(context) diff --git a/end-to-end-tests/features/steps/utilities.py b/end-to-end-tests/features/steps/utilities.py index 95addd4..5d86037 100644 --- a/end-to-end-tests/features/steps/utilities.py +++ b/end-to-end-tests/features/steps/utilities.py @@ -29,7 +29,8 @@ def execute_conventional_commits_linter(context): result = execute_command( context.pre_command + context.conventional_commits_linter_path + - context.arguments) + context.arguments + + context.from_ref) if "GIT_DIR" not in os.environ: os.chdir(context.behave_directory) diff --git a/end-to-end-tests/features/steps/when.py b/end-to-end-tests/features/steps/when.py index 65c86ec..e6c02e2 100644 --- a/end-to-end-tests/features/steps/when.py +++ b/end-to-end-tests/features/steps/when.py @@ -2,29 +2,23 @@ from behave import * -@when( - 'the flag --from-stdin is set and the standard input is "{standard_input}".') -def set_from_stdin(context, standard_input): - context.standard_input = standard_input.strip() - context.pre_command = f"echo -e {context.standard_input} | " - context.arguments += " --from-stdin " +@when('linting the "{commit_message}".') +def set_linting_the(context, commit_message): + context.commit_message = commit_message.strip() + context.pre_command = f"echo -e {context.commit_message} | " + context.from_ref = " \"-\"" # Testing we use stdin when not in a Git repository. # https://gitlab.com/DeveloperC/conventional_commits_linter/-/issues/3 context.remote_repository_cache = tempfile.mkdtemp() -@when('the argument --from-reference is provided as "{from_reference}".') -def set_from_reference(context, from_reference): - context.arguments += f" --from-reference {from_reference} " - - -@when('the argument --from-commit-hash is provided as "{from_commit_hash}".') -def set_from_commit_hash(context, from_commit_hash): - context.arguments += f" --from-commit-hash {from_commit_hash} " +@when('linting from the "{git}".') +def set_linting_from_the(context, git): + context.from_ref = f" \"{git}\"" @when('the argument --history-mode is provided as "{history_mode}".') -def set_batch_commits_flag(context, history_mode): +def set_history_mode(context, history_mode): context.arguments += f" --history-mode {history_mode} " diff --git a/end-to-end-tests/features/violations/angular_type_only.feature b/end-to-end-tests/features/violations/angular_type_only.feature index b221bfa..1e732d9 100644 --- a/end-to-end-tests/features/violations/angular_type_only.feature +++ b/end-to-end-tests/features/violations/angular_type_only.feature @@ -3,7 +3,7 @@ Feature: With the allow Angular types only flag, non-Angular types are picked up Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". Then the linting passes. When the flag --allow-angular-type-only is set. And the argument --output is set as "JSON". @@ -11,7 +11,7 @@ Feature: With the allow Angular types only flag, non-Angular types are picked up Examples: - | standard_input | + | commit_message | | "lint: clean up mutex returns\n\n" | | "composer: updated the packages" | | "major: release v3 (merge #51)\n\n" | @@ -19,12 +19,12 @@ Feature: With the allow Angular types only flag, non-Angular types are picked up Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". And the flag --allow-angular-type-only is set. Then the linting passes. Examples: - | standard_input | + | commit_message | | "chore: update dependencies\n" | | "chore(release): 6.5.0" | diff --git a/end-to-end-tests/features/violations/empty_scope.feature b/end-to-end-tests/features/violations/empty_scope.feature index 2d71c52..c059bda 100644 --- a/end-to-end-tests/features/violations/empty_scope.feature +++ b/end-to-end-tests/features/violations/empty_scope.feature @@ -3,13 +3,13 @@ Feature: When the commit title has a scope which is empty, it is picked up as Co Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". And the argument --output is set as "JSON". Then has a scope which is empty violation is detected. Examples: - | standard_input | + | commit_message | | "test(): adding stdin scenario variations" | | "doc( ): webpack example (#1436)" | | "chore(): 14.2.0" | diff --git a/end-to-end-tests/features/violations/exclamation_mark_before_scope.feature b/end-to-end-tests/features/violations/exclamation_mark_before_scope.feature index 8a757cb..e6673f1 100644 --- a/end-to-end-tests/features/violations/exclamation_mark_before_scope.feature +++ b/end-to-end-tests/features/violations/exclamation_mark_before_scope.feature @@ -3,12 +3,12 @@ Feature: When the commit title has a exclamation mark before the scope, it is pi Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". And the argument --output is set as "JSON". Then has a exclamation mark before the scope violation is detected. Examples: - | standard_input | + | commit_message | | "chore!(release): v12.6.0\n\n" | | "feat!(api): send an email to the customer when a product is shipped" | diff --git a/end-to-end-tests/features/violations/no_description_after_type_and_scope.feature b/end-to-end-tests/features/violations/no_description_after_type_and_scope.feature index 9553cc2..f9922f8 100644 --- a/end-to-end-tests/features/violations/no_description_after_type_and_scope.feature +++ b/end-to-end-tests/features/violations/no_description_after_type_and_scope.feature @@ -3,13 +3,13 @@ Feature: When the commit title has no description after the Conventional Commits Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". And the argument --output is set as "JSON". Then has no description after the type and scope violation is detected. Examples: - | standard_input | + | commit_message | | "test: \n\nCo-authored-by: Renovate Bot \n\n" | | "doc(README): \n\n" | | "chore(release)!: \n\n" | diff --git a/end-to-end-tests/features/violations/no_space_after_colon_preceding_type_and_scope.feature b/end-to-end-tests/features/violations/no_space_after_colon_preceding_type_and_scope.feature index c66cc5e..020bdb6 100644 --- a/end-to-end-tests/features/violations/no_space_after_colon_preceding_type_and_scope.feature +++ b/end-to-end-tests/features/violations/no_space_after_colon_preceding_type_and_scope.feature @@ -3,13 +3,13 @@ Feature: When the commit title has no space after the colon preceding the Conven Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". And the argument --output is set as "JSON". Then has no space after the colon preceding the type and scope violation is detected. Examples: - | standard_input | + | commit_message | | "chore:🌐 fix chinese translations" | | "docs:提亀文仢" | | "fix(deps):update dependency gitmojis to v3" | diff --git a/end-to-end-tests/features/violations/preceding_whitespace.feature b/end-to-end-tests/features/violations/preceding_whitespace.feature index 7077f34..6a8cd0d 100644 --- a/end-to-end-tests/features/violations/preceding_whitespace.feature +++ b/end-to-end-tests/features/violations/preceding_whitespace.feature @@ -3,13 +3,13 @@ Feature: When the commit title has preceding whitespace characters, it is picked Scenario Outline: Given the context and environment are reset. - When the flag --from-stdin is set and the standard input is "". + When linting the "". And the argument --output is set as "JSON". Then has preceding whitespace characters violation is detected. Examples: - | standard_input | + | commit_message | | " docs: 提亀文仢" | | " fix(deps): update dependency gitmojis to v3\n\n" | | "\trelease: 2.1.0 release\n\nCo-authored-by: Renovate Bot \n\n" | diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 7da297b..dde30c4 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,33 +1,8 @@ -use clap::{ArgGroup, Parser}; +use clap::Parser; #[derive(Parser, Debug)] #[command(version, about)] -#[command(group( - ArgGroup::new("from") - .required(true) - ))] pub(crate) struct Arguments { - #[arg( - group = "from", - long, - help = "Read the standard input and lint the input as a Git commit message." - )] - pub(crate) from_stdin: bool, - - #[arg( - group = "from", - long, - help = "The Git reference from where to start taking the range of commits from till HEAD to lint. The range is inclusive of HEAD and exclusive of the provided reference." - )] - pub(crate) from_reference: Option, - - #[arg( - group = "from", - long, - help = "The Git commit hash from where to take the range of commits from till HEAD to lint. The range is inclusive of HEAD and exclusive of the provided commit hash." - )] - pub(crate) from_commit_hash: Option, - #[arg( long, default_value = "first", @@ -47,4 +22,9 @@ pub(crate) struct Arguments { help = "Specifies the format for outputting results, acceptable values are quiet, pretty, and json." )] pub(crate) output: crate::output::Output, + + #[arg( + help = "The Git reference from where to start taking the range of commits from till HEAD to lint. The range is inclusive of HEAD and exclusive of the provided reference. '-' indicates to read the standard input and lint the input as a Git commit message." + )] + pub(crate) from: String, } diff --git a/src/commits/mod.rs b/src/commits/mod.rs index 8e1ce05..1ae49ec 100644 --- a/src/commits/mod.rs +++ b/src/commits/mod.rs @@ -31,26 +31,16 @@ impl Commits { } } - pub fn from_reference>( + pub fn from_git>( repository: &Repository, - reference: T, + git: T, history_mode: HistoryMode, ) -> Result { - let reference_oid = get_reference_oid(repository, reference.as_ref())?; - let commits = get_commits_till_head_from_oid(repository, reference_oid, history_mode)?; - Ok(Commits { - commits, - source: Source::Git, - }) - } + let oid = parse_to_oid(repository, git.as_ref()).or_else(|error| { + get_reference_oid(repository, git.as_ref()).map_err(|e| error.context(e)) + })?; - pub fn from_commit_hash>( - repository: &Repository, - commit_hash: T, - history_mode: HistoryMode, - ) -> Result { - let commit_oid = parse_to_oid(repository, commit_hash.as_ref())?; - let commits = get_commits_till_head_from_oid(repository, commit_oid, history_mode)?; + let commits = get_commits_till_head_from_oid(repository, oid, history_mode)?; Ok(Commits { commits, source: Source::Git, @@ -155,10 +145,7 @@ fn parse_to_oid(repository: &Repository, oid: &str) -> Result { None } - Err(error) => { - error!("{:?}", error); - None - } + Err(_) => None, }) .collect(); diff --git a/src/main.rs b/src/main.rs index 9145bf4..1d3091c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate pretty_env_logger; use std::io::{stdin, Read}; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result}; use clap::Parser; use git2::Repository; @@ -42,30 +42,14 @@ fn main() { } fn run(arguments: Arguments) -> Result { - let commits = match ( - arguments.from_stdin, - arguments.from_commit_hash, - arguments.from_reference, - ) { - (true, None, None) => { - let mut commit_message = String::new(); - stdin().read_to_string(&mut commit_message).unwrap(); - - Ok(Commits::from_commit_message(commit_message)) - } - (false, Some(from_commit_hash), None) => { - let repository = - Repository::open_from_env().context("Unable to open the Git repository.")?; - Commits::from_commit_hash(&repository, from_commit_hash, arguments.history_mode) - } - (false, None, Some(from_reference)) => { - let repository = - Repository::open_from_env().context("Unable to open the Git repository.")?; - Commits::from_reference(&repository, from_reference, arguments.history_mode) - } - (_, _, _) => { - bail!("Invalid combination of from arguments."); - } + let commits = if arguments.from == "-" { + let mut commit_message = String::new(); + stdin().read_to_string(&mut commit_message).unwrap(); + Ok(Commits::from_commit_message(commit_message)) + } else { + let repository = + Repository::open_from_env().context("Unable to open the Git repository.")?; + Commits::from_git(&repository, arguments.from, arguments.history_mode) }?; if let Some(linting_results) = commits.lint(arguments.allow_angular_type_only) {