diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2018-11-16 05:01:54 -0800 |
---|---|---|
committer | Jon Dufresne <jon.dufresne@gmail.com> | 2018-11-20 18:37:05 -0800 |
commit | 77849f0ef6d80fc636f457c2d8925c4932c406c9 (patch) | |
tree | 270f19661ab4fdcf3a53f122688532018262744d /tests/test_util.py | |
parent | 2cbc921946c20e072965f746090003fe41f5ddf1 (diff) | |
download | sphinx-git-77849f0ef6d80fc636f457c2d8925c4932c406c9.tar.gz |
Simplify ensuredir() with Python3 stdlib features
- Simplify ensuredir() to equivalent os.makedir(name, exist_ok=True)
- Do not check if a directory exists before calling
ensuredir() (ensuredir() already handles it)
- Add exist_ok argument to path.makedirs() to follow same pattern
- Drop unnecessary .exists() check immediately before .isdir()
- Add tests for ensuredir
Diffstat (limited to 'tests/test_util.py')
-rw-r--r-- | tests/test_util.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/tests/test_util.py b/tests/test_util.py index 8e4fbd6b0..1ec283187 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -9,12 +9,15 @@ :license: BSD, see LICENSE for details. """ +import os +import tempfile + import pytest from mock import patch from sphinx.testing.util import strip_escseq from sphinx.util import ( - display_chunk, encode_uri, parselinenos, status_iterator, xmlname_checker + display_chunk, encode_uri, ensuredir, parselinenos, status_iterator, xmlname_checker ) from sphinx.util import logging @@ -34,6 +37,20 @@ def test_encode_uri(): assert expected, encode_uri(uri) +def test_ensuredir(): + with tempfile.TemporaryDirectory() as tmp_path: + # Does not raise an exception for an existing directory. + ensuredir(tmp_path) + + path = os.path.join(tmp_path, 'a', 'b', 'c') + ensuredir(path) + assert os.path.isdir(path) + + with tempfile.NamedTemporaryFile() as tmp: + with pytest.raises(OSError): + ensuredir(tmp.name) + + def test_display_chunk(): assert display_chunk('hello') == 'hello' assert display_chunk(['hello']) == 'hello' |