Content-Length: 371348 | pFad | http://github.com/apache/airflow/commit/a4b30593ee1a3aa62d4028f64b9205902f3dad24

6F Make is_stdout work when sys.stdout does not have a fd · apache/airflow@a4b3059 · GitHub
Skip to content

Commit a4b3059

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

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-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: 33 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,38 @@ 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+
344+
def __init__(self):
345+
super().__init__()
346+
self.content = ""
347+
348+
def write(self, s: str) -> int:
349+
self.content += s
350+
return len(s)
351+
352+
tmp_stdout = FakeFileStringIO()
353+
with redirect_stdout(tmp_stdout):
354+
args = self.parser.parse_args(
355+
[
356+
"connections",
357+
"export",
358+
"--format",
359+
"env",
360+
"-",
361+
]
362+
)
363+
connection_command.connections_export(args)
364+
assert tmp_stdout.content.splitlines() == [
365+
"airflow_db=mysql://root:plainpassword@mysql/airflow",
366+
"druid_broker_default=druid://druid-broker:8082/?endpoint=druid%2Fv2%2Fsql",
367+
]
368+
336369

337370
TEST_URL = "postgresql://airflow:airflow@host:5432/airflow"
338371
TEST_JSON = json.dumps(

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/apache/airflow/commit/a4b30593ee1a3aa62d4028f64b9205902f3dad24

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy