From 8053c530453d8aae6f13f4a248c1b30e2e1f901f Mon Sep 17 00:00:00 2001 From: George Roman Date: Thu, 14 Jun 2018 15:17:46 +0300 Subject: Tests: test command --- spec/test_spec.lua | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/spec/test_spec.lua b/spec/test_spec.lua index ffcf2ac0..0111f89a 100644 --- a/spec/test_spec.lua +++ b/spec/test_spec.lua @@ -1,7 +1,9 @@ local test_env = require("spec.util.test_env") local lfs = require("lfs") +local get_tmp_path = test_env.get_tmp_path local run = test_env.run local testing_paths = test_env.testing_paths +local write_file = test_env.write_file test_env.unload_luarocks() @@ -76,3 +78,171 @@ describe("luarocks test #integration", function() end) end) end) + +test_env.unload_luarocks() + +local fs = require("luarocks.fs") +local path = require("luarocks.path") +local test = require("luarocks.test") +local test_busted = require("luarocks.test.busted") +local test_command = require("luarocks.test.command") + +describe("LuaRocks test #unit", function() + local runner + + setup(function() + runner = require("luacov.runner") + runner.init(testing_paths.testrun_dir .. "/luacov.config") + runner.tick = true + end) + + teardown(function() + runner.shutdown() + end) + + local tmpdir + local olddir + + local create_tmp_dir = function() + tmpdir = get_tmp_path() + olddir = lfs.currentdir() + lfs.mkdir(tmpdir) + lfs.chdir(tmpdir) + fs.change_dir(tmpdir) + end + + local destroy_tmp_dir = function() + if olddir then + lfs.chdir(olddir) + if tmpdir then + lfs.rmdir(tmpdir) + end + end + end + + describe("test.command", function() + describe("command.detect_type", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns true if test.lua exists", function() + write_file("test.lua", "", finally) + assert.truthy(test_command.detect_type()) + end) + + it("returns false if test.lua doesn't exist", function() + assert.falsy(test_command.detect_type()) + end) + end) + + describe("command.run_tests", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns the result of the executed tests", function() + write_file("test.lua", "assert(1==1)", finally) + assert.truthy(test_command.run_tests(nil, {})) + + write_file("test.lua", "assert(1==2)", finally) + assert.falsy(test_command.run_tests(nil, {})) + end) + + it("returns the result of the executed tests with custom arguments and test command", function() + write_file("test.lua", "assert(1==1)", finally) + + local test = { + script = "test.lua", + flags = { + arg1 = "1", + arg2 = "2" + }, + command = fs.Q(testing_paths.lua) + } + assert.truthy(test_command.run_tests(test, {})) + end) + + it("returns false and does nothing if the test script doesn't exist", function() + assert.falsy(test_command.run_tests(nil, {})) + end) + end) + end) + + describe("test.busted", function() + describe("busted.detect_type", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns true if .busted exists", function() + write_file(".busted", "", finally) + assert.truthy(test_busted.detect_type()) + end) + + it("returns false if .busted doesn't exist", function() + assert.falsy(test_busted.detect_type()) + end) + end) + + describe("busted.run_tests", function() + before_each(function() + path.use_tree(testing_paths.testing_sys_tree) + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + pending("returns the result of the executed tests", function() + -- FIXME: busted issue + write_file("test_spec.lua", "assert(1==1)", finally) + assert.truthy(test_busted.run_tests(nil, {})) + + write_file("test_spec.lua", "assert(1==2)", finally) + assert.falsy(test_busted.run_tests()) + end) + end) + end) + + describe("test", function() + describe("test.run_test_suite", function() + before_each(function() + create_tmp_dir() + end) + + after_each(function() + destroy_tmp_dir() + end) + + it("returns false if the given rockspec cannot be loaded", function() + assert.falsy(test.run_test_suite("invalid", nil, {})) + end) + + it("returns false if no test type was detected", function() + assert.falsy(test.run_test_suite({ package = "test" }, nil, {})) + end) + + it("returns the result of executing the tests specified in the given rockspec", function() + write_file("test.lua", "assert(1==1)", finally) + assert.truthy(test.run_test_suite({ test_dependencies = {} }, nil, {})) + + write_file("test.lua", "assert(1==2)", finally) + assert.falsy(test.run_test_suite({ test_dependencies = {} }, nil, {})) + end) + end) + end) +end) -- cgit v1.2.3-55-g6feb