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

Code before:
        eq(cwd_rel_root .. '/var', vim.fs.relpath('/', 'var'))
      end

      if is_os('win') then
        eq(nil, vim.fs.relpath('c:/aaaa/', '/aaaa/cccc'))
        eq(nil, vim.fs.relpath('c:/aaaa/', './aaaa/cccc'))
        eq(nil, vim.fs.relpath('c:/aaaa/', 'aaaa/cccc'))
        eq(nil, vim.fs.relpath('c:/blah\\blah', 'd:/games'))
        eq(nil, vim.fs.relpath('c:/games', 'd:/games'))
        eq(nil, vim.fs.relpath('c:/games', 'd:/games/foo'))
        eq(nil, vim.fs.relpath('c:/aaaa/bbbb', 'c:/aaaa'))
        eq('cccc', vim.fs.relpath('c:/aaaa/', 'c:/aaaa/cccc'))
        eq('aaaa/bbbb', vim.fs.relpath('C:/', 'c:\\aaaa\\bbbb'))
        eq('bar/package.json', vim.fs.relpath('C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json'))
        eq('baz', vim.fs.relpath('\\\\foo\\bar', '\\\\foo\\bar\\baz'))
        eq(nil, vim.fs.relpath('a/b/c', 'a\\b'))
        eq('d', vim.fs.relpath('a/b/c', 'a\\b\\c\\d'))
        eq('.', vim.fs.relpath('\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz'))
        eq(nil, vim.fs.relpath('C:\\foo\\test', 'C:\\foo\\Test\\bar\\package.json'))
      end
    end)
  end)

  describe('rm()', function()
    before_each(function()
      fn.mkdir('Xtest_fs-rm')
      fn.writefile({ 'File to link' }, 'Xtest_fs-rm/file-to-link')
      fn.mkdir('Xtest_fs-rm/dir-to-link')
      fn.writefile({ 'File in dir to link' }, 'Xtest_fs-rm/dir-to-link/file')
    end)

    after_each(function()
      fn.delete('Xtest_fs-rm', 'rf')
    end)

    it('works with symlink', function()
      -- File
      vim.uv.fs_symlink('Xtest_fs-rm/file-to-link', 'Xtest_fs-rm/file-as-link')
      vim.fs.rm('Xtest_fs-rm/file-as-link')
      eq(vim.uv.fs_stat('Xtest_fs-rm/file-as-link'), nil)
      eq({ 'File to link' }, fn.readfile('Xtest_fs-rm/file-to-link'))

      -- Directory
      local function assert_rm_symlinked_dir(opts)
        vim.uv.fs_symlink('Xtest_fs-rm/dir-to-link', 'Xtest_fs-rm/dir-as-link')
        vim.fs.rm('Xtest_fs-rm/dir-as-link', opts)
        eq(vim.uv.fs_stat('Xtest_fs-rm/dir-as-link'), nil)
        eq({ 'File in dir to link' }, fn.readfile('Xtest_fs-rm/dir-to-link/file'))
      end

      assert_rm_symlinked_dir({})

Review comment (suggestion):
`fn` runs everything through the embedded `nvim` instance whereas calling `vim.fs` will use `vim.fs` from the test runner instance. 

Ideally the tests should be using the embedded instance (via `exec_lua`), but it's probably fine as they are if you want to keep it that way. If we use `vim.fs` directly, then we should probably avoid using `fn` in `before_each`/`after_each`.

I think you can just use `mkdir_p`, `rmdir` and `n.write_file`

Code after:
        eq(cwd_rel_root .. '/var', vim.fs.relpath('/', 'var'))
      end

      if is_os('win') then
        eq(nil, vim.fs.relpath('c:/aaaa/', '/aaaa/cccc'))
        eq(nil, vim.fs.relpath('c:/aaaa/', './aaaa/cccc'))
        eq(nil, vim.fs.relpath('c:/aaaa/', 'aaaa/cccc'))
        eq(nil, vim.fs.relpath('c:/blah\\blah', 'd:/games'))
        eq(nil, vim.fs.relpath('c:/games', 'd:/games'))
        eq(nil, vim.fs.relpath('c:/games', 'd:/games/foo'))
        eq(nil, vim.fs.relpath('c:/aaaa/bbbb', 'c:/aaaa'))
        eq('cccc', vim.fs.relpath('c:/aaaa/', 'c:/aaaa/cccc'))
        eq('aaaa/bbbb', vim.fs.relpath('C:/', 'c:\\aaaa\\bbbb'))
        eq('bar/package.json', vim.fs.relpath('C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json'))
        eq('baz', vim.fs.relpath('\\\\foo\\bar', '\\\\foo\\bar\\baz'))
        eq(nil, vim.fs.relpath('a/b/c', 'a\\b'))
        eq('d', vim.fs.relpath('a/b/c', 'a\\b\\c\\d'))
        eq('.', vim.fs.relpath('\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz'))
        eq(nil, vim.fs.relpath('C:\\foo\\test', 'C:\\foo\\Test\\bar\\package.json'))
      end
    end)
  end)

  describe('rm()', function()
    before_each(function()
      t.mkdir('Xtest_fs-rm')
      t.write_file('Xtest_fs-rm/file-to-link', 'File to link')
      t.mkdir('Xtest_fs-rm/dir-to-link')
      t.write_file('Xtest_fs-rm/dir-to-link/file', 'File in dir to link')
    end)

    after_each(function()
      vim.uv.fs_unlink('Xtest_fs-rm/dir-to-link/file')
      vim.uv.fs_rmdir('Xtest_fs-rm/dir-to-link')
      vim.uv.fs_unlink('Xtest_fs-rm/file-to-link')
      vim.uv.fs_rmdir('Xtest_fs-rm')
    end)

    it('works with symlink', function()
      -- File
      vim.uv.fs_symlink('Xtest_fs-rm/file-to-link', 'Xtest_fs-rm/file-as-link')
      vim.fs.rm('Xtest_fs-rm/file-as-link')
      eq(vim.uv.fs_stat('Xtest_fs-rm/file-as-link'), nil)
      eq({ 'File to link' }, fn.readfile('Xtest_fs-rm/file-to-link'))

      -- Directory
      local function assert_rm_symlinked_dir(opts)
        vim.uv.fs_symlink('Xtest_fs-rm/dir-to-link', 'Xtest_fs-rm/dir-as-link')
        vim.fs.rm('Xtest_fs-rm/dir-as-link', opts)
        eq(vim.uv.fs_stat('Xtest_fs-rm/dir-as-link'), nil)
        eq({ 'File in dir to link' }, fn.readfile('Xtest_fs-rm/dir-to-link/file'))