From ddeb3e65a304a753ec4299ed2a4be133170f78a1 Mon Sep 17 00:00:00 2001 From: FH <frede@hundewadt.dk> Date: Mon, 20 Feb 2017 20:10:35 +0100 Subject: [PATCH] refactor objective testability Since travis builds are prone to fail if pacman-mirrors.conf is not found It has been made possible to change location by adding it to internal config It can then be changed on-the-fly to fit travis build (reverted from commit 9e9f32d55caf3d47080fba329b418bc2290a0457) --- pacman_mirrors/configfn.py | 6 ++---- pacman_mirrors/customfn.py | 36 ++++++++++++++++---------------- pacman_mirrors/filefn.py | 1 + pacman_mirrors/pacman_mirrors.py | 15 ++++--------- tests/test_command_line_parse.py | 12 ----------- tests/test_default_config.py | 6 ------ tests/test_geoip.py | 2 -- tests/test_pacman_mirrors.py | 1 - 8 files changed, 25 insertions(+), 54 deletions(-) diff --git a/pacman_mirrors/configfn.py b/pacman_mirrors/configfn.py index d6b3deb1..3ad4336f 100644 --- a/pacman_mirrors/configfn.py +++ b/pacman_mirrors/configfn.py @@ -20,7 +20,7 @@ """Pacman-Mirrors Configuration Functions""" from . import txt -from .configuration import CONFIG_FILE, CUSTOM_FILE, MIRROR_DIR, MIRROR_FILE, MIRROR_LIST +from .configuration import CONFIG_FILE, MIRROR_DIR, MIRROR_FILE, MIRROR_LIST class ConfigFn: @@ -34,8 +34,6 @@ class ConfigFn: # is fetched from config file config = { "branch": "stable", - "config_file": CONFIG_FILE, - "custom_file": CUSTOM_FILE, "method": "rank", "mirror_dir": MIRROR_DIR, "mirror_file": MIRROR_FILE, @@ -45,7 +43,7 @@ class ConfigFn: } try: # read configuration from file - with open(config["config_file"]) as conf: + with open(CONFIG_FILE) as conf: for line in conf: line = line.strip() if line.startswith("#") or "=" not in line: diff --git a/pacman_mirrors/customfn.py b/pacman_mirrors/customfn.py index d7864e35..89db39d7 100644 --- a/pacman_mirrors/customfn.py +++ b/pacman_mirrors/customfn.py @@ -56,44 +56,44 @@ class CustomFn: CustomHelper.cleanup() @staticmethod - def modify_config(configfile, customfile, onlycountry, custom=False): + def modify_config(onlycountry, custom=False): """Modify configuration""" if not custom: - if os.path.isfile(customfile): - os.remove(customfile) - CustomFn.write_custom_config(configfile, onlycountry, custom) + if os.path.isfile(CUSTOM_FILE): + os.remove(CUSTOM_FILE) + CustomFn.write_custom_config(CONFIG_FILE, onlycountry, custom) @staticmethod - def write_custom_config(configfile, selectedcountries, custom=False): + def write_custom_config(filename, selection, custom=False): """Writes the configuration to file - :param configfile: - :param selectedcountries: + :param filename: + :param selection: :param custom: """ if custom: - if selectedcountries == ["Custom"]: - selectedcountries = "OnlyCountry = Custom\n" + if selection == ["Custom"]: + new_config = "OnlyCountry = Custom\n" else: - selectedcountries = "OnlyCountry = {list}\n".format( - list=",".join(selectedcountries)) + new_config = "OnlyCountry = {list}\n".format( + list=",".join(selection)) else: - selectedcountries = "# OnlyCountry = \n" + new_config = "# OnlyCountry = \n" try: with open( - configfile) as cnf, tempfile.NamedTemporaryFile( + filename) as cnf, tempfile.NamedTemporaryFile( "w+t", dir=os.path.dirname( - configfile), delete=False) as tmp: + filename), delete=False) as tmp: replaced = False for line in cnf: if "OnlyCountry" in line: - tmp.write(selectedcountries) + tmp.write(new_config) replaced = True else: tmp.write("{}".format(line)) if not replaced: - tmp.write(selectedcountries) - os.replace(tmp.name, configfile) - os.chmod(configfile, 0o644) + tmp.write(new_config) + os.replace(tmp.name, filename) + os.chmod(filename, 0o644) except OSError as err: print(".: {} {}: {}: {}".format(txt.ERR_CLR, txt.CANNOT_READ_FILE, err.filename, err.strerror)) diff --git a/pacman_mirrors/filefn.py b/pacman_mirrors/filefn.py index 753ca9d7..5463b150 100755 --- a/pacman_mirrors/filefn.py +++ b/pacman_mirrors/filefn.py @@ -23,6 +23,7 @@ import os import datetime from .configuration import FALLBACK, MIRROR_FILE, REPO_ARCH, STATUS_FILE from .jsonfn import JsonFn +from .miscfn import MiscFn from . import txt diff --git a/pacman_mirrors/pacman_mirrors.py b/pacman_mirrors/pacman_mirrors.py index 530939e7..6bf982c1 100755 --- a/pacman_mirrors/pacman_mirrors.py +++ b/pacman_mirrors/pacman_mirrors.py @@ -200,14 +200,10 @@ class PacmanMirrors: worklist, quiet=self.quiet) if self.custom or self.config["only_country"] != self.mirrors.mirrorlist: - CustomFn.modify_config(self.config["config_file"], - self.config["custom_file"], - self.config["only_country"], + CustomFn.modify_config(self.config["only_country"], custom=True) else: - CustomFn.modify_config(self.config["config_file"], - self.config["custom_file"], - self.config["only_country"]) + CustomFn.modify_config(self.config["only_country"]) def build_fasttrack_mirror_list(self, number): """Fast-track the mirrorlist by aggressive sorting""" @@ -297,12 +293,9 @@ class PacmanMirrors: worklist, custom=True, quiet=self.quiet) - # always use "Custom" when interactive mode + # always use "Custom" from interactive self.config["only_country"] = ["Custom"] - CustomFn.modify_config(self.config["config_file"], - self.config["custom_file"], - self.config["only_country"], - custom=True) + CustomFn.modify_config(self.config["only_country"], custom=True) print(".: {} {}: {}".format(txt.INF_CLR, txt.MIRROR_LIST_SAVED, CUSTOM_FILE)) diff --git a/tests/test_command_line_parse.py b/tests/test_command_line_parse.py index b263f9be..5144404b 100755 --- a/tests/test_command_line_parse.py +++ b/tests/test_command_line_parse.py @@ -29,7 +29,6 @@ class TestCommandLineParse(unittest.TestCase): "-b", "unstable"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["branch"] == "unstable" @@ -42,7 +41,6 @@ class TestCommandLineParse(unittest.TestCase): "-b", "testing"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["branch"] == "testing" @@ -55,7 +53,6 @@ class TestCommandLineParse(unittest.TestCase): "-m", "random"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["method"] == "random" @@ -68,7 +65,6 @@ class TestCommandLineParse(unittest.TestCase): "-d", "/another/dir/"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["mirror_dir"] == "/another/dir/" @@ -81,7 +77,6 @@ class TestCommandLineParse(unittest.TestCase): "-o", "/another/list"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["mirror_list"] == "/another/list" @@ -94,7 +89,6 @@ class TestCommandLineParse(unittest.TestCase): "-c", "France,Germany"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.config["only_country"] == ["France", "Germany"] @@ -107,7 +101,6 @@ class TestCommandLineParse(unittest.TestCase): "-c Denmark"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.custom is True @@ -120,7 +113,6 @@ class TestCommandLineParse(unittest.TestCase): "--geoip"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.geoip is True @@ -133,7 +125,6 @@ class TestCommandLineParse(unittest.TestCase): "-f 5"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.fasttrack == 5 @@ -146,7 +137,6 @@ class TestCommandLineParse(unittest.TestCase): "-i"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.interactive is True @@ -159,7 +149,6 @@ class TestCommandLineParse(unittest.TestCase): "-t 5"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.max_wait_time == 5 @@ -172,7 +161,6 @@ class TestCommandLineParse(unittest.TestCase): "-q"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() assert app.quiet is True diff --git a/tests/test_default_config.py b/tests/test_default_config.py index 227ce112..ba371eb8 100755 --- a/tests/test_default_config.py +++ b/tests/test_default_config.py @@ -29,7 +29,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["branch"] == "stable" @patch("os.getuid") @@ -41,7 +40,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["method"] == "rank" @patch("os.getuid") @@ -53,7 +51,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["mirror_dir"] == "mock/var/" @patch("os.getuid") @@ -65,7 +62,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["mirror_file"] == "mock/var/mirrors.json" @patch("os.getuid") @@ -77,7 +73,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["mirror_list"] == "mock/etc/mirrorlist" @patch("os.getuid") @@ -89,7 +84,6 @@ class TestDefaultConfig(unittest.TestCase): "-g"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" assert app.config["no_update"] is False # @patch("os.getuid") diff --git a/tests/test_geoip.py b/tests/test_geoip.py index f67d7db4..98985324 100755 --- a/tests/test_geoip.py +++ b/tests/test_geoip.py @@ -32,7 +32,6 @@ class TestGeoip(unittest.TestCase): "--geoip"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() app.load_all_mirrors() assert app.selected_countries == "France" @@ -49,7 +48,6 @@ class TestGeoip(unittest.TestCase): "--geoip"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() app.load_all_mirrors() assert app.selected_countries == app.mirrors.countrylist diff --git a/tests/test_pacman_mirrors.py b/tests/test_pacman_mirrors.py index 7ff4a9d5..43f418f2 100755 --- a/tests/test_pacman_mirrors.py +++ b/tests/test_pacman_mirrors.py @@ -31,7 +31,6 @@ class TestPacmanMirrors(unittest.TestCase): "-m", "random"]): app = PacmanMirrors() app.config = ConfigFn.build_config() - app.config["config_file"] = "conf/pacman-mirrors.conf" app.command_line_parse() app.network = HttpFn.update_mirrors() app.load_all_mirrors() -- GitLab