Skip to content

Commit 1a0cbd8

Browse files
fix: execute_utility2 is corrected (log)
1) It truncates log file 2) It writes ["sh", "-c", "echo BBB"] as "sh-cecho BBB"
1 parent 840f9e0 commit 1a0cbd8

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

src/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,19 @@ def execute_utility2(
117117
# write new log entry if possible
118118
if logfile:
119119
try:
120-
os_ops.write(filename=logfile, data=args, truncate=True)
120+
os_ops.write(
121+
filename=logfile,
122+
data=os_ops.join_command_arguments(args),
123+
truncate=False,
124+
)
121125
if out:
122126
# comment-out lines
123127
lines = [u'\n'] + ['# ' + line for line in out.splitlines()] + [u'\n']
124-
os_ops.write(filename=logfile, data=lines)
128+
os_ops.write(
129+
filename=logfile,
130+
data=lines,
131+
truncate=False,
132+
)
125133
except IOError:
126134
raise ExecUtilException(
127135
"Problem with writing to logfile `{}` during run command `{}`".format(logfile, args))

tests/test_utils.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from src.utils import parse_pg_version
66
from src.utils import get_pg_config2
7+
from src.utils import execute_utility2
78
from src import scoped_config
89

910
import pytest
@@ -64,3 +65,55 @@ def test_get_pg_config2(self, os_ops: OsOperations):
6465
a = get_pg_config2(os_ops, None)
6566
b = get_pg_config2(os_ops, None)
6667
assert (id(a) != id(b))
68+
69+
def test_execute_utility2__log(self, os_ops: OsOperations):
70+
assert isinstance(os_ops, OsOperations)
71+
72+
log_file: typing.Optional[str] = None
73+
74+
try:
75+
C_OUT_DATA = "AAAA"
76+
77+
log_file = os_ops.mkstemp("testgres--")
78+
assert os_ops.path_exists(log_file)
79+
80+
os_ops.write(
81+
log_file,
82+
C_OUT_DATA + "\n",
83+
truncate=False,
84+
binary=False,
85+
)
86+
87+
cmd = ["sh", "-c", "echo BBBB"]
88+
89+
execute_utility2(
90+
os_ops,
91+
cmd,
92+
logfile=log_file,
93+
)
94+
95+
assert os_ops.path_exists(log_file)
96+
97+
log_content = os_ops.read(
98+
log_file,
99+
binary=False,
100+
)
101+
102+
expected_content_lines = [
103+
C_OUT_DATA,
104+
"sh -c 'echo BBBB'",
105+
"# BBBB",
106+
"",
107+
]
108+
109+
expected_content_s = "\n".join(expected_content_lines)
110+
111+
assert log_content == expected_content_s
112+
finally:
113+
if log_file is not None:
114+
assert type(log_file) is str
115+
os_ops.remove_file(log_file)
116+
117+
assert type(log_file) is str
118+
assert not os_ops.path_exists(log_file)
119+
return

0 commit comments

Comments
 (0)