mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-10 12:47:04 +00:00
- Change --log flag to require explicit log directory (--log <LOG_DIR>) - Automatically create log directory with parents=True - Harden permissions best-effort (0700) - Update CLI help text accordingly - Adjust Makefile test targets to pass explicit log directory - Ignore logs/ via .gitignore and .dockerignore - Refactor flag parsing to correctly detect deploy with --log - Add dedicated logging unit tests - Update existing tests for new log semantics - Remove deprecated -i alias for --id in deploy command https://chatgpt.com/share/6969680a-dbf8-800f-aafb-3d32d6212ba0
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from cli.core.run import RunConfig, open_log_file, run_command_once
|
|
|
|
|
|
class TestRun(unittest.TestCase):
|
|
def test_open_log_file_creates_in_given_dir(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
log_dir = Path(td) / "logs"
|
|
|
|
f, path = open_log_file(log_dir)
|
|
try:
|
|
self.assertTrue(path.exists())
|
|
self.assertIn(log_dir, path.parents)
|
|
finally:
|
|
f.close()
|
|
|
|
@patch("cli.core.run.subprocess.Popen")
|
|
@patch("cli.core.run.failure_with_warning_loop", side_effect=SystemExit(1))
|
|
def test_run_command_once_nonzero_exits(self, mock_alarm, mock_popen):
|
|
proc = unittest.mock.Mock()
|
|
proc.wait.return_value = None
|
|
proc.returncode = 2
|
|
mock_popen.return_value = proc
|
|
|
|
cfg = RunConfig(
|
|
no_signal=True, sound_enabled=False, alarm_timeout=1, log_enabled=False
|
|
)
|
|
|
|
with self.assertRaises(SystemExit):
|
|
run_command_once(["python", "-c", "print(1)"], cfg, log_file=None)
|
|
|
|
mock_alarm.assert_called_once()
|
|
|
|
@patch("cli.core.run.subprocess.Popen")
|
|
def test_run_command_once_success_returns_true(self, mock_popen):
|
|
proc = unittest.mock.Mock()
|
|
proc.wait.return_value = None
|
|
proc.returncode = 0
|
|
mock_popen.return_value = proc
|
|
|
|
cfg = RunConfig(
|
|
no_signal=True, sound_enabled=False, alarm_timeout=1, log_enabled=False
|
|
)
|
|
|
|
ok = run_command_once(["python", "-c", "print(1)"], cfg, log_file=None)
|
|
self.assertTrue(ok)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|