Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/test_blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ def exists(path):
with mock.patch.object(aptUpdater, "AptUpdater", FakeAptUpdater), \
mock.patch("os.path.exists", exists), \
mock.patch.object(sys, "argv", ["mintupdate-cli"] + list(args)), \
contextlib.redirect_stdout(output):
contextlib.redirect_stdout(output), \
mock.patch("sys.exit") as exit:
runpy.run_path(os.path.join(libPath, "mintupdate-cli.py"), run_name="__main__")
exit.assert_called_once_with(0)
return [line.split()[1] for line in output.getvalue().splitlines()]

class TestUpdateList(unittest.TestCase):
Expand Down
59 changes: 59 additions & 0 deletions tests/test_exit_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/python3

# Exit status of mintupdate-cli and mint-refresh-cache.
# Run with: /usr/bin/python3 -m unittest discover -s tests

import os
import runpy
import subprocess
import sys
import unittest
from types import SimpleNamespace
from unittest import mock

from test_blacklist import FakeAptUpdater, libPath, myPath
import aptUpdater

def cli_exit_status(refresh_status, apt_status):
# Runs "mintupdate-cli upgrade --refresh-cache" on the stub packages, with
# mint-refresh-cache and apt-get replaced by the given exit statuses
real_exists = os.path.exists
def exists(path):
return False if path == "/etc/mintupdate.blacklist" else real_exists(path)
FakeAptUpdater.gui_blacklist = []
with mock.patch.object(aptUpdater, "AptUpdater", FakeAptUpdater), \
mock.patch("os.path.exists", exists), \
mock.patch.object(sys, "argv", ["mintupdate-cli", "upgrade", "--refresh-cache"]), \
mock.patch("subprocess.run", return_value=SimpleNamespace(returncode=refresh_status)), \
mock.patch("subprocess.call", return_value=apt_status), \
mock.patch("sys.exit") as exit:
runpy.run_path(os.path.join(libPath, "mintupdate-cli.py"), run_name="__main__")
exit.assert_called_once()
return exit.call_args.args[0]

class TestCli(unittest.TestCase):

def test_success(self):
self.assertEqual(cli_exit_status(0, 0), 0)

def test_apt_get_failed(self):
self.assertEqual(cli_exit_status(0, 100), 100)

def test_refresh_failed(self):
# The upgrade still runs on the old cache, but the failure is reported
self.assertEqual(cli_exit_status(1, 0), 1)

def test_both_failed(self):
self.assertEqual(cli_exit_status(1, 100), 100)

class TestRefreshCache(unittest.TestCase):

@unittest.skipIf(os.getuid() == 0, "needs a regular user")
def test_not_root(self):
script = os.path.join(myPath, "..", "usr", "bin", "mint-refresh-cache")
result = subprocess.run([sys.executable, script], capture_output=True, text=True)
self.assertEqual(result.returncode, 1)
self.assertIn("requires admin privileges", result.stdout)

if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions usr/bin/mint-refresh-cache
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ if __name__ == "__main__":
except Exception as e:
print("Error: mint-refresh-cache could not update the cache.")
print(e)
sys.exit(1)
else:
print("Error: mint-refresh-cache requires admin privileges")
sys.exit(1)
6 changes: 4 additions & 2 deletions usr/lib/linuxmint/mintUpdate/mintupdate-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
parser.add_argument("-v", "--version", action="version", version="__DEB_VERSION__", help="Display the current version")

args = parser.parse_args()
returncode = 0
try:
if args.refresh_cache:
subprocess.run("sudo /usr/bin/mint-refresh-cache", shell=True)
returncode = subprocess.run("sudo /usr/bin/mint-refresh-cache", shell=True).returncode
check = AptUpdater()
check.load_cache()
check.find_changes()
Expand Down Expand Up @@ -81,7 +82,8 @@
arguments.append("--install-recommends")
if args.keep_configuration:
arguments.extend(["--option", "Dpkg::Options::=--force-confold"])
subprocess.call(arguments + packages, env=environment)
returncode = subprocess.call(arguments + packages, env=environment) or returncode
except:
traceback.print_exc()
sys.exit(1)
sys.exit(returncode)
10 changes: 8 additions & 2 deletions usr/lib/linuxmint/mintUpdate/system_automatic_upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
except:
pass

returncode = 1
try:
# Parse options file
arguments = []
Expand All @@ -51,7 +52,7 @@
'--who="Update Manager"', "--what=shutdown", "--mode=block",
"/usr/bin/mintupdate-cli", "upgrade", "--refresh-cache", "--yes"]
cmd.extend(arguments)
subprocess.run(cmd, stdout=log, stderr=log)
returncode = subprocess.run(cmd, stdout=log, stderr=log).returncode

except:
import traceback
Expand All @@ -64,7 +65,12 @@
except:
pass

log.write("-- Automatic Upgrade completed\n")
if returncode == 0:
log.write("-- Automatic Upgrade completed\n")
else:
log.write("-- Automatic Upgrade failed (exit %d)\n" % returncode)
else:
log.write("-- Power supply not connected and battery below %d%%, abort automatic update.\n" % MIN_BATTERY_PERCENTAGE)
returncode = 0
log.close()
sys.exit(returncode)
Loading