Skip to content
Merged
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
18 changes: 14 additions & 4 deletions mkdocs_simple_plugin/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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"])
Expand Down
43 changes: 43 additions & 0 deletions tests/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from mkdocs import theme
from mkdocs.config import defaults
import yaml

from mkdocs_simple_plugin import generator

Expand Down Expand Up @@ -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()
Loading