Skip to content

Commit dab6dba

Browse files
committed
style: fix pre-commit lints
This commit fixes pre-commit linting and formatting issues. To avoid refactoring all of the existing Python code (which will is going to be deprecated), the pre-commit configuration has been updated to ignore these Python files. All other changes in this commit are purely stylistic. Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent fbd1fbe commit dab6dba

11 files changed

+173
-280
lines changed

.github/semantic.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
titleAndCommits: true
22
allowMergeCommits: true
3-
4-

.github/workflows/trigger_pact_docs_update.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- master
77
paths:
8-
- '**.md'
8+
- "**.md"
99

1010
jobs:
1111
run:

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ repos:
4141
rev: v0.0.289
4242
hooks:
4343
- id: ruff
44+
# Exclude python files in pact/** and tests/**, except for the
45+
# files in pact/v3/** and tests/v3/**.
46+
exclude: ^(pact|tests)/(?!v3/).*\.py$
4447
args: [--fix, --exit-non-zero-on-fix]
4548
stages: [pre-push]
4649

4750
- repo: https://github.com/psf/black
4851
rev: 23.9.1
4952
hooks:
5053
- id: black
54+
# Exclude python files in pact/** and tests/**, except for the
55+
# files in pact/v3/** and tests/v3/**.
56+
exclude: ^(pact|tests)/(?!v3/).*\.py$
5157
stages: [pre-push]
5258

5359
- repo: https://github.com/commitizen-tools/commitizen

Dockerfile.ubuntu

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ENV DEBIAN_FRONTEND=noninteractive
33
ARG PYTHON_VERSION 3.9
44

55
#Set of all dependencies needed for pyenv to work on Ubuntu
6-
RUN apt-get update \
6+
RUN apt-get update \
77
&& apt-get install -y --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget ca-certificates curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev mecab-ipadic-utf8 git
88

99
# Set-up necessary Env vars for PyEnv

README.md

+100-145
Large diffs are not rendered by default.

RELEASING.md

+32-38
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,53 @@
22

33
## Preparing the release
44

5-
The easiest way is to just run the following command from the root folder with
6-
the HEAD commit on trunk and the appropriate version. We follow
7-
`<MAJOR>.<MINOR>.<PATCH>` versioning.
5+
The easiest way is to just run the following command from the root folder with the HEAD commit on trunk and the appropriate version. We follow `<MAJOR>.<MINOR>.<PATCH>` versioning.
86

9-
```shell
10-
$ script/release_prep.sh X.Y.Z
11-
```
7+
```shell
8+
$ script/release_prep.sh X.Y.Z
9+
```
1210

1311
This script effectively runs the following:
1412

15-
1. Increment the version according to semantic versioning rules in `pact/__version__.py`
13+
1. Increment the version according to semantic versioning rules in `pact/__version__.py`
1614

17-
2. Update the `CHANGELOG.md` using:
18-
```shell
19-
$ git log --pretty=format:' * %h - %s (%an, %ad)' vX.Y.Z..HEAD
20-
```
15+
2. Update the `CHANGELOG.md` using:
2116

22-
3. Add files to git
23-
```shell
24-
$ git add CHANGELOG.md pact/__version__.py
25-
```
17+
```shell
18+
$ git log --pretty=format:' * %h - %s (%an, %ad)' vX.Y.Z..HEAD
19+
```
2620

27-
4. Commit
28-
```shell
29-
$ git commit -m "Releasing version X.Y.Z"
30-
```
21+
3. Add files to git
3122

32-
5. Tag
33-
```shell
34-
$ git tag -a vX.Y.Z -m "Releasing version X.Y.Z"
35-
$ git push origin master --tags
36-
```
23+
```shell
24+
$ git add CHANGELOG.md pact/__version__.py
25+
```
26+
27+
4. Commit
28+
29+
```shell
30+
$ git commit -m "Releasing version X.Y.Z"
31+
```
32+
33+
5. Tag
34+
35+
```shell
36+
$ git tag -a vX.Y.Z -m "Releasing version X.Y.Z"
37+
$ git push origin master --tags
38+
```
3739

3840
## Updating Pact Ruby
3941

40-
To upgrade the versions of `pact-mock_service` and `pact-provider-verifier`, change the
41-
`PACT_STANDALONE_VERSION` in `setup.py` to match the latest version available from the
42-
[pact-ruby-standalone](https://github.com/pact-foundation/pact-ruby-standalone/releases)
43-
repository. Do this before preparing the release.
42+
To upgrade the versions of `pact-mock_service` and `pact-provider-verifier`, change the `PACT_STANDALONE_VERSION` in `setup.py` to match the latest version available from the [pact-ruby-standalone](https://github.com/pact-foundation/pact-ruby-standalone/releases) repository. Do this before preparing the release.
4443

4544
## Publishing to pypi
4645

47-
1. Wait until GitHub Actions have run and the new tag is available at
48-
https://github.com/pact-foundation/pact-python/releases/tag/vX.Y.Z
46+
1. Wait until GitHub Actions have run and the new tag is available at https://github.com/pact-foundation/pact-python/releases/tag/vX.Y.Z
4947

50-
2. Set the title to `pact-python-X.Y.Z`
48+
2. Set the title to `pact-python-X.Y.Z`
5149

52-
3. Save
50+
3. Save
5351

54-
4. Go to GitHub Actions for Pact Python and you should see an 'Upload Python
55-
Package' action blocked for your version.
52+
4. Go to GitHub Actions for Pact Python and you should see an 'Upload Python Package' action blocked for your version.
5653

57-
5. Click this and then 'Review deployments'. Select 'Upload Python Package'
58-
and Approve deploy. If you can't do this you may need an administrator to
59-
give you permissions or do it for you. You should see in Slack #pact-python
60-
that the release has happened. Verify in [pypi](https://pypi.org/project/pact-python/)
54+
5. Click this and then 'Review deployments'. Select 'Upload Python Package' and Approve deploy. If you can't do this you may need an administrator to give you permissions or do it for you. You should see in Slack #pact-python that the release has happened. Verify in [pypi](https://pypi.org/project/pact-python/)

docker/README.md

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
# Introduction
22

3-
This is for contributors who want to make changes and test for all different
4-
versions of python currently supported. If you don't want to set up and install
5-
all the different python versions locally (and there are some difficulties with
6-
that) you can just run them in docker using containers.
3+
This is for contributors who want to make changes and test for all different versions of python currently supported. If you don't want to set up and install all the different python versions locally (and there are some difficulties with that) you can just run them in docker using containers.
74

85
# Setup
96

10-
To build a container say for Python 3.11, change to the root directory of the
11-
project and run:
7+
To build a container say for Python 3.11, change to the root directory of the project and run:
128

139
```bash
1410
(export PY=3.11 && docker build --build-arg PY="$PY" --build-arg TOXPY="$(sed 's/\.//' <<< "$PY")" -t pactfoundation:python${PY} -f docker/Dockerfile .)
1511
```
1612

17-
This uses an Alpine based image (currently 3.17), which is available as of
18-
2023-04 for Python versions 3.7 - 3.11.
13+
This uses an Alpine based image (currently 3.17), which is available as of 2023-04 for Python versions 3.7 - 3.11.
1914

20-
Note: To run tox, the Python version without the '.' is required, i.e. '311'
21-
instead of '3.11', so some manipulation with `sed` is used to remove the '.'
15+
Note: To run tox, the Python version without the '.' is required, i.e. '311' instead of '3.11', so some manipulation with `sed` is used to remove the '.'
2216

23-
To build for Python versions which require a different Alpine image, such as if
24-
trying to build against Python 3.6, an extra `ALPINE` arg can be provided:
17+
To build for Python versions which require a different Alpine image, such as if trying to build against Python 3.6, an extra `ALPINE` arg can be provided:
2518

2619
```bash
2720
(export PY=3.6 && docker build --build-arg PY="$PY" --build-arg TOXPY="$(sed 's/\.//' <<< "$PY")" --build-arg ALPINE=3.15 -t pactfoundation:python${PY} -f docker/Dockerfile .)
@@ -39,16 +32,13 @@ If you need to debug you can change the command to:
3932
docker run -it --rm -v "$(pwd)":/home pactfoundation:python3.11 sh
4033
```
4134

42-
This will open a container with a prompt. From the `/home` location in the
43-
container you can run the same tests manually:
35+
This will open a container with a prompt. From the `/home` location in the container you can run the same tests manually:
4436

4537
```bash
4638
tox -e py311-{test,install}
4739
```
4840

49-
In all the above if you need to run a different version change
50-
`py311`/`python3.11` where appropriate. Or you can run the convenience script
51-
to build:
41+
In all the above if you need to run a different version change `py311`/`python3.11` where appropriate. Or you can run the convenience script to build:
5242

5343
```bash
5444
docker/build.sh 3.11

examples/README.md

+14-63
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
# Examples
22

3-
This directory contains an end-to-end example of using Pact in Python. While
4-
this document and the documentation within the examples themselves are intended
5-
to be mostly self-contained, it is highly recommended that you read the [Pact
6-
Documentation](https://docs.pact.io/) as well.
3+
This directory contains an end-to-end example of using Pact in Python. While this document and the documentation within the examples themselves are intended to be mostly self-contained, it is highly recommended that you read the [Pact Documentation](https://docs.pact.io/) as well.
74

8-
Assuming you have [hatch](https://hatch.pypa.io/latest/) installed, the example
9-
suite can be executed with:
5+
Assuming you have [hatch](https://hatch.pypa.io/latest/) installed, the example suite can be executed with:
106

117
```sh
128
hatch run example
139
```
1410

15-
The code within the examples is intended to be well documented and you are
16-
encouraged to look through the code as well (or submit a PR if anything is
17-
unclear!).
11+
The code within the examples is intended to be well documented and you are encouraged to look through the code as well (or submit a PR if anything is unclear!).
1812

1913
## Overview
2014

21-
Pact is a contract testing tool. Contract testing is a way to ensure that
22-
services (such as an API provider and a client) can communicate with each other.
23-
This example focuses on HTTP interactions, but Pact can be used to test more
24-
general interactions as well such as through message queues.
15+
Pact is a contract testing tool. Contract testing is a way to ensure that services (such as an API provider and a client) can communicate with each other. This example focuses on HTTP interactions, but Pact can be used to test more general interactions as well such as through message queues.
2516

26-
An interaction between a HTTP client (the _consumer_) and a server (the
27-
_provider_) would typically look like this:
17+
An interaction between a HTTP client (the _consumer_) and a server (the _provider_) would typically look like this:
2818

2919
<div align="center">
3020

@@ -40,11 +30,7 @@ sequenceDiagram
4030

4131
</div>
4232

43-
To test this interaction naively would require both the consumer and provider to
44-
be running at the same time. While this is straightforward in the above example,
45-
this quickly becomes impractical as the number of interactions grows between
46-
many microservices. Pact solves this by allowing the consumer and provider to be
47-
tested independently.
33+
To test this interaction naively would require both the consumer and provider to be running at the same time. While this is straightforward in the above example, this quickly becomes impractical as the number of interactions grows between many microservices. Pact solves this by allowing the consumer and provider to be tested independently.
4834

4935
Pact achieves this be mocking the other side of the interaction:
5036

@@ -75,34 +61,20 @@ sequenceDiagram
7561

7662
</div>
7763

78-
In the first stage, the consumer defines a number of interactions in the form
79-
below. Pact sets up a mock server that will respond to the requests as defined
80-
by the consumer. All these interactions, containing both the request and
81-
expected response, are all sent to the Pact Broker.
64+
In the first stage, the consumer defines a number of interactions in the form below. Pact sets up a mock server that will respond to the requests as defined by the consumer. All these interactions, containing both the request and expected response, are all sent to the Pact Broker.
8265

8366
> Given {provider state} \
8467
> Upon receiving {description} \
8568
> With {request} \
8669
> Will respond with {response}
8770
88-
In the second stage, the provider retrieves the interactions from the Pact
89-
Broker. It then sets up a mock client that will make the requests as defined by
90-
the consumer. Pact then verifies that the responses from the provider match the
91-
expected responses defined by the consumer.
71+
In the second stage, the provider retrieves the interactions from the Pact Broker. It then sets up a mock client that will make the requests as defined by the consumer. Pact then verifies that the responses from the provider match the expected responses defined by the consumer.
9272

93-
In this way, Pact is consumer driven and can ensure that the provider is
94-
compatible with the consumer. While this example showcases both sides in Python,
95-
this is absolutely not required. The provider could be written in any language,
96-
and satisfy contracts from a number of consumers all written in different
97-
languages.
73+
In this way, Pact is consumer driven and can ensure that the provider is compatible with the consumer. While this example showcases both sides in Python, this is absolutely not required. The provider could be written in any language, and satisfy contracts from a number of consumers all written in different languages.
9874

9975
### Consumer
10076

101-
The consumer in this example is a simple Python script that makes a HTTP GET
102-
request to a server. It is defined in [`src/consumer.py`](src/consumer.py). The
103-
tests for the consumer are defined in
104-
[`tests/test_00_consumer.py`](tests/test_00_consumer.py). Each interaction is
105-
defined using the format mentioned above. Programmatically, this looks like:
77+
The consumer in this example is a simple Python script that makes a HTTP GET request to a server. It is defined in [`src/consumer.py`](src/consumer.py). The tests for the consumer are defined in [`tests/test_00_consumer.py`](tests/test_00_consumer.py). Each interaction is defined using the format mentioned above. Programmatically, this looks like:
10678

10779
```py
10880
expected: dict[str, Any] = {
@@ -121,17 +93,9 @@ expected: dict[str, Any] = {
12193

12294
### Provider
12395

124-
This example showcases to different providers, one written in Flask and one
125-
written in FastAPI. Both are simple Python web servers that respond to a HTTP
126-
GET request. The Flask provider is defined in [`src/flask.py`](src/flask.py) and
127-
the FastAPI provider is defined in [`src/fastapi.py`](src/fastapi.py). The
128-
tests for the providers are defined in
129-
[`tests/test_01_provider_flask.py`](tests/test_01_provider_flask.py) and
130-
[`tests/test_01_provider_fastapi.py`](tests/test_01_provider_fastapi.py).
96+
This example showcases to different providers, one written in Flask and one written in FastAPI. Both are simple Python web servers that respond to a HTTP GET request. The Flask provider is defined in [`src/flask.py`](src/flask.py) and the FastAPI provider is defined in [`src/fastapi.py`](src/fastapi.py). The tests for the providers are defined in [`tests/test_01_provider_flask.py`](tests/test_01_provider_flask.py) and [`tests/test_01_provider_fastapi.py`](tests/test_01_provider_fastapi.py).
13197

132-
Unlike the consumer side, the provider side is responsible to responding to the
133-
interactions defined by the consumers. In this regard, the provider testing
134-
is rather simple:
98+
Unlike the consumer side, the provider side is responsible to responding to the interactions defined by the consumers. In this regard, the provider testing is rather simple:
13599

136100
```py
137101
code, _ = verifier.verify_with_broker(
@@ -142,21 +106,8 @@ code, _ = verifier.verify_with_broker(
142106
assert code == 0
143107
```
144108

145-
The complication comes from the fact that the provider needs to know what state
146-
to be in before responding to the request. In order to achieve this, a testing
147-
endpoint is defined that sets the state of the provider as defined in the
148-
`provider_states_setup_url` above. For example, the consumer requests has _Given
149-
user 123 exists_ as the provider state, and the provider will need to ensure
150-
that this state is satisfied. This would typically entail setting up a database
151-
with the correct data, but it is advisable to achieve the equivalent state by
152-
mocking the appropriate calls. This has been showcased in both provider
153-
examples.
109+
The complication comes from the fact that the provider needs to know what state to be in before responding to the request. In order to achieve this, a testing endpoint is defined that sets the state of the provider as defined in the `provider_states_setup_url` above. For example, the consumer requests has _Given user 123 exists_ as the provider state, and the provider will need to ensure that this state is satisfied. This would typically entail setting up a database with the correct data, but it is advisable to achieve the equivalent state by mocking the appropriate calls. This has been showcased in both provider examples.
154110

155111
### Broker
156112

157-
The broker acts as the intermediary between these test suites. It stores the
158-
interactions defined by the consumer and makes them available to the provider.
159-
Once the provider has verified that it satisfies all interactions, the broker
160-
also stores the verification results. The example here runs the open source
161-
broker within a Docker container. An alternative is to use the hosted [Pactflow
162-
service](https://pactflow.io).
113+
The broker acts as the intermediary between these test suites. It stores the interactions defined by the consumer and makes them available to the provider. Once the provider has verified that it satisfies all interactions, the broker also stores the verification results. The example here runs the open source broker within a Docker container. An alternative is to use the hosted [Pactflow service](https://pactflow.io).

run-docker.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
for arch in arm64 amd64; do
4-
# for version in 3.6; do
4+
# for version in 3.6; do
55
for version in 3.7 3.8 3.9 3.10 3.11; do
66
docker build -t python-$arch-$version --build-arg PYTHON_VERSION=$version --platform=linux/$arch .
77
docker run -it --rm python-$arch-$version

script/commit_message.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
2+
# ruff: noqa
23
import re
3-
import sys
44
import subprocess
5+
import sys
56

67
examples = """+ 61c8ca9 fix: navbar not responsive on mobile
78
+ 479c48b test: prepared test cases for user authentication
@@ -13,21 +14,21 @@
1314

1415

1516
def main():
16-
1717
cmd_tag = "git describe --abbrev=0"
18-
tag = subprocess.check_output(cmd_tag,
19-
shell=True).decode("utf-8").split('\n')[0]
18+
tag = subprocess.check_output(cmd_tag, shell=True).decode("utf-8").split("\n")[0]
2019

21-
cmd = "git log --pretty=format:'%s' {}..HEAD".format(tag)
20+
cmd = f"git log --pretty=format:'%s' {tag}..HEAD"
2221
commits = subprocess.check_output(cmd, shell=True)
23-
commits = commits.decode("utf-8").split('\n')
22+
commits = commits.decode("utf-8").split("\n")
2423
for commit in commits:
25-
26-
pattern = r'((build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert)(\([\w\-]+\))?:\s.*)|((Merge|Fixed)(\([\w\-]+\))?\s.*)' # noqa
24+
pattern = r"((build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert)(\([\w\-]+\))?:\s.*)|((Merge|Fixed)(\([\w\-]+\))?\s.*)"
2725
m = re.match(pattern, commit)
2826
if m is None:
29-
print("\nError with git message '{}' style".format(commit))
30-
print("\nPlease change commit message to the conventional format and try to commit again. Examples:") # noqa
27+
print(f"\nError with git message '{commit}' style")
28+
print(
29+
"\nPlease change commit message to the conventional format and try to"
30+
" commit again. Examples:",
31+
)
3132

3233
print("\n" + examples)
3334
sys.exit(1)

script/release_prep.sh

-2
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,3 @@ git add CHANGELOG.md pact/__version__.py
2828
git commit -m "chore: Releasing version $VERSION"
2929

3030
git tag -a "$TAG_NAME" -m "Releasing version $VERSION" && git push origin master --tags
31-
32-

0 commit comments

Comments
 (0)