Skip to content

Commit d493be2

Browse files
committed
Change util.log to not write to a file by default
1 parent ec11624 commit d493be2

File tree

3 files changed

+111
-97
lines changed

3 files changed

+111
-97
lines changed

checks/rogue_util_calls_spec.lua

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,56 @@
22
-- util functions like R and log. This isn't perfect, as it tries to exercise
33
-- each command, but doesn't seek edge cases.
44

5-
local load_fixture = require "tests.load_fixture"
6-
local assert = require 'luassert'
7-
local stub = require 'luassert.stub'
8-
local util = require 'treewalker.util'
5+
local load_fixture = require("tests.load_fixture")
6+
local assert = require("luassert")
7+
local stub = require("luassert.stub")
8+
local util = require("treewalker.util")
99

1010
local commands = {
11-
"Treewalker Up",
12-
"Treewalker Down",
13-
"Treewalker Right",
14-
"Treewalker Left",
15-
"Treewalker SwapUp",
16-
"Treewalker SwapDown",
17-
"Treewalker SwapRight",
18-
"Treewalker SwapLeft",
11+
"Treewalker Up",
12+
"Treewalker Down",
13+
"Treewalker Right",
14+
"Treewalker Left",
15+
"Treewalker SwapUp",
16+
"Treewalker SwapDown",
17+
"Treewalker SwapRight",
18+
"Treewalker SwapLeft",
1919
}
2020

2121
-- can't get luassert (plenary) spy working
2222
-- This needs to be generic over obj but can't figure out that either
2323
---@param obj table
2424
---@param method string
2525
local function spy(obj, method)
26-
local orig = obj[method]
27-
local stoob = stub.new(obj, method)
28-
stoob.callback = orig
29-
---@type type obj
30-
return stoob
26+
local orig = obj[method]
27+
local stoob = stub.new(obj, method)
28+
stoob.callback = orig
29+
---@type type obj
30+
return stoob
3131
end
3232

3333
describe("Extent util calls:", function()
34-
local util_R_stub = spy(util, "R")
35-
local util_log_stub = spy(util, "log")
36-
37-
-- Simulate TREEWALKER_NVIM_ENV being set to anything other than "development", see plugin/init.lua
38-
stub(os, "getenv").returns("")
39-
40-
before_each(function()
41-
load_fixture("/lua.lua")
42-
vim.opt.fileencoding = 'utf-8'
43-
vim.fn.cursor(31, 26)
44-
end)
45-
46-
for _, command in ipairs(commands) do
47-
it(command .. " encounters no " .. "util.R calls", function()
48-
vim.cmd(command)
49-
assert.stub(util_R_stub).was.called(0)
50-
end)
51-
52-
it(command .. " encounters no " .. "util.log calls", function()
53-
vim.cmd(command)
54-
assert.stub(util_log_stub).was.called(0)
55-
end)
56-
end
34+
local spies = {
35+
spy(util, "R"),
36+
spy(util, "log"),
37+
spy(util, "log_file"),
38+
}
39+
40+
-- Simulate TREEWALKER_NVIM_ENV being set to anything other than "development", see plugin/init.lua
41+
stub(os, "getenv").returns("")
42+
43+
before_each(function()
44+
load_fixture("/lua.lua")
45+
vim.opt.fileencoding = "utf-8"
46+
vim.fn.cursor(31, 26)
47+
end)
48+
49+
for _, spi in ipairs(spies) do
50+
for _, command in ipairs(commands) do
51+
it(command .. " encounters no " .. "util.R calls", function()
52+
vim.cmd(command)
53+
assert.stub(spi).was.called(0)
54+
end)
55+
end
56+
end
5757
end)

lua/treewalker/util.lua

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,99 @@ local M = {}
33

44
-- remove program code from lua cache, reload
55
M.RELOAD = function(...)
6-
return require("plenary.reload").reload_module(...)
6+
return require("plenary.reload").reload_module(...)
77
end
88

99
-- modified 'require'; use to flush entire program from top level for plugin development.
1010
M.R = function(name)
11-
M.RELOAD(name)
12-
return require(name)
11+
M.RELOAD(name)
12+
return require(name)
1313
end
1414

1515
-- print tables contents
1616
M.P = function(v)
17-
print(vim.inspect(v))
18-
return v
17+
print(vim.inspect(v))
18+
return v
1919
end
2020

2121
-- Log to the file debug.log in the plugin's data dir. File can be watched for easier debugging.
22-
M.log = function(...)
23-
local args = { ... }
24-
25-
-- Canonical log dir
26-
local data_path = vim.fn.stdpath("data") .. "/treewalker"
27-
28-
-- If no dir on fs, make it
29-
if vim.fn.isdirectory(data_path) == 0 then
30-
vim.fn.mkdir(data_path, "p")
31-
end
32-
33-
local log_file = io.open(data_path .. "/debug.log", "a")
34-
35-
-- Guard against no log file by making one
36-
if not log_file then
37-
log_file = io.open(data_path .. "/debug.log", "w+")
38-
end
39-
40-
-- Swallow further errors
41-
-- This is a utility for development, it should never cause issues
42-
-- during real use.
43-
if not log_file then return end
44-
45-
-- Write each arg to disk
46-
for _, arg in ipairs(args) do
47-
if type(arg) == "table" then
48-
arg = vim.inspect(arg)
49-
end
50-
51-
log_file:write(tostring(arg) .. "\n")
52-
end
22+
M.log_file = function(...)
23+
local args = { ... }
24+
25+
-- Canonical log dir
26+
local data_path = vim.fn.stdpath("data") .. "/treewalker"
27+
28+
-- If no dir on fs, make it
29+
if vim.fn.isdirectory(data_path) == 0 then
30+
vim.fn.mkdir(data_path, "p")
31+
end
32+
33+
local log_file = io.open(data_path .. "/debug.log", "a")
34+
35+
-- Guard against no log file by making one
36+
if not log_file then
37+
log_file = io.open(data_path .. "/debug.log", "w+")
38+
end
39+
40+
-- Swallow further errors
41+
-- This is a utility for development, it should never cause issues
42+
-- during real use.
43+
if not log_file then
44+
return
45+
end
46+
47+
-- Write each arg to disk
48+
for _, arg in ipairs(args) do
49+
if type(arg) == "table" then
50+
arg = vim.inspect(arg)
51+
end
52+
53+
log_file:write(tostring(arg) .. "\n")
54+
end
55+
56+
log_file:flush() -- Ensure the output is written immediately
57+
log_file:close()
58+
end
5359

54-
log_file:flush() -- Ensure the output is written immediately
55-
log_file:close()
60+
M.log = function(...)
61+
local args = { ... }
62+
for _, arg in ipairs(args) do
63+
if type(arg) == "table" then
64+
arg = vim.inspect(arg)
65+
end
66+
67+
-- Use vim.api.nvim_echo to print without waiting for keypress
68+
vim.api.nvim_echo({ { tostring(arg) } }, true, {})
69+
end
5670
end
5771

5872
M.guid = function()
59-
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
60-
return string.gsub(template, '[xy]', function(c)
61-
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
62-
return string.format('%x', v)
63-
end)
73+
local template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
74+
return string.gsub(template, "[xy]", function(c)
75+
local v = (c == "x") and math.random(0, 0xf) or math.random(8, 0xb)
76+
return string.format("%x", v)
77+
end)
6478
end
6579

6680
---reverse an array table
6781
---@param t table
68-
M.reverse = function (t)
69-
local reversed = {}
70-
for _, el in ipairs(t) do
71-
table.insert(reversed, 1, el)
72-
end
73-
return reversed
82+
M.reverse = function(t)
83+
local reversed = {}
84+
for _, el in ipairs(t) do
85+
table.insert(reversed, 1, el)
86+
end
87+
return reversed
7488
end
7589

7690
--- Returns true if current buffer's filetype is markdown
7791
---@return boolean
7892
M.is_markdown_file = function()
79-
local ft = (
80-
vim.bo and vim.bo.ft
81-
or (vim.api and vim.api.nvim_buf_get_option and vim.api.nvim_buf_get_option(0, 'filetype'))
82-
or ''
83-
)
84-
return ft == "markdown" or ft == "md"
93+
local ft = (
94+
vim.bo and vim.bo.ft
95+
or (vim.api and vim.api.nvim_buf_get_option and vim.api.nvim_buf_get_option(0, "filetype"))
96+
or ""
97+
)
98+
return ft == "markdown" or ft == "md"
8599
end
86100

87101
return M

tests/treewalker/util_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("util", function()
1919
end)
2020
end)
2121

22-
describe("log", function()
22+
describe("log_file", function()
2323
it("works", function()
2424
local io_open_stub = stub.new(io, "open")
2525

@@ -44,7 +44,7 @@ describe("util", function()
4444

4545
io_open_stub.returns(log_file)
4646

47-
util.log(1, 2, 3, 4, 5)
47+
util.log_file(1, 2, 3, 4, 5)
4848

4949
assert.same({ "1\n", "2\n", "3\n", "4\n", "5\n", }, writes)
5050
assert.equal(1, num_flushes)

0 commit comments

Comments
 (0)