def test_filter_files_by_patterns(self): #Assuming <../tests/foo> is <../User/> base_path = Utils.join_path(self.base_path, 'foo') os.makedirs(Utils.join_path(self.base_path, 'foo', 'bar')) open(Utils.join_path(base_path, 'foo.txt'), 'a').close() open(Utils.join_path(base_path, 'bar.rb'), 'a').close() open(Utils.join_path(base_path, 'bar', 'foo.txt'), 'a').close() open(Utils.join_path(base_path, 'bar', 'foo.py'), 'a').close() files = Utils.get_files(base_path) patterns = Utils.parse_patterns(['.rb', '.py'], base_path) expected = sorted([ Utils.join_path(base_path, 'bar.rb'), Utils.join_path(base_path, 'bar', 'foo.py') ]) filtered_files = sorted(Utils.filter_files_by_patterns( files, patterns)) self.assertEqual(len(filtered_files), 2) self.assertListEqual(filtered_files, expected) patterns = Utils.parse_patterns(['bar/foo.txt'], base_path) expected = [Utils.join_path(base_path, 'bar', 'foo.txt')] filtered_files = sorted(Utils.filter_files_by_patterns( files, patterns)) self.assertEqual(len(filtered_files), 1) self.assertListEqual(filtered_files, expected) shutil.rmtree(Utils.join_path(self.base_path, 'foo'))
def test_filter_files_by_patterns(self): #Assuming <../tests/foo> is <../User/> base_path = Utils.join_path(self.base_path, 'foo') self.create_folder(Utils.join_path(self.base_path, 'foo', 'bar')) open(Utils.join_path(base_path, 'foo.txt'), 'a').close() open(Utils.join_path(base_path, 'bar.rb'), 'a').close() open(Utils.join_path(base_path, 'bar', 'foo.txt'), 'a').close() open(Utils.join_path(base_path, 'bar', 'foo.py'), 'a').close() open(Utils.join_path(base_path, 'bar', 'main.go'), 'a').close() open(Utils.join_path(base_path, 'bar', 'other.go'), 'a').close() files = Utils.get_files(base_path) patterns = Utils.parse_patterns([ '.rb', '.py', Utils.join_path('bar', '*.go') ], base_path) expected = sorted([ Utils.join_path(base_path, 'bar.rb'), Utils.join_path(base_path, 'bar', 'foo.py'), Utils.join_path(base_path, 'bar', 'main.go'), Utils.join_path(base_path, 'bar', 'other.go') ]) filtered_files = sorted(Utils.filter_files_by_patterns(files, patterns)) self.assertEqual(len(filtered_files), 4) self.assertListEqual(filtered_files, expected) patterns = Utils.parse_patterns(['bar/foo.txt'], base_path) expected = [Utils.join_path(base_path, 'bar', 'foo.txt')] filtered_files = sorted(Utils.filter_files_by_patterns(files, patterns)) self.assertEqual(len(filtered_files), 1) self.assertListEqual(filtered_files, expected) self.delete_folder(Utils.join_path(self.base_path, 'foo'))
def test_filter_files_by_patterns(self): #Assuming <../tests/foo> is <../User/> base_path = Utils.join_path(self.base_path, 'foo') self.create_folder(Utils.join_path(self.base_path, 'foo', 'bar')) open(Utils.join_path(base_path, 'foo.txt'), 'a').close() open(Utils.join_path(base_path, 'bar.rb'), 'a').close() open(Utils.join_path(base_path, 'bar', 'foo.txt'), 'a').close() open(Utils.join_path(base_path, 'bar', 'foo.py'), 'a').close() open(Utils.join_path(base_path, 'bar', 'main.go'), 'a').close() open(Utils.join_path(base_path, 'bar', 'other.go'), 'a').close() files = Utils.get_files(base_path) patterns = Utils.parse_patterns( ['.rb', '.py', Utils.join_path('bar', '*.go')], base_path) expected = sorted([ Utils.join_path(base_path, 'bar.rb'), Utils.join_path(base_path, 'bar', 'foo.py'), Utils.join_path(base_path, 'bar', 'main.go'), Utils.join_path(base_path, 'bar', 'other.go') ]) filtered_files = sorted(Utils.filter_files_by_patterns( files, patterns)) self.assertEqual(len(filtered_files), 4) self.assertListEqual(filtered_files, expected) patterns = Utils.parse_patterns(['bar/foo.txt'], base_path) expected = [Utils.join_path(base_path, 'bar', 'foo.txt')] filtered_files = sorted(Utils.filter_files_by_patterns( files, patterns)) self.assertEqual(len(filtered_files), 1) self.assertListEqual(filtered_files, expected) self.delete_folder(Utils.join_path(self.base_path, 'foo'))
def test_get_files(self): with self.assertRaises(TypeError): Utils.get_files() self.assertListEqual(Utils.get_files('t'), []) self.assertListEqual(Utils.get_files(1234), []) self.assertListEqual(Utils.get_files(1234), []) self.assertGreater(len(Utils.get_files(self.base_path)), 0) #Create a test folder structure self.create_folder(Utils.join_path(self.base_path, 'hello', 'world')) open(Utils.join_path(self.base_path, 'hello', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'hello', 'bar.txt'), 'a').close() open(Utils.join_path(self.base_path, 'hello', 'world', 'foo.txt'), 'a').close() allFiles = sorted([ Utils.join_path(self.base_path, 'hello', 'bar.txt'), Utils.join_path(self.base_path, 'hello', 'foo.txt'), Utils.join_path(self.base_path, 'hello', 'world', 'foo.txt') ]) files = sorted(Utils.get_files(Utils.join_path(self.base_path, 'hello'))) self.assertEqual(len(files), 3) self.assertListEqual(files, allFiles) self.delete_folder(Utils.join_path(self.base_path, 'hello'))
def test_get_files(self): with self.assertRaises(TypeError): Utils.get_files() self.assertListEqual(Utils.get_files('t'), []) self.assertListEqual(Utils.get_files(1234), []) self.assertListEqual(Utils.get_files(1234), []) self.assertGreater(len(Utils.get_files(self.base_path)), 0) #Create a test folder structure self.create_folder(Utils.join_path(self.base_path, 'hello', 'world')) open(Utils.join_path(self.base_path, 'hello', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'hello', 'bar.txt'), 'a').close() open(Utils.join_path(self.base_path, 'hello', 'world', 'foo.txt'), 'a').close() allFiles = sorted([ Utils.join_path(self.base_path, 'hello', 'bar.txt'), Utils.join_path(self.base_path, 'hello', 'foo.txt'), Utils.join_path(self.base_path, 'hello', 'world', 'foo.txt') ]) files = sorted( Utils.get_files(Utils.join_path(self.base_path, 'hello'))) self.assertEqual(len(files), 3) self.assertListEqual(files, allFiles) self.delete_folder(Utils.join_path(self.base_path, 'hello'))
def test_exclude_files_by_patterns(self): #Assuming <../tests/foo> is <../User/> self.create_folder(Utils.join_path(self.base_path, 'foo', 'bar')) open(Utils.join_path(self.base_path, 'foo', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go'), 'a').close() files = sorted(Utils.get_files(Utils.join_path(self.base_path, 'foo'))) #Unfiltered self.assertEqual(len(files), 6) self.assertListEqual( sorted(Utils.exclude_files_by_patterns(files, [])), files) self.assertListEqual( sorted(Utils.exclude_files_by_patterns(files, ['.boo'])), files) # By extension filteredFiles = Utils.exclude_files_by_patterns(files, ['.txt']) self.assertEqual(len(filteredFiles), 3) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) filteredFiles = sorted(Utils.exclude_files_by_patterns(files, ['.py'])) self.assertEqual(len(filteredFiles), 5) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) # By Filename filteredFiles = sorted( Utils.exclude_files_by_patterns( files, [Utils.join_path(self.base_path, 'foo', 'bar.txt')])) self.assertEqual(len(filteredFiles), 5) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) filteredFiles = sorted( Utils.exclude_files_by_patterns(files, [ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt') ])) self.assertEqual(len(filteredFiles), 4) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) # By folder filteredFiles = sorted( Utils.exclude_files_by_patterns( files, [Utils.join_path(self.base_path, 'foo', 'bar')])) self.assertEqual(len(filteredFiles), 2) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt'), ])) # By willcard filteredFiles = sorted( Utils.exclude_files_by_patterns( files, [Utils.join_path(self.base_path, 'foo', '**', '*.go')])) self.assertEqual(len(filteredFiles), 4) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py') ])) self.delete_folder(Utils.join_path(self.base_path, 'foo'))
def test_exclude_files_by_patterns(self): #Assuming <../tests/foo> is <../User/> os.makedirs(Utils.join_path(self.base_path, 'foo', 'bar')) open(Utils.join_path(self.base_path, 'foo', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), 'a').close() files = Utils.get_files(Utils.join_path(self.base_path, 'foo')) #Unfiltered self.assertEqual(len(files), 4) self.assertListEqual(Utils.exclude_files_by_patterns(files, []), files) self.assertListEqual(Utils.exclude_files_by_patterns(files, ['.boo']), files) # By extension filteredFiles = Utils.exclude_files_by_patterns(files, ['.txt']) self.assertEqual(len(filteredFiles), 1) self.assertListEqual( filteredFiles, [Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py')]) filteredFiles = sorted(Utils.exclude_files_by_patterns(files, ['.py'])) self.assertEqual(len(filteredFiles), 3) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt') ])) # By Filename filteredFiles = sorted( Utils.exclude_files_by_patterns( files, [Utils.join_path(self.base_path, 'foo', 'bar.txt')])) self.assertEqual(len(filteredFiles), 3) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt') ])) filteredFiles = sorted( Utils.exclude_files_by_patterns(files, [ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt') ])) self.assertEqual(len(filteredFiles), 2) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt') ])) # By folder filteredFiles = sorted( Utils.exclude_files_by_patterns( files, [Utils.join_path(self.base_path, 'foo', 'bar')])) self.assertEqual(len(filteredFiles), 2) self.assertListEqual( filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt') ])) shutil.rmtree(Utils.join_path(self.base_path, 'foo'))
def test_exclude_files_by_patterns(self): #Assuming <../tests/foo> is <../User/> self.create_folder(Utils.join_path(self.base_path, 'foo', 'bar')) open(Utils.join_path(self.base_path, 'foo', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), 'a').close() open(Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go'), 'a').close() files = sorted(Utils.get_files(Utils.join_path(self.base_path, 'foo'))) #Unfiltered self.assertEqual(len(files), 6) self.assertListEqual(sorted(Utils.exclude_files_by_patterns(files, [])), files) self.assertListEqual(sorted(Utils.exclude_files_by_patterns(files, ['.boo'])), files) # By extension filteredFiles = Utils.exclude_files_by_patterns(files, ['.txt']) self.assertEqual(len(filteredFiles), 3) self.assertListEqual(filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) filteredFiles = sorted(Utils.exclude_files_by_patterns(files, ['.py'])) self.assertEqual(len(filteredFiles), 5) self.assertListEqual(filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) # By Filename filteredFiles = sorted(Utils.exclude_files_by_patterns(files, [ Utils.join_path(self.base_path, 'foo', 'bar.txt') ])) self.assertEqual(len(filteredFiles), 5) self.assertListEqual(filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) filteredFiles = sorted(Utils.exclude_files_by_patterns(files, [ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt') ])) self.assertEqual(len(filteredFiles), 4) self.assertListEqual(filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'file.go'), Utils.join_path(self.base_path, 'foo', 'bar', 'new_file.go') ])) # By folder filteredFiles = sorted(Utils.exclude_files_by_patterns(files, [ Utils.join_path(self.base_path, 'foo', 'bar') ])) self.assertEqual(len(filteredFiles), 2) self.assertListEqual(filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'foo.txt'), ])) # By willcard filteredFiles = sorted(Utils.exclude_files_by_patterns(files, [ Utils.join_path(self.base_path, 'foo', '**', '*.go') ])) self.assertEqual(len(filteredFiles), 4) self.assertListEqual(filteredFiles, sorted([ Utils.join_path(self.base_path, 'foo', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.txt'), Utils.join_path(self.base_path, 'foo', 'bar', 'foo.py') ])) self.delete_folder(Utils.join_path(self.base_path, 'foo'))