Repository: neovim/neovim
File: test/functional/lua/ui_spec.lua (Lua)

Code before:
      eq(true, exec_lua('return (nil == result)'))
    end)

    it(
      'can return the identical object when an arbitrary opts.cancelreturn object is given',
      function()
        feed(':lua fn = function() return 42 end<CR>')
        eq(42, exec_lua('return fn()'))
        feed(':lua vim.ui.input({ cancelreturn = fn }, function(input) result = input end)<cr>')
        feed('cancel<esc>')
        eq(true, exec_lua('return (result == fn)'))
        eq(42, exec_lua('return result()'))
      end
    )
  end)

  describe('open()', function()
    it('validation', function()
      if is_os('win') or not is_ci('github') then
        exec_lua [[vim.system = function() return { wait=function() return { code=3 } end } end]]
      end
      if not is_os('bsd') then
        local rv = exec_lua([[
          local orig = vim.ui._get_open_cmd
          if not orig() then
            vim.ui._get_open_cmd = function() return {'fake-xdg-open'}, nil end
          end
          local cmd = vim.ui.open('non-existent-file')
          vim.ui._get_open_cmd = orig
          return cmd:wait(100).code
        ]])
        ok(type(rv) == 'number' and rv ~= 0, 'nonzero exit code', rv)
      end

      exec_lua [[
        vim.fn.has = function() return 0 end
        vim.fn.executable = function() return 0 end
      ]]
      eq(
        'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open, lemonade)',
        exec_lua [[local _, err = vim.ui.open('foo') ; return err]]
      )
    end)

    it('opt.cmd #29490', function()
      t.matches(
        'ENOENT: no such file or directory',
        t.pcall_err(exec_lua, function()
          vim.ui.open('foo', { cmd = { 'non-existent-tool' } })
        end)
      )

Review comment (suggestion):
i guess we can do something like this, but it's not ideal because digging into the internals makes the tests brittle.
```suggestion
          if not vim.ui._get_open_cmd() then
            vim.ui._get_open_cmd = function() return {'fake-xdg-open'}, nil end
```

Code after:
      eq(true, exec_lua('return (nil == result)'))
    end)

    it(
      'can return the identical object when an arbitrary opts.cancelreturn object is given',
      function()
        feed(':lua fn = function() return 42 end<CR>')
        eq(42, exec_lua('return fn()'))
        feed(':lua vim.ui.input({ cancelreturn = fn }, function(input) result = input end)<cr>')
        feed('cancel<esc>')
        eq(true, exec_lua('return (result == fn)'))
        eq(42, exec_lua('return result()'))
      end
    )
  end)

  describe('open()', function()
    it('validation', function()
      if is_os('win') or not is_ci('github') then
        exec_lua [[vim.system = function() return { wait=function() return { code=3 } end } end]]
      end
      if not is_os('bsd') then
        local rv = exec_lua([[
          local cmd, err = vim.ui.open('non-existent-file')
          if err then return nil end
          return cmd:wait(100).code
        ]])
        if type(rv) == 'number' then
          ok(rv ~= 0, 'nonzero exit code', rv)
        end
      end

      exec_lua [[
        vim.fn.has = function() return 0 end
        vim.fn.executable = function() return 0 end
      ]]
      eq(
        'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open, lemonade)',
        exec_lua [[local _, err = vim.ui.open('foo') ; return err]]
      )
    end)

    it('opt.cmd #29490', function()
      t.matches(
        'ENOENT: no such file or directory',
        t.pcall_err(exec_lua, function()
          vim.ui.open('foo', { cmd = { 'non-existent-tool' } })
        end)
      )

      eq(