154 lines
6.6 KiB
Python
154 lines
6.6 KiB
Python
from contextlib import redirect_stdout
|
|||
|
|
from importlib.metadata import version
|
||
|
|
from io import StringIO
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
from tempfile import TemporaryDirectory
|
||
|
|
from unittest.mock import patch
|
||
|
|
|
||
|
|
from wallpaper_changer.argument_parser import parse_args
|
||
|
|
from wallpaper_changer.config import Config
|
||
|
|
|
||
|
|
|
||
|
|
class ConfigTests(unittest.TestCase):
|
||
|
|
def parse_args(self, *arguments: str):
|
||
|
|
return parse_args(list(arguments))
|
||
|
|
|
||
|
|
def test_argument_config_file_creates_file_from_conf_file_argument(self) -> None:
|
||
|
|
with TemporaryDirectory() as temporary_directory:
|
||
|
|
config_path = Path(temporary_directory) / "nested"
|
||
|
|
args = self.parse_args("--conf-dir", str(config_path))
|
||
|
|
|
||
|
|
result = Config.argument_config_file(args.conf_dir)
|
||
|
|
|
||
|
|
self.assertEqual(result, config_path.resolve())
|
||
|
|
self.assertTrue(result.is_file())
|
||
|
|
self.assertEqual(
|
||
|
|
result.read_text(),
|
||
|
|
"# Wallpaper Changer Configuration File\n",
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_argument_config_file_preserves_existing_file(self) -> None:
|
||
|
|
with TemporaryDirectory() as temporary_directory:
|
||
|
|
config_path = Path(temporary_directory) / "wp.conf"
|
||
|
|
config_path.write_text("--folder insta\n")
|
||
|
|
args = self.parse_args("--conf-dir", str(config_path.parent))
|
||
|
|
|
||
|
|
result = Config.argument_config_file(args.conf_dir) / "wp.conf"
|
||
|
|
|
||
|
|
self.assertEqual(result, config_path )
|
||
|
|
self.assertEqual(result.read_text(), "--folder insta\n")
|
||
|
|
|
||
|
|
def test_argument_config_file_is_read(self) -> None:
|
||
|
|
with TemporaryDirectory() as temporary_directory:
|
||
|
|
config_path = Path(temporary_directory) / "wp.conf"
|
||
|
|
config_path.write_text("-s Berg\n")
|
||
|
|
args = self.parse_args("--conf-dir", str(config_path.parent),
|
||
|
|
"-c")
|
||
|
|
self.assertAlmostEqual(args.search, ["Berg"])
|
||
|
|
|
||
|
|
|
||
|
|
def test_argument_config_file_X_is_read(self) -> None:
|
||
|
|
with TemporaryDirectory() as temporary_directory:
|
||
|
|
config_path = Path(temporary_directory) / "wpX.conf"
|
||
|
|
config_path.write_text("-s Hügel\n")
|
||
|
|
args = self.parse_args("--conf-dir", str(config_path.parent),
|
||
|
|
"-c", "-x")
|
||
|
|
self.assertAlmostEqual(args.search, ["Hügel"])
|
||
|
|
|
||
|
|
def test_load_resolves_modes_and_folder_paths(self) -> None:
|
||
|
|
home = Path("/home/test-user")
|
||
|
|
wallpaper_root = home / "Nextcloud" / "Stuff" / "Wallpaper"
|
||
|
|
cases = (
|
||
|
|
(("default", None), "Desktop1920x1080/", wallpaper_root / "Desktop1920x1080"),
|
||
|
|
(("x", None), "corrupt", wallpaper_root / "corrupt"),
|
||
|
|
(("i", None), "insta", wallpaper_root / "insta"),
|
||
|
|
(("default", "custom"), "custom", wallpaper_root / "custom"),
|
||
|
|
(("i", "custom/subdir"), "insta", wallpaper_root / "insta"),
|
||
|
|
(("x", "/tmp/custom-wallpapers"), "/tmp/custom-wallpapers", Path("/tmp/custom-wallpapers")),
|
||
|
|
)
|
||
|
|
|
||
|
|
with patch("wallpaper_changer.config.socket.gethostname", return_value="testhost"):
|
||
|
|
with patch("pathlib.Path.home", return_value=home):
|
||
|
|
for (mode, folder), expected_folder, expected_img_dir in cases:
|
||
|
|
with self.subTest(mode=mode, folder=folder):
|
||
|
|
arguments = [
|
||
|
|
"--mode",
|
||
|
|
mode,
|
||
|
|
"--temp-dir",
|
||
|
|
"/tmp/wallpaper-cache",
|
||
|
|
"--private-key",
|
||
|
|
"/tmp/private-key.pem",
|
||
|
|
]
|
||
|
|
if folder is not None:
|
||
|
|
arguments.extend(("--folder", folder))
|
||
|
|
args = self.parse_args(*arguments)
|
||
|
|
|
||
|
|
config = Config.load(args)
|
||
|
|
|
||
|
|
self.assertEqual(config.host, "testhost")
|
||
|
|
self.assertEqual(config.img_folder, expected_folder)
|
||
|
|
self.assertEqual(config.img_dir, expected_img_dir)
|
||
|
|
self.assertEqual(config.wallpaper_dir, Path("/tmp/wallpaper-cache/wallpaper"))
|
||
|
|
self.assertEqual(config.private_key, Path("/tmp/private-key.pem"))
|
||
|
|
|
||
|
|
|
||
|
|
def test_load_uses_config_default_private_key_when_not_set(self) -> None:
|
||
|
|
home = Path("/home/test-user")
|
||
|
|
args = self.parse_args()
|
||
|
|
args.private_key = None
|
||
|
|
args.conf_dir = home
|
||
|
|
|
||
|
|
with patch("wallpaper_changer.config.socket.gethostname", return_value="testhost"):
|
||
|
|
config = Config.load(args)
|
||
|
|
|
||
|
|
self.assertEqual(config.private_key, Path(args.conf_dir).expanduser().resolve() / "IMG_privateKey.pem")
|
||
|
|
self.assertEqual(config.wallpaper_dir, Path(args.conf_dir).expanduser().resolve() / "wallpaper")
|
||
|
|
|
||
|
|
|
||
|
|
def test_load_uses_arg_default_private_key_when_set(self) -> None:
|
||
|
|
home = Path("/home/test-user")
|
||
|
|
args = self.parse_args()
|
||
|
|
assets = Path(__file__).resolve().parents[1] / "assets"
|
||
|
|
args.private_key = assets / "test_privateKey.pem"
|
||
|
|
|
||
|
|
with patch("wallpaper_changer.config.socket.gethostname", return_value="testhost"):
|
||
|
|
with patch("pathlib.Path.home", return_value=home):
|
||
|
|
config = Config.load(args)
|
||
|
|
|
||
|
|
self.assertEqual(config.private_key, args.private_key)
|
||
|
|
|
||
|
|
def test_mode_shortcuts_and_folder_override(self) -> None:
|
||
|
|
for arguments, expected_mode, expected_folder in (
|
||
|
|
((), "default", None),
|
||
|
|
(("-d",), "default", None),
|
||
|
|
(("-x",), "x", None),
|
||
|
|
(("--mode", "x"), "x", None),
|
||
|
|
(("-i",), "i", None),
|
||
|
|
(("--mode", "i", "--folder", "my-images"), "i", "my-images"),
|
||
|
|
(("-x", "--folder", "my-images"), "x", "my-images"),
|
||
|
|
):
|
||
|
|
with self.subTest(arguments=arguments):
|
||
|
|
args = self.parse_args(*arguments)
|
||
|
|
self.assertEqual(args.mode, expected_mode)
|
||
|
|
self.assertEqual(args.folder, expected_folder)
|
||
|
|
|
||
|
|
|
||
|
|
class VersionArgumentTests(unittest.TestCase):
|
||
|
|
def test_version_flags_print_package_version(self) -> None:
|
||
|
|
for flag in ("--version", "-v"):
|
||
|
|
with self.subTest(flag=flag):
|
||
|
|
output = StringIO()
|
||
|
|
with redirect_stdout(output):
|
||
|
|
with self.assertRaises(SystemExit) as raised:
|
||
|
|
parse_args([flag])
|
||
|
|
|
||
|
|
self.assertEqual(raised.exception.code, 0)
|
||
|
|
self.assertEqual(
|
||
|
|
output.getvalue(),
|
||
|
|
f"wallpaperchanger {version('wallpaper-changer')}\n",
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|