diff options
| author | George Roman <george.roman.99@gmail.com> | 2018-06-21 20:16:10 +0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-21 23:32:19 -0300 |
| commit | 48ae1d2e7705af6a7527ca61710fe18ccdb7d0b9 (patch) | |
| tree | 5f8cfca68dd0abc9a5016a8d90911b125c24ba09 | |
| parent | 7037d5e3898441f1383e7684aaa89ee4c0fc8477 (diff) | |
| download | luarocks-48ae1d2e7705af6a7527ca61710fe18ccdb7d0b9.tar.gz luarocks-48ae1d2e7705af6a7527ca61710fe18ccdb7d0b9.tar.bz2 luarocks-48ae1d2e7705af6a7527ca61710fe18ccdb7d0b9.zip | |
Tests: improve the tests for the fs module
| -rw-r--r-- | spec/fs_spec.lua | 143 |
1 files changed, 139 insertions, 4 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index f1529c39..9b2aa01b 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua | |||
| @@ -8,6 +8,7 @@ local is_win = test_env.TEST_TARGET_OS == "windows" | |||
| 8 | local posix_ok = pcall(require, "posix") | 8 | local posix_ok = pcall(require, "posix") |
| 9 | local testing_paths = test_env.testing_paths | 9 | local testing_paths = test_env.testing_paths |
| 10 | local get_tmp_path = test_env.get_tmp_path | 10 | local get_tmp_path = test_env.get_tmp_path |
| 11 | local write_file = test_env.write_file | ||
| 11 | 12 | ||
| 12 | describe("Luarocks fs test #unit", function() | 13 | describe("Luarocks fs test #unit", function() |
| 13 | local exists_file = function(path) | 14 | local exists_file = function(path) |
| @@ -1190,7 +1191,7 @@ describe("Luarocks fs test #unit", function() | |||
| 1190 | end) | 1191 | end) |
| 1191 | end) | 1192 | end) |
| 1192 | 1193 | ||
| 1193 | describe("fs.download", function() | 1194 | describe("fs.download #mock", function() |
| 1194 | local tmpfile | 1195 | local tmpfile |
| 1195 | local tmpdir | 1196 | local tmpdir |
| 1196 | 1197 | ||
| @@ -1213,7 +1214,7 @@ describe("Luarocks fs test #unit", function() | |||
| 1213 | end | 1214 | end |
| 1214 | end) | 1215 | end) |
| 1215 | 1216 | ||
| 1216 | it("returns true and fetches the url argument into the specified filename #mock", function() | 1217 | it("returns true and fetches the url argument into the specified filename", function() |
| 1217 | tmpfile = get_tmp_path() | 1218 | tmpfile = get_tmp_path() |
| 1218 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) | 1219 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) |
| 1219 | local fd = assert(io.open(tmpfile, "r")) | 1220 | local fd = assert(io.open(tmpfile, "r")) |
| @@ -1225,7 +1226,7 @@ describe("Luarocks fs test #unit", function() | |||
| 1225 | assert.same(downloadcontent, originalcontent) | 1226 | assert.same(downloadcontent, originalcontent) |
| 1226 | end) | 1227 | end) |
| 1227 | 1228 | ||
| 1228 | 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() | 1229 | 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() |
| 1229 | tmpdir = get_tmp_path() | 1230 | tmpdir = get_tmp_path() |
| 1230 | lfs.mkdir(tmpdir) | 1231 | lfs.mkdir(tmpdir) |
| 1231 | fs.change_dir(tmpdir) | 1232 | fs.change_dir(tmpdir) |
| @@ -1241,7 +1242,7 @@ describe("Luarocks fs test #unit", function() | |||
| 1241 | fs.pop_dir() | 1242 | fs.pop_dir() |
| 1242 | end) | 1243 | end) |
| 1243 | 1244 | ||
| 1244 | it("returns false and does nothing if the url argument contains a nonexistent file #mock", function() | 1245 | it("returns false and does nothing if the url argument contains a nonexistent file", function() |
| 1245 | tmpfile = get_tmp_path() | 1246 | tmpfile = get_tmp_path() |
| 1246 | assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) | 1247 | assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) |
| 1247 | end) | 1248 | end) |
| @@ -1250,4 +1251,138 @@ describe("Luarocks fs test #unit", function() | |||
| 1250 | assert.falsy(fs.download("invalidurl")) | 1251 | assert.falsy(fs.download("invalidurl")) |
| 1251 | end) | 1252 | end) |
| 1252 | end) | 1253 | end) |
| 1254 | |||
| 1255 | describe("fs.zip", function() | ||
| 1256 | local tmpdir | ||
| 1257 | local olddir | ||
| 1258 | |||
| 1259 | before_each(function() | ||
| 1260 | olddir = lfs.currentdir() | ||
| 1261 | tmpdir = get_tmp_path() | ||
| 1262 | lfs.mkdir(tmpdir) | ||
| 1263 | lfs.chdir(tmpdir) | ||
| 1264 | |||
| 1265 | write_file("file1", "content1", finally) | ||
| 1266 | write_file("file2", "content2", finally) | ||
| 1267 | lfs.mkdir("dir") | ||
| 1268 | write_file("dir/file3", "content3", finally) | ||
| 1269 | end) | ||
| 1270 | |||
| 1271 | after_each(function() | ||
| 1272 | if olddir then | ||
| 1273 | lfs.chdir(olddir) | ||
| 1274 | if tmpdir then | ||
| 1275 | lfs.rmdir(tmpdir .. "/dir") | ||
| 1276 | lfs.rmdir(tmpdir) | ||
| 1277 | end | ||
| 1278 | end | ||
| 1279 | end) | ||
| 1280 | |||
| 1281 | it("returns true and creates a zip archive of the given files", function() | ||
| 1282 | assert.truthy(fs.zip("archive.zip", "file1", "file2", "dir")) | ||
| 1283 | assert.truthy(exists_file("archive.zip")) | ||
| 1284 | end) | ||
| 1285 | |||
| 1286 | it("returns false and does nothing if the files specified in the arguments are invalid", function() | ||
| 1287 | assert.falsy(fs.zip("archive.zip", "nonexistent")) | ||
| 1288 | assert.falsy(exists_file("nonexistent")) | ||
| 1289 | end) | ||
| 1290 | end) | ||
| 1291 | |||
| 1292 | describe("fs.unzip", function() | ||
| 1293 | local tmpdir | ||
| 1294 | local olddir | ||
| 1295 | |||
| 1296 | before_each(function() | ||
| 1297 | olddir = lfs.currentdir() | ||
| 1298 | tmpdir = get_tmp_path() | ||
| 1299 | lfs.mkdir(tmpdir) | ||
| 1300 | lfs.chdir(tmpdir) | ||
| 1301 | |||
| 1302 | write_file("file1", "content1", finally) | ||
| 1303 | write_file("file2", "content2", finally) | ||
| 1304 | lfs.mkdir("dir") | ||
| 1305 | write_file("dir/file3", "content3", finally) | ||
| 1306 | end) | ||
| 1307 | |||
| 1308 | after_each(function() | ||
| 1309 | if olddir then | ||
| 1310 | lfs.chdir(olddir) | ||
| 1311 | if tmpdir then | ||
| 1312 | lfs.rmdir(tmpdir .. "/dir") | ||
| 1313 | lfs.rmdir(tmpdir) | ||
| 1314 | end | ||
| 1315 | end | ||
| 1316 | end) | ||
| 1317 | |||
| 1318 | it("returns true and unzips the given zip archive", function() | ||
| 1319 | assert.truthy(fs.zip("archive.zip", "file1", "file2", "dir")) | ||
| 1320 | os.remove("file1") | ||
| 1321 | os.remove("file2") | ||
| 1322 | lfs.rmdir("dir") | ||
| 1323 | |||
| 1324 | assert.truthy(fs.unzip("archive.zip")) | ||
| 1325 | assert.truthy(exists_file("file1")) | ||
| 1326 | assert.truthy(exists_file("file2")) | ||
| 1327 | assert.truthy(exists_file("dir/file3")) | ||
| 1328 | |||
| 1329 | local fd | ||
| 1330 | |||
| 1331 | fd = assert(io.open("file1", "r")) | ||
| 1332 | assert.same(fd:read("*a"), "content1") | ||
| 1333 | fd:close() | ||
| 1334 | |||
| 1335 | fd = assert(io.open("file2", "r")) | ||
| 1336 | assert.same(fd:read("*a"), "content2") | ||
| 1337 | fd:close() | ||
| 1338 | |||
| 1339 | fd = assert(io.open("dir/file3", "r")) | ||
| 1340 | assert.same(fd:read("*a"), "content3") | ||
| 1341 | fd:close() | ||
| 1342 | end) | ||
| 1343 | |||
| 1344 | it("does nothing if the given archive is invalid", function() | ||
| 1345 | assert.falsy(fs.unzip("archive.zip")) | ||
| 1346 | end) | ||
| 1347 | end) | ||
| 1348 | |||
| 1349 | describe("fs.copy_binary", function() | ||
| 1350 | local tmpdir | ||
| 1351 | local olddir | ||
| 1352 | |||
| 1353 | before_each(function() | ||
| 1354 | olddir = lfs.currentdir() | ||
| 1355 | tmpdir = get_tmp_path() | ||
| 1356 | lfs.mkdir(tmpdir) | ||
| 1357 | lfs.chdir(tmpdir) | ||
| 1358 | |||
| 1359 | write_file("test.exe", "", finally) | ||
| 1360 | end) | ||
| 1361 | |||
| 1362 | after_each(function() | ||
| 1363 | if olddir then | ||
| 1364 | lfs.chdir(olddir) | ||
| 1365 | if tmpdir then | ||
| 1366 | lfs.rmdir(tmpdir) | ||
| 1367 | end | ||
| 1368 | end | ||
| 1369 | end) | ||
| 1370 | |||
| 1371 | it("returns true and copies the given binary file to the file specified in the dest argument", function() | ||
| 1372 | assert.truthy(fs.copy_binary("test.exe", lfs.currentdir() .. "/copy.exe")) | ||
| 1373 | assert.truthy(exists_file("copy.exe")) | ||
| 1374 | if is_win then | ||
| 1375 | assert.truthy(exists_file("test.lua")) | ||
| 1376 | local fd = assert(io.open("test.lua", "r")) | ||
| 1377 | local content = assert(fd:read("*a")) | ||
| 1378 | assert.truthy(content:find("package.path", 1, true)) | ||
| 1379 | assert.truthy(content:find("package.cpath", 1, true)) | ||
| 1380 | fd:close() | ||
| 1381 | end | ||
| 1382 | end) | ||
| 1383 | |||
| 1384 | it("returns false and does nothing if the source file is invalid", function() | ||
| 1385 | assert.falsy(fs.copy_binary("invalid.exe", "copy.exe")) | ||
| 1386 | end) | ||
| 1387 | end) | ||
| 1253 | end) | 1388 | end) |
