Skip to content

Commit 1e3e240

Browse files
author
Greg
committed
Added Docker support for building images and running tests with pytest
This commit introduces a new commands in setup.py that allows users to build the Docker image and run pytest tests inside it. The README.md file has been updated to document this new feature, providing clear instructions for users to take advantage of reproducible testing environments. * `python setup.py build_docker` to facilitate Docker image building. * `python setup.py docker_test` for running pytest tests within the Docker container. * Enhanced `README.md` with instructions on how to use the new `python setup.py build_docker` and `python setup.py docker_test` commands. * The new Docker-related setup commands ensure consistent testing environments across different systems. * These additions help maintain uniformity in test execution and assist in the debugging process for developers and contributors.
1 parent 54463d8 commit 1e3e240

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,57 @@ options:
4646
--verbose enable verbose output
4747
```
4848

49+
## Using via Docker Image
50+
51+
### Building the Docker Image
52+
53+
You can build the Docker image for this project by running the following command:
54+
55+
```bash
56+
python setup.py build_docker
57+
```
58+
59+
OR directly from root project directory:
60+
61+
```bash
62+
docker build --file docker/Dockerfile -t grep-ast-image .
63+
```
64+
65+
### Calling grep-ast from Docker image
66+
67+
To grep from current working directory we mount it to docker to give access so paths will work also inside docker, example:
68+
69+
```
70+
docker run -it -v "$(pwd)":/app:ro grep-ast-image str grep_ast/*.py
71+
```
72+
73+
### Using Docker image for pytest tests!
74+
75+
To ensure reproducible testing and ease of debugging, you can run pytest tests inside the Docker image. This is particularly useful when collaborating in an open-source manner.
76+
77+
You can use this docker image e.g. to run pytest:
78+
79+
```bash
80+
python setup.py docker_test
81+
```
82+
83+
OR directly:
84+
85+
```
86+
docker run -it --entrypoint bash grep-ast-image -c 'cd /grep-ast; pwd; pytest'
87+
```
88+
89+
This command will build the Docker image and execute pytest tests inside it.
90+
91+
### If you want to debug Docker image itself
92+
93+
If you want to execute commands inside docker image, overwrite entrypoint with bash and give it commands you want, examples:
94+
95+
```bash
96+
$ docker run -it -v "$(pwd)":/app:ro --entrypoint bash grep-ast-image -c 'cd /app; pwd; ls'
97+
$ docker run -it --entrypoint bash grep-ast-image -c 'cd /grep-ast; pwd; ls'
98+
```
99+
49100
## Examples
50101

51102
Here we search for **"encoding"** in the source to this tool:

docker/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:3.10-slim
2+
COPY . /grep-ast
3+
4+
# Unfortunately to build the multi-arch docker image we need `build-essential` for amd64.
5+
# Apparently py-tree-sitter-languages doesn't have a pre-built binary wheel?
6+
7+
RUN apt-get update && \
8+
apt-get install --no-install-recommends -y build-essential git libportaudio2 && \
9+
rm -rf /var/lib/apt/lists/* && \
10+
pip install pytest && \
11+
pip install --no-cache-dir /grep-ast
12+
WORKDIR /app
13+
ENTRYPOINT ["grep-ast"]

setup.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
2+
import subprocess
23

3-
from setuptools import find_packages, setup
4+
from setuptools import Command, find_packages, setup
45

56
with open("requirements.txt") as f:
67
requirements = f.read().splitlines()
@@ -9,7 +10,61 @@
910
long_description = f.read()
1011
long_description = re.sub(r"\n!\[.*\]\(.*\)", "", long_description)
1112

13+
14+
class BuildDockerImageCommand(Command):
15+
description = "Build Docker image for the project"
16+
user_options = []
17+
18+
def initialize_options(self):
19+
pass
20+
21+
def finalize_options(self):
22+
pass
23+
24+
def run(self):
25+
command = ["docker", "build", "--file", "docker/Dockerfile", "-t", "grep-ast-image", "."]
26+
subprocess.check_call(command)
27+
28+
29+
class DockerTestCommand(Command):
30+
description = "Build Docker image and run pytest inside it"
31+
user_options = []
32+
33+
def initialize_options(self):
34+
pass
35+
36+
def finalize_options(self):
37+
pass
38+
39+
def run(self):
40+
build_command = [
41+
"docker",
42+
"build",
43+
"--file",
44+
"docker/Dockerfile",
45+
"-t",
46+
"grep-ast-image",
47+
".",
48+
]
49+
test_command = [
50+
"docker",
51+
"run",
52+
"-it",
53+
"--entrypoint",
54+
"bash",
55+
"grep-ast-image",
56+
"-c",
57+
"cd /grep-ast; pytest",
58+
]
59+
subprocess.check_call(build_command)
60+
subprocess.check_call(test_command)
61+
62+
1263
setup(
64+
cmdclass={
65+
"build_docker": BuildDockerImageCommand,
66+
"docker_test": DockerTestCommand,
67+
},
1368
name="grep-ast",
1469
version="0.2.4",
1570
description="A tool to grep through the AST of a source file",

0 commit comments

Comments
 (0)