From e80194d1c475467b21093bdf94dcaafb5914bec5 Mon Sep 17 00:00:00 2001 From: James Powis Date: Wed, 17 Jan 2024 14:55:37 -0700 Subject: [PATCH] handle symlinks in a semi dry manner --- tests/common.py | 10 +++++++++- tests/test_cli.py | 16 ++++++++++------ yamllint/.cli.py.swp | Bin 0 -> 24576 bytes yamllint/cli.py | 23 +++++++++++++++++++---- 4 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 yamllint/.cli.py.swp diff --git a/tests/common.py b/tests/common.py index 1aef9e62..df235693 100644 --- a/tests/common.py +++ b/tests/common.py @@ -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' diff --git a/tests/test_cli.py b/tests/test_cli.py index bcdd000b..7dd6c015 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -86,6 +86,8 @@ def setUpModule(): class CommandLineTestCase(unittest.TestCase): + maxDiff = None + @classmethod def setUpClass(cls): super().setUpClass() @@ -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 @@ -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'), @@ -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'), @@ -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'), @@ -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'), @@ -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'), diff --git a/yamllint/.cli.py.swp b/yamllint/.cli.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..3a17f5d93ef6d5aa0e32f4c7d396acf9bb71382b GIT binary patch literal 24576 zcmeHOYm6k02%!udfEvUnK@Fb)F6IEWAwgbZ=ke}E*2BuFa~ zB))SWRkwP&XU3}}il9pRx~gy8d+s^sKI)!x&)u9nJHNo*QJ%DL{itO<8vHhY>2Ghk z6T5EL_q`y= z9-6*a?(H5xvUpu+pwPf!G|*4lrTcHUPE1dUp?>VA?q+YgYyB`Ki#&w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aE?^Cp9L#PUy7JCIXQl!jci(KuFW9tIvRNys-qghESdWxhssQUf; zj|wU{3=5(T(tC4@H!mHeWuQm4PEiubzFnnVTM?W~)v38!Y>AKB*P-LlQ5hE|5i%XZ%q* z$%j@MA`QJ2hxmkQ^xi(D(EgKSm(6Xv^pAKzFU($-ce}m~zNtkIZZsPW1Q|u0N`GlE zL$+@(x9D}i41}eHaNCRU@xWj=BYwoTD|I;WB{Q_ANK`7=i5K%qMIR8@mmD3vQ(rxU z7&V0?I49zaD855XI(Z}5=z3IwF3EJ2&AEu;vP6>cBWY4gWSy2?gGEs=Ys1E620$cO!ej5URS2aj=tU<2#vnK|kvvD5_%PM3>i9NN=9#^T7_6QnRIB>$`m#A^Ynm8yib2@1CnKZ44ZqGvtP<9G$y1L#HZM2QrfiMFm3|M6A7) zlzOz%mr>+5RXQoAxKZJVG>CISaQIToK*w~xBgxd9N6ti10i|irqHdMk8D=ws={ge! zr7s#b3q;cd@@Hm=0= 2 and target_path[1] == ':') + if is_relative: + 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 + else: + if os.path.exists(target_path) and conf.is_yaml_file(target_path) and not conf.is_file_ignored(target_path): + yield target_path + + elif conf.is_yaml_file(filepath): + yield filepath else: yield item @@ -208,14 +223,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: