ethanholz / freeze.nvim

A Neovim plugin for freeze
MIT License
28 stars 5 forks source link

Add tests to ensure working code #9

Open AlejandroSuero opened 4 months ago

AlejandroSuero commented 4 months ago

Motivation

To ship code with the security that it won't break things.

Testing enviroment

There are two ways I test when using lua:

To use them we just have to create a tests folder (in the root for example) and for plenary.nvim a minimal_init.{lua|vim} (I prefer .lua) and testing files following the convention of name_of_the_file_spec.lua, the _spec.lua will be detected as tests files.

-- minimal_init.lua
local plenary_dir = os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim"
local is_not_a_directory = vim.fn.isdirectory(plenary_dir) == 0
if is_not_a_directory then
  vim.fn.system({ "git", "clone", "https://github.com/nvim-lua/plenary.nvim", plenary_dir })
end

vim.opt.rtp:append(".")
vim.opt.rtp:append(plenary_dir)

vim.cmd("runtime plugin/plenary.vim")
require("plenary.busted")

Commands to run

vusted ./tests/freeze
nvim --headless --noplugin -u tests/minimal_init.lua -c "PlenaryBustedDirectory tests/freeze {minimal_init = 'tests/minimal_init.lua'}"

Tests files

-- ./tests/freeze/setup_freeze_spec.lua
local freeze = require("freeze")

describe("[freeze.nvim tests]", function()
  describe("setup", function()
    it("sets up with default config", function()
      local expected = {
      dir = ".",
      output = default_output,
      theme = "default",
      config = "base",
      open = false,
      }
      local actual = require("freeze")
      freeze.setup(expected)
      assert.are.same(expected, actual.opts)
    end)
    it("sets up with custom config", function()
      local opts = {
        dir = "/tmp/freeze_images",
        theme = "rose-pine-moon",
      }
      freeze.setup(opts)
      local actual = freeze.opts
      assert.are.same(opts.dir, actual.dir)
      -- or assert.are.same("/tmp/freeze_images", actual.dir)
      assert.are.same(opts.theme, actual.theme)
      -- or assert.are.same("rose-pine-moon", actual.theme)
      assert.are.same(opts, actual)
    end)
  end)
end)
ethanholz commented 4 months ago

I like the bones proposed in #11 but I prefer to use Nix to handle my CI. Adopting Nix will also allow for us to specify and use devshells for development.

I am open to these suggestions but tests should be a separate PR from any CI changes (to include the included Makefile).

Additionally, I make no guarantees on running testing on Windows as this is not an environment I plan to support (although it should work fine).

Lastly, I will be offline for awhile so do not expect movement on this project for the near future.

AlejandroSuero commented 4 months ago

@ethanholz I removed the workflow on the PR, and added the tests for using freeze command.