diff options
| author | Elvis Claros Castro <elvis@claros.ar> | 2024-07-12 11:18:14 -0300 |
|---|---|---|
| committer | Elvis Claros Castro <elvis@claros.ar> | 2024-07-12 11:18:14 -0300 |
| commit | ea0f4bf8ffade657b1d45152dbc183d4679e75c4 (patch) | |
| tree | cc1fb023bc308e019ddce33a4b0aed39f56805dc /lua/pi/test_strategy.lua | |
| parent | 1fd0af14d592d612c2aaab7717c04c8806ba3daa (diff) | |
| download | nvim-conf-ea0f4bf8ffade657b1d45152dbc183d4679e75c4.tar.gz nvim-conf-ea0f4bf8ffade657b1d45152dbc183d4679e75c4.zip | |
Feat: PI
Diffstat (limited to 'lua/pi/test_strategy.lua')
| -rw-r--r-- | lua/pi/test_strategy.lua | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lua/pi/test_strategy.lua b/lua/pi/test_strategy.lua new file mode 100644 index 0000000..9a2d34a --- /dev/null +++ b/lua/pi/test_strategy.lua @@ -0,0 +1,65 @@ +local Job = require "plenary.job" + +local function splitString(input_string) + local parts = {} + local in_quote = false + local part_start = 1 + + for i = 1, #input_string do + local char = input_string:sub(i, i) + + if char == "'" then + in_quote = not in_quote + elseif char == " " and not in_quote then + local part = input_string:sub(part_start, i - 1) + -- Remove surrounding quotes if they exist + if part:sub(1, 1) == "'" and part:sub(-1) == "'" then + part = part:sub(2, -2) + end + table.insert(parts, part) + part_start = i + 1 + end + end + + -- Add the last part (and remove surrounding quotes if they exist) + local part = input_string:sub(part_start) + if part:sub(1, 1) == "'" and part:sub(-1) == "'" then + part = part:sub(2, -2) + end + table.insert(parts, part) + + return parts +end + +local function myStrategy(cmd) + --convert cmd from string to table + if type(cmd) == "string" then + -- do not split quoted parts of the string + cmd = splitString(cmd) + end + + -- get first element of cmd and remove from the cmd + local cmd_name = table.remove(cmd, 1) + + ---@diagnostic disable-next-line: missing-fields + Job:new({ + command = cmd_name, + args = cmd, + on_exit = function(j, return_val) + -- in case of result val 0 show success message + if return_val == 0 then + vim.notify("Test run successfully", vim.log.levels.INFO, { title = "Test" }) + return + end + local result = j:stderr_result() or j:stdout_result() + -- join result in a single string + result = table.concat(result, "\n") + -- escape / characters + result = result:gsub("/", "\\/") + vim.notify(result, vim.log.levels.ERROR, { title = "Test" }) + end, + }):start() +end + +vim.g["test#custom_strategies"] = { myStrategy = myStrategy } +vim.g["test#strategy"] = "myStrategy" |