Skip to content

Commit

Permalink
Use mkdocs.utils.load_config instead of ConfigData
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Jul 9, 2021
1 parent 4578211 commit 494147a
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 69 deletions.
12 changes: 6 additions & 6 deletions mike/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _redirect_template(user_template=None):


def _add_redirect_to_commit(commit, template, src, dst,
use_directory_urls=True):
use_directory_urls):
if os.path.splitext(src)[1] == '.html':
reldst = os.path.relpath(dst, os.path.dirname(src))
href = '/'.join(reldst.split(os.path.sep))
Expand Down Expand Up @@ -81,15 +81,15 @@ def deploy(cfg, version, title=None, aliases=[], update_aliases=False,
with git_utils.Commit(branch, message) as commit:
commit.delete_files([version_str] + list(info.aliases))

for f in git_utils.walk_real_files(cfg.site_dir):
canonical_file = f.copy(destdir, cfg.site_dir)
for f in git_utils.walk_real_files(cfg['site_dir']):
canonical_file = f.copy(destdir, cfg['site_dir'])
commit.add_file(canonical_file)
for d in alias_destdirs:
alias_file = f.copy(d, cfg.site_dir)
alias_file = f.copy(d, cfg['site_dir'])
if redirect:
_add_redirect_to_commit(
commit, t, alias_file.path, canonical_file.path,
cfg.use_directory_urls
cfg['use_directory_urls']
)
else:
commit.add_file(alias_file)
Expand Down Expand Up @@ -172,7 +172,7 @@ def alias(cfg, version, aliases, update_aliases=False, redirect=True,
if redirect:
_add_redirect_to_commit(
commit, t, alias_file.path, canonical_file.path,
cfg.use_directory_urls
cfg['use_directory_urls']
)
else:
commit.add_file(alias_file)
Expand Down
18 changes: 9 additions & 9 deletions mike/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ def add_git_arguments(parser, *, commit=True, prefix=True):

def load_mkdocs_config(args, strict=False):
try:
cfg = mkdocs_utils.ConfigData(args.config_file)
cfg = mkdocs_utils.load_config(args.config_file)
if args.branch is None:
args.branch = cfg.remote_branch
args.branch = cfg['remote_branch']
if args.remote is None:
args.remote = cfg.remote_name
args.remote = cfg['remote_name']
return cfg
except OSError:
except FileNotFoundError as e:
if strict:
raise RuntimeError('{!r} not found'.format(args.config_file))
raise
if args.branch is None or args.remote is None:
raise RuntimeError((
'{!r} not found; pass --config-file or set ' +
'--remote/--branch explicitly'
).format(args.config_file))
raise FileNotFoundError(
'{}; pass --config-file or set --remote/--branch explicitly'
.format(str(e))
)


def check_remote_status(args, strict=False):
Expand Down
2 changes: 1 addition & 1 deletion mike/mkdocs_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

try:
from mkdocs.exceptions import PluginError
except ImportError:
except ImportError: # pragma: no cover
PluginError = ValueError


Expand Down
30 changes: 21 additions & 9 deletions mike/mkdocs_utils.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import mkdocs.config
import os
import re
import subprocess
import yaml
from collections.abc import Iterable
from contextlib import contextmanager
from tempfile import NamedTemporaryFile

docs_version_var = 'MIKE_DOCS_VERSION'


class ConfigData:
def __init__(self, config_file):
with open(config_file) as f:
config = yaml.load(f, Loader=yaml.Loader)
self.site_dir = os.path.join(os.path.dirname(config_file),
config.get('site_dir', 'site'))
self.remote_name = config.get('remote_name', 'origin')
self.remote_branch = config.get('remote_branch', 'gh-pages')
self.use_directory_urls = config.get('use_directory_urls', True)
def _open_config(config_file=None):
if config_file is None:
config_file = ['mkdocs.yml', 'mkdocs.yaml']
elif not isinstance(config_file, Iterable) or isinstance(config_file, str):
config_file = [config_file]

exc = None
for i in config_file:
try:
return open(i, 'rb')
except FileNotFoundError as e:
if not exc:
exc = e
raise exc


def load_config(config_file=None, **kwargs):
with _open_config(config_file) as f:
return mkdocs.config.load_config(f, **kwargs)


@contextmanager
Expand Down
1 change: 1 addition & 0 deletions test/data/basic_theme/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
site_name: test
site_url: https://example.invalid/
theme: mkdocs

nav:
Expand Down
2 changes: 1 addition & 1 deletion test/data/mkdocs_plugin/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
site_name: test
site_url: https://example.com/
site_url: https://example.invalid/
theme:
name: mkdocs
plugins:
Expand Down
1 change: 1 addition & 0 deletions test/data/no_directory_urls/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
site_name: test
site_url: https://example.invalid/
theme: mkdocs
use_directory_urls: false

Expand Down
1 change: 1 addition & 0 deletions test/data/remote/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
site_name: test
site_url: https://example.invalid/
theme: mkdocs
remote_name: myremote
remote_branch: mybranch
Expand Down
1 change: 1 addition & 0 deletions test/data/site_dir/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
site_name: test
site_url: https://example.invalid/
theme: mkdocs
site_dir: built_docs

Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@unittest.skipIf(platform.system() == 'Windows',
"SIGINT doesn't work on windows")
class TestList(unittest.TestCase):
class TestServe(unittest.TestCase):
def setUp(self):
self.stage = stage_dir('serve')
git_init()
Expand Down
31 changes: 15 additions & 16 deletions test/unit/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
from mike import commands, git_utils, versions


class MockConfig:
def __init__(self, site_dir, remote_name='origin',
remote_branch='gh-pages', use_directory_urls=True):
self.site_dir = site_dir
self.remote_name = remote_name
self.remote_branch = remote_branch
self.use_directory_urls = use_directory_urls
def mock_config(site_dir, remote_name='origin',
remote_branch='gh-pages', use_directory_urls=True):
return {'site_dir': site_dir,
'remote_name': remote_name,
'remote_branch': remote_branch,
'use_directory_urls': use_directory_urls}


class TestListVersions(unittest.TestCase):
Expand All @@ -42,7 +41,7 @@ def test_versions_nonexistent(self):
class TestBase(unittest.TestCase):
def setUp(self):
self.stage = stage_dir(self.stage_dir)
self.cfg = MockConfig(self.stage)
self.cfg = mock_config(self.stage)
git_init()
commit_files(['page.html', 'file.txt', 'dir/index.html'])

Expand Down Expand Up @@ -75,10 +74,10 @@ class TestDeploy(TestBase):

def setUp(self):
super().setUp()
self.cfg.site_dir = os.path.join(self.cfg.site_dir, 'site')
self.cfg['site_dir'] = os.path.join(self.cfg['site_dir'], 'site')

def _mock_build(self):
copytree(self.stage, self.cfg.site_dir)
copytree(self.stage, self.cfg['site_dir'])

def _test_deploy(self, expected_message=None,
expected_versions=[versions.VersionInfo('1.0')],
Expand All @@ -90,8 +89,8 @@ def _test_deploy(self, expected_message=None,
.format(rev, expected_versions[0].version)
)

if os.path.exists(self.cfg.site_dir):
shutil.rmtree(self.cfg.site_dir)
if os.path.exists(self.cfg['site_dir']):
shutil.rmtree(self.cfg['site_dir'])
self._test_state(expected_message, expected_versions, **kwargs)

def _mock_commit(self):
Expand Down Expand Up @@ -134,7 +133,7 @@ def test_aliases(self):
self.assertRegex(f.read(), match_redir('../../1.0/dir/'))

def test_aliases_no_directory_urls(self):
self.cfg.use_directory_urls = False
self.cfg['use_directory_urls'] = False
with commands.deploy(self.cfg, '1.0', aliases=['latest']):
self._mock_build()
check_call_silent(['git', 'checkout', 'gh-pages'])
Expand Down Expand Up @@ -365,7 +364,7 @@ def test_alias_from_version(self):

def test_alias_no_directory_urls(self):
self._deploy()
self.cfg.use_directory_urls = False
self.cfg['use_directory_urls'] = False
commands.alias(self.cfg, '1.0', ['greatest'])
check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_alias()
Expand Down Expand Up @@ -465,7 +464,7 @@ def test_alias_invalid_version(self):
class TestRetitle(unittest.TestCase):
def setUp(self):
self.stage = stage_dir('retitle')
self.cfg = MockConfig(self.stage)
self.cfg = mock_config(self.stage)
git_init()
commit_files(['file.txt'])

Expand Down Expand Up @@ -527,7 +526,7 @@ def test_retitle_invalid(self):
class TestSetDefault(unittest.TestCase):
def setUp(self):
self.stage = stage_dir('set_default')
self.cfg = MockConfig(self.stage)
self.cfg = mock_config(self.stage)
git_init()
commit_files(['file.txt'])

Expand Down
67 changes: 41 additions & 26 deletions test/unit/test_mkdocs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,63 @@
from mike import mkdocs_utils


class TestConfigData(unittest.TestCase):
# This mostly just tests `load_config` from MkDocs, but we want to be sure it
# behaves as we want it.
class TestLoadConfig(unittest.TestCase):
def test_default(self):
os.chdir(os.path.join(test_data_dir, 'basic_theme'))
cfg = mkdocs_utils.ConfigData('mkdocs.yml')
self.assertEqual(cfg.site_dir, 'site')
self.assertEqual(cfg.remote_name, 'origin')
self.assertEqual(cfg.remote_branch, 'gh-pages')
self.assertEqual(cfg.use_directory_urls, True)
cfg = mkdocs_utils.load_config()
self.assertEqual(cfg['site_dir'], os.path.abspath('site'))
self.assertEqual(cfg['remote_name'], 'origin')
self.assertEqual(cfg['remote_branch'], 'gh-pages')
self.assertEqual(cfg['use_directory_urls'], True)

def test_abs_path(self):
cfg = mkdocs_utils.ConfigData(
cfg = mkdocs_utils.load_config(
os.path.join(test_data_dir, 'basic_theme', 'mkdocs.yml')
)
self.assertEqual(cfg.site_dir,
self.assertEqual(cfg['site_dir'],
os.path.join(test_data_dir, 'basic_theme', 'site'))
self.assertEqual(cfg.remote_name, 'origin')
self.assertEqual(cfg.remote_branch, 'gh-pages')
self.assertEqual(cfg.use_directory_urls, True)
self.assertEqual(cfg['remote_name'], 'origin')
self.assertEqual(cfg['remote_branch'], 'gh-pages')
self.assertEqual(cfg['use_directory_urls'], True)

def test_custom_site_dir(self):
os.chdir(os.path.join(test_data_dir, 'site_dir'))
cfg = mkdocs_utils.ConfigData('mkdocs.yml')
self.assertEqual(cfg.site_dir, 'built_docs')
self.assertEqual(cfg.remote_name, 'origin')
self.assertEqual(cfg.remote_branch, 'gh-pages')
self.assertEqual(cfg.use_directory_urls, True)
cfg = mkdocs_utils.load_config()
self.assertEqual(cfg['site_dir'], os.path.abspath('built_docs'))
self.assertEqual(cfg['remote_name'], 'origin')
self.assertEqual(cfg['remote_branch'], 'gh-pages')
self.assertEqual(cfg['use_directory_urls'], True)

def test_remote(self):
os.chdir(os.path.join(test_data_dir, 'remote'))
cfg = mkdocs_utils.ConfigData('mkdocs.yml')
self.assertEqual(cfg.site_dir, 'site')
self.assertEqual(cfg.remote_name, 'myremote')
self.assertEqual(cfg.remote_branch, 'mybranch')
self.assertEqual(cfg.use_directory_urls, True)
cfg = mkdocs_utils.load_config()
self.assertEqual(cfg['site_dir'], os.path.abspath('site'))
self.assertEqual(cfg['remote_name'], 'myremote')
self.assertEqual(cfg['remote_branch'], 'mybranch')
self.assertEqual(cfg['use_directory_urls'], True)

def test_no_directory_urls(self):
os.chdir(os.path.join(test_data_dir, 'no_directory_urls'))
cfg = mkdocs_utils.ConfigData('mkdocs.yml')
self.assertEqual(cfg.site_dir, 'site')
self.assertEqual(cfg.remote_name, 'origin')
self.assertEqual(cfg.remote_branch, 'gh-pages')
self.assertEqual(cfg.use_directory_urls, False)
cfg = mkdocs_utils.load_config()
self.assertEqual(cfg['site_dir'], os.path.abspath('site'))
self.assertEqual(cfg['remote_name'], 'origin')
self.assertEqual(cfg['remote_branch'], 'gh-pages')
self.assertEqual(cfg['use_directory_urls'], False)

def test_nonexist(self):
os.chdir(os.path.join(test_data_dir, 'basic_theme'))
with self.assertRaisesRegex(FileNotFoundError, r"'nonexist.yml'"):
mkdocs_utils.load_config('nonexist.yml')
with self.assertRaisesRegex(FileNotFoundError, r"'nonexist.yml'"):
mkdocs_utils.load_config(['nonexist.yml', 'nonexist2.yml'])

cfg = mkdocs_utils.load_config(['nonexist.yml', 'mkdocs.yml'])
self.assertEqual(cfg['site_dir'], os.path.abspath('site'))
self.assertEqual(cfg['remote_name'], 'origin')
self.assertEqual(cfg['remote_branch'], 'gh-pages')
self.assertEqual(cfg['use_directory_urls'], True)


class TestInjectPlugin(unittest.TestCase):
Expand Down

0 comments on commit 494147a

Please sign in to comment.