|
| 1 | +local dir_manager = require("conform.dir_manager") |
| 2 | +local test_util = require("tests.test_util") |
| 3 | + |
| 4 | +local function touch(path) |
| 5 | + local fd = assert(vim.uv.fs_open(path, "w", 448)) |
| 6 | + vim.uv.fs_write(fd, "") |
| 7 | + vim.uv.fs_close(fd) |
| 8 | +end |
| 9 | + |
| 10 | +describe("dir_manager", function() |
| 11 | + after_each(function() |
| 12 | + test_util.reset_editor() |
| 13 | + end) |
| 14 | + |
| 15 | + it("creates and cleans up nested created directories", function() |
| 16 | + local root, err = vim.uv.fs_mkdtemp("conform_XXXXX") |
| 17 | + assert(root, err) |
| 18 | + dir_manager.ensure_parent(root .. "/foo/bar/baz.txt") |
| 19 | + assert(vim.uv.fs_stat(root .. "/foo")) |
| 20 | + assert(vim.uv.fs_stat(root .. "/foo/bar")) |
| 21 | + assert(not vim.uv.fs_stat(root .. "/foo/bar/baz.txt")) |
| 22 | + dir_manager.cleanup() |
| 23 | + assert(not vim.uv.fs_stat(root .. "/foo/bar")) |
| 24 | + assert(not vim.uv.fs_stat(root .. "/foo")) |
| 25 | + assert(vim.uv.fs_stat(root)) |
| 26 | + assert(vim.uv.fs_rmdir(root)) |
| 27 | + end) |
| 28 | + |
| 29 | + it("handles race condition for two concurrent processes", function() |
| 30 | + local root, err = vim.uv.fs_mkdtemp("conform_XXXXX") |
| 31 | + assert(root, err) |
| 32 | + dir_manager.ensure_parent(root .. "/foo/bar/baz.txt") |
| 33 | + touch(root .. "/foo/bar/baz.txt") |
| 34 | + assert(vim.uv.fs_stat(root .. "/foo")) |
| 35 | + assert(vim.uv.fs_stat(root .. "/foo/bar")) |
| 36 | + assert(vim.uv.fs_stat(root .. "/foo/bar/baz.txt")) |
| 37 | + |
| 38 | + -- This cleanup will fail because baz.txt exists |
| 39 | + dir_manager.cleanup() |
| 40 | + assert(vim.uv.fs_stat(root .. "/foo/bar/baz.txt")) |
| 41 | + |
| 42 | + assert(vim.uv.fs_unlink(root .. "/foo/bar/baz.txt")) |
| 43 | + -- This cleanup should succeed |
| 44 | + dir_manager.cleanup() |
| 45 | + assert(not vim.uv.fs_stat(root .. "/foo/bar")) |
| 46 | + assert(not vim.uv.fs_stat(root .. "/foo")) |
| 47 | + |
| 48 | + assert(vim.uv.fs_stat(root)) |
| 49 | + assert(vim.uv.fs_rmdir(root)) |
| 50 | + end) |
| 51 | + |
| 52 | + it("handles race condition for semi-matched nested paths", function() |
| 53 | + local root, err = vim.uv.fs_mkdtemp("conform_XXXXX") |
| 54 | + assert(root, err) |
| 55 | + dir_manager.ensure_parent(root .. "/foo/bar/baz.txt") |
| 56 | + dir_manager.ensure_parent(root .. "/foo/qux/foo.txt") |
| 57 | + touch(root .. "/foo/qux/foo.txt") |
| 58 | + assert(vim.uv.fs_stat(root .. "/foo")) |
| 59 | + assert(vim.uv.fs_stat(root .. "/foo/bar")) |
| 60 | + assert(vim.uv.fs_stat(root .. "/foo/qux")) |
| 61 | + assert(vim.uv.fs_stat(root .. "/foo/qux/foo.txt")) |
| 62 | + |
| 63 | + -- This cleanup will partially succeed because foo.txt exists |
| 64 | + dir_manager.cleanup() |
| 65 | + assert(vim.uv.fs_stat(root .. "/foo")) |
| 66 | + assert(not vim.uv.fs_stat(root .. "/bar")) |
| 67 | + assert(vim.uv.fs_stat(root .. "/foo/qux")) |
| 68 | + assert(vim.uv.fs_stat(root .. "/foo/qux/foo.txt")) |
| 69 | + |
| 70 | + assert(vim.uv.fs_unlink(root .. "/foo/qux/foo.txt")) |
| 71 | + -- This cleanup should succeed |
| 72 | + dir_manager.cleanup() |
| 73 | + assert(not vim.uv.fs_stat(root .. "/foo/qux")) |
| 74 | + assert(not vim.uv.fs_stat(root .. "/foo")) |
| 75 | + |
| 76 | + assert(vim.uv.fs_stat(root)) |
| 77 | + assert(vim.uv.fs_rmdir(root)) |
| 78 | + end) |
| 79 | +end) |
0 commit comments