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/unit | |
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/unit')
-rw-r--r-- | spec/unit/build_spec.lua | 3 | ||||
-rw-r--r-- | spec/unit/deps_spec.lua | 144 | ||||
-rw-r--r-- | spec/unit/dir_spec.lua | 1 | ||||
-rw-r--r-- | spec/unit/fetch_spec.lua | 1 | ||||
-rw-r--r-- | spec/unit/fs_spec.lua | 66 | ||||
-rw-r--r-- | spec/unit/fun_spec.lua | 1 | ||||
-rw-r--r-- | spec/unit/loader_spec.lua | 21 | ||||
-rw-r--r-- | spec/unit/persist_spec.lua | 1 | ||||
-rw-r--r-- | spec/unit/test_spec.lua | 174 | ||||
-rw-r--r-- | spec/unit/tools_spec.lua | 1 | ||||
-rw-r--r-- | spec/unit/util_spec.lua | 161 |
11 files changed, 502 insertions, 72 deletions
diff --git a/spec/unit/build_spec.lua b/spec/unit/build_spec.lua index 6ab143c6..fb680858 100644 --- a/spec/unit/build_spec.lua +++ b/spec/unit/build_spec.lua | |||
@@ -6,9 +6,6 @@ local testing_paths = test_env.testing_paths | |||
6 | local write_file = test_env.write_file | 6 | local write_file = test_env.write_file |
7 | local P = test_env.P | 7 | local P = test_env.P |
8 | 8 | ||
9 | test_env.unload_luarocks() | ||
10 | |||
11 | test_env.unload_luarocks() | ||
12 | test_env.setup_specs() | 9 | test_env.setup_specs() |
13 | local cfg = require("luarocks.core.cfg") | 10 | local cfg = require("luarocks.core.cfg") |
14 | local deps = require("luarocks.deps") | 11 | local deps = require("luarocks.deps") |
diff --git a/spec/unit/deps_spec.lua b/spec/unit/deps_spec.lua new file mode 100644 index 00000000..b8c89a2d --- /dev/null +++ b/spec/unit/deps_spec.lua | |||
@@ -0,0 +1,144 @@ | |||
1 | local test_env = require("spec.util.test_env") | ||
2 | local testing_paths = test_env.testing_paths | ||
3 | |||
4 | local cfg = require("luarocks.core.cfg") | ||
5 | local deps = require("luarocks.deps") | ||
6 | local fs = require("luarocks.fs") | ||
7 | |||
8 | describe("LuaRocks deps #unit", function() | ||
9 | local runner | ||
10 | |||
11 | lazy_setup(function() | ||
12 | cfg.init() | ||
13 | fs.init() | ||
14 | deps.check_lua_incdir(cfg.variables) | ||
15 | deps.check_lua_libdir(cfg.variables) | ||
16 | |||
17 | runner = require("luacov.runner") | ||
18 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | ||
19 | runner.tick = true | ||
20 | end) | ||
21 | |||
22 | lazy_teardown(function() | ||
23 | runner.shutdown() | ||
24 | end) | ||
25 | |||
26 | describe("deps", function() | ||
27 | describe("deps.autodetect_external_dependencies", function() | ||
28 | it("returns false if the given build table has no external dependencies", function() | ||
29 | local build_table = { | ||
30 | type = "builtin" | ||
31 | } | ||
32 | |||
33 | assert.falsy(deps.autodetect_external_dependencies(build_table)) | ||
34 | end) | ||
35 | |||
36 | it("returns a table of the external dependencies found in the given build table", function() | ||
37 | local build_table = { | ||
38 | type = "builtin", | ||
39 | modules = { | ||
40 | module1 = { | ||
41 | libraries = { "foo1", "foo2" }, | ||
42 | }, | ||
43 | module2 = { | ||
44 | libraries = "foo3" | ||
45 | }, | ||
46 | } | ||
47 | } | ||
48 | |||
49 | local extdeps = deps.autodetect_external_dependencies(build_table) | ||
50 | assert.same(extdeps["FOO1"], { library = "foo1" }) | ||
51 | assert.same(extdeps["FOO2"], { library = "foo2" }) | ||
52 | assert.same(extdeps["FOO3"], { library = "foo3" }) | ||
53 | end) | ||
54 | |||
55 | it("adds proper include and library dirs to the given build table", function() | ||
56 | local build_table | ||
57 | |||
58 | build_table = { | ||
59 | type = "builtin", | ||
60 | modules = { | ||
61 | module1 = { | ||
62 | libraries = "foo" | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | deps.autodetect_external_dependencies(build_table) | ||
67 | assert.same(build_table, { | ||
68 | type = "builtin", | ||
69 | modules = { | ||
70 | module1 = { | ||
71 | libraries = "foo", | ||
72 | incdirs = { "$(FOO_INCDIR)" }, | ||
73 | libdirs = { "$(FOO_LIBDIR)" } | ||
74 | } | ||
75 | } | ||
76 | }) | ||
77 | |||
78 | build_table = { | ||
79 | type = "builtin", | ||
80 | modules = { | ||
81 | module1 = { | ||
82 | libraries = "foo", | ||
83 | incdirs = { "INCDIRS" } | ||
84 | } | ||
85 | } | ||
86 | } | ||
87 | deps.autodetect_external_dependencies(build_table) | ||
88 | assert.same(build_table, { | ||
89 | type = "builtin", | ||
90 | modules = { | ||
91 | module1 = { | ||
92 | libraries = "foo", | ||
93 | incdirs = { "INCDIRS" }, | ||
94 | libdirs = { "$(FOO_LIBDIR)" } | ||
95 | } | ||
96 | } | ||
97 | }) | ||
98 | |||
99 | build_table = { | ||
100 | type = "builtin", | ||
101 | modules = { | ||
102 | module1 = { | ||
103 | libraries = "foo", | ||
104 | libdirs = { "LIBDIRS" } | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | deps.autodetect_external_dependencies(build_table) | ||
109 | assert.same(build_table, { | ||
110 | type = "builtin", | ||
111 | modules = { | ||
112 | module1 = { | ||
113 | libraries = "foo", | ||
114 | incdirs = { "$(FOO_INCDIR)" }, | ||
115 | libdirs = { "LIBDIRS" } | ||
116 | } | ||
117 | } | ||
118 | }) | ||
119 | |||
120 | build_table = { | ||
121 | type = "builtin", | ||
122 | modules = { | ||
123 | module1 = { | ||
124 | libraries = "foo", | ||
125 | incdirs = { "INCDIRS" }, | ||
126 | libdirs = { "LIBDIRS" } | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | deps.autodetect_external_dependencies(build_table) | ||
131 | assert.same(build_table, { | ||
132 | type = "builtin", | ||
133 | modules = { | ||
134 | module1 = { | ||
135 | libraries = "foo", | ||
136 | incdirs = { "INCDIRS" }, | ||
137 | libdirs = { "LIBDIRS" } | ||
138 | } | ||
139 | } | ||
140 | }) | ||
141 | end) | ||
142 | end) | ||
143 | end) | ||
144 | end) | ||
diff --git a/spec/unit/dir_spec.lua b/spec/unit/dir_spec.lua index b5dadda8..f340beea 100644 --- a/spec/unit/dir_spec.lua +++ b/spec/unit/dir_spec.lua | |||
@@ -2,7 +2,6 @@ local test_env = require("spec.util.test_env") | |||
2 | local testing_paths = test_env.testing_paths | 2 | local testing_paths = test_env.testing_paths |
3 | local P = test_env.P | 3 | local P = test_env.P |
4 | 4 | ||
5 | test_env.unload_luarocks() | ||
6 | test_env.setup_specs() | 5 | test_env.setup_specs() |
7 | local dir = require("luarocks.dir") | 6 | local dir = require("luarocks.dir") |
8 | 7 | ||
diff --git a/spec/unit/fetch_spec.lua b/spec/unit/fetch_spec.lua index 38da379e..c58e2cea 100644 --- a/spec/unit/fetch_spec.lua +++ b/spec/unit/fetch_spec.lua | |||
@@ -1,6 +1,5 @@ | |||
1 | local test_env = require("spec.util.test_env") | 1 | local test_env = require("spec.util.test_env") |
2 | 2 | ||
3 | test_env.unload_luarocks() | ||
4 | test_env.setup_specs() | 3 | test_env.setup_specs() |
5 | local cfg = require("luarocks.core.cfg") | 4 | local cfg = require("luarocks.core.cfg") |
6 | local fetch = require("luarocks.fetch") | 5 | local fetch = require("luarocks.fetch") |
diff --git a/spec/unit/fs_spec.lua b/spec/unit/fs_spec.lua index aea86af3..7643e9b5 100644 --- a/spec/unit/fs_spec.lua +++ b/spec/unit/fs_spec.lua | |||
@@ -1,6 +1,5 @@ | |||
1 | local test_env = require("spec.util.test_env") | 1 | local test_env = require("spec.util.test_env") |
2 | 2 | ||
3 | test_env.unload_luarocks() | ||
4 | test_env.setup_specs() | 3 | test_env.setup_specs() |
5 | local fs = require("luarocks.fs") | 4 | local fs = require("luarocks.fs") |
6 | local path = require("luarocks.path") | 5 | local path = require("luarocks.path") |
@@ -542,7 +541,7 @@ describe("luarocks.fs #unit", function() | |||
542 | if is_win then | 541 | if is_win then |
543 | assert.same(tmpdir, fs.current_dir()) | 542 | assert.same(tmpdir, fs.current_dir()) |
544 | else | 543 | else |
545 | assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(fs.current_dir()).ino) | 544 | assert.same(lfs.attributes(tmpdir).ino, lfs.attributes((fs.current_dir())).ino) |
546 | end | 545 | end |
547 | end) | 546 | end) |
548 | end) | 547 | end) |
@@ -578,7 +577,7 @@ describe("luarocks.fs #unit", function() | |||
578 | if is_win then | 577 | if is_win then |
579 | assert.same(tmpdir, fs.current_dir()) | 578 | assert.same(tmpdir, fs.current_dir()) |
580 | else | 579 | else |
581 | assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(lfs.currentdir()).ino) | 580 | assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(fs.current_dir()).ino) |
582 | end | 581 | end |
583 | end) | 582 | end) |
584 | 583 | ||
@@ -1283,67 +1282,6 @@ describe("luarocks.fs #unit", function() | |||
1283 | end) | 1282 | end) |
1284 | end) | 1283 | end) |
1285 | 1284 | ||
1286 | describe("fs.download #mock", function() | ||
1287 | local tmpfile | ||
1288 | local tmpdir | ||
1289 | |||
1290 | setup(function() | ||
1291 | test_env.mock_server_init() | ||
1292 | end) | ||
1293 | |||
1294 | teardown(function() | ||
1295 | test_env.mock_server_done() | ||
1296 | end) | ||
1297 | |||
1298 | after_each(function() | ||
1299 | if tmpfile then | ||
1300 | os.remove(tmpfile) | ||
1301 | tmpfile = nil | ||
1302 | end | ||
1303 | if tmpdir then | ||
1304 | lfs.rmdir(tmpdir) | ||
1305 | tmpdir = nil | ||
1306 | end | ||
1307 | end) | ||
1308 | |||
1309 | it("returns true and fetches the url argument into the specified filename", function() | ||
1310 | tmpfile = get_tmp_path() | ||
1311 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile)) | ||
1312 | local fd = assert(io.open(tmpfile, "r")) | ||
1313 | local downloadcontent = assert(fd:read("*a")) | ||
1314 | fd:close() | ||
1315 | fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) | ||
1316 | local originalcontent = assert(fd:read("*a")) | ||
1317 | fd:close() | ||
1318 | assert.same(downloadcontent, originalcontent) | ||
1319 | end) | ||
1320 | |||
1321 | 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() | ||
1322 | tmpdir = get_tmp_path() | ||
1323 | lfs.mkdir(tmpdir) | ||
1324 | fs.change_dir(tmpdir) | ||
1325 | assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua")) | ||
1326 | tmpfile = tmpdir .. "/a_rock.lua" | ||
1327 | local fd = assert(io.open(tmpfile, "r")) | ||
1328 | local downloadcontent = assert(fd:read("*a")) | ||
1329 | fd:close() | ||
1330 | fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) | ||
1331 | local originalcontent = assert(fd:read("*a")) | ||
1332 | fd:close() | ||
1333 | assert.same(downloadcontent, originalcontent) | ||
1334 | fs.pop_dir() | ||
1335 | end) | ||
1336 | |||
1337 | it("returns false and does nothing if the url argument contains a nonexistent file", function() | ||
1338 | tmpfile = get_tmp_path() | ||
1339 | assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile)) | ||
1340 | end) | ||
1341 | |||
1342 | it("returns false and does nothing if the url argument is invalid", function() | ||
1343 | assert.falsy(fs.download("invalidurl")) | ||
1344 | end) | ||
1345 | end) | ||
1346 | |||
1347 | describe("fs.zip", function() | 1285 | describe("fs.zip", function() |
1348 | local tmpdir | 1286 | local tmpdir |
1349 | local olddir | 1287 | local olddir |
diff --git a/spec/unit/fun_spec.lua b/spec/unit/fun_spec.lua index 9844ec27..c2b3e864 100644 --- a/spec/unit/fun_spec.lua +++ b/spec/unit/fun_spec.lua | |||
@@ -1,7 +1,6 @@ | |||
1 | local test_env = require("spec.util.test_env") | 1 | local test_env = require("spec.util.test_env") |
2 | local testing_paths = test_env.testing_paths | 2 | local testing_paths = test_env.testing_paths |
3 | 3 | ||
4 | test_env.unload_luarocks() | ||
5 | local fun = require("luarocks.fun") | 4 | local fun = require("luarocks.fun") |
6 | 5 | ||
7 | describe("luarocks.fun #unit", function() | 6 | describe("luarocks.fun #unit", function() |
diff --git a/spec/unit/loader_spec.lua b/spec/unit/loader_spec.lua new file mode 100644 index 00000000..7650c80c --- /dev/null +++ b/spec/unit/loader_spec.lua | |||
@@ -0,0 +1,21 @@ | |||
1 | local test_env = require("spec.util.test_env") | ||
2 | local run = test_env.run | ||
3 | |||
4 | describe("luarocks.loader", function() | ||
5 | |||
6 | before_each(function() | ||
7 | test_env.setup_specs() | ||
8 | end) | ||
9 | |||
10 | describe("#unit", function() | ||
11 | it("starts", function() | ||
12 | assert(run.lua_bool([[-e "require 'luarocks.loader'; print(package.loaded['luarocks.loaded'])"]])) | ||
13 | end) | ||
14 | |||
15 | describe("which", function() | ||
16 | it("finds modules using package.path", function() | ||
17 | assert(run.lua_bool([[-e "loader = require 'luarocks.loader'; local x,y,z,p = loader.which('luarocks.loader', 'p'); assert(p == 'p')"]])) | ||
18 | end) | ||
19 | end) | ||
20 | end) | ||
21 | end) | ||
diff --git a/spec/unit/persist_spec.lua b/spec/unit/persist_spec.lua index f426fd83..ea5985aa 100644 --- a/spec/unit/persist_spec.lua +++ b/spec/unit/persist_spec.lua | |||
@@ -1,7 +1,6 @@ | |||
1 | local test_env = require("spec.util.test_env") | 1 | local test_env = require("spec.util.test_env") |
2 | local testing_paths = test_env.testing_paths | 2 | local testing_paths = test_env.testing_paths |
3 | 3 | ||
4 | test_env.unload_luarocks() | ||
5 | local persist = require("luarocks.persist") | 4 | local persist = require("luarocks.persist") |
6 | 5 | ||
7 | describe("luarocks.persist #unit", function() | 6 | describe("luarocks.persist #unit", function() |
diff --git a/spec/unit/test_spec.lua b/spec/unit/test_spec.lua new file mode 100644 index 00000000..4d338984 --- /dev/null +++ b/spec/unit/test_spec.lua | |||
@@ -0,0 +1,174 @@ | |||
1 | local test_env = require("spec.util.test_env") | ||
2 | local lfs = require("lfs") | ||
3 | local get_tmp_path = test_env.get_tmp_path | ||
4 | local testing_paths = test_env.testing_paths | ||
5 | local write_file = test_env.write_file | ||
6 | |||
7 | local fs = require("luarocks.fs") | ||
8 | local cfg = require("luarocks.core.cfg") | ||
9 | local path = require("luarocks.path") | ||
10 | local test = require("luarocks.test") | ||
11 | local test_busted = require("luarocks.test.busted") | ||
12 | local test_command = require("luarocks.test.command") | ||
13 | |||
14 | describe("LuaRocks test #unit", function() | ||
15 | local runner | ||
16 | |||
17 | lazy_setup(function() | ||
18 | cfg.init() | ||
19 | fs.init() | ||
20 | runner = require("luacov.runner") | ||
21 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | ||
22 | runner.tick = true | ||
23 | end) | ||
24 | |||
25 | lazy_teardown(function() | ||
26 | runner.shutdown() | ||
27 | end) | ||
28 | |||
29 | local tmpdir | ||
30 | local olddir | ||
31 | |||
32 | local create_tmp_dir = function() | ||
33 | tmpdir = get_tmp_path() | ||
34 | olddir = lfs.currentdir() | ||
35 | lfs.mkdir(tmpdir) | ||
36 | lfs.chdir(tmpdir) | ||
37 | fs.change_dir(tmpdir) | ||
38 | end | ||
39 | |||
40 | local destroy_tmp_dir = function() | ||
41 | if olddir then | ||
42 | lfs.chdir(olddir) | ||
43 | if tmpdir then | ||
44 | lfs.rmdir(tmpdir) | ||
45 | end | ||
46 | end | ||
47 | end | ||
48 | |||
49 | describe("test.command", function() | ||
50 | describe("command.detect_type", function() | ||
51 | before_each(function() | ||
52 | create_tmp_dir() | ||
53 | end) | ||
54 | |||
55 | after_each(function() | ||
56 | destroy_tmp_dir() | ||
57 | end) | ||
58 | |||
59 | it("returns true if test.lua exists", function() | ||
60 | write_file("test.lua", "", finally) | ||
61 | assert.truthy(test_command.detect_type()) | ||
62 | end) | ||
63 | |||
64 | it("returns false if test.lua doesn't exist", function() | ||
65 | assert.falsy(test_command.detect_type()) | ||
66 | end) | ||
67 | end) | ||
68 | |||
69 | describe("command.run_tests", function() | ||
70 | before_each(function() | ||
71 | create_tmp_dir() | ||
72 | end) | ||
73 | |||
74 | after_each(function() | ||
75 | destroy_tmp_dir() | ||
76 | end) | ||
77 | |||
78 | it("returns the result of the executed tests", function() | ||
79 | write_file("test.lua", "assert(1==1)", finally) | ||
80 | assert.truthy(test_command.run_tests(nil, {})) | ||
81 | |||
82 | write_file("test.lua", "assert(1==2)", finally) | ||
83 | assert.falsy(test_command.run_tests(nil, {})) | ||
84 | end) | ||
85 | |||
86 | it("returns the result of the executed tests with custom arguments and test command", function() | ||
87 | write_file("test.lua", "assert(1==1)", finally) | ||
88 | |||
89 | local test = { | ||
90 | script = "test.lua", | ||
91 | flags = { | ||
92 | arg1 = "1", | ||
93 | arg2 = "2" | ||
94 | }, | ||
95 | command = fs.Q(testing_paths.lua) | ||
96 | } | ||
97 | assert.truthy(test_command.run_tests(test, {})) | ||
98 | end) | ||
99 | |||
100 | it("returns false and does nothing if the test script doesn't exist", function() | ||
101 | assert.falsy(test_command.run_tests(nil, {})) | ||
102 | end) | ||
103 | end) | ||
104 | end) | ||
105 | |||
106 | describe("test.busted", function() | ||
107 | describe("busted.detect_type", function() | ||
108 | before_each(function() | ||
109 | create_tmp_dir() | ||
110 | end) | ||
111 | |||
112 | after_each(function() | ||
113 | destroy_tmp_dir() | ||
114 | end) | ||
115 | |||
116 | it("returns true if .busted exists", function() | ||
117 | write_file(".busted", "", finally) | ||
118 | assert.truthy(test_busted.detect_type()) | ||
119 | end) | ||
120 | |||
121 | it("returns false if .busted doesn't exist", function() | ||
122 | assert.falsy(test_busted.detect_type()) | ||
123 | end) | ||
124 | end) | ||
125 | |||
126 | describe("busted.run_tests", function() | ||
127 | before_each(function() | ||
128 | path.use_tree(testing_paths.testing_sys_tree) | ||
129 | create_tmp_dir() | ||
130 | end) | ||
131 | |||
132 | after_each(function() | ||
133 | destroy_tmp_dir() | ||
134 | end) | ||
135 | |||
136 | pending("returns the result of the executed tests", function() | ||
137 | -- FIXME: busted issue | ||
138 | write_file("test_spec.lua", "assert(1==1)", finally) | ||
139 | assert.truthy(test_busted.run_tests(nil, {})) | ||
140 | |||
141 | write_file("test_spec.lua", "assert(1==2)", finally) | ||
142 | assert.falsy(test_busted.run_tests()) | ||
143 | end) | ||
144 | end) | ||
145 | end) | ||
146 | |||
147 | describe("test", function() | ||
148 | describe("test.run_test_suite", function() | ||
149 | before_each(function() | ||
150 | create_tmp_dir() | ||
151 | end) | ||
152 | |||
153 | after_each(function() | ||
154 | destroy_tmp_dir() | ||
155 | end) | ||
156 | |||
157 | it("returns false if the given rockspec cannot be loaded", function() | ||
158 | assert.falsy(test.run_test_suite("invalid", nil, {})) | ||
159 | end) | ||
160 | |||
161 | it("returns false if no test type was detected", function() | ||
162 | assert.falsy(test.run_test_suite({ package = "test" }, nil, {})) | ||
163 | end) | ||
164 | |||
165 | it("returns the result of executing the tests specified in the given rockspec", function() | ||
166 | write_file("test.lua", "assert(1==1)", finally) | ||
167 | assert.truthy(test.run_test_suite({ test_dependencies = {} }, nil, {})) | ||
168 | |||
169 | write_file("test.lua", "assert(1==2)", finally) | ||
170 | assert.falsy(test.run_test_suite({ test_dependencies = {} }, nil, {})) | ||
171 | end) | ||
172 | end) | ||
173 | end) | ||
174 | end) | ||
diff --git a/spec/unit/tools_spec.lua b/spec/unit/tools_spec.lua index 29e21740..0863c316 100644 --- a/spec/unit/tools_spec.lua +++ b/spec/unit/tools_spec.lua | |||
@@ -3,7 +3,6 @@ local get_tmp_path = test_env.get_tmp_path | |||
3 | local testing_paths = test_env.testing_paths | 3 | local testing_paths = test_env.testing_paths |
4 | local write_file = test_env.write_file | 4 | local write_file = test_env.write_file |
5 | 5 | ||
6 | test_env.unload_luarocks() | ||
7 | local fs = require("luarocks.fs") | 6 | local fs = require("luarocks.fs") |
8 | local cfg = require("luarocks.core.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
9 | local patch = require("luarocks.tools.patch") | 8 | local patch = require("luarocks.tools.patch") |
diff --git a/spec/unit/util_spec.lua b/spec/unit/util_spec.lua new file mode 100644 index 00000000..0e380f2a --- /dev/null +++ b/spec/unit/util_spec.lua | |||
@@ -0,0 +1,161 @@ | |||
1 | local test_env = require("spec.util.test_env") | ||
2 | local testing_paths = test_env.testing_paths | ||
3 | local P = test_env.P | ||
4 | |||
5 | local util = require("luarocks.util") | ||
6 | local core_util = require("luarocks.core.util") | ||
7 | |||
8 | describe("luarocks.util #unit", function() | ||
9 | local runner | ||
10 | |||
11 | setup(function() | ||
12 | runner = require("luacov.runner") | ||
13 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | ||
14 | runner.tick = true | ||
15 | end) | ||
16 | |||
17 | teardown(function() | ||
18 | runner.shutdown() | ||
19 | end) | ||
20 | |||
21 | describe("util.variable_substitutions", function() | ||
22 | it("replaces variables", function() | ||
23 | local t = { | ||
24 | ["hello"] = "$(KIND) world", | ||
25 | } | ||
26 | util.variable_substitutions(t, { | ||
27 | ["KIND"] = "happy", | ||
28 | }) | ||
29 | assert.are.same({ | ||
30 | ["hello"] = "happy world", | ||
31 | }, t) | ||
32 | end) | ||
33 | |||
34 | it("missing variables are empty", function() | ||
35 | local t = { | ||
36 | ["hello"] = "$(KIND) world", | ||
37 | } | ||
38 | util.variable_substitutions(t, { | ||
39 | }) | ||
40 | assert.are.same({ | ||
41 | ["hello"] = " world", | ||
42 | }, t) | ||
43 | end) | ||
44 | end) | ||
45 | |||
46 | describe("util.sortedpairs", function() | ||
47 | local function collect(iter, state, var) | ||
48 | local collected = {} | ||
49 | |||
50 | while true do | ||
51 | local returns = {iter(state, var)} | ||
52 | |||
53 | if returns[1] == nil then | ||
54 | return collected | ||
55 | else | ||
56 | table.insert(collected, returns) | ||
57 | var = returns[1] | ||
58 | end | ||
59 | end | ||
60 | end | ||
61 | |||
62 | it("default sort", function() | ||
63 | assert.are.same({}, collect(util.sortedpairs({}))) | ||
64 | assert.are.same({ | ||
65 | {1, "v1"}, | ||
66 | {2, "v2"}, | ||
67 | {3, "v3"}, | ||
68 | {"bar", "v5"}, | ||
69 | {"foo", "v4"} | ||
70 | }, collect(util.sortedpairs({"v1", "v2", "v3", foo = "v4", bar = "v5"}))) | ||
71 | end) | ||
72 | |||
73 | it("sort by function", function() | ||
74 | local function compare(a, b) return a > b end | ||
75 | assert.are.same({}, collect(util.sortedpairs({}, compare))) | ||
76 | assert.are.same({ | ||
77 | {3, "v3"}, | ||
78 | {2, "v2"}, | ||
79 | {1, "v1"} | ||
80 | }, collect(util.sortedpairs({"v1", "v2", "v3"}, compare))) | ||
81 | end) | ||
82 | |||
83 | it("sort by priority table", function() | ||
84 | assert.are.same({}, collect(util.sortedpairs({}, {"k1", "k2"}))) | ||
85 | assert.are.same({ | ||
86 | {"k3", "v3"}, | ||
87 | {"k2", "v2", {"sub order"}}, | ||
88 | {"k1", "v1"}, | ||
89 | {"k4", "v4"}, | ||
90 | {"k5", "v5"}, | ||
91 | }, collect(util.sortedpairs({ | ||
92 | k1 = "v1", k2 = "v2", k3 = "v3", k4 = "v4", k5 = "v5" | ||
93 | }, {"k3", {"k2", {"sub order"}}, "k1"}))) | ||
94 | end) | ||
95 | end) | ||
96 | |||
97 | describe("core.util.show_table", function() | ||
98 | it("returns a pretty-printed string containing the representation of the given table", function() | ||
99 | local result | ||
100 | |||
101 | local t1 = {1, 2, 3} | ||
102 | result = core_util.show_table(t1) | ||
103 | assert.truthy(result:find("[1] = 1", 1, true)) | ||
104 | assert.truthy(result:find("[2] = 2", 1, true)) | ||
105 | assert.truthy(result:find("[3] = 3", 1, true)) | ||
106 | |||
107 | local t2 = {a = 1, b = 2, c = 3} | ||
108 | result = core_util.show_table(t2) | ||
109 | assert.truthy(result:find("[\"a\"] = 1", 1, true)) | ||
110 | assert.truthy(result:find("[\"b\"] = 2", 1, true)) | ||
111 | assert.truthy(result:find("[\"c\"] = 3", 1, true)) | ||
112 | |||
113 | local t3 = {a = 1, b = "2", c = {3}} | ||
114 | result = core_util.show_table(t3) | ||
115 | assert.truthy(result:find("[\"a\"] = 1", 1, true)) | ||
116 | assert.truthy(result:find("[\"b\"] = \"2\"", 1, true)) | ||
117 | assert.truthy(result:find("[\"c\"] = {", 1, true)) | ||
118 | assert.truthy(result:find("[1] = 3", 1, true)) | ||
119 | |||
120 | local t4 = {a = 1, b = {c = 2, d = {e = "4"}}} | ||
121 | result = core_util.show_table(t4) | ||
122 | assert.truthy(result:find("[\"a\"] = 1", 1, true)) | ||
123 | assert.truthy(result:find("[\"b\"] = {", 1, true)) | ||
124 | assert.truthy(result:find("[\"c\"] = 2", 1, true)) | ||
125 | assert.truthy(result:find("[\"d\"] = {", 1, true)) | ||
126 | assert.truthy(result:find("[\"e\"] = \"4\"", 1, true)) | ||
127 | end) | ||
128 | end) | ||
129 | |||
130 | describe("core.util.cleanup_path", function() | ||
131 | it("does not change order of existing items of prepended path", function() | ||
132 | local sys_path = P'/usr/local/bin;/usr/bin' | ||
133 | local lr_path = P'/home/user/.luarocks/bin;/usr/bin' | ||
134 | local path = lr_path .. ';' .. sys_path | ||
135 | |||
136 | local result = core_util.cleanup_path(path, ';', '5.3', false) | ||
137 | assert.are.equal(P'/home/user/.luarocks/bin;/usr/local/bin;/usr/bin', result) | ||
138 | end) | ||
139 | |||
140 | it("does not change order of existing items of appended path", function() | ||
141 | local sys_path = P'/usr/local/bin;/usr/bin' | ||
142 | local lr_path = P'/home/user/.luarocks/bin;/usr/bin' | ||
143 | local path = sys_path .. ';' .. lr_path | ||
144 | |||
145 | local result = core_util.cleanup_path(path, ';', '5.3', true) | ||
146 | assert.are.equal(P'/usr/local/bin;/usr/bin;/home/user/.luarocks/bin', result) | ||
147 | end) | ||
148 | |||
149 | it("rewrites versions that do not match the provided version", function() | ||
150 | local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3/?.lua' | ||
151 | local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3/?.lua', ';', '5.3') | ||
152 | assert.are.equal(expected, result) | ||
153 | end) | ||
154 | |||
155 | it("does not rewrite versions for which the provided version is a substring", function() | ||
156 | local expected = P'a/b/lua/5.3/?.lua;a/b/c/lua/5.3.4/?.lua' | ||
157 | local result = core_util.cleanup_path(P'a/b/lua/5.2/?.lua;a/b/c/lua/5.3.4/?.lua', ';', '5.3') | ||
158 | assert.are.equal(expected, result) | ||
159 | end) | ||
160 | end) | ||
161 | end) | ||