diff options
| author | George Roman <george.roman.99@gmail.com> | 2018-05-18 13:14:33 +0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-05-22 10:22:49 -0300 |
| commit | 38fce46096bed1c56b4ebed2dfc2189c4103534e (patch) | |
| tree | 40a741bed99927152c1a5c8461a306fe5732610b | |
| parent | 158b31967ca4d275db7269991bd5a598c214e307 (diff) | |
| download | luarocks-38fce46096bed1c56b4ebed2dfc2189c4103534e.tar.gz luarocks-38fce46096bed1c56b4ebed2dfc2189c4103534e.tar.bz2 luarocks-38fce46096bed1c56b4ebed2dfc2189c4103534e.zip | |
Improve fs module tests
| -rw-r--r-- | spec/fs_spec.lua | 289 |
1 files changed, 289 insertions, 0 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index 4a557629..a60dac56 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua | |||
| @@ -71,6 +71,24 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
| 71 | end) | 71 | end) |
| 72 | end) | 72 | end) |
| 73 | 73 | ||
| 74 | describe("fs.execute_string", function() | ||
| 75 | local tmpdir | ||
| 76 | |||
| 77 | after_each(function() | ||
| 78 | if tmpdir then | ||
| 79 | lfs.rmdir(tmpdir) | ||
| 80 | tmpdir = nil | ||
| 81 | end | ||
| 82 | end) | ||
| 83 | |||
| 84 | it("returns the status code and runs the command given in the argument", function() | ||
| 85 | tmpdir = get_tmp_path() | ||
| 86 | assert.truthy(fs.execute_string("mkdir " .. fs.Q(tmpdir))) | ||
| 87 | assert.truthy(fs.is_dir(tmpdir)) | ||
| 88 | assert.falsy(fs.execute_string("invalidcommand")) | ||
| 89 | end) | ||
| 90 | end) | ||
| 91 | |||
| 74 | describe("fs.dir_iterator", function() | 92 | describe("fs.dir_iterator", function() |
| 75 | local tmpfile1 | 93 | local tmpfile1 |
| 76 | local tmpfile2 | 94 | local tmpfile2 |
| @@ -671,6 +689,70 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
| 671 | assert.truthy(exists_file(tmpdir)) | 689 | assert.truthy(exists_file(tmpdir)) |
| 672 | end) | 690 | end) |
| 673 | end) | 691 | end) |
| 692 | |||
| 693 | describe("fs.list_dir", function() | ||
| 694 | local intfile1 | ||
| 695 | local intfile2 | ||
| 696 | local intdir | ||
| 697 | local tmpdir | ||
| 698 | |||
| 699 | before_each(function() | ||
| 700 | if intfile1 then | ||
| 701 | os.remove(intfile1) | ||
| 702 | intfile1 = nil | ||
| 703 | end | ||
| 704 | if intfile2 then | ||
| 705 | os.remove(intfile2) | ||
| 706 | intfile2 = nil | ||
| 707 | end | ||
| 708 | if intdir then | ||
| 709 | lfs.rmdir(intdir) | ||
| 710 | intdir = nil | ||
| 711 | end | ||
| 712 | if tmpdir then | ||
| 713 | lfs.rmdir(tmpdir) | ||
| 714 | tmpdir = nil | ||
| 715 | end | ||
| 716 | end) | ||
| 717 | |||
| 718 | it("returns a table with the contents of the given directory", function() | ||
| 719 | tmpdir = get_tmp_path() | ||
| 720 | lfs.mkdir(tmpdir) | ||
| 721 | intfile1 = tmpdir .. "/intfile1" | ||
| 722 | create_file(intfile1) | ||
| 723 | intdir = tmpdir .. "/intdir" | ||
| 724 | lfs.mkdir(intdir) | ||
| 725 | intfile2 = intdir .. "/intfile2" | ||
| 726 | create_file(intfile2) | ||
| 727 | local result = fs.list_dir(tmpdir) | ||
| 728 | assert.same(#result, 2) | ||
| 729 | assert.truthy(result[1] == "intfile1" or result[1] == "intdir") | ||
| 730 | assert.truthy(result[2] == "intfile1" or result[2] == "intdir") | ||
| 731 | assert.is_not.same(result[1], result[2]) | ||
| 732 | end) | ||
| 733 | |||
| 734 | it("returns an empty table if the argument is a file", function() | ||
| 735 | intfile1 = get_tmp_path() | ||
| 736 | create_file(intfile1) | ||
| 737 | local result = fs.list_dir(intfile1) | ||
| 738 | assert.same(#result, 0) | ||
| 739 | end) | ||
| 740 | |||
| 741 | it("does nothing if the argument is nonexistent", function() | ||
| 742 | assert.falsy(pcall(fs.list_dir("/nonexistent"))) | ||
| 743 | end) | ||
| 744 | |||
| 745 | it("does nothing if the argument doesn't have the proper permissions", function() | ||
| 746 | tmpdir = get_tmp_path() | ||
| 747 | lfs.mkdir(tmpdir) | ||
| 748 | make_unreadable(tmpdir) | ||
| 749 | if is_win then | ||
| 750 | assert.same(fs.list_dir(tmpdir), {}) | ||
| 751 | else | ||
| 752 | assert.falsy(pcall(fs.list_dir, tmpdir)) | ||
| 753 | end | ||
| 754 | end) | ||
| 755 | end) | ||
| 674 | 756 | ||
| 675 | describe("fs.copy", function() | 757 | describe("fs.copy", function() |
| 676 | local srcfile | 758 | local srcfile |
| @@ -839,6 +921,213 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
| 839 | assert.falsy(exists_file(dstdir .. "/internalfile")) | 921 | assert.falsy(exists_file(dstdir .. "/internalfile")) |
| 840 | end) | 922 | end) |
| 841 | end) | 923 | end) |
| 924 | |||
| 925 | describe("fs.find", function() | ||
| 926 | local tmpdir | ||
| 927 | local intdir | ||
| 928 | local intfile1 | ||
| 929 | local intfile2 | ||
| 930 | |||
| 931 | after_each(function() | ||
| 932 | if intfile1 then | ||
| 933 | os.remove(intfile1) | ||
| 934 | intfile1 = nil | ||
| 935 | end | ||
| 936 | if intfile2 then | ||
| 937 | os.remove(intfile2) | ||
| 938 | intfile2 = nil | ||
| 939 | end | ||
| 940 | if intdir then | ||
| 941 | lfs.rmdir(intdir) | ||
| 942 | intdir = nil | ||
| 943 | end | ||
| 944 | if tmpdir then | ||
| 945 | lfs.rmdir(tmpdir) | ||
| 946 | tmpdir = nil | ||
| 947 | end | ||
| 948 | end) | ||
| 949 | |||
| 950 | local create_dir_tree = function() | ||
| 951 | tmpdir = get_tmp_path() | ||
| 952 | lfs.mkdir(tmpdir) | ||
| 953 | intfile1 = tmpdir .. "/intfile1" | ||
| 954 | create_file(intfile1) | ||
| 955 | intdir = tmpdir .. "/intdir" | ||
| 956 | lfs.mkdir(intdir) | ||
| 957 | intfile2 = intdir .. "/intfile2" | ||
| 958 | create_file(intfile2) | ||
| 959 | end | ||
| 960 | |||
| 961 | it("returns a table of all the contents in the directory given as argument", function() | ||
| 962 | create_dir_tree() | ||
| 963 | local contents = {} | ||
| 964 | local count = 0 | ||
| 965 | for _, file in pairs(fs.find(tmpdir)) do | ||
| 966 | contents[file] = true | ||
| 967 | count = count + 1 | ||
| 968 | end | ||
| 969 | assert.same(count, 3) | ||
| 970 | assert.is_not.same(contents[tmpdir], true) | ||
| 971 | assert.same(contents["intfile1"], true) | ||
| 972 | assert.same(contents["intdir"], true) | ||
| 973 | assert.same(contents["intdir/intfile2"], true) | ||
| 974 | end) | ||
| 975 | |||
| 976 | it("uses the current working directory if the argument is nil", function() | ||
| 977 | create_dir_tree() | ||
| 978 | local olddir = fs.current_dir() | ||
| 979 | fs.change_dir(intdir) | ||
| 980 | local contents = {} | ||
| 981 | local count = 0 | ||
| 982 | for _, file in pairs(fs.find()) do | ||
| 983 | contents[file] = true | ||
| 984 | count = count + 1 | ||
| 985 | end | ||
| 986 | assert.same(count, 1) | ||
| 987 | assert.is_not.same(contents["intfile1"], true) | ||
| 988 | assert.is_not.same(contents["intdir"], true) | ||
| 989 | assert.same(contents["intfile2"], true) | ||
| 990 | fs.change_dir(olddir) | ||
| 991 | end) | ||
| 992 | |||
| 993 | it("returns an empty table if the argument is nonexistent", function() | ||
| 994 | local contents = fs.find("/nonexistent") | ||
| 995 | local count = 0 | ||
| 996 | for _, file in pairs(contents) do | ||
| 997 | count = count + 1 | ||
| 998 | end | ||
| 999 | assert.same(count, 0) | ||
| 1000 | end) | ||
| 1001 | |||
| 1002 | it("returns an empty table if the argument is a file", function() | ||
| 1003 | intfile1 = get_tmp_path() | ||
| 1004 | create_file(intfile1) | ||
| 1005 | local contents = fs.find(intfile1) | ||
| 1006 | local count = 0 | ||
| 1007 | for _, file in pairs(contents) do | ||
| 1008 | count = count + 1 | ||
| 1009 | end | ||
| 1010 | assert.same(count, 0) | ||
| 1011 | end) | ||
| 1012 | |||
| 1013 | it("does nothing if the argument doesn't have the proper permissions", function() | ||
| 1014 | tmpdir = get_tmp_path() | ||
| 1015 | lfs.mkdir(tmpdir) | ||
| 1016 | make_unreadable(tmpdir) | ||
| 1017 | if is_win then | ||
| 1018 | assert.same(fs.find(tmpdir), {}) | ||
| 1019 | else | ||
| 1020 | assert.falsy(pcall(fs.find, tmpdir)) | ||
| 1021 | end | ||
| 1022 | end) | ||
| 1023 | end) | ||
| 1024 | |||
| 1025 | describe("fs.move", function() | ||
| 1026 | local srcfile | ||
| 1027 | local dstfile | ||
| 1028 | local tmpdir | ||
| 1029 | |||
| 1030 | after_each(function() | ||
| 1031 | if srcfile then | ||
| 1032 | os.remove(srcfile) | ||
| 1033 | srcfile = nil | ||
| 1034 | end | ||
| 1035 | if dstfile then | ||
| 1036 | os.remove(dstfile) | ||
| 1037 | dstfile = nil | ||
| 1038 | end | ||
| 1039 | if tmpdir then | ||
| 1040 | lfs.rmdir(tmpdir) | ||
| 1041 | tmpdir = nil | ||
| 1042 | end | ||
| 1043 | end) | ||
| 1044 | |||
| 1045 | it("returns true and moves the source (together with its permissions) to the destination", function() | ||
| 1046 | srcfile = get_tmp_path() | ||
| 1047 | create_file(srcfile) | ||
| 1048 | dstfile = get_tmp_path() | ||
| 1049 | local oldperms = lfs.attributes(srcfile, "permissions") | ||
| 1050 | assert.truthy(fs.move(srcfile, dstfile)) | ||
| 1051 | assert.truthy(fs.exists(dstfile)) | ||
| 1052 | assert.falsy(fs.exists(srcfile)) | ||
| 1053 | fd = assert(io.open(dstfile, "r")) | ||
| 1054 | local dstcontents = assert(fd:read("*a")) | ||
| 1055 | assert.same(dstcontents, "foo") | ||
| 1056 | if posix_ok then | ||
| 1057 | assert.same(oldperms, lfs.attributes(dstfile, "permissions")) | ||
| 1058 | end | ||
| 1059 | end) | ||
| 1060 | |||
| 1061 | it("returns true and moves the source (with custom permissions) to the destination", function() | ||
| 1062 | srcfile = get_tmp_path() | ||
| 1063 | create_file(srcfile) | ||
| 1064 | dstfile = get_tmp_path() | ||
| 1065 | assert.truthy(fs.move(srcfile, dstfile, "read")) | ||
| 1066 | assert.truthy(fs.exists(dstfile)) | ||
| 1067 | assert.falsy(fs.exists(srcfile)) | ||
| 1068 | fd = assert(io.open(dstfile, "r")) | ||
| 1069 | local dstcontents = assert(fd:read("*a")) | ||
| 1070 | assert.same(dstcontents, "foo") | ||
| 1071 | end) | ||
| 1072 | |||
| 1073 | it("returns false and does nothing if the source doesn't exist", function() | ||
| 1074 | dstfile = get_tmp_path() | ||
| 1075 | assert.falsy(fs.move("/nonexistent", dstfile)) | ||
| 1076 | assert.falsy(fs.exists(dstfile)) | ||
| 1077 | end) | ||
| 1078 | |||
| 1079 | it("returns false and does nothing if the destination already exists", function() | ||
| 1080 | srcfile = get_tmp_path() | ||
| 1081 | create_file(srcfile) | ||
| 1082 | dstfile = get_tmp_path() | ||
| 1083 | create_file(dstfile, "bar") | ||
| 1084 | assert.falsy(fs.move(srcfile, dstfile)) | ||
| 1085 | assert.truthy(fs.exists(srcfile)) | ||
| 1086 | fd = assert(io.open(dstfile, "r")) | ||
| 1087 | local dstcontents = assert(fd:read("*a")) | ||
| 1088 | assert.same(dstcontents, "bar") | ||
| 1089 | end) | ||
| 1090 | |||
| 1091 | it("returns false and does nothing if the destination path doesn't have the proper permissions", function() | ||
| 1092 | srcfile = get_tmp_path() | ||
| 1093 | create_file(srcfile) | ||
| 1094 | tmpdir = get_tmp_path() | ||
| 1095 | lfs.mkdir(tmpdir) | ||
| 1096 | make_unwritable(tmpdir) | ||
| 1097 | assert.falsy(fs.move(srcfile, tmpdir .. "/dstfile")) | ||
| 1098 | assert.falsy(fs.exists(tmpdir .. "/dstfile")) | ||
| 1099 | end) | ||
| 1100 | end) | ||
| 1101 | |||
| 1102 | describe("fs.is_lua", function() | ||
| 1103 | local tmpfile | ||
| 1104 | |||
| 1105 | after_each(function() | ||
| 1106 | if tmpfile then | ||
| 1107 | os.remove(tmpfile) | ||
| 1108 | tmpfile = nil | ||
| 1109 | end | ||
| 1110 | end) | ||
| 1111 | |||
| 1112 | it("returns true if the argument is a valid lua script", function() | ||
| 1113 | tmpfile = get_tmp_path() | ||
| 1114 | create_file(tmpfile, "print(\"foo\")") | ||
| 1115 | assert.truthy(fs.is_lua(tmpfile)) | ||
| 1116 | end) | ||
| 1117 | |||
| 1118 | it("returns false if the argument is not a valid lua script", function() | ||
| 1119 | tmpfile = os.tmpname() | ||
| 1120 | create_file(tmpfile) | ||
| 1121 | assert.falsy(fs.is_lua(tmpfile)) | ||
| 1122 | end) | ||
| 1123 | |||
| 1124 | it("returns false if the argument is a valid lua script but doesn't have the proper permissions", function() | ||
| 1125 | tmpfile = get_tmp_path() | ||
| 1126 | create_file(tmpfile, "print(\"foo\")") | ||
| 1127 | make_unreadable(tmpfile) | ||
| 1128 | assert.falsy(fs.is_lua(tmpfile)) | ||
| 1129 | end) | ||
| 1130 | end) | ||
| 842 | 1131 | ||
| 843 | describe("fs.delete", function() | 1132 | describe("fs.delete", function() |
| 844 | local tmpfile1 | 1133 | local tmpfile1 |
