From 65dce18908ac5309f32b47b766192b94e9226fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Mon, 20 May 2019 19:09:58 +0200 Subject: [PATCH] Add TUTOR_VERSION to template variables --- tests/test_config.py | 10 ++++++---- tests/test_env.py | 5 +++++ tutor/commands/config.py | 20 ++++++++++++-------- tutor/env.py | 4 ++-- tutor/templates/version | 1 + 5 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 tutor/templates/version diff --git a/tests/test_config.py b/tests/test_config.py index 71ff667..d963c3f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -11,6 +11,10 @@ class ConfigTests(unittest.TestCase): # This is necessary to avoid cached mocks env.Renderer.reset() + def test_version(self): + defaults = tutor_config.load_defaults() + self.assertNotIn("TUTOR_VERSION", defaults) + def test_merge(self): config = {} defaults = tutor_config.load_defaults() @@ -39,16 +43,14 @@ class ConfigTests(unittest.TestCase): tutor_config.utils, "random_string" ) as mock_random_string: mock_random_string.return_value = "abcd" - defaults = tutor_config.load_defaults() - config1 = tutor_config.load_current(root, defaults) + config1, defaults = tutor_config.load_all(root) password1 = config1["MYSQL_ROOT_PASSWORD"] config1.pop("MYSQL_ROOT_PASSWORD") tutor_config.save_config(root, config1) mock_random_string.return_value = "efgh" - defaults = tutor_config.load_defaults() - config2 = tutor_config.load_current(root, defaults) + config2, defaults = tutor_config.load_all(root) password2 = config2["MYSQL_ROOT_PASSWORD"] self.assertEqual("abcd", password1) diff --git a/tests/test_env.py b/tests/test_env.py index 08967c8..ed79c37 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -39,3 +39,8 @@ class EnvTests(unittest.TestCase): self.assertRaises( exceptions.TutorError, env.render_file, {}, "local", "docker-compose.yml" ) + + def test_render_full(self): + with tempfile.TemporaryDirectory() as root: + defaults = tutor_config.load_defaults() + env.render_full(root, defaults) diff --git a/tutor/commands/config.py b/tutor/commands/config.py index f07a424..ebddc2a 100644 --- a/tutor/commands/config.py +++ b/tutor/commands/config.py @@ -33,8 +33,7 @@ def save_command(root, silent1, silent2, set_): def save(root, silent=False, keyvalues=None): keyvalues = keyvalues or [] - defaults = load_defaults() - config = load_current(root, defaults) + config, defaults = load_all(root) for k, v in keyvalues: config[k] = v if not silent: @@ -54,8 +53,7 @@ def printroot(root): @opts.root @click.argument("key") def printvalue(root, key): - defaults = load_defaults() - config = load_current(root, defaults) + config, defaults = load_all(root) merge(config, defaults) try: fmt.echo(config[key]) @@ -132,6 +130,16 @@ def pre_upgrade_announcement(root): ) +def load_all(root): + defaults = load_defaults() + current = load_current(root, defaults) + return current, defaults + + +def load_defaults(): + return serialize.load(env.read("config.yml")) + + def load_current(root, defaults): convert_json2yml(root) config = load_user(root) @@ -308,10 +316,6 @@ def load_interactive(config, defaults): ) -def load_defaults(): - return serialize.load(env.read("config.yml")) - - def ask(question, key, config, defaults): default = env.render_str(config, config.get(key, defaults[key])) config[key] = click.prompt( diff --git a/tutor/env.py b/tutor/env.py index bbb11b8..e89aff9 100644 --- a/tutor/env.py +++ b/tutor/env.py @@ -27,6 +27,7 @@ class Renderer: environment.filters["random_string"] = utils.random_string environment.filters["common_domain"] = utils.random_string environment.filters["reverse_host"] = utils.reverse_host + environment.globals["TUTOR_VERSION"] = __version__ cls.ENVIRONMENT = environment return cls.ENVIRONMENT @@ -69,8 +70,7 @@ def render_full(root, config): for subdir in ["android", "apps", "k8s", "local", "webui"]: render_subdir(subdir, root, config) copy_subdir("build", root) - with open(pathjoin(root, VERSION_FILENAME), "w") as f: - f.write(__version__) + render_file(config, VERSION_FILENAME) def render_subdir(subdir, root, config): diff --git a/tutor/templates/version b/tutor/templates/version new file mode 100644 index 0000000..a033755 --- /dev/null +++ b/tutor/templates/version @@ -0,0 +1 @@ +{{ TUTOR_VERSION }} \ No newline at end of file