Improve MakefileInstaller: only run 'make install' if an install target exists

- Added parsing of Makefile to detect real 'install' target
- Added safe fallback when Makefile cannot be read
- Added informative skip message when install target is missing
- Updated unit tests to cover: install present, install missing, file read
- Removed circular self-import causing test import failures

Conversation reference: https://chatgpt.com/share/6936a2bf-6aa4-800f-a1bc-7c8eac66bc18
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-08 11:05:08 +01:00
parent 26ba3c50cd
commit 775c30149c
2 changed files with 97 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
import os
import unittest
from unittest.mock import patch
from unittest.mock import patch, mock_open
from pkgmgr.context import RepoContext
from pkgmgr.installers.makefile import MakefileInstaller
@@ -36,9 +36,25 @@ class TestMakefileInstaller(unittest.TestCase):
self.assertFalse(self.installer.supports(self.ctx))
@patch("pkgmgr.installers.makefile.run_command")
@patch(
"builtins.open",
new_callable=mock_open,
read_data="install:\n\t@echo 'installing'\n",
)
@patch("os.path.exists", return_value=True)
def test_run_executes_make_install(self, mock_exists, mock_run_command):
def test_run_executes_make_install_when_target_present(
self, mock_exists, mock_file, mock_run_command
):
self.installer.run(self.ctx)
# Ensure we checked the Makefile and then called make install.
mock_file.assert_called_once_with(
os.path.join(self.ctx.repo_dir, "Makefile"),
"r",
encoding="utf-8",
errors="ignore",
)
cmd = mock_run_command.call_args[0][0]
self.assertEqual(cmd, "make install")
self.assertEqual(
@@ -46,6 +62,27 @@ class TestMakefileInstaller(unittest.TestCase):
self.ctx.repo_dir,
)
@patch("pkgmgr.installers.makefile.run_command")
@patch(
"builtins.open",
new_callable=mock_open,
read_data="all:\n\t@echo 'nothing here'\n",
)
@patch("os.path.exists", return_value=True)
def test_run_skips_when_no_install_target(
self, mock_exists, mock_file, mock_run_command
):
self.installer.run(self.ctx)
# We should have read the Makefile, but not called run_command().
mock_file.assert_called_once_with(
os.path.join(self.ctx.repo_dir, "Makefile"),
"r",
encoding="utf-8",
errors="ignore",
)
mock_run_command.assert_not_called()
if __name__ == "__main__":
unittest.main()