neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

011_autocommands_spec.lua (8332B)


      1 -- Tests for autocommands
      2 -- - FileWritePre		writing a compressed file
      3 -- - FileReadPost		reading a compressed file
      4 -- - BufNewFile			reading a file template
      5 -- - BufReadPre			decompressing the file to be read
      6 -- - FilterReadPre		substituting characters in the temp file
      7 -- - FilterReadPost		substituting characters after filtering
      8 -- - FileReadPre		set options for decompression
      9 -- - FileReadPost		decompress the file
     10 -- Note: This test is skipped if "gzip" is not available.
     11 -- $GZIP is made empty, "-v" would cause trouble.
     12 -- Use a FileChangedShell autocommand to avoid a prompt for "Xtestfile.gz"
     13 -- being modified outside of Vim (noticed on Solaris).
     14 
     15 local t = require('test.testutil')
     16 local n = require('test.functional.testnvim')()
     17 
     18 local clear, feed_command, expect, eq, neq, dedent, write_file, feed =
     19  n.clear, n.feed_command, n.expect, t.eq, t.neq, t.dedent, t.write_file, n.feed
     20 local command = n.command
     21 local read_file = t.read_file
     22 local is_os = t.is_os
     23 
     24 local function has_gzip()
     25  local null = is_os('win') and 'nul' or '/dev/null'
     26  return os.execute('gzip --help >' .. null .. ' 2>&1') == 0
     27 end
     28 
     29 local function prepare_gz_file(name, text)
     30  write_file(name, text .. '\n')
     31  -- Compress the file with gzip.
     32  command([[call system(['gzip', '--force', ']] .. name .. [['])]])
     33  -- This should create the .gz file and delete the original.
     34  neq(nil, vim.uv.fs_stat(name .. '.gz'))
     35  eq(nil, vim.uv.fs_stat(name))
     36 end
     37 
     38 local function prepare_file(name, text)
     39  os.remove(name)
     40  write_file(name, text)
     41 end
     42 
     43 describe('file reading, writing and bufnew and filter autocommands', function()
     44  local text1 = dedent([[
     45      start of testfile
     46      line 2	Abcdefghijklmnopqrstuvwxyz
     47      line 3	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     48      line 4	Abcdefghijklmnopqrstuvwxyz
     49      line 5	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     50      line 6	Abcdefghijklmnopqrstuvwxyz
     51      line 7	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     52      line 8	Abcdefghijklmnopqrstuvwxyz
     53      line 9	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     54      line 10 Abcdefghijklmnopqrstuvwxyz
     55      end of testfile]])
     56  setup(function()
     57    write_file(
     58      'Xtest.c',
     59      [[
     60      /*
     61       * Here is a new .c file
     62       */
     63      ]]
     64    )
     65  end)
     66  before_each(function()
     67    clear({ env = { GZIP = nil } })
     68  end)
     69  teardown(function()
     70    os.remove('Xtestfile.gz')
     71    os.remove('Xtestfile')
     72    os.remove('XtestfileByFileReadPost')
     73    os.remove('Xtest.c')
     74    os.remove('test.out')
     75  end)
     76 
     77  it('FileReadPost', function()
     78    feed_command('set bin')
     79    prepare_file('Xtestfile', text1)
     80    os.remove('XtestfileByFileReadPost')
     81    --execute('au FileChangedShell * echo "caught FileChangedShell"')
     82    feed_command("au FileReadPost    Xtestfile   '[,']w XtestfileByFileReadPost")
     83    -- Read the testfile.
     84    feed_command('$r Xtestfile')
     85    expect('\n' .. text1)
     86    eq(text1, read_file('XtestfileByFileReadPost'))
     87  end)
     88 
     89  if not has_gzip() then
     90    pending('skipped (missing `gzip` utility)', function() end)
     91  else
     92    it('BufReadPre, BufReadPost (using gzip)', function()
     93      prepare_gz_file('Xtestfile', text1)
     94      local gzip_data = read_file('Xtestfile.gz')
     95      -- Setup autocommands to decompress before reading and re-compress afterwards.
     96      feed_command("au BufReadPre   *.gz  exe '!gzip -d ' . shellescape(expand('<afile>'))")
     97      feed_command("au BufReadPre   *.gz  call rename(expand('<afile>:r'), expand('<afile>'))")
     98      feed_command("au BufReadPost  *.gz  call rename(expand('<afile>'), expand('<afile>:r'))")
     99      feed_command("au BufReadPost  *.gz  exe '!gzip ' . shellescape(expand('<afile>:r'))")
    100      -- Edit compressed file.
    101      feed_command('e! Xtestfile.gz')
    102      -- Discard all prompts and messages.
    103      feed('<C-L>')
    104      -- Expect the decompressed file in the buffer.
    105      expect(text1)
    106      -- Expect the original file to be unchanged.
    107      eq(gzip_data, read_file('Xtestfile.gz'))
    108    end)
    109 
    110    -- luacheck: ignore 621 (Indentation)
    111    -- luacheck: ignore 611 (Line contains only whitespaces)
    112    it('FileReadPre, FileReadPost', function()
    113      prepare_gz_file('Xtestfile', text1)
    114      feed_command(
    115        'au! FileReadPre    *.gz   exe "silent !gzip -d " . shellescape(expand("<afile>"))'
    116      )
    117      feed_command('au  FileReadPre    *.gz   call rename(expand("<afile>:r"), expand("<afile>"))')
    118      feed_command("au! FileReadPost   *.gz   '[,']s/l/L/")
    119      -- Read compressed file.
    120      feed_command('$r Xtestfile.gz')
    121      -- Discard all prompts and messages.
    122      feed('<C-L>')
    123      expect([[
    124        
    125        start of testfiLe
    126        Line 2	Abcdefghijklmnopqrstuvwxyz
    127        Line 3	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    128        Line 4	Abcdefghijklmnopqrstuvwxyz
    129        Line 5	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    130        Line 6	Abcdefghijklmnopqrstuvwxyz
    131        Line 7	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    132        Line 8	Abcdefghijklmnopqrstuvwxyz
    133        Line 9	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    134        Line 10 Abcdefghijklmnopqrstuvwxyz
    135        end of testfiLe]])
    136    end)
    137  end
    138 
    139  it('FileAppendPre, FileAppendPost', function()
    140    feed_command('au BufNewFile      *.c    read Xtest.c')
    141    -- Will load Xtest.c.
    142    feed_command('e! foo.c')
    143    feed_command("au FileAppendPre   *.out  '[,']s/new/NEW/")
    144    feed_command('au FileAppendPost  *.out  !cat Xtest.c >test.out')
    145    -- Append it to the output file.
    146    feed_command('w>>test.out')
    147    -- Discard all prompts and messages.
    148    feed('<C-L>')
    149    expect([[
    150      
    151      /*
    152       * Here is a NEW .c file
    153       */]])
    154  end)
    155 
    156  it('FilterReadPre, FilterReadPost', function()
    157    -- Write a special input file for this test block.
    158    write_file('test.out', dedent([[
    159      startstart
    160      ]]) .. text1 .. dedent([[
    161      
    162      
    163      start of test.c
    164      /*
    165       * Here is a new .c file
    166       */
    167      end of test.c
    168      ]]) .. text1 .. dedent([[
    169      
    170      
    171      /*
    172       * Here is a NEW .c file
    173       */
    174      /*
    175       * Here is a new .c file
    176       */
    177      ]]) .. text1 .. dedent([[
    178      
    179      /*
    180       * Here is a new .c file
    181       */]]))
    182    -- Need temp files here.
    183    feed_command('set shelltemp')
    184    feed_command(
    185      'au FilterReadPre   *.out  call rename(expand("<afile>"), expand("<afile>") . ".t")'
    186    )
    187    feed_command(
    188      'au FilterReadPre   *.out  exe "silent !sed s/e/E/ " . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))'
    189    )
    190    feed_command(
    191      'au FilterReadPre   *.out  exe "silent !rm " . shellescape(expand("<afile>")) . ".t"'
    192    )
    193    feed_command("au FilterReadPost  *.out  '[,']s/x/X/g")
    194    -- Edit the output file.
    195    feed_command('e! test.out')
    196    feed_command('23,$!cat')
    197    -- Discard all prompts and messages.
    198    feed('<C-L>')
    199    -- Remove CR for when sed adds them.
    200    feed_command([[23,$s/\r$//]])
    201    expect([[
    202      startstart
    203      start of testfile
    204      line 2	Abcdefghijklmnopqrstuvwxyz
    205      line 3	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    206      line 4	Abcdefghijklmnopqrstuvwxyz
    207      line 5	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    208      line 6	Abcdefghijklmnopqrstuvwxyz
    209      line 7	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    210      line 8	Abcdefghijklmnopqrstuvwxyz
    211      line 9	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    212      line 10 Abcdefghijklmnopqrstuvwxyz
    213      end of testfile
    214      
    215      start of test.c
    216      /*
    217       * Here is a new .c file
    218       */
    219      end of test.c
    220      start of testfile
    221      line 2	Abcdefghijklmnopqrstuvwxyz
    222      line 3	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    223      line 4	Abcdefghijklmnopqrstuvwxyz
    224      linE 5	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    225      linE 6	AbcdefghijklmnopqrstuvwXyz
    226      linE 7	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    227      linE 8	AbcdefghijklmnopqrstuvwXyz
    228      linE 9	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    229      linE 10 AbcdefghijklmnopqrstuvwXyz
    230      End of testfile
    231      
    232      /*
    233       * HEre is a NEW .c file
    234       */
    235      /*
    236       * HEre is a new .c file
    237       */
    238      start of tEstfile
    239      linE 2	AbcdefghijklmnopqrstuvwXyz
    240      linE 3	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    241      linE 4	AbcdefghijklmnopqrstuvwXyz
    242      linE 5	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    243      linE 6	AbcdefghijklmnopqrstuvwXyz
    244      linE 7	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    245      linE 8	AbcdefghijklmnopqrstuvwXyz
    246      linE 9	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    247      linE 10 AbcdefghijklmnopqrstuvwXyz
    248      End of testfile
    249      /*
    250       * HEre is a new .c file
    251       */]])
    252  end)
    253 end)