From f417ecab87a7ecd13c2db76b9eed0cb56abc4afb Mon Sep 17 00:00:00 2001 From: barrancojared <51723690+jaredbarranco@users.noreply.github.com> Date: Sun, 16 Feb 2025 21:37:40 -0800 Subject: [PATCH 1/7] use a temporary directory and epoch/operation for file operations --- lua/json-nvim/jq.lua | 45 +++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua index f5d1e66..9fae352 100644 --- a/lua/json-nvim/jq.lua +++ b/lua/json-nvim/jq.lua @@ -1,11 +1,22 @@ local utils = require("json-nvim.utils") -local temp_file_path = utils.get_plugin_root() .. "temp.json" +local temp_file_path = utils.get_plugin_root() .. "tmp/" -local function write_to_temp(input) - local f = io.open(temp_file_path, "w") +--write data to a temporary file in plugin root +---@param arg { input: string, operation: string } +---@return string +local function write_to_temp(arg) + if type(arg.input) ~= "string" then + error("no input to write_to_temp()") + end + local tmp_file = temp_file_path .. os.time() .. (arg.operation and ("-" .. arg.operation .. ".json") or ".json") + local f = io.open(tmp_file, "w") + -- if not f then + -- error("Failed to open file: " .. tmp_file) + -- end f:write(input) f:close() + return tmp_file end local M = {} @@ -27,13 +38,12 @@ end function M.get_formatted(input) local result local cmd + local tmp_file = write_to_temp({ input = input, operation = "get_formatted" }) if vim.fn.has("win32") == 1 then - write_to_temp(input) - cmd = "jq . " .. temp_file_path + cmd = "jq . " .. tmp_file result = vim.fn.system(cmd) else - write_to_temp(input) - cmd = "jq . -e " .. temp_file_path + cmd = "jq . -e " .. tmp_file result = vim.fn.system(cmd) end @@ -46,14 +56,13 @@ end function M.get_collapsed(input) local result local cmd + local tmp_file = write_to_temp({ input = input, operation = "get_collapsed" }) if vim.fn.has("win32") == 1 then - write_to_temp(input) - cmd = "jq -c . " .. temp_file_path + cmd = "jq -c . " .. tmp_file result = vim.fn.system(cmd) result = vim.fn.substitute(result, [[\n]], "", "g") else - write_to_temp(input) - cmd = "jq . -e " .. temp_file_path + cmd = "jq . -e " .. tmp_file result = vim.fn.system(cmd) result = result:gsub("\r?\n", "") end @@ -67,14 +76,13 @@ end function M.get_rawed(input) local result local cmd + local tmp_file = write_to_temp({ input = input, operation = "get_rawed" }) if vim.fn.has("win32") == 1 then - write_to_temp(input) - cmd = "jq -r . " .. temp_file_path + cmd = "jq -r . " .. tmp_file result = vim.fn.system(cmd) result = vim.fn.substitute(result, [[\n]], "", "g") else - write_to_temp(input) - cmd = "jq . -r " .. temp_file_path + cmd = "jq . -r " .. tmp_file result = vim.fn.system(cmd) result = result:gsub("[\n\r]", "") end @@ -88,9 +96,9 @@ end function M.is_valid(input) local cmd local result + local tmp_file = write_to_temp({ input = input, operation = "is_valid" }) if vim.fn.has("win32") == 1 then - write_to_temp(input) - cmd = "jq . -e " .. temp_file_path + cmd = "jq . -e " .. tmp_file result = vim.fn.system(cmd) local exit_status = vim.v.shell_error @@ -98,8 +106,7 @@ function M.is_valid(input) return exit_status == 0 else - write_to_temp(input) - cmd = "jq . -e " .. temp_file_path .. " Date: Sun, 16 Feb 2025 21:53:21 -0800 Subject: [PATCH 2/7] remove apparent non-required code The init_path logic below seems to account for OS path separators --- lua/json-nvim/utils.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lua/json-nvim/utils.lua b/lua/json-nvim/utils.lua index 62df204..aba82b6 100644 --- a/lua/json-nvim/utils.lua +++ b/lua/json-nvim/utils.lua @@ -5,12 +5,6 @@ local M = {} function M.get_plugin_root() local file_separator = "" - if vim.fn.has("win32") == 1 then - file_separator = "\\" - else - file_separator = "/" - end - local init_path = debug.getinfo(1).source if not init_path:find("\\") then From 1eae66b75f6ad07ef3436021ad20f06e388bab65 Mon Sep 17 00:00:00 2001 From: barrancojared <51723690+jaredbarranco@users.noreply.github.com> Date: Sun, 16 Feb 2025 22:29:07 -0800 Subject: [PATCH 3/7] use OS specific temp directory For OS management of temp files --- lua/json-nvim/jq.lua | 4 +++- lua/json-nvim/utils.lua | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua index 9fae352..4abfc7f 100644 --- a/lua/json-nvim/jq.lua +++ b/lua/json-nvim/jq.lua @@ -1,6 +1,8 @@ local utils = require("json-nvim.utils") -local temp_file_path = utils.get_plugin_root() .. "tmp/" +local temp_file_path, os_file_sep = utils.get_os_temp_file_path() + +temp_file_path = temp_file_path .. os_file_sep .. "json-nvim_" --write data to a temporary file in plugin root ---@param arg { input: string, operation: string } diff --git a/lua/json-nvim/utils.lua b/lua/json-nvim/utils.lua index aba82b6..bb112b7 100644 --- a/lua/json-nvim/utils.lua +++ b/lua/json-nvim/utils.lua @@ -2,15 +2,19 @@ local M = {} -- some of the functions below -- are just in case - -function M.get_plugin_root() - local file_separator = "" +---@return string +local function get_os_file_separator() + local file_separator local init_path = debug.getinfo(1).source if not init_path:find("\\") then file_separator = "/" end + return file_separator +end +function M.get_plugin_root() + local file_separator = get_os_file_separator() local target_index = 0 local separator_encountered = 0 for i = #init_path, 1, -1 do @@ -33,6 +37,23 @@ function M.get_jq_modules_directory() return result end +-- Returns the OS TMP DIR and path_separator +---@return string? +---@return string +function M.get_os_temp_file_path() + local tmp_dir + if vim.fn.has("win32") == 1 then + tmp_dir = os.getenv("TEMP") + else + tmp_dir = os.getenv("TMPDIR") + end + if not tmp_dir then + error("json-nvim uses temp files. Either $TEMP (Windows) or $TMPDIR (Unix) must be defined") + end + + return tmp_dir, get_os_file_separator() +end + ---takes json string as keys and returns which casing is used ---@param input string ---@return string From 2c96d7ea21048954acd18c4ffd541a5860b5f4c4 Mon Sep 17 00:00:00 2001 From: barrancojared <51723690+jaredbarranco@users.noreply.github.com> Date: Sun, 16 Feb 2025 22:29:31 -0800 Subject: [PATCH 4/7] use correct arg.input --- lua/json-nvim/jq.lua | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lua/json-nvim/jq.lua b/lua/json-nvim/jq.lua index 4abfc7f..c2946d5 100644 --- a/lua/json-nvim/jq.lua +++ b/lua/json-nvim/jq.lua @@ -13,10 +13,7 @@ local function write_to_temp(arg) end local tmp_file = temp_file_path .. os.time() .. (arg.operation and ("-" .. arg.operation .. ".json") or ".json") local f = io.open(tmp_file, "w") - -- if not f then - -- error("Failed to open file: " .. tmp_file) - -- end - f:write(input) + f:write(arg.input) f:close() return tmp_file end From 4a8c59837e4cccda023149c01f9815e672ebc2b8 Mon Sep 17 00:00:00 2001 From: barrancojared <51723690+jaredbarranco@users.noreply.github.com> Date: Fri, 16 May 2025 08:35:46 -0700 Subject: [PATCH 5/7] typo fix in jq.lua comments --- lua/json_nvim/jq.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/json_nvim/jq.lua b/lua/json_nvim/jq.lua index 5545091..3977ed3 100644 --- a/lua/json_nvim/jq.lua +++ b/lua/json_nvim/jq.lua @@ -21,7 +21,7 @@ end local M = {} ---get all keys from json text ----this function is used for scase_switching feature +---this function is used for case_switching feature ---@param json string ---@return string[] function M.get_keys(json) From e72eb1a7051aae85134a251e4418f3ae1fb554cd Mon Sep 17 00:00:00 2001 From: barrancojared <51723690+jaredbarranco@users.noreply.github.com> Date: Fri, 16 May 2025 08:42:13 -0700 Subject: [PATCH 6/7] update workflow echo runner temp cache apt, env. namespace incorrect fix tmpdir declaration set temp for windows --- .github/workflows/unit_tests.yaml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/unit_tests.yaml b/.github/workflows/unit_tests.yaml index 6502963..d7cdae0 100644 --- a/.github/workflows/unit_tests.yaml +++ b/.github/workflows/unit_tests.yaml @@ -13,11 +13,14 @@ jobs: - name: Checkout Code uses: actions/checkout@v3 - - name: Install Neovim - run: | - sudo apt update - sudo apt install -y neovim + - uses: awalsh128/cache-apt-pkgs-action@latest + with: + packages: neovim + version: 1.0 - name: Run Lua Tests + env: + TMPDIR: ${{ runner.temp }} + TEMP: ${{ runner.temp }} run: nvim --headless -l tests/run_tests.lua From 2e2bb1a7c8fc58bbc93280bb885801db128a5f6f Mon Sep 17 00:00:00 2001 From: barrancojared <51723690+jaredbarranco@users.noreply.github.com> Date: Fri, 16 May 2025 09:23:47 -0700 Subject: [PATCH 7/7] ignore .actual and .expected in test dir ignore .expected --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index fdd8d68..42e6967 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ vendor/plenary.nvim temp.json +tests/**.actual +tests/**.expected