Skip to content

Commit

Permalink
Merge d5edf5b into 59e1bac
Browse files Browse the repository at this point in the history
  • Loading branch information
ericboyangan committed Jul 19, 2019
2 parents 59e1bac + d5edf5b commit 72cbd93
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion yapf/yapflib/file_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE):
if config.has_section('yapf'):
return config_file

dirname = os.path.dirname(dirname)
if (not dirname or not os.path.basename(dirname) or
dirname == os.path.abspath(os.path.sep)):
break
dirname = os.path.dirname(dirname)

global_file = os.path.expanduser(style.GLOBAL_STYLE)
if os.path.exists(global_file):
Expand Down
47 changes: 47 additions & 0 deletions yapftests/file_resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def _restore_working_dir():
os.chdir(curdir)


@contextlib.contextmanager
def _exists_mocked_in_module(module, mock_implementation):
unmocked_exists = getattr(module, 'exists')
setattr(module, 'exists', mock_implementation)
try:
yield
finally:
setattr(module, 'exists', unmocked_exists)


class GetExcludePatternsForDir(unittest.TestCase):

def setUp(self): # pylint: disable=g-missing-super-call
Expand Down Expand Up @@ -92,6 +102,43 @@ def test_with_local_style(self):
self.assertEqual(style_file,
file_resources.GetDefaultStyleForDir(test_filename))

def test_setup_config(self):
# An empty setup.cfg file should not be used
setup_config = os.path.join(self.test_tmpdir, 'setup.cfg')
open(setup_config, 'w').close()

test_dir = os.path.join(self.test_tmpdir, 'dir1')
style_name = file_resources.GetDefaultStyleForDir(test_dir)
self.assertEqual(style_name, 'pep8')

# One with a '[yapf]' section should be used
with open(setup_config, 'w') as f:
f.write('[yapf]\n')
self.assertEqual(setup_config,
file_resources.GetDefaultStyleForDir(test_dir))

def test_local_style_at_root(self):
# Test behavior of files located on the root, and under root.
rootdir = os.path.abspath(os.path.sep)
test_dir_at_root = os.path.join(rootdir, 'dir1')
test_dir_under_root = os.path.join(rootdir, 'dir1', 'dir2')

# Fake placing only a style file at the root by mocking `os.path.exists`.
style_file = os.path.join(rootdir, '.style.yapf')

def mock_exists_implementation(path):
return path == style_file

with _exists_mocked_in_module(file_resources.os.path,
mock_exists_implementation):
# Both files should find the style file at the root.
default_style_at_root = file_resources.GetDefaultStyleForDir(
test_dir_at_root)
self.assertEqual(style_file, default_style_at_root)
default_style_under_root = file_resources.GetDefaultStyleForDir(
test_dir_under_root)
self.assertEqual(style_file, default_style_under_root)


def _touch_files(filenames):
for name in filenames:
Expand Down

0 comments on commit 72cbd93

Please sign in to comment.