-
Notifications
You must be signed in to change notification settings - Fork 76
feat(dev): Add Git pre-commit hooks [gapic-generator-python] #908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7602aed
feat(tests): Add integration test framework, goldens for 4 APIs
miraleung df63cf7
fix: exclude generated sources from style checks
miraleung 91f98a4
fix: add integration tests to CI
miraleung 1ff6af1
fix: split out integration tests
miraleung f212c81
fix: bazel install
miraleung 1050c84
fix: Use 3.8 only for integration tests
miraleung 59d5b1d
Merge branch 'master' into feat/goldens
miraleung 0c9c1a3
fix: add integration test steps to docs
miraleung ddc6aba
feat(dev): Add Git pre-commit hooks
miraleung 3c897a8
fix: use unit-3.9 instead
miraleung 016d87c
Merge branch 'master' into dev/git_setup
miraleung 294a942
Merge branch 'master' into dev/git_setup
miraleung 1024c1d
fix: add license header to pre-commit
miraleung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Pre-commit Git checks. | ||
| # Set up: | ||
| # ln -s .githooks/pre-commit .git/hooks/pre-commit | ||
|
|
||
| # Constants. | ||
| BOLD="\e[1m" | ||
| UNSET="\e[0m" | ||
| WHITE="\e[97m" | ||
| RED="\e[91m" | ||
| BACK_MAGENTA="\e[45m" | ||
| BACK_BLUE="\e[44m" | ||
| BACK_RED="\e[41m" | ||
| BACK_GREEN="\e[42m" | ||
|
|
||
| # Methods. | ||
| function echo_error { | ||
| ERR_MSG=$1 | ||
| HELP_MSG=$2 | ||
| echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED Changes NOT committed. $UNSET" | ||
| echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED $WHITE $ERR_MSG $BACK_BLUE $HELP_MSG $UNSET" | ||
| } | ||
|
|
||
| function echo_status { | ||
| STATUS_MSG=$1 | ||
| echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $STATUS_MSG $UNSET" | ||
| } | ||
|
|
||
| function echo_success { | ||
| echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_GREEN $WHITE SUCCESS. $UNSET All checks passed!" | ||
| } | ||
|
|
||
| function header_check_preparation { | ||
| echo_status "Setting up license check environment" | ||
| export GOPATH=$(go env GOPATH) | ||
| if [ $? -ne 0 ]; | ||
| then | ||
| echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install." | ||
| else | ||
| export ENV_PATH=$(echo $PATH) | ||
| if [[ $ENV_PATH != *$GOPATH* ]]; | ||
| then | ||
| echo_status "GOPATH is not in the system path, adding it now." | ||
| export PATH=$GOPATH/bin:$PATH | ||
| fi | ||
| which addlicense | ||
| if [ $? -ne 0 ]; | ||
| then | ||
| echo_status "addlicense tool is not yet installed, downloading it now." | ||
| go get -u github.com/google/addlicense | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| # Disk cache. | ||
| BAZEL_CACHE_DIR=/tmp/bazel_cache_gapic_generator_java | ||
| if [ ! -d $BAZEL_CACHE_DIR ] | ||
| then | ||
| mkdir $BAZEL_CACHE_DIR | ||
| fi | ||
|
|
||
| # Check only the staged files. | ||
| NUM_TOTAL_FILES_CHANGED=$(git diff --cached --name-only | wc -l) | ||
| NUM_PYTHON_FILES_CHANGED=$(git diff --cached --name-only "*.java" | wc -l) | ||
| NUM_UNIT_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "src/test/*/*.golden" | wc -l) | ||
| NUM_INTEGRATION_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "tests/integration/goldens/*/*.golden" | wc -l) | ||
| NUM_INTEGRATION_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "tests/integration/*/BUILD.bazel" | wc -l) | ||
| NUM_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "*BUILD.bazel" | wc -l) | ||
|
|
||
| if [ $NUM_TOTAL_FILES_CHANGED -le 0 ] | ||
| then | ||
| echo_error "No new files to commit." "" | ||
| exit 1 | ||
| fi | ||
|
|
||
|
|
||
| if [ -x /usr/lib/git-core/google_hook ]; then | ||
| /usr/lib/git-core/google_hook pre-commit "$@" | ||
| fi | ||
|
|
||
| # Check Python format. | ||
| if [ $NUM_PYTHON_FILES_CHANGED -gt 0 ] | ||
| then | ||
| echo_status "Running Python linter..." | ||
| find gapic tests -name "*.py" -not -path 'tests/integration/goldens/*' | xargs autopep8 --diff --exit-code | ||
| FORMAT_STATUS=$? | ||
| if [ $FORMAT_STATUS != 0 ] | ||
| then | ||
| echo_error "Linting failed." "Please try again after running autopep8 on the gapic/ and tests/ directories." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # Check unit tests. | ||
| if [ $NUM_PYTHON_FILES_CHANGED -gt 0 ] || [ $NUM_UNIT_GOLDEN_FILES_CHANGED -gt 0 ] | ||
| then | ||
| echo_status "Checking unit tests..." | ||
| nox -s unit-3.9 | ||
| TEST_STATUS=$? | ||
| if [ $TEST_STATUS != 0 ] | ||
| then | ||
| echo_error "Tests failed." "Please fix them and try again." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # Check integration tests. | ||
| if [ $NUM_PYTHON_FILES_CHANGED -gt 0 ] \ | ||
| || [ $NUM_INTEGRATION_GOLDEN_FILES_CHANGED -gt 0 ] \ | ||
| || [ $NUM_INTEGRATION_BAZEL_FILES_CHANGED -gt 0 ] | ||
| then | ||
| echo_status "Checking integration tests..." | ||
| bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //tests/integration/... | ||
| TEST_STATUS=$? | ||
| if [ $TEST_STATUS != 0 ] | ||
| then | ||
| echo_error "Tests failed." "Please fix them and try again." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # Check and fix Bazel format. | ||
| if [ $NUM_BAZEL_FILES_CHANGED -gt 0 ] | ||
| then | ||
| for FILE in $(find ./ -name BUILD.bazel) | ||
| do | ||
| buildifier --lint=fix $FILE | ||
| done | ||
| fi | ||
|
|
||
| echo_success | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,47 @@ | ||
| # Development | ||
|
|
||
| - Install dependencies with `pip install .` | ||
| - See all nox sessions with `nox -l` | ||
| - Execute unit tests by running one of the sessions prefixed with `unit-` | ||
| - Example: `nox -s unit-3.8` | ||
| - Lint sources by running `autopep8`. | ||
| ## Setup | ||
|
|
||
| 1. Clone this repo. | ||
|
|
||
| 2. Copy the Git pre-commit hooks. This will automatically check the build, run | ||
| tests, and perform linting before each commit. (Symlinks don't seem to work, | ||
| but if you find a way, please add it here!) | ||
|
|
||
| ```sh | ||
| cp .githooks/pre-commit .git/hooks/pre-commit | ||
| ``` | ||
|
|
||
| 3. Install dependencies with `pip install .` | ||
|
|
||
| ## Unit Tests | ||
|
|
||
| Execute unit tests by running one of the sessions prefixed with `unit-`. | ||
|
|
||
| - Example: `nox -s unit-3.8` | ||
| - See all Nox sessions with `nox -l`. | ||
|
|
||
| ## Formatting | ||
|
|
||
| - Lint sources by running `autopep8`. The specific command is the following. | ||
|
|
||
| ``` | ||
| find gapic tests -name "*.py" -not -path 'tests/integration/goldens/*' | xargs autopep8 --diff --exit-code | ||
| ``` | ||
|
|
||
| ## Integration Tests | ||
| - Running tests: `bazel test tests/integration:asset`. See the full list of targets in `tests/integration/BUILD.bazel`. | ||
| - Updating golden files: `bazel run tests/integration:asset_update` | ||
|
|
||
| - Run a single integration test for one API. This generates Python source code | ||
| with the microgenerator and compares them to the golden files in | ||
| `test/integration/goldens/asset`. | ||
|
|
||
| ```sh | ||
| bazel test //test/integration:asset | ||
| ``` | ||
|
|
||
| - Update goldens files. This overwrites the golden files in | ||
| `test/integration/goldens/asset`. | ||
|
|
||
| ```sh | ||
| bazel run //test/integration:asset_update | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.