diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/luarocks_site.lua | 6 | ||||
-rw-r--r-- | test/mock-server.lua | 80 | ||||
-rw-r--r-- | test/test_environment.lua | 39 |
3 files changed, 114 insertions, 11 deletions
diff --git a/test/luarocks_site.lua b/test/luarocks_site.lua new file mode 100644 index 00000000..cfa77dca --- /dev/null +++ b/test/luarocks_site.lua | |||
@@ -0,0 +1,6 @@ | |||
1 | -- Config file of LuaRocks site for tests | ||
2 | upload = { | ||
3 | server = "http://localhost:8080", | ||
4 | tool_version = "1.0.0", | ||
5 | api_version = "1", | ||
6 | } \ No newline at end of file | ||
diff --git a/test/mock-server.lua b/test/mock-server.lua new file mode 100644 index 00000000..797e2bc5 --- /dev/null +++ b/test/mock-server.lua | |||
@@ -0,0 +1,80 @@ | |||
1 | #!/usr/bin/env lua | ||
2 | |||
3 | --- A simple LuaRocks mock-server for testing. | ||
4 | local restserver = require("restserver") | ||
5 | local server = restserver:new():port(8080) | ||
6 | |||
7 | server:add_resource("api/tool_version", { | ||
8 | { | ||
9 | method = "GET", | ||
10 | path = "/", | ||
11 | produces = "application/json", | ||
12 | handler = function(query) | ||
13 | local json = { version = query.current } | ||
14 | return restserver.response():status(200):entity(json) | ||
15 | end | ||
16 | } | ||
17 | }) | ||
18 | |||
19 | server:add_resource("api/1/{id:[0-9]+}/status", { | ||
20 | { | ||
21 | method = "GET", | ||
22 | path = "/", | ||
23 | produces = "application/json", | ||
24 | handler = function(query) | ||
25 | local json = { user_id = "123", created_at = "29.1.1993" } | ||
26 | return restserver.response():status(200):entity(json) | ||
27 | end | ||
28 | } | ||
29 | }) | ||
30 | |||
31 | server:add_resource("/api/1/{id:[0-9]+}/check_rockspec", { | ||
32 | { | ||
33 | method = "GET", | ||
34 | path = "/", | ||
35 | produces = "application/json", | ||
36 | handler = function(query) | ||
37 | local json = {} | ||
38 | return restserver.response():status(200):entity(json) | ||
39 | end | ||
40 | } | ||
41 | }) | ||
42 | |||
43 | server:add_resource("/api/1/{id:[0-9]+}/upload", { | ||
44 | { | ||
45 | method = "POST", | ||
46 | path = "/", | ||
47 | produces = "application/json", | ||
48 | handler = function(query) | ||
49 | local json = {module = "luasocket", version = {id = "1.0"}, module_url = "http://localhost/luasocket", manifests = "root", is_new = "is_new"} | ||
50 | return restserver.response():status(200):entity(json) | ||
51 | end | ||
52 | } | ||
53 | }) | ||
54 | |||
55 | server:add_resource("/api/1/{id:[0-9]+}/upload_rock/{id:[0-9]+}", { | ||
56 | { | ||
57 | method = "POST", | ||
58 | path = "/", | ||
59 | produces = "application/json", | ||
60 | handler = function(query) | ||
61 | local json = {"rock","module_url"} | ||
62 | return restserver.response():status(200):entity(json) | ||
63 | end | ||
64 | } | ||
65 | }) | ||
66 | |||
67 | -- SHUTDOWN this mock-server | ||
68 | server:add_resource("/shutdown", { | ||
69 | { | ||
70 | method = "GET", | ||
71 | path = "/", | ||
72 | handler = function(query) | ||
73 | os.exit() | ||
74 | return restserver.response():status(200):entity() | ||
75 | end | ||
76 | } | ||
77 | }) | ||
78 | |||
79 | -- This loads the restserver.xavante plugin | ||
80 | server:enable("restserver.xavante"):start() \ No newline at end of file | ||
diff --git a/test/test_environment.lua b/test/test_environment.lua index eb545a47..5d1e8198 100644 --- a/test/test_environment.lua +++ b/test/test_environment.lua | |||
@@ -36,12 +36,24 @@ local function exists(path) | |||
36 | return lfs.attributes(path, "mode") ~= nil | 36 | return lfs.attributes(path, "mode") ~= nil |
37 | end | 37 | end |
38 | 38 | ||
39 | function test_env.quiet(commad) | ||
40 | if not test_env.VERBOSE then | ||
41 | if test_env.TEST_TARGET_OS == "linux" or test_env.TEST_TARGET_OS == "osx" then | ||
42 | return commad .. " 1> /dev/null 2> /dev/null" | ||
43 | elseif test_env.TEST_TARGET_OS == "windows" then | ||
44 | return commad .. " 2> NUL 1> NUL" | ||
45 | end | ||
46 | else | ||
47 | return command | ||
48 | end | ||
49 | end | ||
50 | |||
39 | --- Helper function for execute_bool and execute_output | 51 | --- Helper function for execute_bool and execute_output |
40 | -- @param command string: command to execute | 52 | -- @param command string: command to execute |
41 | -- @param print_command boolean: print command if 'true' | 53 | -- @param print_command boolean: print command if 'true' |
42 | -- @param env_variables table: table of environment variables to export {FOO="bar", BAR="foo"} | 54 | -- @param env_variables table: table of environment variables to export {FOO="bar", BAR="foo"} |
43 | -- @return final_command string: concatenated command to execution | 55 | -- @return final_command string: concatenated command to execution |
44 | local function execute_helper(command, print_command, env_variables) | 56 | function test_env.execute_helper(command, print_command, env_variables) |
45 | local final_command = "" | 57 | local final_command = "" |
46 | 58 | ||
47 | if print_command then | 59 | if print_command then |
@@ -66,7 +78,7 @@ end | |||
66 | -- In Lua5.1 os.execute returns numeric value, but in Lua5.2+ returns boolean | 78 | -- In Lua5.1 os.execute returns numeric value, but in Lua5.2+ returns boolean |
67 | -- @return true/false boolean: status of the command execution | 79 | -- @return true/false boolean: status of the command execution |
68 | local function execute_bool(command, print_command, env_variables) | 80 | local function execute_bool(command, print_command, env_variables) |
69 | command = execute_helper(command, print_command, env_variables) | 81 | command = test_env.execute_helper(command, print_command, env_variables) |
70 | 82 | ||
71 | local ok = os.execute(command) | 83 | local ok = os.execute(command) |
72 | return ok == true or ok == 0 | 84 | return ok == true or ok == 0 |
@@ -75,7 +87,7 @@ end | |||
75 | --- Execute command and returns output of command | 87 | --- Execute command and returns output of command |
76 | -- @return output string: output the command execution | 88 | -- @return output string: output the command execution |
77 | local function execute_output(command, print_command, env_variables) | 89 | local function execute_output(command, print_command, env_variables) |
78 | command = execute_helper(command, print_command, env_variables) | 90 | command = test_env.execute_helper(command, print_command, env_variables) |
79 | 91 | ||
80 | local file = assert(io.popen(command)) | 92 | local file = assert(io.popen(command)) |
81 | local output = file:read('*all') | 93 | local output = file:read('*all') |
@@ -106,6 +118,8 @@ function test_env.set_args() | |||
106 | test_env.RESET_ENV = false | 118 | test_env.RESET_ENV = false |
107 | elseif argument == "clean" then | 119 | elseif argument == "clean" then |
108 | test_env.TEST_ENV_CLEAN = true | 120 | test_env.TEST_ENV_CLEAN = true |
121 | elseif argument == "verbose" then | ||
122 | test_env.VERBOSE = true | ||
109 | elseif argument == "travis" then | 123 | elseif argument == "travis" then |
110 | test_env.TRAVIS = true | 124 | test_env.TRAVIS = true |
111 | elseif argument:find("^os=") then | 125 | elseif argument:find("^os=") then |
@@ -415,10 +429,11 @@ end | |||
415 | 429 | ||
416 | --- Test if required rock is installed if not, install it | 430 | --- Test if required rock is installed if not, install it |
417 | function test_env.need_rock(rock) | 431 | function test_env.need_rock(rock) |
418 | if test_env.run.luarocks_nocov("show " .. rock) then | 432 | print("Check if " .. rock .. " is installed") |
433 | if test_env.run.luarocks_noprint_nocov(test_env.quiet("show " .. rock)) then | ||
419 | return true | 434 | return true |
420 | else | 435 | else |
421 | return test_env.run.luarocks_nocov("install " .. rock) | 436 | return test_env.run.luarocks_noprint_nocov(test_env.quiet("install " .. rock)) |
422 | end | 437 | end |
423 | end | 438 | end |
424 | 439 | ||
@@ -525,18 +540,20 @@ local function clean() | |||
525 | test_env.remove_subdirs(test_env.testing_paths.testing_dir, "testing[_%-]") | 540 | test_env.remove_subdirs(test_env.testing_paths.testing_dir, "testing[_%-]") |
526 | test_env.remove_files(test_env.testing_paths.testing_dir, "testing_") | 541 | test_env.remove_files(test_env.testing_paths.testing_dir, "testing_") |
527 | test_env.remove_files(test_env.testing_paths.testing_dir, "luacov") | 542 | test_env.remove_files(test_env.testing_paths.testing_dir, "luacov") |
543 | test_env.remove_files(test_env.testing_paths.testing_dir, "upload_config") | ||
528 | print("Cleaning done!") | 544 | print("Cleaning done!") |
529 | end | 545 | end |
530 | 546 | ||
531 | --- Install luarocks into testing prefix. | 547 | --- Install luarocks into testing prefix. |
532 | local function install_luarocks(install_env_vars) | 548 | local function install_luarocks(install_env_vars) |
533 | -- Configure LuaRocks testing environment | 549 | -- Configure LuaRocks testing environment |
534 | local configure_cmd = "./configure --with-lua=" .. test_env.testing_paths.luadir .. | 550 | title("Installing LuaRocks") |
535 | " --prefix=" .. test_env.testing_paths.testing_lrprefix .. | 551 | local configure_cmd = "./configure --with-lua=" .. test_env.testing_paths.luadir .. " --prefix=" .. test_env.testing_paths.testing_lrprefix |
536 | " && make clean" | 552 | assert(execute_bool(test_env.quiet(configure_cmd)), false, install_env_vars) |
537 | 553 | assert(execute_bool(test_env.quiet("make clean")), false, install_env_vars) | |
538 | assert(execute_bool(configure_cmd, false, install_env_vars)) | 554 | assert(execute_bool(test_env.quiet("make src/luarocks/site_config.lua")), false, install_env_vars) |
539 | assert(execute_bool("make src/luarocks/site_config.lua && make dev", false, install_env_vars)) | 555 | assert(execute_bool(test_env.quiet("make dev")), false, install_env_vars) |
556 | print("LuaRocks installed correctly!") | ||
540 | end | 557 | end |
541 | 558 | ||
542 | --- | 559 | --- |