From 2d3ab680aa637c24642124f6ff102cff22ebc774 Mon Sep 17 00:00:00 2001 From: Allison Thackston Date: Mon, 16 Mar 2026 12:24:54 -0700 Subject: [PATCH] Don't overwrite config keys if they already exist --- mkdocs_simple_plugin/generator.py | 18 ++++++++++--- tests/test_generator.py | 43 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/mkdocs_simple_plugin/generator.py b/mkdocs_simple_plugin/generator.py index 7d3302e15..eaf2e32db 100644 --- a/mkdocs_simple_plugin/generator.py +++ b/mkdocs_simple_plugin/generator.py @@ -53,24 +53,31 @@ def default_config(): # Set the default edit uri to empty since doc files are built from source # and may not exist. config['edit_uri'] = '' + return config + + +def apply_environment_config(config, existing_config=None): + """Set config values from the environment unless already in the file.""" + existing_config = existing_config or {} def maybe_set_string(name): env_variable = "INPUT_" + name.upper() config_variable = name.lower() - if os.environ.get(env_variable): + config_exists = config_variable in existing_config + if os.environ.get(env_variable) and not config_exists: config[config_variable] = os.environ[env_variable] def maybe_set_dict(name, key): env_variable = "INPUT_" + name.upper() config_variable = name.lower() - if os.environ.get(env_variable): + config_exists = config_variable in existing_config + if os.environ.get(env_variable) and not config_exists: config[config_variable] = {key: os.environ[env_variable]} - # Set the config variables via environment if exist + maybe_set_string("site_name") maybe_set_string("site_url") maybe_set_string("repo_url") maybe_set_dict("theme", "name") - return config class MkdocsConfigDumper(yaml.Dumper): @@ -100,9 +107,11 @@ def write_config(config_file, config): def setup_config(config_file="mkdocs.yml"): """Create the mkdocs.yml file with defaults for params that don't exist.""" config = default_config() + local_config = {} if not os.path.exists(config_file): # If config file doesn't exit, create a simple one, guess the site name # from the folder name. + apply_environment_config(config) write_config(config_file, config) # Open the config file to verify settings. with open(config_file, 'r', encoding="utf-8") as stream: @@ -112,6 +121,7 @@ def setup_config(config_file="mkdocs.yml"): # Overwrite default config values with local mkdocs.yml config.update(local_config) print(config) + apply_environment_config(config, local_config) if not os.path.exists(config["docs_dir"]): # Ensure docs directory exists. print("making docs_dir %s", config["docs_dir"]) diff --git a/tests/test_generator.py b/tests/test_generator.py index df06e56b1..879989531 100755 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -6,6 +6,7 @@ from mkdocs import theme from mkdocs.config import defaults +import yaml from mkdocs_simple_plugin import generator @@ -106,6 +107,48 @@ def test_theme(self): config_value={"name": "readthedocs"}, loaded_type=theme.Theme) + def test_existing_site_name_is_not_overwritten_by_env(self): + """Test that an existing site name in mkdocs.yml is preserved.""" + os.environ["INPUT_SITE_NAME"] = "from-env" + generator.write_config( + self.test_mkdocs_filename, + { + "site_name": "from-file", + "docs_dir": tempfile.mkdtemp(), + "plugins": ("simple", "search"), + "edit_uri": "", + }) + + test_config = generator.setup_config(self.test_mkdocs_filename) + + self.assertEqual(test_config["site_name"], "from-file") + with open(self.test_mkdocs_filename, 'r', encoding="utf-8") as stream: + written_config = yaml.load(stream, yaml.Loader) + self.assertEqual(written_config["site_name"], "from-file") + + def test_existing_theme_is_not_overwritten_by_env(self): + """Test that an existing theme in mkdocs.yml is preserved.""" + os.environ["INPUT_THEME"] = "readthedocs" + generator.write_config( + self.test_mkdocs_filename, + { + "site_name": "from-file", + "docs_dir": tempfile.mkdtemp(), + "plugins": ("simple", "search"), + "edit_uri": "", + "theme": {"name": "mkdocs"}, + }) + + test_config = generator.setup_config(self.test_mkdocs_filename) + + self.assertEqual(test_config["theme"], {"name": "mkdocs"}) + cfg = defaults.MkDocsConfig() + cfg.load_dict(test_config) + errors, warnings = cfg.validate() + self.assertEqual(len(errors), 0, errors) + self.assertEqual(len(warnings), 0, warnings) + self.assertIsInstance(cfg["theme"], theme.Theme) + if __name__ == '__main__': unittest.main()