Skip to content

Commit 4efbba7

Browse files
committed
docs: add documentation for tests
1 parent bd7203e commit 4efbba7

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

tests/README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Neogit Tests
2+
3+
## Running Tests
4+
5+
As a base requirement you must have `make` installed.
6+
7+
Once `make` is installed you can run tests by entering `make test` in the top level directory of Neogit into your command line.
8+
9+
## Adding a test dependency
10+
11+
If you're adding a lua plugin dependency to Neogit and wish to test it, open `tests/init.lua` in your editor.
12+
13+
Look for the following lines:
14+
15+
```lua
16+
if os.getenv("CI") then
17+
vim.opt.runtimepath:prepend(vim.fn.getcwd())
18+
vim.cmd([[runtime! plugin/plenary.vim]])
19+
vim.cmd([[runtime! plugin/neogit.lua]])
20+
else
21+
ensure_installed("nvim-lua/plenary.nvim")
22+
ensure_installed("nvim-telescope/telescope.nvim")
23+
end
24+
```
25+
26+
As an example let's say we want to add [`vim-fugitive`](https://github.com/tpope/vim-fugitive) to our test dependencies. Our new dependency section would look like this:
27+
28+
```lua
29+
if os.getenv("CI") then
30+
vim.opt.runtimepath:prepend(vim.fn.getcwd())
31+
vim.cmd([[runtime! plugin/plenary.vim]])
32+
vim.cmd([[runtime! plugin/neogit.lua]])
33+
vim.cmd([[runtime! plugin/fugitive.vim]])
34+
else
35+
ensure_installed("nvim-lua/plenary.nvim")
36+
ensure_installed("nvim-telescope/telescope.nvim")
37+
ensure_installed("tpope/vim-fugitive")
38+
end
39+
```
40+
41+
## Test Organization
42+
43+
### Where do tests go?
44+
45+
All tests are to be placed within the `tests/specs` directory, and placed mocking the path of the Neogit module the test is responsible for. For instance, say you wanted to test `lua/neogit/config.lua` then you would create the test file in `tests/specs/config_spec.lua` which mirrors the path in the main Neogit module.
46+
47+
### Where do utility functions go?
48+
49+
If you have any utility code that has to do with git, it should be placed in `tests/util/git_harness.lua`.
50+
51+
If you have a generic utility function _only_ relevant for tests then it should go in `tests/util/util.lua`. If it is generic enough that it could be useful in the general Neogit code then a consideration should be made to place this utility code in `lua/neogit/lib/util.lua`.
52+
53+
### Where should raw content files go?
54+
55+
Raw content files that you want to test against should go into `tests/test_files`. If you have a raw file you'd like to use against in git, then you'll need to add it to the git repository within `tests/.repo`. This can be done by changing directory into `tests/.repo` and renaming `.git.orig` to `.git` then adding any relevant changes to that repository. Once you're done, make sure you rename `.git` back to `.git.org`.
56+
57+
As a note the above is likely to become deprecated when a improved declarative lua git repository creation is made.
58+
59+
## Writing a test
60+
61+
Let's write a basic test to validate two things about a variable to get a quick intro to writing tests.
62+
63+
1. Validate the variable's type
64+
2. Validate the variable's content
65+
66+
```lua
67+
local our_variable = "Hello World!"
68+
describe("validating a string variable", function ()
69+
it("should be of type string", function()
70+
assert.True(type(our_variable) == "string")
71+
end)
72+
73+
it("should have content 'Hello World!'", function()
74+
assert.are.same("Hello World!", our_varible)
75+
)
76+
end)
77+
```
78+
79+
Nothing too crazy there.
80+
81+
Now let's take a look at a test for Neogit, specifically our `tests/specs/neogit/lib/git/cli_spec.lua` test.
82+
83+
```lua
84+
local eq = assert.are.same
85+
local git_cli = require("neogit.lib.git.cli")
86+
local git_harness = require("tests.util.git_harness")
87+
local in_prepared_repo = git_harness.in_prepared_repo
88+
89+
describe("git cli", function()
90+
describe("root detection", function()
91+
it(
92+
"finds the correct git root for a non symlinked directory",
93+
in_prepared_repo(function(root_dir)
94+
local detected_root_dir = git_cli.git_root()
95+
eq(detected_root_dir, root_dir)
96+
end)
97+
)
98+
end)
99+
end)
100+
```
101+
102+
This test gets the root directory a git repository. You'll notice something interesting we do in our `it` statement different from the prior example. We're passing `in_prepared_repo` to `it`. This function sets up a simple test bed repository (specifically the repository found with `tests/.repo`) to test Neogit against. If you ever need to test Neogit in a way that requires a git repository, you probably want to use `in_prepared_repo`.
103+
104+
For more test examples take a look at the tests written within the `tests` directory or our test runner's testing guide: [plenary test guide](https://github.com/nvim-lua/plenary.nvim/blob/master/TESTS_README.md).
105+
106+
For the assertions available, most assertions from [`luassert`](https://github.com/lunarmodules/luassert) are accessible.

0 commit comments

Comments
 (0)