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