summaryrefslogtreecommitdiff
path: root/spec/fs_spec.lua
diff options
context:
space:
mode:
authorMike Vink <mike@pionative.com>2025-02-03 21:29:42 +0100
committerMike Vink <mike@pionative.com>2025-02-03 21:29:42 +0100
commit5155816b7b925dec5d5feb1568b1d7ceb00938b9 (patch)
treedeca28ea15e79f6f804c3d90d2ba757881638af5 /spec/fs_spec.lua
fetch tarballHEADmaster
Diffstat (limited to 'spec/fs_spec.lua')
-rw-r--r--spec/fs_spec.lua76
1 files changed, 76 insertions, 0 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua
new file mode 100644
index 0000000..d5e93c4
--- /dev/null
+++ b/spec/fs_spec.lua
@@ -0,0 +1,76 @@
+local test_env = require("spec.util.test_env")
+
+local lfs = require("lfs")
+local testing_paths = test_env.testing_paths
+local get_tmp_path = test_env.get_tmp_path
+
+describe("luarocks.fs #integration", function()
+
+ local fs
+
+ describe("fs.download #mock", function()
+ local tmpfile
+ local tmpdir
+
+ lazy_setup(function()
+ test_env.setup_specs(nil, "mock")
+ local cfg = require("luarocks.core.cfg")
+ fs = require("luarocks.fs")
+ cfg.init()
+ fs.init()
+ test_env.mock_server_init()
+ end)
+
+ lazy_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", 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", 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", 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)