1
1
#
2
2
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
3
#
4
+ from __future__ import annotations
4
5
5
6
import logging
7
+ from pathlib import Path
8
+ from typing import TYPE_CHECKING
6
9
7
10
import asyncclick as click
11
+ import asyncer
8
12
from pipelines .cli .click_decorators import click_ignore_unused_kwargs , click_merge_args_into_context_obj
9
13
from pipelines .consts import DOCKER_VERSION
10
14
from pipelines .helpers .utils import sh_dash_c
11
15
from pipelines .models .contexts .click_pipeline_context import ClickPipelineContext , pass_pipeline_context
12
16
17
+ if TYPE_CHECKING :
18
+ from typing import List , Tuple
19
+
20
+ import dagger
21
+
22
+ ## HELPERS
23
+ async def run_poetry_command (container : dagger .Container , command : str ) -> Tuple [str , str ]:
24
+ """Run a poetry command in a container and return the stdout and stderr.
25
+
26
+ Args:
27
+ container (dagger.Container): The container to run the command in.
28
+ command (str): The command to run.
29
+
30
+ Returns:
31
+ Tuple[str, str]: The stdout and stderr of the command.
32
+ """
33
+ container = container .with_exec (["poetry" , "run" , * command .split (" " )])
34
+ return await container .stdout (), await container .stderr ()
35
+
13
36
14
37
@click .command ()
15
38
@click .argument ("poetry_package_path" )
16
- @click .option ("--test-directory" , default = "tests" , help = "The directory containing the tests to run." )
39
+ @click .option (
40
+ "-c" ,
41
+ "--poetry-run-command" ,
42
+ multiple = True ,
43
+ help = "The poetry run command to run." ,
44
+ required = True ,
45
+ )
17
46
@click_merge_args_into_context_obj
18
47
@pass_pipeline_context
19
48
@click_ignore_unused_kwargs
@@ -24,7 +53,10 @@ async def test(pipeline_context: ClickPipelineContext):
24
53
pipeline_context (ClickPipelineContext): The context object.
25
54
"""
26
55
poetry_package_path = pipeline_context .params ["poetry_package_path" ]
27
- test_directory = pipeline_context .params ["test_directory" ]
56
+ if not Path (f"{ poetry_package_path } /pyproject.toml" ).exists ():
57
+ raise click .UsageError (f"Could not find pyproject.toml in { poetry_package_path } " )
58
+
59
+ commands_to_run : List [str ] = pipeline_context .params ["poetry_run_command" ]
28
60
29
61
logger = logging .getLogger (f"{ poetry_package_path } .tests" )
30
62
logger .info (f"Running tests for { poetry_package_path } " )
@@ -47,7 +79,7 @@ async def test(pipeline_context: ClickPipelineContext):
47
79
48
80
pipeline_name = f"Unit tests for { poetry_package_path } "
49
81
dagger_client = await pipeline_context .get_dagger_client (pipeline_name = pipeline_name )
50
- pytest_container = await (
82
+ test_container = await (
51
83
dagger_client .container ()
52
84
.from_ ("python:3.10.12" )
53
85
.with_env_variable ("PIPX_BIN_DIR" , "/usr/local/bin" )
@@ -73,10 +105,20 @@ async def test(pipeline_context: ClickPipelineContext):
73
105
),
74
106
)
75
107
.with_workdir (f"/airbyte/{ poetry_package_path } " )
76
- .with_exec (["poetry" , "install" ])
108
+ .with_exec (["poetry" , "install" , "--with=dev" ])
77
109
.with_unix_socket ("/var/run/docker.sock" , dagger_client .host ().unix_socket ("/var/run/docker.sock" ))
78
110
.with_env_variable ("CI" , str (pipeline_context .params ["is_ci" ]))
79
- .with_exec ([ "poetry" , "run" , "pytest" , test_directory ] )
111
+ .with_workdir ( f"/airbyte/ { poetry_package_path } " )
80
112
)
81
113
82
- await pytest_container
114
+ soon_command_executions_results = []
115
+ async with asyncer .create_task_group () as poetry_commands_task_group :
116
+ for command in commands_to_run :
117
+ logger .info (f"Running command: { command } " )
118
+ soon_command_execution_result = poetry_commands_task_group .soonify (run_poetry_command )(test_container , command )
119
+ soon_command_executions_results .append (soon_command_execution_result )
120
+
121
+ for result in soon_command_executions_results :
122
+ stdout , stderr = result .value
123
+ logger .info (stdout )
124
+ logger .error (stderr )
0 commit comments