|
| 1 | +local fs_utils = require('orgmode.utils.fs') |
| 2 | +local helpers = require('tests.plenary.helpers') |
| 3 | + |
| 4 | +local uv = vim.uv or vim.loop |
| 5 | +local file = helpers.create_file({}) |
| 6 | + |
| 7 | +describe('get_current_file_dir', function() |
| 8 | + it('gives the dirname of current_file_path', function() |
| 9 | + assert.are.same(vim.fs.dirname(file.filename), fs_utils.get_current_file_dir()) |
| 10 | + end) |
| 11 | + |
| 12 | + it('always gives an absolute path', function() |
| 13 | + local tempdir = vim.fs.dirname(file.filename) |
| 14 | + helpers.with_cwd(tempdir, function() |
| 15 | + assert.are.same('.', vim.fs.dirname(vim.fn.bufname())) |
| 16 | + local dir = fs_utils.get_current_file_dir() |
| 17 | + assert.are.same(dir .. '/', vim.fn.fnamemodify(dir, ':p')) |
| 18 | + end) |
| 19 | + end) |
| 20 | +end) |
| 21 | + |
| 22 | +describe('substitute_path', function() |
| 23 | + it('leaves absolute paths untouched', function() |
| 24 | + assert.are.same('/a/b/c', fs_utils.substitute_path('/a/b/c')) |
| 25 | + end) |
| 26 | + |
| 27 | + it('expands ~ to HOME', function() |
| 28 | + local output |
| 29 | + helpers.with_var(vim.env, 'HOME', '/home/org', function() |
| 30 | + output = fs_utils.substitute_path('~/foobar') |
| 31 | + end) |
| 32 | + assert.are.same('/home/org/foobar', output) |
| 33 | + end) |
| 34 | + |
| 35 | + it('expands . to the current file dir', function() |
| 36 | + local output = fs_utils.substitute_path('./a/b') |
| 37 | + assert(output) |
| 38 | + assert(vim.startswith(output, vim.fs.dirname(file.filename))) |
| 39 | + end) |
| 40 | + |
| 41 | + it('expands .. to the current file dir plus ..', function() |
| 42 | + local output = fs_utils.substitute_path('../a/b') |
| 43 | + assert(output) |
| 44 | + assert(vim.startswith(output, vim.fs.dirname(file.filename))) |
| 45 | + local normalized = vim.fs.normalize(output) |
| 46 | + assert.are.Not.same(output, normalized, "normalize didn't remove ..") |
| 47 | + end) |
| 48 | + |
| 49 | + it('fails on all other relative paths', function() |
| 50 | + assert.is.False(fs_utils.substitute_path('a/b/c')) |
| 51 | + end) |
| 52 | +end) |
| 53 | + |
| 54 | +describe('get_real_path', function() |
| 55 | + local link_name = nil |
| 56 | + |
| 57 | + before_each(function() |
| 58 | + if not link_name then |
| 59 | + link_name = vim.fn.tempname() |
| 60 | + assert(uv.fs_symlink(file.filename, link_name)) |
| 61 | + end |
| 62 | + end) |
| 63 | + |
| 64 | + it('resolves symlinks', function() |
| 65 | + assert(link_name) |
| 66 | + assert.are.same(file.filename, fs_utils.get_real_path(link_name)) |
| 67 | + end) |
| 68 | + |
| 69 | + it('resolves relative paths', function() |
| 70 | + assert(link_name) |
| 71 | + local relpath = vim.fs.joinpath('.', vim.fs.basename(link_name)) |
| 72 | + assert.are.same(file.filename, fs_utils.get_real_path(relpath)) |
| 73 | + end) |
| 74 | + |
| 75 | + it('keeps trailing slash if it is there', function() |
| 76 | + local path = fs_utils.get_real_path('././') |
| 77 | + assert.are.same(vim.fs.dirname(file.filename) .. '/', path) |
| 78 | + end) |
| 79 | + |
| 80 | + it('keeps trailing slash away if it is not there', function() |
| 81 | + local path = fs_utils.get_real_path('./.') |
| 82 | + assert.are.same(vim.fs.dirname(file.filename), path) |
| 83 | + end) |
| 84 | + |
| 85 | + it('fails on bare dot "."', function() |
| 86 | + assert.is.False(fs_utils.get_real_path('.')) |
| 87 | + end) |
| 88 | +end) |
0 commit comments