Skip to content

Commit f777673

Browse files
CarliJoyalexanderankin
authored andcommitted
fix(doctests): Ensure paths are correct
1 parent 6336d25 commit f777673

10 files changed

Lines changed: 26 additions & 12 deletions

File tree

docs/community/generic.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ FastAPI container that is using :code:`ServerContainer`
1111
>>> from testcontainers.core.waiting_utils import wait_for_logs
1212
>>> from testcontainers.core.image import DockerImage
1313

14-
>>> with DockerImage(path="./tests/community/generic/samples/fastapi", tag="fastapi-test:latest") as image:
14+
>>> with DockerImage(path=f"{TEST_DIR}/community/generic/samples/fastapi", tag="fastapi-test:latest") as image:
1515
... with ServerContainer(port=80, image=image) as fastapi_server:
1616
... delay = wait_for_logs(fastapi_server, "Uvicorn running on http://0.0.0.0:80")
1717
... fastapi_server.get_api_url = lambda: fastapi_server._create_connection_url() + "/api/v1/"
@@ -31,7 +31,7 @@ A more advance use-case, where we are using a FastAPI container that is using Re
3131
... redis_container_port = redis.port
3232
... redis_container_ip_address = redis.get_docker_client().bridge_ip(redis._container.id)
3333

34-
... with DockerImage(path="./tests/community/generic/samples/advance_1", tag="advance-1:latest") as image:
34+
... with DockerImage(path=f"{TEST_DIR}/community/generic/samples/advance_1", tag="advance-1:latest") as image:
3535
... web_server = ServerContainer(port=80, image=image)
3636
... web_server.with_env(key="REDIS_HOST", value=redis_container_ip_address)
3737
... web_server.with_env(key="REDIS_PORT", value=redis_container_port)

docs/conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
# import sys
1919
# sys.path.insert(0, os.path.abspath('.'))
2020

21+
from pathlib import Path
22+
23+
BASE_DIR = Path(__file__).parent.parent.resolve()
24+
TEST_DIR = BASE_DIR / "tests"
2125

2226
# -- General configuration ------------------------------------------------
2327

@@ -170,3 +174,5 @@
170174
("py:class", "docker.models.containers.ExecResult"),
171175
("py:class", "testcontainers.core.docker_client.ContainerInspectInfo"),
172176
]
177+
178+
doctest_global_setup = f"TEST_DIR = '{TEST_DIR}'"

docs/core/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Using `DockerContainer` and `DockerImage` to create a container:
4545
>>> from testcontainers.core.waiting_utils import wait_for_logs
4646
>>> from testcontainers.core.image import DockerImage
4747

48-
>>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-sample:latest") as image:
48+
>>> with DockerImage(path=f"{TEST_DIR}/core/image_fixtures/sample/", tag="test-sample:latest") as image:
4949
... with DockerContainer(str(image)) as container:
5050
... delay = wait_for_logs(container, "Test Sample Image")
5151

doctests/conf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
from pathlib import Path
2+
3+
BASE_DIR = Path(__file__).parent.parent.resolve()
4+
TEST_DIR = BASE_DIR / "tests"
5+
16
extensions = [
27
"sphinx.ext.autodoc",
38
"sphinx.ext.doctest",
49
]
510
master_doc = "README"
11+
12+
doctest_global_setup = f"TEST_DIR = '{TEST_DIR}'"

src/testcontainers/community/aws/aws_lambda.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AWSLambdaContainer(ServerContainer):
2424
>>> from testcontainers.core.waiting_utils import wait_for_logs
2525
>>> from testcontainers.core.image import DockerImage
2626
27-
>>> with DockerImage(path="./modules/aws/tests/lambda_sample", tag="test-lambda:latest") as image:
27+
>>> with DockerImage(path=f"{TEST_DIR}/community/aws/lambda_sample", tag="test-lambda:latest") as image:
2828
... with AWSLambdaContainer(image=image, port=8080) as func:
2929
... response = func.send_request(data={'payload': 'some data'})
3030
... assert response.status_code == 200

src/testcontainers/community/generic/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ServerContainer(DockerContainer):
2121
>>> from testcontainers.core.waiting_utils import wait_for_logs
2222
>>> from testcontainers.core.image import DockerImage
2323
24-
>>> with DockerImage(path="./modules/generic/tests/samples/python_server", tag="test-srv:latest") as image:
24+
>>> with DockerImage(path=f"{TEST_DIR}/community/generic/samples/python_server", tag="test-srv:latest") as image:
2525
... with ServerContainer(port=9000, image=image) as srv:
2626
... url = srv._create_connection_url()
2727
... response = httpx.get(f"{url}", timeout=5)

src/testcontainers/community/mysql/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MySqlContainer(DbContainer):
3838
>>> import sqlalchemy
3939
>>> from testcontainers.community.mysql import MySqlContainer
4040
41-
>>> with MySqlContainer("mysql:5.7.17", dialect="pymysql") as mysql:
41+
>>> with MySqlContainer("mysql:8.0", dialect="pymysql") as mysql:
4242
... engine = sqlalchemy.create_engine(mysql.get_connection_url())
4343
... with engine.begin() as connection:
4444
... result = connection.execute(sqlalchemy.text("select version()"))
@@ -50,12 +50,13 @@ class MySqlContainer(DbContainer):
5050
automatically.
5151
5252
.. doctest::
53+
5354
>>> import sqlalchemy
5455
>>> from testcontainers.community.mysql import MySqlContainer
55-
>>> with MySqlContainer(seed="../../tests/seeds/") as mysql:
56+
>>> with MySqlContainer(seed=f"{TEST_DIR}/community/mysql/seeds/", dialect="pymysql") as mysql:
5657
... engine = sqlalchemy.create_engine(mysql.get_connection_url())
5758
... with engine.begin() as connection:
58-
... query = "select * from stuff" # Can now rely on schema/data
59+
... query = "select name from stuff"
5960
... result = connection.execute(sqlalchemy.text(query))
6061
... first_stuff, = result.fetchone()
6162

src/testcontainers/community/sftp/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class SFTPContainer(DockerContainer):
199199
... host_port = sftp_container.get_exposed_sftp_port()
200200
... ssh = paramiko.SSHClient()
201201
... ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
202-
... ssh.connect(host_ip, host_port, "basic", "password")
202+
... ssh.connect(host_ip, host_port, "basic", "password", allow_agent=False, look_for_keys=False)
203203
... # ssh.get(...)
204204
... # ssh.listdir()
205205
... # ssh.chdir("upload")
@@ -219,7 +219,7 @@ class SFTPContainer(DockerContainer):
219219
... ssh = paramiko.SSHClient()
220220
... ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
221221
... private_key_file = sftp_container.users[1].private_key_file
222-
... ssh.connect(host_ip, host_port, "keypair", key_filename=private_key_file)
222+
... ssh.connect(host_ip, host_port, "keypair", key_filename=private_key_file, allow_agent=False)
223223
... # ssh.listdir()
224224
... # ssh.get(...)
225225
... # ssh.chdir("upload")

src/testcontainers/compose/compose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class DockerCompose:
232232
233233
>>> from testcontainers.compose import DockerCompose
234234
235-
>>> compose = DockerCompose("core/tests/compose_fixtures/basic", compose_file_name="hello.yaml",
235+
>>> compose = DockerCompose(f"{TEST_DIR}/core/compose_fixtures/basic", compose_file_name="hello.yaml",
236236
... pull=True)
237237
>>> with compose:
238238
... stdout, stderr = compose.get_logs()

src/testcontainers/core/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DockerImage:
2727
2828
>>> from testcontainers.core.image import DockerImage
2929
30-
>>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-image") as image:
30+
>>> with DockerImage(path=f"{TEST_DIR}/core/image_fixtures/sample/", tag="test-image") as image:
3131
... logs = image.get_logs()
3232
3333
:param path: Path to the build context

0 commit comments

Comments
 (0)