Skip to content

Commit

Permalink
handle symlinks in a semi dry manner
Browse files Browse the repository at this point in the history
  • Loading branch information
james-powis committed Jan 18, 2024
1 parent 0d7ae7b commit fe91e44
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
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
Binary file added yamllint/.cli.py.swp
Binary file not shown.
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

0 comments on commit fe91e44

Please sign in to comment.