Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mkdocs.yaml when '--config' is not passed + refactor #2478

Merged
merged 6 commits into from
Jul 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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