diff --git a/readthedocs/config/find.py b/readthedocs/config/find.py index e518da13f2c..cb3e5e0c56d 100644 --- a/readthedocs/config/find.py +++ b/readthedocs/config/find.py @@ -6,18 +6,11 @@ import re -def find_all(path, filename_regex): - """Find all files in ``path`` that match ``filename_regex`` regex.""" - path = os.path.abspath(path) - for root, dirs, files in os.walk(path, topdown=True): - dirs.sort() - for filename in files: - if re.match(filename_regex, filename): - yield os.path.abspath(os.path.join(root, filename)) - - def find_one(path, filename_regex): """Find the first file in ``path`` that match ``filename_regex`` regex.""" - for _path in find_all(path, filename_regex): - return _path + _path = os.path.abspath(path) + for filename in os.listdir(_path): + if re.match(filename_regex, filename): + return os.path.join(_path, filename) + return '' diff --git a/readthedocs/config/tests/test_config.py b/readthedocs/config/tests/test_config.py index ec101523680..4683a017781 100644 --- a/readthedocs/config/tests/test_config.py +++ b/readthedocs/config/tests/test_config.py @@ -104,12 +104,11 @@ def get_env_config(extra=None): @pytest.mark.parametrize('files', [ - {}, - {'readthedocs.ymlmore': ''}, - {'startreadthedocs.yml': ''}, - {'noroot': {'readthedocs.ymlmore': ''}}, - {'noroot': {'startreadthedocs.yml': ''}}, - {'readthebots.yaml': ''}, + {'readthedocs.ymlmore': ''}, {'first': {'readthedocs.yml': ''}}, + {'startreadthedocs.yml': ''}, {'second': {'confuser.txt': 'content'}}, + {'noroot': {'readthedocs.ymlmore': ''}}, {'third': {'readthedocs.yml': 'content', 'Makefile': ''}}, + {'noroot': {'startreadthedocs.yml': ''}}, {'fourth': {'samplefile.yaml': 'content'}}, + {'readthebots.yaml': ''}, {'fifth': {'confuser.txt': '', 'readthedocs.yml': 'content'}}, ]) def test_load_no_config_file(tmpdir, files): apply_fs(tmpdir, files) diff --git a/readthedocs/config/tests/test_find.py b/readthedocs/config/tests/test_find.py index 2005d839277..7ba651059e3 100644 --- a/readthedocs/config/tests/test_find.py +++ b/readthedocs/config/tests/test_find.py @@ -1,77 +1,24 @@ from __future__ import division, print_function, unicode_literals import os - import pytest import six - -from readthedocs.config.find import find_all, find_one +from readthedocs.config.find import find_one from .utils import apply_fs def test_find_no_files(tmpdir): with tmpdir.as_cwd(): - paths = list(find_all(os.getcwd(), r'readthedocs.yml')) - assert len(paths) == 0 + path = find_one(os.getcwd(), r'readthedocs.yml') + assert path == '' def test_find_at_root(tmpdir): apply_fs(tmpdir, {'readthedocs.yml': '', 'otherfile.txt': ''}) - - base = str(tmpdir) - paths = list(find_all(base, r'readthedocs\.yml')) - assert paths == [ - os.path.abspath(os.path.join(base, 'readthedocs.yml')) - ] - - -def test_find_nested(tmpdir): - apply_fs(tmpdir, { - 'first': { - 'readthedocs.yml': '', - }, - 'second': { - 'confuser.txt': 'content', - }, - 'third': { - 'readthedocs.yml': 'content', - 'Makefile': '', - }, - }) - apply_fs(tmpdir, {'first/readthedocs.yml': ''}) - - base = str(tmpdir) - paths = set(find_all(base, r'readthedocs\.yml')) - assert paths == { - str(tmpdir.join('first', 'readthedocs.yml')), - str(tmpdir.join('third', 'readthedocs.yml')), - } - - -def test_find_multiple_files(tmpdir): - apply_fs(tmpdir, { - 'first': { - 'readthedocs.yml': '', - '.readthedocs.yml': 'content', - }, - 'second': { - 'confuser.txt': 'content', - }, - 'third': { - 'readthedocs.yml': 'content', - 'Makefile': '', - }, - }) - apply_fs(tmpdir, {'first/readthedocs.yml': ''}) - base = str(tmpdir) - paths = set(find_all(base, r'\.?readthedocs\.yml')) - assert paths == { - str(tmpdir.join('first', 'readthedocs.yml')), - str(tmpdir.join('first', '.readthedocs.yml')), - str(tmpdir.join('third', 'readthedocs.yml')), - } + path = find_one(base, r'readthedocs\.yml') + assert path == os.path.abspath(os.path.join(base, 'readthedocs.yml')) @pytest.mark.skipif(not six.PY2, reason='Only for python2')