Skip to content

Commit

Permalink
test: add init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes committed Dec 9, 2023
1 parent 98c5962 commit 126bfd0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 21 deletions.
15 changes: 0 additions & 15 deletions lua/img-clip/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,18 @@ M.get_clip_cmd = function()
if util.has("win32") or util.has("wsl") then
if util.executable("powershell.exe") then
return "powershell.exe"
else
util.error("Dependency check failed. 'powershell.exe' is not installed.")
return nil
end

-- Linux (X11)
elseif os.getenv("DISPLAY") then
if util.executable("xclip") then
return "xclip"
else
util.error("Dependency check failed. 'xclip' is not installed.")
return nil
end

-- Linux (Wayland)
elseif os.getenv("WAYLAND_DISPLAY") then
if util.executable("wl-paste") then
return "wl-paste"
else
util.error("Dependency check failed. 'wl-clipboard' is not installed.")
return nil
end

-- MacOS
Expand All @@ -37,14 +28,8 @@ M.get_clip_cmd = function()
return "pngpaste"
elseif util.executable("osascript") then
return "osascript"
else
util.error("Dependency check failed. 'osascript' is not installed.")
return nil
end

-- Other OS
else
util.error("Operating system is not supported.")
return nil
end
end
Expand Down
21 changes: 15 additions & 6 deletions lua/img-clip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,53 @@ end
local clip_cmd = nil

---@param opts? table
---@return boolean
M.pasteImage = function(opts)
if not clip_cmd then
clip_cmd = clipboard.get_clip_cmd()
if not clip_cmd then
return
util.error("Could not get clipboard command. See :checkhealth img-clip.")
return false
end
end

-- check if clipboard content is an image
local is_image = clipboard.check_if_content_is_image(clip_cmd)
if not is_image then
return util.warn("Clipboard content is not an image.")
util.warn("Clipboard content is not an image.")
return false
end

-- get the file path
local file_path = fs.get_file_path("png", opts)
if not file_path then
return util.error("Could not determine file path.")
util.error("Could not determine file path.")
return false
end

-- mkdir if not exists
local dir_path = vim.fn.fnamemodify(file_path, ":h")
local dir_ok = fs.mkdirp(dir_path)
if not dir_ok then
return util.error("Could not create directories.")
util.error("Could not create directories.")
return false
end

-- save image to specified file path
local save_ok = clipboard.save_clipboard_image(clip_cmd, file_path)
if not save_ok then
return util.error("Could not save image to disk.")
util.error("Could not save image to disk.")
return false
end

-- get the markup for the image
local markup_ok = markup.insert_markup(file_path, opts)
if not markup_ok then
return util.error("Could not insert markup code.")
util.error("Could not insert markup code.")
return false
end

return true
end

return M
98 changes: 98 additions & 0 deletions tests/init_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
local init = require("img-clip.init")
local clipboard = require("img-clip.clipboard")
local markup = require("img-clip.markup")
local util = require("img-clip.util")
local fs = require("img-clip.fs")
local spy = require("luassert.spy")

describe("img-clip.init", function()
describe("pasteImage", function()
before_each(function()
clipboard.get_clip_cmd = function()
return "xclip"
end
clipboard.check_if_content_is_image = function(cmd)
return cmd == "xclip"
end
fs.get_file_path = function()
return "/path/to/image.png"
end
fs.mkdirp = function()
return true
end
clipboard.save_clipboard_image = function()
return true
end
markup.insert_markup = function()
return true
end

spy.on(util, "warn")
spy.on(util, "error")
end)

after_each(function()
util.warn:revert()
util.error:revert()
end)

it("errors if clipboard command is not found", function()
clipboard.get_clip_cmd = function()
return nil
end
local success = init.pasteImage()
assert.is_false(success)
assert.spy(util.error).was_called_with("Could not get clipboard command. See :checkhealth img-clip.")
end)

it("warns if clipboard content is not an image", function()
clipboard.check_if_content_is_image = function()
return false
end
local success = init.pasteImage()
assert.is_false(success)
assert.spy(util.warn).was_called_with("Clipboard content is not an image.")
end)

it("errors if file path cannot be determined", function()
fs.get_file_path = function()
return nil
end
local success = init.pasteImage()
assert.is_false(success)
assert.spy(util.error).was_called_with("Could not determine file path.")
end)

it("errors if directories cannot be created", function()
fs.mkdirp = function()
return false
end
local success = init.pasteImage()
assert.is_false(success)
assert.spy(util.error).was_called_with("Could not create directories.")
end)

it("errors if image cannot be saved to disk", function()
clipboard.save_clipboard_image = function()
return false
end
local success = init.pasteImage()
assert.is_false(success)
assert.spy(util.error).was_called_with("Could not save image to disk.")
end)

it("errors if markup cannot be inserted", function()
markup.insert_markup = function()
return false
end
local success = init.pasteImage()
assert.is_false(success)
assert.spy(util.error).was_called_with("Could not insert markup code.")
end)

it("successfully pastes an image", function()
local success = init.pasteImage()
assert.is_true(success)
end)
end)
end)

0 comments on commit 126bfd0

Please sign in to comment.