environ_spec.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local eq = t.eq
  5. local environ = n.fn.environ
  6. local exists = n.fn.exists
  7. local system = n.fn.system
  8. local nvim_prog = n.nvim_prog
  9. local command = n.command
  10. local eval = n.eval
  11. local setenv = n.fn.setenv
  12. describe('environment variables', function()
  13. it('environ() handles empty env variable', function()
  14. clear({ env = { EMPTY_VAR = '' } })
  15. eq('', environ()['EMPTY_VAR'])
  16. eq(nil, environ()['DOES_NOT_EXIST'])
  17. end)
  18. it('exists() handles empty env variable', function()
  19. clear({ env = { EMPTY_VAR = '' } })
  20. eq(1, exists('$EMPTY_VAR'))
  21. eq(0, exists('$DOES_NOT_EXIST'))
  22. end)
  23. end)
  24. describe('empty $HOME', function()
  25. local original_home = os.getenv('HOME')
  26. before_each(clear)
  27. -- recover $HOME after each test
  28. after_each(function()
  29. if original_home ~= nil then
  30. setenv('HOME', original_home)
  31. end
  32. os.remove('test_empty_home')
  33. os.remove('./~')
  34. end)
  35. local function tilde_in_cwd()
  36. -- get files in cwd
  37. command("let test_empty_home_cwd_files = split(globpath('.', '*'), '\n')")
  38. -- get the index of the file named '~'
  39. command('let test_empty_home_tilde_index = index(test_empty_home_cwd_files, "./~")')
  40. return eval('test_empty_home_tilde_index') ~= -1
  41. end
  42. local function write_and_test_tilde()
  43. system({
  44. nvim_prog,
  45. '-u',
  46. 'NONE',
  47. '-i',
  48. 'NONE',
  49. '--headless',
  50. '-c',
  51. 'write test_empty_home',
  52. '+q',
  53. })
  54. eq(false, tilde_in_cwd())
  55. end
  56. it("'~' folder not created in cwd if $HOME and related env not defined", function()
  57. command('unlet $HOME')
  58. write_and_test_tilde()
  59. command("let $HOMEDRIVE='C:'")
  60. command("let $USERPROFILE='C:\\'")
  61. write_and_test_tilde()
  62. command('unlet $HOMEDRIVE')
  63. write_and_test_tilde()
  64. command('unlet $USERPROFILE')
  65. write_and_test_tilde()
  66. command("let $HOME='%USERPROFILE%'")
  67. command("let $USERPROFILE='C:\\'")
  68. write_and_test_tilde()
  69. end)
  70. it("'~' folder not created in cwd if writing a file with invalid $HOME", function()
  71. setenv('HOME', '/path/does/not/exist')
  72. write_and_test_tilde()
  73. end)
  74. it("'~' folder not created in cwd if writing a file with $HOME=''", function()
  75. command("let $HOME=''")
  76. write_and_test_tilde()
  77. end)
  78. end)