Skip to content

Commit 5caee80

Browse files
committed
fix: all output to logs/*
1 parent 556ec4e commit 5caee80

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

bin/start-api.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
kill $(fuser 8888/tcp 2>/dev/null | awk '{ print $1 }')
55

6-
PYTHONPATH=. python api/main.py main:app &
6+
PYTHONPATH=. python api/main.py main:app >logs/error.log 2>&1 &

bin/start-proxy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env sh
22
. lib/functions.sh
33

4-
dcpp up
4+
dcpp up >logs/error.log 2>&1

lib/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ def stream_output(process: subprocess.Popen[Any]) -> None:
99

1010

1111
def run_command(command: List[str], cwd: str = None) -> int:
12-
# pylint: disable=consider-using-with
13-
process = subprocess.Popen(
14-
command,
15-
cwd=cwd,
16-
stdout=subprocess.PIPE,
17-
)
18-
stream_output(process)
19-
process.wait()
12+
with open("logs/error.log", "w", encoding="utf-8") as f:
13+
process = subprocess.run(
14+
command,
15+
check=True,
16+
cwd=cwd,
17+
stdout=f,
18+
stderr=f,
19+
)
2020
return process.returncode

lib/utils_test.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class TestStreamOutput(unittest.TestCase):
1515
# Outputs the process stdout to sys.stdout.buffer.
1616
@mock.patch("subprocess.Popen")
1717
@mock.patch("sys.stdout.buffer.write")
18-
def test_outputs_stdout(self, mock_write: Mock, mock_popen: Mock) -> None:
19-
process = mock_popen.return_value
18+
def test_outputs_stdout(self, mock_write: Mock, mock_run: Mock) -> None:
19+
process = mock_run.return_value
2020
process.stdout.read.side_effect = [b"output1", b"output2", b""]
2121

2222
stream_output(process)
@@ -26,8 +26,8 @@ def test_outputs_stdout(self, mock_write: Mock, mock_popen: Mock) -> None:
2626
# Handles the process stdout stream correctly.
2727
@mock.patch("subprocess.Popen")
2828
@mock.patch("sys.stdout.buffer.write")
29-
def test_handles_stdout_stream(self, mock_write: Mock, mock_popen: Mock) -> None:
30-
process = mock_popen.return_value
29+
def test_handles_stdout_stream(self, mock_write: Mock, mock_run: Mock) -> None:
30+
process = mock_run.return_value
3131
process.stdout.read.side_effect = [b"output1", b"output2", b""]
3232

3333
stream_output(process)
@@ -38,20 +38,25 @@ def test_handles_stdout_stream(self, mock_write: Mock, mock_popen: Mock) -> None
3838
class TestRunCommand(unittest.TestCase):
3939

4040
# Runs command with no errors and returns exit code
41-
@mock.patch("lib.utils.stream_output")
42-
@mock.patch("subprocess.Popen")
43-
def test_runs_command_no_errors(self, mock_popen: Mock, _: Mock) -> None:
41+
@mock.patch("builtins.open", new_callable=mock.mock_open)
42+
@mock.patch("subprocess.run")
43+
def test_runs_command_no_errors(self, mock_run: Mock, mock_open: Mock) -> None:
4444

4545
# Set up mock
46-
mock_process = mock_popen.return_value
46+
mock_process = mock_run.return_value
4747
mock_process.returncode = 0
4848

4949
# Call the function under test
5050
exit_code = run_command(["ls"])
5151

5252
# Assert the exit code is returned correctly
5353
self.assertEqual(exit_code, 0)
54-
mock_popen.assert_called_once_with(["ls"], cwd=None, stdout=subprocess.PIPE)
54+
# Assert the open call was made correctly
55+
mock_open.assert_called_with("logs/error.log", "w", encoding="utf-8")
56+
# Assert the subprocess.run call was made correctly
57+
mock_run.assert_called_once_with(
58+
["ls"], check=True, cwd=None, stdout=mock_open.return_value, stderr=mock_open.return_value
59+
)
5560

5661

5762
if __name__ == "__main__":

0 commit comments

Comments
 (0)