@@ -626,6 +626,22 @@ def is_library_by_path(path: str) -> bool:
626626 return path .lower ().endswith ((".py" , ".java" , ".class" , "/" , os .sep ))
627627
628628
629+ def update_python_path_and_env (
630+ working_dir : str = "." , pythonpath : Optional [List [str ]] = None , environment : Optional [Dict [str , str ]] = None
631+ ) -> None :
632+ os .chdir (Path (working_dir ))
633+
634+ if pythonpath is not None :
635+ for p in pythonpath :
636+ absolute_path = str (Path (p ).absolute ())
637+ if absolute_path not in sys .path :
638+ sys .path .insert (0 , absolute_path )
639+
640+ if environment :
641+ for k , v in environment .items ():
642+ os .environ [k ] = v
643+
644+
629645__PRELOADED_MODULES : Optional [Set [ModuleType ]] = None
630646
631647
@@ -640,23 +656,12 @@ def _update_env(
640656 top = file .parents [3 ]
641657 for p in filter (lambda v : path_is_relative_to (v , top ), sys .path .copy ()):
642658 sys .path .remove (p )
643- wd = Path (working_dir )
644659
645660 importlib .invalidate_caches ()
646661
647662 gc .collect ()
648663
649- os .chdir (wd )
650-
651- if pythonpath is not None :
652- for p in pythonpath :
653- absolute_path = str (Path (p ).absolute ())
654- if absolute_path not in sys .path :
655- sys .path .insert (0 , absolute_path )
656-
657- if environment :
658- for k , v in environment .items ():
659- os .environ [k ] = v
664+ update_python_path_and_env (working_dir , pythonpath , environment )
660665
661666
662667def unload_preloaded_modules () -> None :
@@ -885,7 +890,10 @@ def report_invalid_syntax(self, message: str, level: str = "ERROR") -> None:
885890
886891
887892def resolve_robot_variables (
888- working_dir : str = "." , base_dir : str = "." , variables : Optional [Dict [str , Optional [Any ]]] = None
893+ working_dir : str = "." ,
894+ base_dir : str = "." ,
895+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
896+ variables : Optional [Dict [str , Optional [Any ]]] = None ,
889897) -> Any :
890898 from robot .variables import Variables
891899
@@ -928,6 +936,10 @@ def resolve_robot_variables(
928936 }.items ():
929937 result [k ] = v
930938
939+ if command_line_variables :
940+ for k , v in command_line_variables .items ():
941+ result [f"${{{ k } }}" ] = v
942+
931943 if variables is not None :
932944
933945 vars = [_Variable (k , v ) for k , v in variables .items () if v is not None ]
@@ -942,10 +954,15 @@ def resolve_variable(
942954 name : str ,
943955 working_dir : str = "." ,
944956 base_dir : str = "." ,
957+ pythonpath : Optional [List [str ]] = None ,
958+ environment : Optional [Dict [str , str ]] = None ,
959+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
945960 variables : Optional [Dict [str , Optional [Any ]]] = None ,
946961 ignore_errors : bool = True ,
947962) -> Any :
948- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
963+ update_python_path_and_env (working_dir , pythonpath , environment )
964+
965+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
949966
950967 return robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = ignore_errors )
951968
@@ -978,6 +995,7 @@ def _find_library_internal(
978995 base_dir : str = "." ,
979996 pythonpath : Optional [List [str ]] = None ,
980997 environment : Optional [Dict [str , str ]] = None ,
998+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
981999 variables : Optional [Dict [str , Optional [Any ]]] = None ,
9821000) -> Tuple [str , Any ]:
9831001
@@ -986,7 +1004,7 @@ def _find_library_internal(
9861004
9871005 _update_env (working_dir , pythonpath , environment )
9881006
989- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1007+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
9901008
9911009 name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
9921010
@@ -1007,10 +1025,13 @@ def find_library(
10071025 base_dir : str = "." ,
10081026 pythonpath : Optional [List [str ]] = None ,
10091027 environment : Optional [Dict [str , str ]] = None ,
1028+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
10101029 variables : Optional [Dict [str , Optional [Any ]]] = None ,
10111030) -> str :
10121031
1013- return _find_library_internal (name , working_dir , base_dir , pythonpath , environment , variables )[0 ]
1032+ return _find_library_internal (
1033+ name , working_dir , base_dir , pythonpath , environment , command_line_variables , variables
1034+ )[0 ]
10141035
10151036
10161037def get_library_doc (
@@ -1020,6 +1041,7 @@ def get_library_doc(
10201041 base_dir : str = "." ,
10211042 pythonpath : Optional [List [str ]] = None ,
10221043 environment : Optional [Dict [str , str ]] = None ,
1044+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
10231045 variables : Optional [Dict [str , Optional [Any ]]] = None ,
10241046) -> LibraryDoc :
10251047
@@ -1070,6 +1092,7 @@ def get_test_library(
10701092 base_dir = base_dir ,
10711093 pythonpath = pythonpath ,
10721094 environment = environment ,
1095+ command_line_variables = command_line_variables ,
10731096 variables = variables ,
10741097 )
10751098
@@ -1301,14 +1324,15 @@ def find_file(
13011324 base_dir : str = "." ,
13021325 pythonpath : Optional [List [str ]] = None ,
13031326 environment : Optional [Dict [str , str ]] = None ,
1327+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
13041328 variables : Optional [Dict [str , Optional [Any ]]] = None ,
13051329 file_type : str = "Resource" ,
13061330) -> str :
13071331 from robot .utils .robotpath import find_file as robot_find_file
13081332
13091333 _update_env (working_dir , pythonpath , environment )
13101334
1311- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1335+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
13121336
13131337 name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
13141338
@@ -1394,6 +1418,7 @@ def complete_library_import(
13941418 base_dir : str = "." ,
13951419 pythonpath : Optional [List [str ]] = None ,
13961420 environment : Optional [Dict [str , str ]] = None ,
1421+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
13971422 variables : Optional [Dict [str , Optional [Any ]]] = None ,
13981423) -> Optional [List [CompleteResult ]]:
13991424
@@ -1409,7 +1434,7 @@ def complete_library_import(
14091434 ]
14101435
14111436 if name is not None :
1412- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1437+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
14131438
14141439 name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
14151440
@@ -1460,6 +1485,7 @@ def complete_resource_import(
14601485 base_dir : str = "." ,
14611486 pythonpath : Optional [List [str ]] = None ,
14621487 environment : Optional [Dict [str , str ]] = None ,
1488+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
14631489 variables : Optional [Dict [str , Optional [Any ]]] = None ,
14641490) -> Optional [List [CompleteResult ]]:
14651491
@@ -1468,7 +1494,7 @@ def complete_resource_import(
14681494 result : List [CompleteResult ] = []
14691495
14701496 if name is not None :
1471- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1497+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
14721498
14731499 name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
14741500
@@ -1519,6 +1545,7 @@ def complete_variables_import(
15191545 base_dir : str = "." ,
15201546 pythonpath : Optional [List [str ]] = None ,
15211547 environment : Optional [Dict [str , str ]] = None ,
1548+ command_line_variables : Optional [Dict [str , Optional [Any ]]] = None ,
15221549 variables : Optional [Dict [str , Optional [Any ]]] = None ,
15231550) -> Optional [List [CompleteResult ]]:
15241551
@@ -1527,7 +1554,7 @@ def complete_variables_import(
15271554 result : List [CompleteResult ] = []
15281555
15291556 if name is not None :
1530- robot_variables = resolve_robot_variables (working_dir , base_dir , variables )
1557+ robot_variables = resolve_robot_variables (working_dir , base_dir , command_line_variables , variables )
15311558
15321559 name = robot_variables .replace_string (name .replace ("\\ " , "\\ \\ " ), ignore_errors = True )
15331560
0 commit comments