|
1 | 1 | local Path = require "plenary.path" |
2 | 2 | local path = Path.path |
3 | 3 |
|
| 4 | +local function new_env() |
| 5 | + local env, trash = {}, {} |
| 6 | + |
| 7 | + function env.new_path(opts) |
| 8 | + opts = opts or {} |
| 9 | + local ret = Path:new(opts.filename or vim.fn.tempname()) |
| 10 | + if opts.touch then |
| 11 | + ret:touch() |
| 12 | + assert(ret:exists()) |
| 13 | + end |
| 14 | + table.insert(trash, ret) |
| 15 | + return ret |
| 16 | + end |
| 17 | + |
| 18 | + function env.cleanup() |
| 19 | + for _, v in ipairs(trash) do |
| 20 | + if type((v or {}).rm) == "function" then |
| 21 | + pcall(v.rm, v) |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + return env |
| 27 | +end |
| 28 | + |
4 | 29 | describe("Path", function() |
5 | 30 | it("should find valid files", function() |
6 | 31 | local p = Path:new "README.md" |
@@ -389,54 +414,48 @@ describe("Path", function() |
389 | 414 | end) |
390 | 415 | end) |
391 | 416 |
|
| 417 | + -- TODO: tests for bugs, and new behavior, allows rename to self |
392 | 418 | describe("rename", function() |
393 | | - it("can rename a file", function() |
394 | | - local p = Path:new "a_random_filename.lua" |
395 | | - assert(pcall(p.touch, p)) |
396 | | - assert(p:exists()) |
397 | | - |
398 | | - assert(pcall(p.rename, p, { new_name = "not_a_random_filename.lua" })) |
399 | | - assert.are.same("not_a_random_filename.lua", p.filename) |
| 419 | + local env = new_env() |
| 420 | + after_each(env.cleanup) |
400 | 421 |
|
401 | | - p:rm() |
402 | | - end) |
403 | | - |
404 | | - it("can handle an invalid filename", function() |
405 | | - local p = Path:new "some_random_filename.lua" |
406 | | - assert(pcall(p.touch, p)) |
407 | | - assert(p:exists()) |
408 | | - |
409 | | - assert(not pcall(p.rename, p, { new_name = "" })) |
410 | | - assert(not pcall(p.rename, p)) |
411 | | - assert.are.same("some_random_filename.lua", p.filename) |
412 | | - |
413 | | - p:rm() |
| 422 | + it("can rename a file", function() |
| 423 | + local before, after = env.new_path { touch = true }, env.new_path() |
| 424 | + before:rename { new_name = after } |
| 425 | + assert.is.False(before:exists()) |
| 426 | + assert.is.True(after:exists()) |
| 427 | + end) |
| 428 | + |
| 429 | + it("should throw on invalid args", function() |
| 430 | + local before = env.new_path { touch = true } |
| 431 | + assert.errors(function() |
| 432 | + before:rename { new_name = "" } |
| 433 | + end) |
| 434 | + assert.errors(function() |
| 435 | + before:rename {} |
| 436 | + end) |
| 437 | + assert.errors(function() |
| 438 | + before:rename() |
| 439 | + end) |
| 440 | + assert.is.True(before:exists()) |
414 | 441 | end) |
415 | 442 |
|
416 | 443 | it("can move to parent dir", function() |
417 | | - local p = Path:new "some_random_filename.lua" |
418 | | - assert(pcall(p.touch, p)) |
419 | | - assert(p:exists()) |
420 | | - |
421 | | - assert(pcall(p.rename, p, { new_name = "../some_random_filename.lua" })) |
422 | | - assert.are.same(vim.loop.fs_realpath(Path:new("../some_random_filename.lua"):absolute()), p:absolute()) |
423 | | - |
424 | | - p:rm() |
425 | | - end) |
426 | | - |
427 | | - it("cannot rename to an existing filename", function() |
428 | | - local p1 = Path:new "a_random_filename.lua" |
429 | | - local p2 = Path:new "not_a_random_filename.lua" |
430 | | - assert(pcall(p1.touch, p1)) |
431 | | - assert(pcall(p2.touch, p2)) |
432 | | - assert(p1:exists()) |
433 | | - assert(p2:exists()) |
434 | | - |
435 | | - assert(not pcall(p1.rename, p1, { new_name = "not_a_random_filename.lua" })) |
436 | | - assert.are.same(p1.filename, "a_random_filename.lua") |
437 | | - |
438 | | - p1:rm() |
439 | | - p2:rm() |
| 444 | + local before, after = env.new_path { filename = "random_file" }, env.new_path { filename = "../random_file" } |
| 445 | + assert.is.False(before:exists()) |
| 446 | + before:touch() |
| 447 | + assert.is.True(before:exists()) |
| 448 | + assert.is.False(after:exists()) |
| 449 | + before:rename { new_name = after } |
| 450 | + assert.is.False(before:exists()) |
| 451 | + assert.is.True(after:exists()) |
| 452 | + end) |
| 453 | + |
| 454 | + it("should throw on rename to existing filename", function() |
| 455 | + local before, after = env.new_path { touch = true }, env.new_path { touch = true } |
| 456 | + assert.errors(function() |
| 457 | + before:rename { new_name = after } |
| 458 | + end) |
440 | 459 | end) |
441 | 460 | end) |
442 | 461 |
|
|
0 commit comments