diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-26 18:30:31 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-27 13:28:33 -0300 |
| commit | 03e401231972d7b4db0145a71ce74d5d18d1a901 (patch) | |
| tree | 10e59cd4fe0065ec87db0699965bb5e2f0436d74 /spec/fs_spec.lua | |
| parent | 5b2e63e6b087bdf626dd7cef319d6ba29f5e197c (diff) | |
| download | luarocks-03e401231972d7b4db0145a71ce74d5d18d1a901.tar.gz luarocks-03e401231972d7b4db0145a71ce74d5d18d1a901.tar.bz2 luarocks-03e401231972d7b4db0145a71ce74d5d18d1a901.zip | |
tests: reorganize and speed up unit tests
Diffstat (limited to 'spec/fs_spec.lua')
| -rw-r--r-- | spec/fs_spec.lua | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua new file mode 100644 index 00000000..d5e93c4b --- /dev/null +++ b/spec/fs_spec.lua | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | local test_env = require("spec.util.test_env") | ||
| 2 | |||
| 3 | local lfs = require("lfs") | ||
| 4 | local testing_paths = test_env.testing_paths | ||
| 5 | local get_tmp_path = test_env.get_tmp_path | ||
| 6 | |||
| 7 | describe("luarocks.fs #integration", function() | ||
| 8 | |||
| 9 | local fs | ||
| 10 | |||
| 11 | describe("fs.download #mock", function() | ||
| 12 | local tmpfile | ||
| 13 | local tmpdir | ||
| 14 | |||
| 15 | lazy_setup(function() | ||
| 16 | test_env.setup_specs(nil, "mock") | ||
| 17 | local cfg = require("luarocks.core.cfg") | ||
| 18 | fs = require("luarocks.fs") | ||
| 19 | cfg.init() | ||
| 20 | fs.init() | ||
| 21 | test_env.mock_server_init() | ||
| 22 | end) | ||
| 23 | |||
| 24 | lazy_teardown(function() | ||
| 25 | test_env.mock_server_done() | ||
| 26 | end) | ||
| 27 | |||
| 28 | after_each(function() | ||
| 29 | if tmpfile then | ||
| 30 | os.remove(tmpfile) | ||
| 31 | tmpfile = nil | ||
| 32 | end | ||
| 33 | if tmpdir then | ||
| 34 | lfs.rmdir(tmpdir) | ||
| 35 | tmpdir = nil | ||
| 36 | end | ||
| 37 | end) | ||
| 38 | |||
| 39 | it("returns true and fetches the url argument into the specified filename", function() | ||
| 40 | tmpfile = get_tmp_path() | ||
| 41 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) | ||
| 42 | local fd = assert(io.open(tmpfile, "r")) | ||
| 43 | local downloadcontent = assert(fd:read("*a")) | ||
| 44 | fd:close() | ||
| 45 | fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) | ||
| 46 | local originalcontent = assert(fd:read("*a")) | ||
| 47 | fd:close() | ||
| 48 | assert.same(downloadcontent, originalcontent) | ||
| 49 | end) | ||
| 50 | |||
| 51 | 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() | ||
| 52 | tmpdir = get_tmp_path() | ||
| 53 | lfs.mkdir(tmpdir) | ||
| 54 | fs.change_dir(tmpdir) | ||
| 55 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua")) | ||
| 56 | tmpfile = tmpdir .. "/a_rock.lua" | ||
| 57 | local fd = assert(io.open(tmpfile, "r")) | ||
| 58 | local downloadcontent = assert(fd:read("*a")) | ||
| 59 | fd:close() | ||
| 60 | fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) | ||
| 61 | local originalcontent = assert(fd:read("*a")) | ||
| 62 | fd:close() | ||
| 63 | assert.same(downloadcontent, originalcontent) | ||
| 64 | fs.pop_dir() | ||
| 65 | end) | ||
| 66 | |||
| 67 | it("returns false and does nothing if the url argument contains a nonexistent file", function() | ||
| 68 | tmpfile = get_tmp_path() | ||
| 69 | assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) | ||
| 70 | end) | ||
| 71 | |||
| 72 | it("returns false and does nothing if the url argument is invalid", function() | ||
| 73 | assert.falsy(fs.download("invalidurl")) | ||
| 74 | end) | ||
| 75 | end) | ||
| 76 | end) | ||
