Skip to content

Commit

Permalink
feat: support for importing .json files in RF 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Jun 14, 2023
1 parent 3df22dd commit 0f84c4e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,8 @@ def is_library_by_path(path: str) -> bool:


def is_variables_by_path(path: str) -> bool:
if get_robot_version() >= (6, 1):
return path.lower().endswith((".py", ".yml", ".yaml", ".json", "/", os.sep))
return path.lower().endswith((".py", ".yml", ".yaml", "/", os.sep))


Expand Down Expand Up @@ -1529,6 +1531,9 @@ def get_variables_doc(
from robot.utils.importer import Importer
from robot.variables.filesetter import PythonImporter, YamlImporter

if get_robot_version() >= (6, 1):
from robot.variables.filesetter import JsonImporter

import_name: str = name
stem = Path(name).stem
module_spec: Optional[ModuleSpec] = None
Expand All @@ -1541,6 +1546,8 @@ def get_variables_doc(
if import_name.lower().endswith((".yaml", ".yml")):
source = import_name
importer = YamlImporter()
if get_robot_version() >= (6, 1) and import_name.lower().endswith(".json"):
importer = JsonImporter()
else:
if not is_variables_by_path(import_name):
module_spec = get_module_spec(import_name)
Expand Down Expand Up @@ -1901,6 +1908,7 @@ def iter_variables_from_python_path(
if allow_modules and f.suffix.lower() not in [
".yaml",
".yml",
*[".json" if get_robot_version() >= (6, 1) else []],
]:
yield CompleteResult(f.stem, CompleteResultKind.VARIABLES_MODULE)
if allow_files:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*** Settings ***
Variables ./vars.json

*** Test Cases ***
first
Log ${var from json}
Log ${var_from_json}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"var from json": "a var from json",
"var_from_json": "asd"
}

0 comments on commit 0f84c4e

Please sign in to comment.