diff options
Diffstat (limited to 'spec/fs_spec.lua')
| -rw-r--r-- | spec/fs_spec.lua | 62 |
1 files changed, 62 insertions, 0 deletions
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") | |||
| 5 | local lfs = require("lfs") | 5 | local lfs = require("lfs") |
| 6 | local is_win = test_env.TEST_TARGET_OS == "windows" | 6 | local is_win = test_env.TEST_TARGET_OS == "windows" |
| 7 | local posix_ok = pcall(require, "posix") | 7 | local posix_ok = pcall(require, "posix") |
| 8 | local testing_paths = test_env.testing_paths | ||
| 8 | 9 | ||
| 9 | describe("Luarocks fs test #whitebox #w_fs", function() | 10 | describe("Luarocks fs test #whitebox #w_fs", function() |
| 10 | local get_tmp_path = function() | 11 | local get_tmp_path = function() |
| @@ -1183,4 +1184,65 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
| 1183 | assert.falsy(exists_file(tmpdir)) | 1184 | assert.falsy(exists_file(tmpdir)) |
| 1184 | end) | 1185 | end) |
| 1185 | end) | 1186 | end) |
| 1187 | |||
| 1188 | describe("fs.download", function() | ||
| 1189 | local tmpfile | ||
| 1190 | local tmpdir | ||
| 1191 | |||
| 1192 | setup(function() | ||
| 1193 | test_env.mock_server_init() | ||
| 1194 | end) | ||
| 1195 | |||
| 1196 | teardown(function() | ||
| 1197 | test_env.mock_server_done() | ||
| 1198 | end) | ||
| 1199 | |||
| 1200 | after_each(function() | ||
| 1201 | if tmpfile then | ||
| 1202 | os.remove(tmpfile) | ||
| 1203 | tmpfile = nil | ||
| 1204 | end | ||
| 1205 | if tmpdir then | ||
| 1206 | lfs.rmdir(tmpdir) | ||
| 1207 | tmpdir = nil | ||
| 1208 | end | ||
| 1209 | end) | ||
| 1210 | |||
| 1211 | it("returns true and fetches the url argument into the specified filename #mock", function() | ||
| 1212 | tmpfile = get_tmp_path() | ||
| 1213 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) | ||
| 1214 | local fd = assert(io.open(tmpfile, "r")) | ||
| 1215 | local downloadcontent = assert(fd:read("*a")) | ||
| 1216 | fd:close() | ||
| 1217 | fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) | ||
| 1218 | local originalcontent = assert(fd:read("*a")) | ||
| 1219 | fd:close() | ||
| 1220 | assert.same(downloadcontent, originalcontent) | ||
| 1221 | end) | ||
| 1222 | |||
| 1223 | 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() | ||
| 1224 | tmpdir = get_tmp_path() | ||
| 1225 | lfs.mkdir(tmpdir) | ||
| 1226 | fs.change_dir(tmpdir) | ||
| 1227 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua")) | ||
| 1228 | tmpfile = tmpdir .. "/a_rock.lua" | ||
| 1229 | local fd = assert(io.open(tmpfile, "r")) | ||
| 1230 | local downloadcontent = assert(fd:read("*a")) | ||
| 1231 | fd:close() | ||
| 1232 | fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) | ||
| 1233 | local originalcontent = assert(fd:read("*a")) | ||
| 1234 | fd:close() | ||
| 1235 | assert.same(downloadcontent, originalcontent) | ||
| 1236 | fs.pop_dir() | ||
| 1237 | end) | ||
| 1238 | |||
| 1239 | it("returns false and does nothing if the url argument contains a nonexistent file #mock", function() | ||
| 1240 | tmpfile = get_tmp_path() | ||
| 1241 | assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) | ||
| 1242 | end) | ||
| 1243 | |||
| 1244 | it("returns false and does nothing if the url argument is invalid", function() | ||
| 1245 | assert.falsy(fs.download("invalidurl")) | ||
| 1246 | end) | ||
| 1247 | end) | ||
| 1186 | end) | 1248 | end) |
