Skip to content

Commit 63a4a91

Browse files
authored
Merge pull request #336 from cameronr/main
fix: #335 session_root_dir didn't have a path separator
2 parents 5dd9600 + f96ec5a commit 63a4a91

12 files changed

+257
-74
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ doc/tags
77
tests/test_sessions
88
tests/custom_sessions
99
tests/test_git
10-

Makefile

+36-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
PLENARY_VER = v0.1.4
1+
# FIXME: Using my fork of plenary just for https://github.com/nvim-lua/plenary.nvim/pull/611
2+
PLENARY_URL = https://github.com/cameronr/plenary.nvim
23
PLENARY_DIR = .test/plenary
3-
PLENARY_URL = https://github.com/nvim-lua/plenary.nvim
4+
PLENARY_VER = master
5+
6+
# Telescope for session-lens test
7+
TELESCOPE_URL = https://github.com/nvim-telescope/telescope.nvim
8+
TELESCOPE_DIR = .test/telescope
9+
TELESCOPE_VER = 0.1.8
10+
11+
# FIXME: Using my fork on mini.nvim just for https://github.com/echasnovski/mini.nvim/pull/1101
12+
MINI_URL = https://github.com/cameronr/mini.nvim
13+
MINI_DIR = .test/mini.nvim
14+
MINI_VER = windows
15+
16+
PLUGINS := $(PLENARY_DIR) $(TELESCOPE_DIR) ${MINI_DIR}
417

518
FILES := $(wildcard tests/*_spec.lua)
619

7-
.PHONY: test $(FILES) args-tests
8-
test: $(PLENARY_DIR) $(FILES) args-tests
20+
.PHONY: test ${FILES} plenary-tests mini-tests
921

10-
$(FILES): $(PLENARY_DIR)
11-
nvim --clean --headless -u tests/minimal.lua +"PlenaryBustedFile $@"
12-
22+
test: plenary-tests mini-tests
1323

14-
args-tests: $(PLENARY_DIR)
15-
nvim --clean --headless -u tests/minimal.lua +"PlenaryBustedFile tests/args/args_setup_spec.lua"
16-
nvim --clean --headless -u tests/minimal.lua +"PlenaryBustedFile tests/args/args_not_enabled_spec.lua"
17-
nvim --clean --headless -u tests/minimal.lua +"PlenaryBustedFile tests/args/args_single_dir_enabled_spec.lua"
18-
nvim --clean --headless -u tests/minimal.lua +"PlenaryBustedFile tests/args/args_files_enabled_spec.lua"
24+
# It's faster to run the tests via PlenaryBustedDirectory because it only needs one overall, managing nvim process and then one per spec.
25+
# PlenaryBustedFile, on the other hand, starts two processes per spec (one to manage running the spec and then one to actually run the spec)
26+
# But it's very convenient to be able to run a single spec when test/developing
27+
plenary-tests: $(PLUGINS)
28+
nvim --clean --headless -u scripts/minimal_init.lua +"PlenaryBustedDirectory tests {minimal_init = 'scripts/minimal_init.lua', sequential=true}"
1929

30+
# Rule that lets you run an individual spec. Currently requires my Plenary fork above
31+
$(FILES): $(PLUGINS)
32+
nvim --clean --headless -u scripts/minimal_init.lua +"PlenaryBustedFile $@ {minimal_init = 'scripts/minimal_init.lua'}"
33+
34+
# We use mini.test for some end to end UI testing of session lens
35+
mini-tests: $(PLUGINS)
36+
nvim --headless --noplugin -u scripts/minimal_init_mini.lua -c "lua MiniTest.run()"
2037

2138
$(PLENARY_DIR):
22-
git clone --depth=1 --branch $(PLENARY_VER) $(PLENARY_URL) $(PLENARY_DIR)
39+
git clone --depth=1 --branch $(PLENARY_VER) $(PLENARY_URL) $@
40+
41+
$(TELESCOPE_DIR):
42+
git clone --depth=1 --branch $(TELESCOPE_VER) $(TELESCOPE_URL) $@
43+
44+
$(MINI_DIR):
45+
git clone --depth=1 --filter=blob:none --branch $(MINI_VER) $(MINI_URL) $@

README.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -363,23 +363,6 @@ Another possibility is to only save the session if there are at least two window
363363

364364
```
365365

366-
## Disabling the plugin
367-
368-
You might run into issues with Firenvim or another plugin and want to disable `auto_session` altogether based on some condition.
369-
For this example, as to not try and save sessions for Firenvim, we disable the plugin if the `started_by_firenvim` variable is set.
370-
371-
```viml
372-
if exists('g:started_by_firenvim')
373-
let g:auto_session_enabled = v:false
374-
endif
375-
```
376-
377-
One can also disable the plugin by setting the `auto_session_enabled` option to false at startup.
378-
379-
```sh
380-
nvim "+let g:auto_session_enabled = v:false"
381-
```
382-
383366
## 🔭 Session Lens
384367

385368
You can use Telescope to see, load, and delete your sessions. It's enabled by default if you have Telescope, but here's the Lazy config that shows the configuration options:
@@ -427,11 +410,11 @@ NOTE: If you previously installed `rmagatti/session-lens`, you should remove it
427410

428411
<img src="https://github.com/rmagatti/readme-assets/blob/main/session-lens.gif" width=800 />
429412

430-
### Statusline
413+
## Statusline
431414

432-
One can show the current session name in the statusline by using an AutoSession helper function.
415+
You can show the current session name in the statusline by using the function `current_session_name()`. With no arguments, it will return the full session name. For automatically created sessions that will be the path where the session was saved. If you only want the last directory in the path, you can call `current_session_name(true)`.
433416

434-
Lualine example config and how it looks
417+
Here's an example using [Lualine](https://github.com/nvim-lualine/lualine.nvim):
435418

436419
```lua
437420
require('lualine').setup{
@@ -450,6 +433,23 @@ require('lualine').setup{
450433

451434
<img width="1904" alt="Screen Shot 2021-10-30 at 3 58 57 PM" src="https://user-images.githubusercontent.com/2881382/139559478-8edefdb8-8254-42e7-a0f3-babd3dfd6ff2.png">
452435

436+
## Disabling the plugin
437+
438+
You might run into issues with Firenvim or another plugin and want to disable `auto_session` altogether based on some condition.
439+
For this example, as to not try and save sessions for Firenvim, we disable the plugin if the `started_by_firenvim` variable is set.
440+
441+
```viml
442+
if exists('g:started_by_firenvim')
443+
let g:auto_session_enabled = v:false
444+
endif
445+
```
446+
447+
One can also disable the plugin by setting the `auto_session_enabled` option to false at startup.
448+
449+
```sh
450+
nvim "+let g:auto_session_enabled = v:false"
451+
```
452+
453453
## 🚧 Troubleshooting
454454

455455
For troubleshooting refer to the [wiki page](https://github.com/rmagatti/auto-session/wiki/Troubleshooting).

lua/auto-session/session-lens/init.lua

+11-5
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ function SessionLens.setup()
3636
end
3737
end
3838

39-
local function make_telescope_callback(opts)
40-
-- We don't want the trailing separator because plenary will add one
41-
local session_root_dir = AutoSession.get_root_dir(false)
39+
---@private
40+
---Function generator that returns the function for generating telescope file entries. Only exported
41+
---for testing.
42+
---@param opts table Options for how paths sould be displayed. Only supports opts.shorten
43+
---@return function The function to be set as entry_maker in Telescope picker options
44+
function SessionLens.make_telescope_callback(opts)
45+
local session_root_dir = AutoSession.get_root_dir()
46+
47+
-- just used for shortening the display_name (if enabled)
4248
local path = require "plenary.path"
4349
return function(file_name)
4450
-- Don't include <session>x.vim files that nvim makes for custom user
@@ -83,7 +89,7 @@ local function make_telescope_callback(opts)
8389
filename = file_name,
8490
cwd = session_root_dir,
8591
display = display_name,
86-
path = path:new(session_root_dir, file_name):absolute(),
92+
path = session_root_dir .. file_name,
8793
}
8894
end
8995
end
@@ -122,7 +128,7 @@ SessionLens.search_session = function(custom_opts)
122128

123129
local opts = {
124130
prompt_title = "Sessions",
125-
entry_maker = make_telescope_callback(custom_opts),
131+
entry_maker = SessionLens.make_telescope_callback(custom_opts),
126132
cwd = session_root_dir,
127133
attach_mappings = function(_, map)
128134
telescope_actions.select_default:replace(Actions.source_session)

tests/minimal.lua scripts/minimal_init.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ for _, name in ipairs { "config", "data", "state", "cache" } do
66
vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. name
77
end
88

9-
-- Add plenary so we can run it
9+
-- Add plenary, so we can run it
1010
vim.opt.rtp:append "./.test/plenary"
1111

12+
-- Add telescope path for session-lens test
13+
vim.opt.rtp:append "./.test/telescope"
14+
1215
require "plenary"

scripts/minimal_init_mini.lua

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- Keep all testing data in ./.test
2+
local root = vim.fn.fnamemodify("./.test", ":p")
3+
4+
-- set stdpaths to use .repro
5+
for _, name in ipairs { "config", "data", "state", "cache" } do
6+
vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. name
7+
end
8+
9+
-- Add current directory to 'runtimepath' to be able to use 'lua' files
10+
vim.cmd [[let &rtp.=','.getcwd()]]
11+
12+
-- Set up 'mini.test' only when calling headless Neovim (like with `make test`)
13+
if #vim.api.nvim_list_uis() ~= 0 then
14+
return
15+
end
16+
17+
-- Add 'mini.nvim' to 'runtimepath' to be able to use 'mini.test'
18+
-- Assumed that 'mini.nvim' is stored in 'deps/mini.nvim'
19+
vim.opt.rtp:append "./.test/mini.nvim"
20+
21+
-- Add telescope path for session-lens test
22+
vim.opt.rtp:append "./.test/telescope"
23+
24+
-- Need plenary (even with mini.test) for telescope
25+
vim.opt.rtp:append "./.test/plenary"
26+
27+
-- Set up 'mini.test'
28+
require("mini.test").setup {
29+
collect = {
30+
find_files = function()
31+
return vim.fn.globpath("tests/mini-tests", "**/test_*.lua", true, true)
32+
end,
33+
},
34+
}

tests/args/args_setup_spec.lua

-34
This file was deleted.

tests/args/args_files_enabled_spec.lua tests/args_files_enabled_spec.lua

+17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ describe("The args files enabled config", function()
1717
},
1818
}
1919

20+
TL.clearSessionFilesAndBuffers()
21+
22+
it("can save a session", function()
23+
vim.cmd("e " .. TL.test_file)
24+
25+
vim.cmd "SessionSave"
26+
27+
-- Make sure the session was created
28+
assert.equals(1, vim.fn.filereadable(TL.default_session_path))
29+
30+
-- Make sure the session has our buffer
31+
TL.assertSessionHasFile(TL.default_session_path, TL.test_file)
32+
33+
-- now clear the buffers
34+
vim.cmd "%bw!"
35+
end)
36+
2037
it("doesn't restore a session when run with a single directory", function()
2138
assert.equals(false, no_restore_hook_called)
2239

tests/args/args_not_enabled_spec.lua tests/args_not_enabled_spec.lua

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ describe("The args not enabled config", function()
1616
end,
1717
},
1818
}
19+
TL.clearSessionFilesAndBuffers()
20+
21+
it("can save a session", function()
22+
vim.cmd("e " .. TL.test_file)
23+
24+
vim.cmd "SessionSave"
25+
26+
-- Make sure the session was created
27+
assert.equals(1, vim.fn.filereadable(TL.default_session_path))
28+
29+
-- Make sure the session has our buffer
30+
TL.assertSessionHasFile(TL.default_session_path, TL.test_file)
31+
32+
-- now clear the buffers
33+
vim.cmd "%bw!"
34+
end)
1935

2036
it("doesn't restore a session when run with a single directory", function()
2137
assert.equals(false, no_restore_hook_called)

tests/args/args_single_dir_enabled_spec.lua tests/args_single_dir_enabled_spec.lua

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ describe("The args single dir enabled config", function()
1818
},
1919
-- log_level = "debug",
2020
}
21+
TL.clearSessionFilesAndBuffers()
22+
23+
it("can save a session", function()
24+
vim.cmd("e " .. TL.test_file)
25+
26+
vim.cmd "SessionSave"
27+
28+
-- Make sure the session was created
29+
assert.equals(1, vim.fn.filereadable(TL.default_session_path))
30+
31+
-- Make sure the session has our buffer
32+
TL.assertSessionHasFile(TL.default_session_path, TL.test_file)
33+
34+
-- now clear the buffers
35+
vim.cmd "%bw!"
36+
end)
2137

2238
it("does not autosave for cwd if single directory arg does not have a session", function()
2339
no_restore_hook_called = false

tests/mini-tests/test_ui.lua

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---@diagnostic disable: undefined-field, undefined-global
2+
-- Define helper aliases
3+
local new_set = MiniTest.new_set
4+
local expect, eq = MiniTest.expect, MiniTest.expect.equality
5+
6+
-- Create (but not start) child Neovim object
7+
local child = MiniTest.new_child_neovim()
8+
9+
local TL = require "tests/test_lib"
10+
11+
-- Define main test set of this file
12+
local T = new_set {
13+
-- Register hooks
14+
hooks = {
15+
pre_once = function()
16+
TL.clearSessionFilesAndBuffers()
17+
end,
18+
-- This will be executed before every (even nested) case
19+
pre_case = function()
20+
-- Restart child process with custom 'init.lua' script
21+
child.restart { "-u", "scripts/minimal_init_mini.lua" }
22+
-- Load tested plugin
23+
child.lua [[M = require('auto-session').setup({
24+
auto_save_enabled = false,
25+
auto_restore_enabled = false,
26+
})]]
27+
end,
28+
-- This will be executed one after all tests from this set are finished
29+
post_once = child.stop,
30+
},
31+
}
32+
33+
T["session lens"] = new_set {}
34+
35+
T["session lens"]["save a default session"] = function()
36+
child.cmd("e " .. TL.test_file)
37+
expect.equality(1, child.fn.bufexists(TL.test_file))
38+
child.cmd "SessionSave"
39+
40+
expect.equality(1, child.fn.bufexists(TL.test_file))
41+
expect.equality(1, vim.fn.filereadable(TL.default_session_path))
42+
end
43+
44+
T["session lens"]["save a named session"] = function()
45+
child.cmd("e " .. TL.test_file)
46+
expect.equality(1, child.fn.bufexists(TL.test_file))
47+
child.cmd("SessionSave " .. TL.named_session_name)
48+
expect.equality(1, vim.fn.filereadable(TL.named_session_path))
49+
50+
child.cmd("e " .. TL.other_file)
51+
child.cmd "SessionSave project_x"
52+
end
53+
54+
T["session lens"]["can load a session"] = function()
55+
expect.equality(0, child.fn.bufexists(TL.test_file))
56+
child.cmd "SessionSearch"
57+
-- give the UI time to come up
58+
vim.loop.sleep(100)
59+
child.type_keys "project_x"
60+
child.type_keys "<cr>"
61+
-- give the session time to load
62+
vim.loop.sleep(500)
63+
expect.equality(1, child.fn.bufexists(TL.other_file))
64+
end
65+
66+
T["session lens"]["can delete a session"] = function()
67+
expect.equality(1, vim.fn.filereadable(TL.named_session_path))
68+
child.cmd "SessionSearch"
69+
-- give the UI time to come up
70+
vim.loop.sleep(100)
71+
child.type_keys "mysession"
72+
child.type_keys "<c-d>"
73+
vim.loop.sleep(100)
74+
expect.equality(0, vim.fn.filereadable(TL.named_session_path))
75+
end
76+
77+
-- Return test set which will be collected and execute inside `MiniTest.run()`
78+
return T

0 commit comments

Comments
 (0)