Skip to content

Commit 8ba8d66

Browse files
Make is_stdout work when sys.stdout does not have a fd
1 parent 407ef72 commit 8ba8d66

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

airflow-core/src/airflow/cli/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ def is_stdout(fileio: IOBase) -> bool:
4444
with argparse.FileType points to stdout (by setting the path to ``-``). This
4545
is why there is no equivalent for stderr; argparse does not allow using it.
4646
47-
.. warning:: *fileio* must be open for this check to be successful.
4847
"""
49-
return fileio.fileno() == sys.stdout.fileno()
48+
return fileio is sys.stdout
5049

5150

5251
def print_export_output(command_type: str, exported_items: Collection, file: TextIOWrapper):
53-
if not file.closed and is_stdout(file):
52+
if is_stdout(file):
5453
print(f"\n{len(exported_items)} {command_type} successfully exported.", file=sys.stderr)
5554
else:
5655
print(f"{len(exported_items)} {command_type} successfully exported to {file.name}.")

airflow-core/tests/unit/cli/commands/test_connection_command.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
from __future__ import annotations
1818

19+
import io
1920
import json
2021
import os
2122
import re
@@ -333,6 +334,37 @@ def test_cli_connections_export_should_force_export_as_specified_format(self, tm
333334
}
334335
assert json.loads(output_filepath.read_text()) == expected_connections
335336

337+
def test_cli_connections_export_should_work_when_stdout_is_not_a_real_fd(self, tmp_path):
338+
class FakeFileStringIO(io.StringIO):
339+
"""
340+
Buffer the contents of a StringIO
341+
to make them accessible after close
342+
"""
343+
def __init__(self):
344+
super().__init__()
345+
self.content = ""
346+
347+
def write(self, s: str) -> int:
348+
self.content += s
349+
return len(s)
350+
351+
tmp_stdout = FakeFileStringIO()
352+
with redirect_stdout(tmp_stdout):
353+
args = self.parser.parse_args(
354+
[
355+
"connections",
356+
"export",
357+
"--format",
358+
"env",
359+
"-",
360+
]
361+
)
362+
connection_command.connections_export(args)
363+
assert tmp_stdout.content.splitlines() == [
364+
'airflow_db=mysql://root:plainpassword@mysql/airflow',
365+
'druid_broker_default=druid://druid-broker:8082/?endpoint=druid%2Fv2%2Fsql',
366+
]
367+
336368

337369
TEST_URL = "postgresql://airflow:airflow@host:5432/airflow"
338370
TEST_JSON = json.dumps(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy