Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle symlinks in a semi dry manner #647

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ def build_temp_workspace(files):
tempdir = tempfile.mkdtemp(prefix='yamllint-tests-')

for path, content in files.items():
is_symlink = False
if path.startswith("symlink:"):
is_symlink = True
path = path.replace("symlink:", "")

path = os.path.join(tempdir, path).encode('utf-8')
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
if is_symlink:
target = os.path.join(os.path.dirname(path), content.encode('utf-8'))
os.symlink(target, path)

if isinstance(content, list):
elif isinstance(content, list):
os.mkdir(path)
else:
mode = 'wb' if isinstance(content, bytes) else 'w'
Expand Down
16 changes: 10 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def setUpModule():


class CommandLineTestCase(unittest.TestCase):
maxDiff = None

@classmethod
def setUpClass(cls):
super().setUpClass()
Expand Down Expand Up @@ -130,7 +132,9 @@ def setUpClass(cls):
'a: true',
'en.yaml': '---\n'
'a: true\n'
'A: true'
'A: true',
'symlink:some/other/directory/en.yaml': '../../../en.yaml',
'symlink:abslink.yml': '/tmp/non-exist.yml',
})

@classmethod
Expand All @@ -142,7 +146,7 @@ def tearDownClass(cls):
def test_find_files_recursively(self):
conf = config.YamlLintConfig('extends: default')
self.assertEqual(
sorted(cli.find_files_recursively([self.wd], conf)),
sorted(set(cli.find_files_recursively([self.wd], conf))),
[os.path.join(self.wd, 'a.yaml'),
os.path.join(self.wd, 'c.yaml'),
os.path.join(self.wd, 'dos.yml'),
Expand All @@ -151,7 +155,7 @@ def test_find_files_recursively(self):
os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'),
os.path.join(self.wd, 'sub/directory.yaml/empty.yml'),
os.path.join(self.wd, 'sub/ok.yaml'),
os.path.join(self.wd, 'warn.yaml')],
os.path.join(self.wd, 'warn.yaml')]
)

items = [os.path.join(self.wd, 'sub/ok.yaml'),
Expand Down Expand Up @@ -182,7 +186,7 @@ def test_find_files_recursively(self):
'yaml-files:\n'
' - \'*.yaml\' \n')
self.assertEqual(
sorted(cli.find_files_recursively([self.wd], conf)),
sorted(set(cli.find_files_recursively([self.wd], conf))),
[os.path.join(self.wd, 'a.yaml'),
os.path.join(self.wd, 'c.yaml'),
os.path.join(self.wd, 'en.yaml'),
Expand Down Expand Up @@ -213,7 +217,7 @@ def test_find_files_recursively(self):
'yaml-files:\n'
' - \'*\'\n')
self.assertEqual(
sorted(cli.find_files_recursively([self.wd], conf)),
sorted(set(cli.find_files_recursively([self.wd], conf))),
[os.path.join(self.wd, 'a.yaml'),
os.path.join(self.wd, 'c.yaml'),
os.path.join(self.wd, 'dos.yml'),
Expand All @@ -234,7 +238,7 @@ def test_find_files_recursively(self):
' - \'*\'\n'
' - \'**\'\n')
self.assertEqual(
sorted(cli.find_files_recursively([self.wd], conf)),
sorted(set(cli.find_files_recursively([self.wd], conf))),
[os.path.join(self.wd, 'a.yaml'),
os.path.join(self.wd, 'c.yaml'),
os.path.join(self.wd, 'dos.yml'),
Expand Down
17 changes: 13 additions & 4 deletions yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ def find_files_recursively(items, conf):
for root, _dirnames, filenames in os.walk(item):
for f in filenames:
filepath = os.path.join(root, f)
if conf.is_yaml_file(filepath):
yield filepath
if conf.is_yaml_file(filepath) and not conf.is_file_ignored(filepath):
if os.path.islink(filepath):
target_path = os.readlink(filepath)
relative_path = os.path.join(filepath, target_path)
if os.path.exists(relative_path):
norm_path = os.path.normpath(relative_path)
if conf.is_yaml_file(norm_path) and not conf.is_file_ignored(norm_path):
yield norm_path

elif conf.is_yaml_file(filepath):
yield filepath
else:
yield item

Expand Down Expand Up @@ -208,14 +217,14 @@ def run(argv=None):
locale.setlocale(locale.LC_ALL, conf.locale)

if args.list_files:
for file in find_files_recursively(args.files, conf):
for file in set(find_files_recursively(args.files, conf)):
if not conf.is_file_ignored(file):
print(file)
sys.exit(0)

max_level = 0

for file in find_files_recursively(args.files, conf):
for file in set(find_files_recursively(args.files, conf)):
filepath = file[2:] if file.startswith('./') else file
try:
with open(file, newline='') as f:
Expand Down
Loading