Skip to content

Commit

Permalink
fix: Account for spaces when diffing files and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorris180 committed Jan 28, 2024
1 parent c638332 commit 23b0dce
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 29 deletions.
11 changes: 3 additions & 8 deletions lua/salesforce/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function Debugger:log(scope, item, ...)

if type(item) == "table" then
self:tprint(item)
return
end

local info = debug.getinfo(2, "Sl")
Expand All @@ -41,7 +40,9 @@ function Debugger:log(scope, item, ...)
line = "L" .. info.currentline
end

local final_arg = (select("#", ...) == 0 and item) or string.format(item, ...)
local final_arg = (type(item) == "table" and "TABLE")
or (type(item) == "string" and select("#", ...) == 0 and item)
or (type(item) == "string" and string.format(item, ...))

local debug_str =
string.format("[salesforce:%s %s in %s] > %s", os.date("%H:%M:%S"), line, scope, final_arg)
Expand All @@ -63,12 +64,6 @@ end
---@param indent number?: the default indent value, starts at 0.
---@private
function Debugger:tprint(table, indent)
if
not Config:get_options().debug.to_file and not Config:get_options().debug.to_command_line
then
return
end

if not indent then
indent = 0
end
Expand Down
34 changes: 21 additions & 13 deletions lua/salesforce/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,23 @@ local function diff_callback(j)
end)
end

local function execute_job(command)
local args = Util.split(command, " ")
table.remove(args, 1)
local function expand(t)
local res = {}
for k, v in pairs(t) do
res[#res + 1] = k
res[#res + 1] = v
end
return res
end

local function execute_job(args)
local all_args = { "project", "retrieve", "start", unpack(expand(args)) }
table.insert(all_args, "--json")
Debug:log("diff.lua", "Command: ")
Debug:log("diff.lua", all_args)
local new_job = Job:new({
command = "sf",
args = args,
args = all_args,
on_exit = diff_callback,
on_stderr = function(_, data)
vim.schedule(function()
Expand Down Expand Up @@ -117,16 +128,13 @@ M.diff_with_org = function()
local temp_dir_with_suffix = temp_dir .. "/main/default"
vim.fn.mkdir(temp_dir_with_suffix, "p")
Debug:log("diff.lua", "Created temp dir: " .. temp_dir)
local args = {
["-m"] = string.format("%s:%s", metadataType, file_name_no_ext),
["-r"] = temp_dir,
["-o"] = default_username,
}

local command = string.format(
"sf project retrieve start -m %s:%s -r %s -o %s --json",
metadataType,
string.gsub(file_name_no_ext, " ", "\\ "),
temp_dir,
default_username
)
Debug:log("diff.lua", "Command: " .. command)
execute_job(command)
execute_job(args)
end

return M
4 changes: 3 additions & 1 deletion lua/salesforce/execute_anon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ M.execute_anon = function()
local default_username = OrgManager:get_default_username()

if file_type ~= "apex" then
vim.notify("Not an Apex script file.", vim.log.levels.ERROR)
local message = "Not an Apex script file"
Debug:log("execute_anon.lua", message)
vim.notify(message, vim.log.levels.ERROR)
return
end

Expand Down
4 changes: 3 additions & 1 deletion lua/salesforce/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function M.notify_command_in_progress(category)
end

function M.notify_default_org_not_set()
vim.notify("Please select a default org", vim.log.levels.ERROR)
local message = "No default org set"
Debug:log("util.lua", message)
vim.notify(message, vim.log.levels.ERROR)
end

function M.clear_and_notify(msg, log_level)
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local function error_message(str, pattern)
end

-- Check equality of a config `prop` against `value` in the given `child` process.
-- @usage config_equality(child, "debug", true)
-- @usage config_equality(child, "debug", {})
Helpers.expect.config_equality = MiniTest.new_expectation(
"config option matches",
function(child, prop, value)
Expand All @@ -22,7 +22,7 @@ Helpers.expect.config_equality = MiniTest.new_expectation(
)

-- Check type equality of a config `prop` against `value` in the given `child` process.
-- @usage config_type_equality(child, "debug", "boolean")
-- @usage config_type_equality(child, "debug", "table")
Helpers.expect.config_type_equality = MiniTest.new_expectation(
"config option type matches",
function(child, prop, value)
Expand Down
14 changes: 14 additions & 0 deletions tests/resources/execute_anon/lua/mocks/salesforce/org_manager.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local MockOrgManager = {}

function MockOrgManager:new()
local o = {}
setmetatable(o, self)
self.__index = self
return o
end

function MockOrgManager:get_default_username()
return "test@username.com"
end

return MockOrgManager
1 change: 0 additions & 1 deletion tests/resources/execute_anon/plenary_override.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---@diagnostic disable: different-requires
vim.cmd("set rtp+=tests/resources/execute_anon/")
vim.cmd("set cmdheight=20")
require("plenary.reload").reload_module("plenary.job")
7 changes: 5 additions & 2 deletions tests/test_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ T["setup()"]["sets exposed methods and default options value"] = function()
child.lua([[M.setup()]])

-- global object that holds your plugin information
eq_type_config(child, "debug", "boolean")
eq_config(child, "debug", false)
eq_type_config(child, "debug", "table")
eq_config(child, "debug", {
to_command_line = false,
to_file = false,
})
end

T["setup()"]["overrides default values"] = function()
Expand Down
12 changes: 11 additions & 1 deletion tests/test_execute_anon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,23 @@ T["execute_anon()"] = MiniTest.new_set()

T["execute_anon()"]["displays a mock value in the scratch buffer"] = function()
mock_schedule()
child.lua(string.format([[M.setup({debug = %s})]], tostring(helpers.debug())))
local debug = string.format(
[[
{
to_file = %s,
to_command_line = false,
}
]],
tostring(helpers.debug())
)
child.lua(string.format([[M.setup({debug = %s})]], debug))
child.lua([[
dofile("tests/resources/execute_anon/plenary_override.lua")
]])
-- adding here to avoid "same file is required with different names" error
child.lua([[
package.loaded["plenary.job"] = require("mocks.plenary.job")
package.loaded["salesforce.org_manager"] = require("mocks.salesforce.org_manager")
require("plenary.reload").reload_module("salesforce.execute_anon")
]])
child.cmd(string.format("e %s", test_dir))
Expand Down

0 comments on commit 23b0dce

Please sign in to comment.