From 653142eb2074ca216ca132d957f3d520536920d6 Mon Sep 17 00:00:00 2001 From: sedrubal Date: Sun, 28 May 2017 22:57:53 +0200 Subject: [PATCH] Add and fix unittests for --exclude --- tests/test_cli.py | 63 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 8a5bab5..4849616 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -84,8 +84,9 @@ class CommandLineTestCase(unittest.TestCase): shutil.rmtree(self.wd) def test_find_files_recursively(self): + # without excludes self.assertEqual( - sorted(cli.find_files_recursively([self.wd])), + sorted(cli.find_files_recursively([self.wd], [])), [os.path.join(self.wd, 'a.yaml'), os.path.join(self.wd, 'empty.yml'), os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml'), @@ -96,14 +97,14 @@ class CommandLineTestCase(unittest.TestCase): items = [os.path.join(self.wd, 'sub/ok.yaml'), os.path.join(self.wd, 'empty-dir')] self.assertEqual( - sorted(cli.find_files_recursively(items)), + sorted(cli.find_files_recursively(items, [])), [os.path.join(self.wd, 'sub/ok.yaml')], ) items = [os.path.join(self.wd, 'empty.yml'), os.path.join(self.wd, 's')] self.assertEqual( - sorted(cli.find_files_recursively(items)), + sorted(cli.find_files_recursively(items, [])), [os.path.join(self.wd, 'empty.yml'), os.path.join(self.wd, 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml')], ) @@ -111,11 +112,65 @@ class CommandLineTestCase(unittest.TestCase): items = [os.path.join(self.wd, 'sub'), os.path.join(self.wd, '/etc/another/file')] self.assertEqual( - sorted(cli.find_files_recursively(items)), + sorted(cli.find_files_recursively(items, [])), [os.path.join(self.wd, '/etc/another/file'), os.path.join(self.wd, 'sub/ok.yaml')], ) + # with excludes + self.assertEqual( + sorted(cli.find_files_recursively([self.wd], + [os.path.join(self.wd, 's')])), + [os.path.join(self.wd, 'a.yaml'), + os.path.join(self.wd, 'empty.yml'), + os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'warn.yaml')], + ) + + self.assertEqual( + sorted(cli.find_files_recursively([self.wd], + [os.path.join(self.wd, 's*')])), + [os.path.join(self.wd, 'a.yaml'), + os.path.join(self.wd, 'empty.yml'), + os.path.join(self.wd, 'warn.yaml')], + ) + + self.assertEqual( + sorted(cli.find_files_recursively([self.wd], ['*.yml', '*.yaml'])), + [], + ) + + items = [os.path.join(self.wd, 'sub/ok.yaml'), + os.path.join(self.wd, 'empty-dir')] + exclude = [os.path.join(self.wd, 'sub')] + self.assertEqual( + sorted(cli.find_files_recursively(items, exclude)), [items[0]], + ) + exclude[0] += '/' + self.assertEqual( + sorted(cli.find_files_recursively(items, exclude)), [items[0]], + ) + exclude[0] += '*' + self.assertEqual( + sorted(cli.find_files_recursively(items, exclude)), [], + ) + + items = [os.path.join(self.wd, 'empty.yml'), + os.path.join(self.wd, 's')] + exclude = [os.path.join(self.wd, 'empty.yml'), + os.path.join(self.wd, 's/s')] + self.assertEqual( + sorted(cli.find_files_recursively(items, exclude)), [], + ) + + items = [os.path.join(self.wd, 'sub'), + os.path.join(self.wd, '/etc/another/file')] + exclude = [os.path.join(self.wd, 'sub/ok.yaml')] + self.assertEqual( + sorted(cli.find_files_recursively(items, exclude)), + [os.path.join(self.wd, '/etc/another/file')], + ) + def test_run_with_bad_arguments(self): sys.stdout, sys.stderr = StringIO(), StringIO() with self.assertRaises(SystemExit) as ctx: