Skip to content

Commit

Permalink
Allow mkdocs.yaml when '--config' is not passed + refactor (#2478)
Browse files Browse the repository at this point in the history
Co-authored-by: Olaf Gladis <olaf.gladis@trivago.com>
  • Loading branch information
oprypin and hwmrocker committed Jul 8, 2021
1 parent f6d8830 commit 06d8b47
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 15 deletions.
38 changes: 24 additions & 14 deletions mkdocs/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,34 @@ def _open_config_file(config_file):

# Default to the standard config filename.
if config_file is None:
config_file = os.path.abspath('mkdocs.yml')

# If closed file descriptor, get file path to reopen later.
if hasattr(config_file, 'closed') and config_file.closed:
config_file = config_file.name

log.debug(f"Loading configuration file: {config_file}")

paths_to_try = ['mkdocs.yml', 'mkdocs.yaml']
# If it is a string, we can assume it is a path and attempt to open it.
if isinstance(config_file, str):
if os.path.exists(config_file):
config_file = open(config_file, 'rb')
elif isinstance(config_file, str):
paths_to_try = [config_file]
# If closed file descriptor, get file path to reopen later.
elif getattr(config_file, 'closed', False):
paths_to_try = [config_file.name]
else:
paths_to_try = None

if paths_to_try:
# config_file is not a file descriptor, so open it as a path.
for path in paths_to_try:
path = os.path.abspath(path)
log.debug(f"Loading configuration file: {path}")
try:
config_file = open(path, 'rb')
break
except FileNotFoundError:
continue
else:
raise exceptions.ConfigurationError(
f"Config file '{config_file}' does not exist.")
f"Config file '{paths_to_try[0]}' does not exist.")
else:
log.debug(f"Loading configuration file: {config_file}")
# Ensure file descriptor is at begining
config_file.seek(0)

# Ensure file descriptor is at begining
config_file.seek(0)
try:
yield config_file
finally:
Expand Down
71 changes: 70 additions & 1 deletion mkdocs/tests/config/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,76 @@ def test_load_from_file(self):
self.assertTrue(isinstance(cfg, base.Config))
self.assertEqual(cfg['site_name'], 'MkDocs Test')
finally:
os.remove(config_file.name)
temp_dir.cleanup()

def test_load_default_file(self):
"""
test that `mkdocs.yml` will be loaded when '--config' is not set.
"""

temp_dir = TemporaryDirectory()
config_file = open(os.path.join(temp_dir.name, 'mkdocs.yml'), 'w')
os.mkdir(os.path.join(temp_dir.name, 'docs'))
old_dir = os.getcwd()
try:
os.chdir(temp_dir.name)
config_file.write("site_name: MkDocs Test\n")
config_file.flush()
config_file.close()

cfg = base.load_config(config_file=None)
self.assertTrue(isinstance(cfg, base.Config))
self.assertEqual(cfg['site_name'], 'MkDocs Test')
finally:
os.chdir(old_dir)
temp_dir.cleanup()

def test_load_default_file_with_yaml(self):
"""
test that `mkdocs.yml` will be loaded when '--config' is not set.
"""

temp_dir = TemporaryDirectory()
config_file = open(os.path.join(temp_dir.name, 'mkdocs.yaml'), 'w')
os.mkdir(os.path.join(temp_dir.name, 'docs'))
old_dir = os.getcwd()
try:
os.chdir(temp_dir.name)
config_file.write("site_name: MkDocs Test\n")
config_file.flush()
config_file.close()

cfg = base.load_config(config_file=None)
self.assertTrue(isinstance(cfg, base.Config))
self.assertEqual(cfg['site_name'], 'MkDocs Test')
finally:
os.chdir(old_dir)
temp_dir.cleanup()

def test_load_default_file_prefer_yml(self):
"""
test that `mkdocs.yml` will be loaded when '--config' is not set.
"""

temp_dir = TemporaryDirectory()
config_file1 = open(os.path.join(temp_dir.name, 'mkdocs.yml'), 'w')
config_file2 = open(os.path.join(temp_dir.name, 'mkdocs.yaml'), 'w')
os.mkdir(os.path.join(temp_dir.name, 'docs'))
old_dir = os.getcwd()
try:
os.chdir(temp_dir.name)
config_file1.write("site_name: MkDocs Test1\n")
config_file1.flush()
config_file1.close()
config_file2.write("site_name: MkDocs Test2\n")
config_file2.flush()
config_file2.close()

cfg = base.load_config(config_file=None)
self.assertTrue(isinstance(cfg, base.Config))
self.assertEqual(cfg['site_name'], 'MkDocs Test1')
finally:
os.chdir(old_dir)
temp_dir.cleanup()

def test_load_from_missing_file(self):
Expand Down

0 comments on commit 06d8b47

Please sign in to comment.