Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hirosassa committed Jul 10, 2021
1 parent c9a7af0 commit eba2d63
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion yapf/yapflib/file_resources.py
Expand Up @@ -69,7 +69,7 @@ def _GetExcludePatternsFromPyprojectToml(filename):
if line.strip() and not line.startswith('#'):
ignore_patterns.append(line.strip())
if any(e.startswith('./') for e in ignore_patterns):
raise errors.YapfError('path in .yapfignore should not start with ./')
raise errors.YapfError('path in pyproject.toml should not start with ./')

return ignore_patterns

Expand Down
41 changes: 41 additions & 0 deletions yapftests/file_resources_test.py
Expand Up @@ -64,37 +64,78 @@ def test_get_exclude_file_patterns_from_yapfignore(self):
sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),
sorted(ignore_patterns))

def test_get_exclude_file_patterns_from_yapfignore_with_wrong_syntax(self):
local_ignore_file = os.path.join(self.test_tmpdir, '.yapfignore')
ignore_patterns = ['temp/**/*.py', './wrong/syntax/*.py']
with open(local_ignore_file, 'w') as f:
f.writelines('\n'.join(ignore_patterns))

with self.assertRaises(errors.YapfError):
file_resources.GetExcludePatternsForDir(self.test_tmpdir)

def test_get_exclude_file_patterns_from_pyproject(self):
try:
import toml
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
ignore_patterns = ['temp/**/*.py', 'temp2/*.py']
with open(local_ignore_file, 'w') as f:
f.write('[tool.yapfignore]\n')
f.write('ignore_patterns="""')
f.writelines('\n'.join(ignore_patterns))
f.write('"""')

self.assertEqual(
sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),
sorted(ignore_patterns))

def test_get_exclude_file_patterns_from_pyproject_with_wrong_syntax(self):
try:
import toml
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
ignore_patterns = ['temp/**/*.py', './wrong/syntax/*.py']
with open(local_ignore_file, 'w') as f:
f.write('[tool.yapfignore]\n')
f.write('ignore_patterns="""')
f.writelines('\n'.join(ignore_patterns))
f.write('"""')

with self.assertRaises(errors.YapfError):
file_resources.GetExcludePatternsForDir(self.test_tmpdir)

def test_get_exclude_file_patterns_from_pyproject_no_ignore_section(self):
try:
import toml
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
ignore_patterns = []
open(local_ignore_file, 'w').close()

self.assertEqual(
sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),
sorted(ignore_patterns))

def test_get_exclude_file_patterns_from_pyproject_ignore_section_empty(self):
try:
import toml
except ImportError:
return
local_ignore_file = os.path.join(self.test_tmpdir, 'pyproject.toml')
ignore_patterns = []
with open(local_ignore_file, 'w') as f:
f.write('[tool.yapfignore]\n')

self.assertEqual(
sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),
sorted(ignore_patterns))

def test_get_exclude_file_patterns_with_no_config_files(self):
ignore_patterns = []

self.assertEqual(
sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),
sorted(ignore_patterns))
Expand Down

0 comments on commit eba2d63

Please sign in to comment.