From 06a46560944c0f2685222931362061a3f64db8b6 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 22 Feb 2018 17:35:41 -0300 Subject: Tests: add unit test for fs.is_file --- spec/fs_spec.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'spec') diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index 17ac90ab..a6622f16 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua @@ -2,6 +2,7 @@ local test_env = require("spec.util.test_env") test_env.unload_luarocks() local fs = require("luarocks.fs") +local lfs = require("lfs") local is_win = test_env.TEST_TARGET_OS == "windows" describe("Luarocks fs test #whitebox #w_fs", function() @@ -18,4 +19,39 @@ describe("Luarocks fs test #whitebox #w_fs", function() assert.are.same(is_win and [["\\"%" \\\\" \\\\\\"]] or [['\% \\" \\\']], fs.Q([[\% \\" \\\]])) end) end) + + describe("fs.is_file", function() + local tmpfile + local tmpdir + + after_each(function() + if tmpfile then + os.remove(tmpfile) + tmpfile = nil + end + if tmpdir then + lfs.rmdir(tmpdir) + tmpdir = nil + end + end) + + it("returns true when the argument is a file", function() + tmpfile = os.tmpname() + local fd = assert(io.open(tmpfile, "w")) + fd:write("foo") + fd:close() + assert.same(true, fs.is_file(tmpfile)) + end) + + it("returns false when the argument does not exist", function() + assert.same(false, fs.is_file("/nonexistent")) + end) + + it("returns false when arguments exists but is not a file", function() + tmpdir = os.tmpname() + os.remove(tmpdir) + lfs.mkdir(tmpdir) + assert.same(false, fs.is_file("/nonexistent")) + end) + end) end) -- cgit v1.2.3-55-g6feb