From 4ac4aad3bf7e37c6e1467f1885036850f903d70c Mon Sep 17 00:00:00 2001 From: George Roman Date: Mon, 21 May 2018 21:34:51 +0300 Subject: Add tests for fs.download --- spec/fs_spec.lua | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'spec') diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index a60dac56..29176af5 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua @@ -5,6 +5,7 @@ local fs = require("luarocks.fs") local lfs = require("lfs") local is_win = test_env.TEST_TARGET_OS == "windows" local posix_ok = pcall(require, "posix") +local testing_paths = test_env.testing_paths describe("Luarocks fs test #whitebox #w_fs", function() local get_tmp_path = function() @@ -1183,4 +1184,65 @@ describe("Luarocks fs test #whitebox #w_fs", function() assert.falsy(exists_file(tmpdir)) end) end) + + describe("fs.download", function() + local tmpfile + local tmpdir + + setup(function() + test_env.mock_server_init() + end) + + teardown(function() + test_env.mock_server_done() + end) + + 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 and fetches the url argument into the specified filename #mock", function() + tmpfile = get_tmp_path() + assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) + local fd = assert(io.open(tmpfile, "r")) + local downloadcontent = assert(fd:read("*a")) + fd:close() + fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) + local originalcontent = assert(fd:read("*a")) + fd:close() + assert.same(downloadcontent, originalcontent) + end) + + it("returns true and fetches the url argument into a file whose name matches the basename of the url if the filename argument is not given #mock", function() + tmpdir = get_tmp_path() + lfs.mkdir(tmpdir) + fs.change_dir(tmpdir) + assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua")) + tmpfile = tmpdir .. "/a_rock.lua" + local fd = assert(io.open(tmpfile, "r")) + local downloadcontent = assert(fd:read("*a")) + fd:close() + fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) + local originalcontent = assert(fd:read("*a")) + fd:close() + assert.same(downloadcontent, originalcontent) + fs.pop_dir() + end) + + it("returns false and does nothing if the url argument contains a nonexistent file #mock", function() + tmpfile = get_tmp_path() + assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) + end) + + it("returns false and does nothing if the url argument is invalid", function() + assert.falsy(fs.download("invalidurl")) + end) + end) end) -- cgit v1.2.3-55-g6feb