aboutsummaryrefslogtreecommitdiff
path: root/spec/fs_spec.lua
blob: d5e93c4b3e1da7f252b053dc39b93b792cd4659b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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)