diff options
Diffstat (limited to 'spec/unit/test_spec.lua')
-rw-r--r-- | spec/unit/test_spec.lua | 174 |
1 files changed, 174 insertions, 0 deletions
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) | ||