refactor load_folder_files: change filter behavior

This commit is contained in:
debugtalk
2017-09-18 11:45:12 +08:00
parent 8f73e11f49
commit 479f0e5860
2 changed files with 27 additions and 7 deletions

View File

@@ -1,5 +1,4 @@
import codecs
import fnmatch
import hashlib
import hmac
import imp
@@ -57,20 +56,35 @@ def load_testcases(testcase_file_path):
# '' or other suffix
return []
def load_folder_files(folder_path, match_filter_list=["*"]):
def load_folder_files(folder_path, file_type, recursive=False):
""" load folder path, return all files in list format.
@param
folder_path: specified folder path to load
file_type: "test" or "api"
recursive: if True, will load files recursively
"""
file_list = []
for dirpath, dirnames, filenames in os.walk(folder_path):
filenames_list = []
for match_filter in match_filter_list:
filenames_list.extend(fnmatch.filter(filenames, match_filter))
for filename in filenames:
if not filename.endswith(('.yml', '.yaml', '.json')):
continue
if file_type == "api" and not filename.startswith(('api.', 'api-')):
continue
filenames_list.append(filename)
for filename in filenames_list:
file_path = os.path.join(dirpath, filename)
file_list.append(file_path)
if not recursive:
break
return file_list
def load_testcases_by_path(path):
@@ -99,7 +113,7 @@ def load_testcases_by_path(path):
path = os.path.join(os.getcwd(), path)
if os.path.isdir(path):
files_list = load_folder_files(path, ["*.yml", "*.yaml", "*.json"])
files_list = load_folder_files(path, file_type="test", recursive=True)
return load_testcases_by_path(files_list)
elif os.path.isfile(path):

View File

@@ -36,10 +36,16 @@ class TestUtils(ApiServerUnittest):
file1 = os.path.join(os.getcwd(), 'tests', 'test_utils.py')
file2 = os.path.join(os.getcwd(), 'tests', 'data', 'demo_binds.yml')
files = utils.load_folder_files(folder, ["*.py"])
self.assertIn(file1, files)
files = utils.load_folder_files(folder, file_type="test", recursive=False)
self.assertNotIn(file2, files)
files = utils.load_folder_files(folder, file_type="test", recursive=True)
self.assertIn(file2, files)
self.assertNotIn(file1, files)
files = utils.load_folder_files(folder, file_type="api", recursive=True)
self.assertEqual(files, [])
def test_load_testcases_by_path_files(self):
testsets_list = []