Line data Source code
1 : -- lua/yoda/config_loader.lua
2 : -- Configuration loading utilities
3 :
4 1 : local M = {}
5 :
6 : --- Load JSON configuration file (uses consolidated core/io module)
7 : --- @param path string Path to JSON file
8 : --- @return table|nil Parsed JSON data
9 1 : function M.load_json_config(path)
10 : -- Input validation for perfect assertiveness
11 9 : if type(path) ~= "string" or path == "" then
12 3 : vim.notify("load_json_config: path must be a non-empty string", vim.log.levels.ERROR, { title = "Config Loader Error" })
13 3 : return nil
14 : end
15 :
16 6 : local io = require("yoda-core.io")
17 6 : local ok, data = io.parse_json_file(path)
18 6 : return ok and data or nil
19 1 : end
20 :
21 : --- Load ingress mapping configuration
22 : --- @return table|nil Parsed environment mapping
23 1 : function M.load_ingress_mapping()
24 7 : local yaml_path = "ingress-mapping.yaml"
25 7 : local io = require("yoda-core.io")
26 :
27 14 : if not io.is_file(yaml_path) then
28 2 : return nil
29 : end
30 :
31 5 : local yaml_parser = require("yoda.yaml_parser")
32 5 : local result = yaml_parser.parse_ingress_mapping(yaml_path)
33 5 : if not result or not result.environments or not next(result.environments) then
34 3 : vim.notify("Failed to parse ingress-mapping.yaml", vim.log.levels.WARN)
35 3 : return nil
36 : end
37 :
38 2 : return result
39 1 : end
40 :
41 : --- Load environment and region configuration
42 : --- @return table, string Configuration data and source type
43 1 : function M.load_env_region()
44 : -- Use testing defaults (user-overridable for OCP)
45 4 : local defaults = require("yoda.testing.defaults")
46 4 : local fallback = {
47 8 : environments = defaults.get_environments(),
48 : }
49 :
50 : -- First try to load environments.json
51 4 : local file_path = "environments.json"
52 4 : local io = require("yoda-core.io")
53 :
54 8 : if io.is_file(file_path) then
55 2 : local config = M.load_json_config(file_path)
56 2 : if config then
57 2 : return config, "json"
58 : end
59 : end
60 :
61 : -- Fallback to ingress-mapping.yaml
62 2 : local ingress_map = M.load_ingress_mapping()
63 2 : if ingress_map then
64 1 : return ingress_map, "yaml"
65 : end
66 :
67 1 : return fallback, "fallback"
68 1 : end
69 :
70 : --- Load test picker marker from cache
71 : --- @param cache_file string Path to cache file
72 : --- @return table Default marker configuration
73 1 : function M.load_marker(cache_file)
74 : -- Input validation
75 4 : if type(cache_file) ~= "string" or cache_file == "" then
76 2 : local defaults = require("yoda.testing.defaults")
77 2 : return defaults.get_marker_defaults()
78 : end
79 :
80 2 : local config = M.load_json_config(cache_file)
81 :
82 : -- Use testing defaults for fallback (OCP - user-overridable!)
83 2 : local defaults = require("yoda.testing.defaults")
84 3 : return config or defaults.get_marker_defaults()
85 1 : end
86 :
87 : -- NOTE: load_pytest_markers function has been moved to pytest-atlas.nvim plugin
88 :
89 : --- Save test picker marker to cache
90 : --- @param cache_file string Path to cache file
91 : --- @param env string Environment name
92 : --- @param region string Region name
93 : --- @param markers string|nil Markers used
94 : --- @param open_allure boolean|nil Whether to open Allure report
95 1 : function M.save_marker(cache_file, env, region, markers, open_allure)
96 4 : local config = {
97 4 : environment = env,
98 4 : region = region,
99 4 : markers = markers or "bdd",
100 4 : open_allure = open_allure or false,
101 : }
102 4 : local Path = require("plenary.path")
103 8 : local ok, err = pcall(function()
104 12 : Path.new(cache_file):write(vim.json.encode(config), "w")
105 7 : end)
106 4 : if not ok then
107 1 : vim.notify("Failed to save test picker marker: " .. tostring(err), vim.log.levels.WARN)
108 : end
109 5 : end
110 :
111 1 : return M
|