Skip to content

Commit c4f9eee

Browse files
committed
pull in refactoring from downstream pr
1 parent 0cc217e commit c4f9eee

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

airbyte_cdk/cli/airbyte_cdk/_connector.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import rich_click as click
4646

4747
# from airbyte_cdk.test.standard_tests import pytest_hooks
48+
from airbyte_cdk.cli.airbyte_cdk._util import resolve_connector_name_and_directory
4849
from airbyte_cdk.test.standard_tests.test_resources import find_connector_root_from_name
4950
from airbyte_cdk.test.standard_tests.util import create_connector_test_suite
5051

@@ -97,12 +98,12 @@ def connector_cli_group() -> None:
9798
@click.option(
9899
"--connector-name",
99100
type=str,
100-
# help="Name of the connector to test. Ignored if --connector-directory is provided.",
101+
help="Name of the connector to test. Ignored if --connector-directory is provided.",
101102
)
102103
@click.option(
103104
"--connector-directory",
104105
type=click.Path(exists=True, file_okay=False, path_type=Path),
105-
# help="Path to the connector directory.",
106+
help="Path to the connector directory.",
106107
)
107108
@click.option(
108109
"--collect-only",
@@ -129,23 +130,10 @@ def test(
129130
"pytest is not installed. Please install pytest to run the connector tests."
130131
)
131132
click.echo("Connector test command executed.")
132-
if not connector_name and not connector_directory:
133-
cwd = Path().resolve().absolute()
134-
if cwd.name.startswith("source-") or cwd.name.startswith("destination-"):
135-
connector_name = cwd.name
136-
connector_directory = cwd
137-
else:
138-
raise ValueError(
139-
"Either connector_name or connector_directory must be provided if not "
140-
"running from a connector directory."
141-
)
142-
143-
if connector_directory:
144-
connector_directory = connector_directory.resolve().absolute()
145-
elif connector_name:
146-
connector_directory = find_connector_root_from_name(connector_name)
147-
else:
148-
raise ValueError("Either connector_name or connector_directory must be provided.")
133+
connector_name, connector_directory = resolve_connector_name_and_directory(
134+
connector_name=connector_name,
135+
connector_directory=connector_directory,
136+
)
149137

150138
connector_test_suite = create_connector_test_suite(
151139
connector_name=connector_name if not connector_directory else None,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Common utilities for Airbyte CDK CLI."""
2+
3+
from pathlib import Path
4+
5+
from airbyte_cdk.test.standard_tests.test_resources import find_connector_root_from_name
6+
7+
8+
def resolve_connector_name_and_directory(
9+
connector_name: str | None = None,
10+
connector_directory: Path | None = None,
11+
) -> tuple[str, Path]:
12+
"""Resolve the connector name and directory.
13+
14+
This function will resolve the connector name and directory based on the provided
15+
arguments. If no connector name or directory is provided, it will look within the
16+
current working directory. If the current working directory is not a connector
17+
directory (e.g. starting with 'source-') and no connector name or path is provided,
18+
the process will fail.
19+
"""
20+
if not connector_directory:
21+
if connector_name:
22+
connector_directory = find_connector_root_from_name(connector_name)
23+
else:
24+
cwd = Path().resolve().absolute()
25+
if cwd.name.startswith("source-") or cwd.name.startswith("destination-"):
26+
connector_directory = cwd
27+
else:
28+
raise ValueError(
29+
"Either connector_name or connector_directory must be provided if not "
30+
"running from a connector directory."
31+
)
32+
33+
if not connector_name:
34+
connector_name = connector_directory.name
35+
36+
if connector_directory:
37+
connector_directory = connector_directory.resolve().absolute()
38+
elif connector_name:
39+
connector_directory = find_connector_root_from_name(connector_name)
40+
else:
41+
raise ValueError("Either connector_name or connector_directory must be provided.")
42+
43+
return connector_name, connector_directory

0 commit comments

Comments
 (0)