sphinx/tests/test_util/test_util.py

39 lines
1.3 KiB
Python
Raw Normal View History

2022-02-19 21:05:56 -06:00
"""Tests util functions."""
import os
import tempfile
import pytest
2024-10-05 10:43:05 -05:00
from sphinx.util import parselinenos
2024-07-22 20:38:45 -05:00
from sphinx.util.osutil import ensuredir
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)
2017-02-11 08:42:39 -06:00
def test_parselinenos():
assert parselinenos('1,2,3', 10) == [0, 1, 2]
assert parselinenos('4, 5, 6', 10) == [3, 4, 5]
assert parselinenos('-4', 10) == [0, 1, 2, 3]
2017-02-11 08:42:39 -06:00
assert parselinenos('7-9', 10) == [6, 7, 8]
assert parselinenos('7-', 10) == [6, 7, 8, 9]
assert parselinenos('1,7-', 10) == [0, 6, 7, 8, 9]
assert parselinenos('7-7', 10) == [6]
assert parselinenos('11-', 10) == [10]
with pytest.raises(ValueError, match="invalid line number spec: '1-2-3'"):
2017-02-11 08:42:39 -06:00
parselinenos('1-2-3', 10)
with pytest.raises(ValueError, match="invalid line number spec: 'abc-def'"):
2017-02-11 08:42:39 -06:00
parselinenos('abc-def', 10)
with pytest.raises(ValueError, match="invalid line number spec: '-'"):
2017-02-11 08:42:39 -06:00
parselinenos('-', 10)
with pytest.raises(ValueError, match="invalid line number spec: '3-1'"):
parselinenos('3-1', 10)