Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions spec/std/dir_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,82 @@ describe "Dir" do
end
end

describe "mktmpdir" do
it "creates the temporary directory" do
tmp_dir = Dir.mktmpdir

Dir.exists?(tmp_dir).should be_true
end

it "accepts a prefix when creating the temporary directory" do
prefix = "foo"
path = Dir.mktmpdir(prefix)

Dir.exists?(path).should be_true
(path =~ /#{prefix}/).should_not be_nil
end

describe "with a block" do
it "creates the dir" do
Dir.mktmpdir do |dir|
Dir.exists?(dir).should be_true
end
end

it "removes the dir" do
tmp_dir = "/"
Dir.mktmpdir do |dir|
tmp_dir = dir
end

Dir.exists?(tmp_dir).should be_false
end
end
end

describe "tmpdir" do
it "returns default tmp directory" do
original_tmpdir = ENV["TMPDIR"]?
ENV.delete("TMPDIR")
Dir.tmpdir.should eq("/tmp")
ENV["TMPDIR"] = original_tmpdir if original_tmpdir
end

it "returns configured tmp directory" do
original_tmpdir = ENV["TMPDIR"]?
ENV["TMPDIR"] = "/my/tmp"
Dir.tmpdir.should eq("/my/tmp")

if original_tmpdir
ENV["TMPDIR"] = original_tmpdir
else
ENV.delete("TMPDIR")
end
end
end

describe "rm_r" do
it "deletes a directory recursively" do
data_path = File.join(__DIR__, "data")
path = File.join(data_path, "rm_r_test")

begin
Dir.mkdir(path)
File.write(File.join(path, "a"), "")
Dir.mkdir(File.join(path, "b"))
File.write(File.join(path, "b/c"), "")

Dir.rm_r(path)
Dir.exists?(path).should be_false
ensure
File.delete(File.join(path, "b/c")) if File.exists?(File.join(path, "b/c"))
File.delete(File.join(path, "a")) if File.exists?(File.join(path, "a"))
Dir.rmdir(File.join(path, "b")) if Dir.exists?(File.join(path, "b"))
Dir.rmdir(path) if Dir.exists?(path)
end
end
end

describe "glob" do
it "tests glob with a single pattern" do
assert_dir_glob [
Expand Down
285 changes: 0 additions & 285 deletions spec/std/file_utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -156,289 +156,4 @@ describe "FileUtils" do
end
end
end

describe "rm_r" do
it "deletes a directory recursively" do
data_path = File.join(__DIR__, "data")
path = File.join(data_path, "rm_r_test")

begin
Dir.mkdir(path)
File.write(File.join(path, "a"), "")
Dir.mkdir(File.join(path, "b"))
File.write(File.join(path, "b/c"), "")

FileUtils.rm_r(path)
Dir.exists?(path).should be_false
ensure
File.delete(File.join(path, "b/c")) if File.exists?(File.join(path, "b/c"))
File.delete(File.join(path, "a")) if File.exists?(File.join(path, "a"))
Dir.rmdir(File.join(path, "b")) if Dir.exists?(File.join(path, "b"))
Dir.rmdir(path) if Dir.exists?(path)
end
end

it "doesn't follow symlinks" do
data_path = File.join(__DIR__, "data")
removed_path = File.join(data_path, "rm_r_test_removed")
linked_path = File.join(data_path, "rm_r_test_linked")
link_path = File.join(removed_path, "link")
file_path = File.join(linked_path, "file")

begin
Dir.mkdir(removed_path)
Dir.mkdir(linked_path)
File.symlink(linked_path, link_path)
File.write(file_path, "")

FileUtils.rm_r(removed_path)
Dir.exists?(removed_path).should be_false
Dir.exists?(linked_path).should be_true
File.exists?(file_path).should be_true
ensure
File.delete(file_path) if File.exists?(file_path)
File.delete(link_path) if File.exists?(link_path)
Dir.rmdir(linked_path) if Dir.exists?(linked_path)
Dir.rmdir(removed_path) if Dir.exists?(removed_path)
end
end
end

describe "rm_rf" do
it "delete recursively a directory" do
path = "/tmp/crystal_rm_rftest_#{Process.pid}/"
FileUtils.mkdir(path)
File.write(File.join(path, "a"), "")
FileUtils.mkdir(File.join(path, "b"))
FileUtils.rm_rf(path).should be_nil
Dir.exists?(path).should be_false
end

it "delete recursively multiple directory" do
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = "/tmp/crystal_rm_rftest_#{Process.pid + 1}/"
FileUtils.mkdir(path1)
FileUtils.mkdir(path2)
File.write(File.join(path1, "a"), "")
File.write(File.join(path2, "a"), "")
FileUtils.mkdir(File.join(path1, "b"))
FileUtils.mkdir(File.join(path2, "b"))
FileUtils.rm_rf([path1, path2]).should be_nil
Dir.exists?(path1).should be_false
Dir.exists?(path2).should be_false
end

it "doesn't return error on non existing file" do
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = File.join(path1, "a")
FileUtils.mkdir(path1)
FileUtils.rm_rf([path1, path2]).should be_nil
end
end

describe "mv" do
it "moves a file from one place to another" do
begin
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = "/tmp/crystal_rm_rftest_#{Process.pid + 1}/"
FileUtils.mkdir([path1, path2])
path1 = File.join(path1, "a")
path2 = File.join(path2, "b")
File.write(path1, "")
FileUtils.mv(path1, path2).should be_nil
File.exists?(path1).should be_false
File.exists?(path2).should be_true
ensure
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = "/tmp/crystal_rm_rftest_#{Process.pid + 1}/"
FileUtils.rm_rf([path1, path2])
end
end

it "raises an error if non correct arguments" do
expect_raises Errno do
FileUtils.mv("/tmp/crystal_mv_test/a", "/tmp/crystal_mv_test/b")
end
end

it "moves multiple files to one place" do
begin
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = "/tmp/crystal_rm_rftest_#{Process.pid + 1}/"
path3 = "/tmp/crystal_rm_rftest_#{Process.pid + 2}/"
FileUtils.mkdir([path1, path2, path3])
path1 = File.join(path1, "a")
path2 = File.join(path2, "b")
File.write(path1, "")
File.write(path2, "")
FileUtils.mv([path1, path2], path3).should be_nil
File.exists?(path1).should be_false
File.exists?(path2).should be_false
File.exists?(File.join(path3, "a")).should be_true
File.exists?(File.join(path3, "b")).should be_true
ensure
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = "/tmp/crystal_rm_rftest_#{Process.pid + 1}/"
path3 = "/tmp/crystal_rm_rftest_#{Process.pid + 2}/"
FileUtils.rm_rf([path1, path2, path3])
end
end

it "raises an error if dest is non correct" do
expect_raises ArgumentError do
FileUtils.mv(["/tmp/crystal_mv_test/a", "/tmp/crystal_mv_test/b"], "/tmp/crystal_not_here")
end
end

it "moves all existing files to destination" do
begin
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = "/tmp/crystal_rm_rftest_#{Process.pid + 1}/"
path3 = "/tmp/crystal_rm_rftest_#{Process.pid + 2}/"
path4 = "/tmp/crystal_rm_rftest_#{Process.pid + 3}/a"
FileUtils.mkdir([path1, path2, path3])
path1 = File.join(path1, "a")
path2 = File.join(path2, "b")
File.write(path1, "")
File.write(path2, "")
FileUtils.mv([path1, path2, path4], path3).should be_nil
File.exists?(path1).should be_false
File.exists?(path2).should be_false
File.exists?(File.join(path3, "a")).should be_true
File.exists?(File.join(path3, "b")).should be_true
ensure
path1 = "/tmp/crystal_rm_rftest_#{Process.pid}/"
path2 = "/tmp/crystal_rm_rftest_#{Process.pid + 1}/"
path3 = "/tmp/crystal_rm_rftest_#{Process.pid + 2}/"
FileUtils.rm_rf([path1, path2, path3])
end
end
end

it "tests mkdir and rmdir with a new path" do
path = "/tmp/crystal_mkdir_test_#{Process.pid}/"
FileUtils.mkdir(path, 0o700).should be_nil
Dir.exists?(path).should be_true
FileUtils.rmdir(path).should be_nil
Dir.exists?(path).should be_false
end

it "tests mkdir and rmdir with multiple new paths" do
path1 = "/tmp/crystal_mkdir_test_#{Process.pid}/"
path2 = "/tmp/crystal_mkdir_test_#{Process.pid + 1}/"
FileUtils.mkdir([path1, path2], 0o700).should be_nil
Dir.exists?(path1).should be_true
Dir.exists?(path2).should be_true
FileUtils.rmdir([path1, path2]).should be_nil
Dir.exists?(path1).should be_false
Dir.exists?(path2).should be_false
end

it "tests mkdir with an existing path" do
expect_raises Errno do
Dir.mkdir(__DIR__, 0o700)
end
end

it "tests mkdir with multiples existing paths" do
expect_raises Errno do
FileUtils.mkdir([__DIR__, __DIR__], 0o700)
end
expect_raises Errno do
FileUtils.mkdir(["/tmp/crystal_mkdir_test_#{Process.pid}/", __DIR__], 0o700)
end
end

it "tests mkdir_p with a new path" do
path = "/tmp/crystal_mkdir_ptest_#{Process.pid}/"
FileUtils.mkdir_p(path).should be_nil
Dir.exists?(path).should be_true
path = File.join({path, "a", "b", "c"})
FileUtils.mkdir_p(path).should be_nil
Dir.exists?(path).should be_true
end

it "tests mkdir_p with multiples new path" do
path1 = "/tmp/crystal_mkdir_ptest_#{Process.pid}/"
path2 = "/tmp/crystal_mkdir_ptest_#{Process.pid + 1}"
FileUtils.mkdir_p([path1, path2]).should be_nil
Dir.exists?(path1).should be_true
Dir.exists?(path2).should be_true
path1 = File.join({path1, "a", "b", "c"})
path2 = File.join({path2, "a", "b", "c"})
FileUtils.mkdir_p([path1, path2]).should be_nil
Dir.exists?(path1).should be_true
Dir.exists?(path2).should be_true
end

it "tests mkdir_p with an existing path" do
FileUtils.mkdir_p(__DIR__).should be_nil
expect_raises Errno do
FileUtils.mkdir_p(__FILE__)
end
end

it "tests mkdir_p with multiple existing path" do
FileUtils.mkdir_p([__DIR__, __DIR__]).should be_nil
expect_raises Errno do
FileUtils.mkdir_p([__FILE__, "/tmp/crystal_mkdir_ptest_#{Process.pid}/"])
end
end

it "tests rmdir with an non existing path" do
expect_raises Errno do
FileUtils.rmdir("/tmp/crystal_mkdir_test_#{Process.pid}/tmp/")
end
end

it "tests rmdir with multiple non existing path" do
expect_raises Errno do
FileUtils.rmdir(["/tmp/crystal_mkdir_test_#{Process.pid}/tmp/", "/tmp/crystal_mkdir_test_#{Process.pid + 1}/tmp/"])
end
end

it "tests rmdir with a path that cannot be removed" do
expect_raises Errno do
FileUtils.rmdir(__DIR__)
end
end

it "tests rmdir with multiple path that cannot be removed" do
expect_raises Errno do
FileUtils.rmdir([__DIR__, __DIR__])
end
end

it "tests rm with an existing path" do
path = "/tmp/crystal_rm_test_#{Process.pid}"
File.write(path, "")
FileUtils.rm(path).should be_nil
File.exists?(path).should be_false
end

it "tests rm with non existing path" do
expect_raises Errno do
FileUtils.rm("/tmp/crystal_rm_test_#{Process.pid}")
end
end

it "tests rm with multiple existing paths" do
path1 = "/tmp/crystal_rm_test_#{Process.pid}"
path2 = "/tmp/crystal_rm_test_#{Process.pid + 1}"
File.write(path1, "")
File.write(path2, "")
FileUtils.rm([path1, path2]).should be_nil
File.exists?(path1).should be_false
File.exists?(path2).should be_false
end

it "tests rm with some non existing paths" do
expect_raises Errno do
path1 = "/tmp/crystal_rm_test_#{Process.pid}"
path2 = "/tmp/crystal_rm_test_#{Process.pid + 1}"
File.write(path1, "")
File.write(path2, "")
FileUtils.rm([path1, path2, path2])
end
end
end
14 changes: 0 additions & 14 deletions spec/std/tempfile_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,4 @@ describe Tempfile do
tempfile.gets(chomp: false).should eq("Hello!\n")
tempfile.close
end

it "returns default directory for tempfiles" do
old_tmpdir = ENV["TMPDIR"]?
ENV.delete("TMPDIR")
Tempfile.dirname.should eq("/tmp")
ENV["TMPDIR"] = old_tmpdir if old_tmpdir
end

it "returns configure directory for tempfiles" do
old_tmpdir = ENV["TMPDIR"]?
ENV["TMPDIR"] = "/my/tmp"
Tempfile.dirname.should eq("/my/tmp")
ENV["TMPDIR"] = old_tmpdir if old_tmpdir
end
end
Loading