relocate functions related to debugtalk.py

This commit is contained in:
debugtalk
2018-11-15 16:06:49 +08:00
parent 626612061e
commit 58fa2a4bf4
2 changed files with 49 additions and 41 deletions

View File

@@ -184,7 +184,7 @@ def load_dot_env_file(dot_env_path):
def locate_file(start_path, file_name): def locate_file(start_path, file_name):
""" locate filename and return file path. """ locate filename and return absolute file path.
searching will be recursive upward until current working directory. searching will be recursive upward until current working directory.
Args: Args:
@@ -206,7 +206,7 @@ def locate_file(start_path, file_name):
file_path = os.path.join(start_dir_path, file_name) file_path = os.path.join(start_dir_path, file_name)
if os.path.isfile(file_path): if os.path.isfile(file_path):
return file_path return os.path.abspath(file_path)
# current working directory # current working directory
if os.path.abspath(start_dir_path) in [os.getcwd(), os.path.abspath(os.sep)]: if os.path.abspath(start_dir_path) in [os.getcwd(), os.path.abspath(os.sep)]:
@@ -251,13 +251,10 @@ def load_builtin_functions():
return load_module_functions(built_in) return load_module_functions(built_in)
def load_debugtalk_functions(debugtalk_path): def load_debugtalk_functions():
""" load project debugtalk.py module functions """ load project debugtalk.py module functions
debugtalk.py should be located in project working directory. debugtalk.py should be located in project working directory.
Args:
debugtalk_path(str): debugtalk.py path
Returns: Returns:
dict: debugtalk module functions mapping dict: debugtalk module functions mapping
{ {
@@ -266,9 +263,6 @@ def load_debugtalk_functions(debugtalk_path):
} }
""" """
if not debugtalk_path:
return {}
# load debugtalk.py module # load debugtalk.py module
imported_module = importlib.import_module("debugtalk") imported_module = importlib.import_module("debugtalk")
return load_module_functions(imported_module) return load_module_functions(imported_module)
@@ -839,18 +833,40 @@ def load_test_folder(test_folder_path):
return test_definition_mapping return test_definition_mapping
def locate_debugtalk_py(start_path): def load_debugtalk_py(start_path):
""" locate debugtalk.py file. """ locate debugtalk.py file and returns PWD and debugtalk.py functions.
Args: Args:
start_path (str): start locating path, maybe testcase file path or directory path start_path (str): start locating path, maybe testcase file path or directory path
Returns:
tuple: (project_working_directory, debugtalk_functions)
""" """
try: try:
# locate debugtalk.py file.
debugtalk_path = locate_file(start_path, "debugtalk.py") debugtalk_path = locate_file(start_path, "debugtalk.py")
return os.path.abspath(debugtalk_path)
# The folder contains debugtalk.py will be treated as PWD.
project_working_directory = os.path.dirname(debugtalk_path)
# add PWD to sys.path
sys.path.insert(0, project_working_directory)
# load debugtalk.py functions
debugtalk_functions = load_debugtalk_functions()
except exceptions.FileNotFound: except exceptions.FileNotFound:
return None
# debugtalk.py not found, use os.getcwd() as PWD.
project_working_directory = os.getcwd()
# add PWD to sys.path
sys.path.insert(0, project_working_directory)
debugtalk_functions = {}
return project_working_directory, debugtalk_functions
def load_project_tests(test_path, dot_env_path=None): def load_project_tests(test_path, dot_env_path=None):
@@ -867,17 +883,9 @@ def load_project_tests(test_path, dot_env_path=None):
""" """
project_mapping = {} project_mapping = {}
debugtalk_path = locate_debugtalk_py(test_path) # locate PWD and load debugtalk.py functions
# locate PWD with debugtalk.py path project_working_directory, debugtalk_functions = load_debugtalk_py(test_path)
if debugtalk_path: project_mapping["functions"] = debugtalk_functions
# The folder contains debugtalk.py will be treated as PWD.
project_working_directory = os.path.dirname(debugtalk_path)
else:
# debugtalk.py is not found, use os.getcwd() as PWD.
project_working_directory = os.getcwd()
# add PWD to sys.path
sys.path.insert(0, project_working_directory)
# load .env # load .env
dot_env_path = dot_env_path or os.path.join(project_working_directory, ".env") dot_env_path = dot_env_path or os.path.join(project_working_directory, ".env")
@@ -886,9 +894,6 @@ def load_project_tests(test_path, dot_env_path=None):
else: else:
project_mapping["env"] = {} project_mapping["env"] = {}
# load debugtalk.py
project_mapping["functions"] = load_debugtalk_functions(debugtalk_path)
project_mapping["def-api"] = load_api_folder(os.path.join(project_working_directory, "api")) project_mapping["def-api"] = load_api_folder(os.path.join(project_working_directory, "api"))
# TODO: replace suite with testcases # TODO: replace suite with testcases
project_mapping["def-testcase"] = load_test_folder(os.path.join(project_working_directory, "suite")) project_mapping["def-testcase"] = load_test_folder(os.path.join(project_working_directory, "suite"))

View File

@@ -172,19 +172,19 @@ class TestFileLoader(unittest.TestCase):
) )
self.assertEqual( self.assertEqual(
loader.locate_file("tests/", "debugtalk.py"), loader.locate_file("tests/", "debugtalk.py"),
"tests/debugtalk.py" os.path.join(os.getcwd(), "tests", "debugtalk.py")
) )
self.assertEqual( self.assertEqual(
loader.locate_file("tests", "debugtalk.py"), loader.locate_file("tests", "debugtalk.py"),
"tests/debugtalk.py" os.path.join(os.getcwd(), "tests", "debugtalk.py")
) )
self.assertEqual( self.assertEqual(
loader.locate_file("tests/base.py", "debugtalk.py"), loader.locate_file("tests/base.py", "debugtalk.py"),
"tests/debugtalk.py" os.path.join(os.getcwd(), "tests", "debugtalk.py")
) )
self.assertEqual( self.assertEqual(
loader.locate_file("tests/data/demo_testcase.yml", "debugtalk.py"), loader.locate_file("tests/data/demo_testcase.yml", "debugtalk.py"),
"tests/debugtalk.py" os.path.join(os.getcwd(), "tests", "debugtalk.py")
) )
@@ -206,24 +206,27 @@ class TestModuleLoader(unittest.TestCase):
self.assertTrue(is_status_code_200(200)) self.assertTrue(is_status_code_200(200))
self.assertFalse(is_status_code_200(500)) self.assertFalse(is_status_code_200(500))
def test_locate_debugtalk_py(self): def test_load_debugtalk_py(self):
debugtalk_path = loader.locate_debugtalk_py("tests/data/demo_testcase.yml") project_working_directory, debugtalk_functions = loader.load_debugtalk_py("tests/data/demo_testcase.yml")
self.assertEqual( self.assertEqual(
debugtalk_path, project_working_directory,
os.path.join(os.getcwd(), "tests", "debugtalk.py") os.path.join(os.getcwd(), "tests")
) )
self.assertIn("gen_md5", debugtalk_functions)
debugtalk_path = loader.locate_debugtalk_py("tests/base.py") project_working_directory, debugtalk_functions = loader.load_debugtalk_py("tests/base.py")
self.assertEqual( self.assertEqual(
debugtalk_path, project_working_directory,
os.path.join(os.getcwd(), "tests", "debugtalk.py") os.path.join(os.getcwd(), "tests")
) )
self.assertIn("gen_md5", debugtalk_functions)
debugtalk_path = loader.locate_debugtalk_py("httprunner/__init__.py") project_working_directory, debugtalk_functions = loader.load_debugtalk_py("httprunner/__init__.py")
self.assertEqual( self.assertEqual(
debugtalk_path, project_working_directory,
None os.getcwd()
) )
self.assertEqual(debugtalk_functions, {})
def test_load_tests(self): def test_load_tests(self):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(