robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
3.3 KiB95 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local eq = t.eq
6local clear = n.clear
7local command = n.command
8local exec_capture = n.exec_capture
9local matches = t.matches
10local is_os = t.is_os
11local fn = n.fn
12
13describe(':trust', function()
14  local xstate = 'Xstate_ex_trust'
15  local test_file = 'Xtest_functional_ex_cmds_trust'
16  local empty_file = 'Xtest_functional_ex_cmds_trust_empty'
17
18  before_each(function()
19    n.mkdir_p(vim.fs.joinpath(xstate, is_os('win') and 'nvim-data' or 'nvim'))
20    t.write_file(test_file, 'test')
21    t.write_file(empty_file, '')
22    clear { env = { XDG_STATE_HOME = xstate } }
23  end)
24
25  after_each(function()
26    os.remove(test_file)
27    os.remove(empty_file)
28    n.rmdir(xstate)
29  end)
30
31  --- @param s string
32  local function fmt(s)
33    return s:format(test_file)
34  end
35
36  local function assert_trust_entry(expected)
37    local trust = t.read_file(vim.fs.joinpath(fn.stdpath('state'), 'trust'))
38    eq(expected, vim.trim(trust))
39  end
40
41  it('is not executed when inside false condition', function()
42    command(fmt('edit %s'))
43    eq('', exec_capture('if 0 | trust | endif'))
44    eq(nil, vim.uv.fs_stat(vim.fs.joinpath(fn.stdpath('state'), 'trust')))
45  end)
46
47  it('trust then deny then remove a file using current buffer', function()
48    local cwd = fn.getcwd()
49    local hash = fn.sha256(assert(t.read_file(test_file)))
50
51    command(fmt('edit %s'))
52    matches(fmt('^Allowed in trust database%%: ".*%s"$'), exec_capture('trust'))
53    assert_trust_entry(('%s %s'):format(hash, vim.fs.joinpath(cwd, test_file)))
54
55    matches(fmt('^Denied in trust database%%: ".*%s"$'), exec_capture('trust ++deny'))
56    assert_trust_entry(('! %s'):format(vim.fs.joinpath(cwd, test_file)))
57
58    matches(fmt('^Removed from trust database%%: ".*%s"$'), exec_capture('trust ++remove'))
59    assert_trust_entry('')
60  end)
61
62  it('trust an empty file using current buffer', function()
63    local cwd = fn.getcwd()
64    local hash = fn.sha256(assert(t.read_file(empty_file)))
65
66    command('edit ' .. empty_file)
67    matches('^Allowed in trust database%: ".*' .. empty_file .. '"$', exec_capture('trust'))
68    assert_trust_entry(('%s %s'):format(hash, vim.fs.joinpath(cwd, empty_file)))
69  end)
70
71  it('deny then trust then remove a file using current buffer', function()
72    local cwd = fn.getcwd()
73    local hash = fn.sha256(assert(t.read_file(test_file)))
74
75    command(fmt('edit %s'))
76    matches(fmt('^Denied in trust database%%: ".*%s"$'), exec_capture('trust ++deny'))
77    assert_trust_entry(('! %s'):format(vim.fs.joinpath(cwd, test_file)))
78
79    matches(fmt('^Allowed in trust database%%: ".*%s"$'), exec_capture('trust'))
80    assert_trust_entry(('%s %s'):format(hash, vim.fs.joinpath(cwd, test_file)))
81
82    matches(fmt('^Removed from trust database%%: ".*%s"$'), exec_capture('trust ++remove'))
83    assert_trust_entry('')
84  end)
85
86  it('deny then remove a file using file path', function()
87    local cwd = fn.getcwd()
88
89    matches(fmt('^Denied in trust database%%: ".*%s"$'), exec_capture(fmt('trust ++deny %s')))
90    assert_trust_entry(('! %s'):format(vim.fs.joinpath(cwd, test_file)))
91
92    matches(fmt('^Removed from trust database%%: ".*%s"$'), exec_capture(fmt('trust ++remove %s')))
93    assert_trust_entry('')
94  end)
95end)