From 8e6a83911027de167d689d060ce581f653d73213 Mon Sep 17 00:00:00 2001 From: George Roman Date: Tue, 24 Jul 2018 11:09:42 +0300 Subject: Tests: add test_env.run_in_tmp utility function --- spec/build_spec.lua | 336 ++++++++++++++++++++++--------------------------- spec/init_spec.lua | 109 +++++++--------- spec/install_spec.lua | 18 +-- spec/util/test_env.lua | 19 +++ 4 files changed, 217 insertions(+), 265 deletions(-) diff --git a/spec/build_spec.lua b/spec/build_spec.lua index 62fbb353..4bf0d96c 100644 --- a/spec/build_spec.lua +++ b/spec/build_spec.lua @@ -54,31 +54,25 @@ describe("LuaRocks build tests #integration", function() end) it("LuaRocks build with no arguments behaves as luarocks make", function() - local olddir = lfs.currentdir() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("c_module-1.0-1.rockspec", [[ - package = "c_module" - version = "1.0-1" - source = { - url = "http://example.com/c_module" - } - build = { - type = "builtin", - modules = { - c_module = { "c_module.c" } + test_env.run_in_tmp(function(tmpdir) + write_file("c_module-1.0-1.rockspec", [[ + package = "c_module" + version = "1.0-1" + source = { + url = "http://example.com/c_module" } - } - ]], finally) - write_file("c_module.c", c_module_source, finally) - - assert.is_true(run.luarocks_bool("build")) - assert.truthy(lfs.attributes(tmpdir .. "/c_module." .. test_env.lib_extension)) - - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + build = { + type = "builtin", + modules = { + c_module = { "c_module.c" } + } + } + ]], finally) + write_file("c_module.c", c_module_source, finally) + + assert.is_true(run.luarocks_bool("build")) + assert.truthy(lfs.attributes(tmpdir .. "/c_module." .. test_env.lib_extension)) + end, finally) end) end) @@ -94,31 +88,25 @@ describe("LuaRocks build tests #integration", function() end) it("LuaRocks build verbose", function() - local olddir = lfs.currentdir() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("test-1.0-1.rockspec", [[ - package = "test" - version = "1.0-1" - source = { - url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" - } - build = { - type = "builtin", - modules = { - test = "test.lua" + test_env.run_in_tmp(function(tmpdir) + write_file("test-1.0-1.rockspec", [[ + package = "test" + version = "1.0-1" + source = { + url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" } - } - ]], finally) - write_file("test.lua", "return {}", finally) - - assert.is_true(run.luarocks_bool("build --verbose test-1.0-1.rockspec")) - assert.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + build = { + type = "builtin", + modules = { + test = "test.lua" + } + } + ]], finally) + write_file("test.lua", "return {}", finally) - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + assert.is_true(run.luarocks_bool("build --verbose test-1.0-1.rockspec")) + assert.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + end, finally) end) it("LuaRocks build lpeg branch=master", function() @@ -163,70 +151,58 @@ describe("LuaRocks build tests #integration", function() end) it("LuaRocks build fails if the current platform is not supported", function() - local olddir = lfs.currentdir() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("test-1.0-1.rockspec", [[ - package = "test" - version = "1.0-1" - source = { - url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" - } - supported_platforms = { - "unix", "macosx" - } - build = { - type = "builtin", - modules = { - test = "test.lua" + test_env.run_in_tmp(function(tmpdir) + write_file("test-1.0-1.rockspec", [[ + package = "test" + version = "1.0-1" + source = { + url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" } - } - ]], finally) - write_file("test.lua", "return {}", finally) - - if test_env.TEST_TARGET_OS == "windows" then - assert.is_false(run.luarocks_bool("build test-1.0-1.rockspec")) -- Error: This rockspec does not support windows platforms - assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) - else - assert.is_true(run.luarocks_bool("build test-1.0-1.rockspec")) - assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) - end + supported_platforms = { + "unix", "macosx" + } + build = { + type = "builtin", + modules = { + test = "test.lua" + } + } + ]], finally) + write_file("test.lua", "return {}", finally) - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + if test_env.TEST_TARGET_OS == "windows" then + assert.is_false(run.luarocks_bool("build test-1.0-1.rockspec")) -- Error: This rockspec does not support windows platforms + assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + else + assert.is_true(run.luarocks_bool("build test-1.0-1.rockspec")) + assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + end + end, finally) end) it("LuaRocks build with skipping dependency checks", function() - local olddir = lfs.currentdir() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("test-1.0-1.rockspec", [[ - package = "test" - version = "1.0-1" - source = { - url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" - } - dependencies = { - "a_rock 1.0" - } - build = { - type = "builtin", - modules = { - test = "test.lua" + test_env.run_in_tmp(function(tmpdir) + write_file("test-1.0-1.rockspec", [[ + package = "test" + version = "1.0-1" + source = { + url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" } - } - ]], finally) - write_file("test.lua", "return {}", finally) - - assert.is_true(run.luarocks_bool("build test-1.0-1.rockspec --deps-mode=none")) - assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + dependencies = { + "a_rock 1.0" + } + build = { + type = "builtin", + modules = { + test = "test.lua" + } + } + ]], finally) + write_file("test.lua", "return {}", finally) - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + assert.is_true(run.luarocks_bool("build test-1.0-1.rockspec --deps-mode=none")) + assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + end) end) it("LuaRocks build lmathx deps partial match", function() @@ -291,70 +267,58 @@ describe("LuaRocks build tests #integration", function() end) it("LuaRocks build only deps of a given rockspec", function() - local olddir = lfs.currentdir() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("test-1.0-1.rockspec", [[ - package = "test" - version = "1.0-1" - source = { - url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" - } - dependencies = { - "a_rock 1.0" - } - build = { - type = "builtin", - modules = { - test = "test.lua" + test_env.run_in_tmp(function(tmpdir) + write_file("test-1.0-1.rockspec", [[ + package = "test" + version = "1.0-1" + source = { + url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" } - } - ]], finally) - write_file("test.lua", "return {}", finally) - - assert.is.truthy(run.luarocks_bool("build --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.rockspec --only-deps")) - assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) - assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/a_rock/1.0-1/a_rock-1.0-1.rockspec")) + dependencies = { + "a_rock 1.0" + } + build = { + type = "builtin", + modules = { + test = "test.lua" + } + } + ]], finally) + write_file("test.lua", "return {}", finally) - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + assert.is.truthy(run.luarocks_bool("build --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.rockspec --only-deps")) + assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/a_rock/1.0-1/a_rock-1.0-1.rockspec")) + end, finally) end) it("LuaRocks build only deps of a given rock", function() - local olddir = lfs.currentdir() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("test-1.0-1.rockspec", [[ - package = "test" - version = "1.0-1" - source = { - url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" - } - dependencies = { - "a_rock 1.0" - } - build = { - type = "builtin", - modules = { - test = "test.lua" + test_env.run_in_tmp(function(tmpdir) + write_file("test-1.0-1.rockspec", [[ + package = "test" + version = "1.0-1" + source = { + url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/test.lua" } - } - ]], finally) - write_file("test.lua", "return {}", finally) - - assert.is.truthy(run.luarocks_bool("pack test-1.0-1.rockspec")) - assert.is.truthy(lfs.attributes("test-1.0-1.src.rock")) + dependencies = { + "a_rock 1.0" + } + build = { + type = "builtin", + modules = { + test = "test.lua" + } + } + ]], finally) + write_file("test.lua", "return {}", finally) - assert.is.truthy(run.luarocks_bool("build --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.src.rock --only-deps")) - assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) - assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/a_rock/1.0-1/a_rock-1.0-1.rockspec")) + assert.is.truthy(run.luarocks_bool("pack test-1.0-1.rockspec")) + assert.is.truthy(lfs.attributes("test-1.0-1.src.rock")) - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + assert.is.truthy(run.luarocks_bool("build --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.src.rock --only-deps")) + assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) + assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/a_rock/1.0-1/a_rock-1.0-1.rockspec")) + end, finally) end) it("LuaRocks build with https", function() @@ -480,37 +444,31 @@ describe("LuaRocks build tests #integration", function() end) it("fails when missing external dependency", function() - local tmpdir = get_tmp_path() - local olddir = lfs.currentdir() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("missing_external-0.1-1.rockspec", [[ - package = "missing_external" - version = "0.1-1" - source = { - url = "https://example.com/build.lua" - } - external_dependencies = { - INEXISTENT = { - library = "inexistentlib*", - header = "inexistentheader*.h", + test_env.run_in_tmp(function(tmpdir) + write_file("missing_external-0.1-1.rockspec", [[ + package = "missing_external" + version = "0.1-1" + source = { + url = "https://example.com/build.lua" } - } - dependencies = { - "lua >= 5.1" - } - build = { - type = "builtin", - modules = { - build = "build.lua" + external_dependencies = { + INEXISTENT = { + library = "inexistentlib*", + header = "inexistentheader*.h", + } } - } - ]], finally) - assert.is_false(run.luarocks_bool("build missing_external-0.1-1.rockspec INEXISTENT_INCDIR=\"/invalid/dir\"")) - - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + dependencies = { + "lua >= 5.1" + } + build = { + type = "builtin", + modules = { + build = "build.lua" + } + } + ]], finally) + assert.is_false(run.luarocks_bool("build missing_external-0.1-1.rockspec INEXISTENT_INCDIR=\"/invalid/dir\"")) + end, finally) end) it("builds with external dependency", function() diff --git a/spec/init_spec.lua b/spec/init_spec.lua index 2d28bfe7..a4751741 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -8,76 +8,57 @@ local is_win = test_env.TEST_TARGET_OS == "windows" test_env.unload_luarocks() describe("Luarocks init test #integration", function() - local tmpdir - - after_each(function() - if tmpdir then - lfs.rmdir(tmpdir) - tmpdir = nil - end - end) - it("LuaRocks init with no arguments", function() - tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - local myproject = tmpdir .. "/myproject" - lfs.mkdir(myproject) - local olddir = lfs.currentdir() - lfs.chdir(myproject) - - assert(run.luarocks("init")) - if is_win then - assert.truthy(lfs.attributes(myproject .. "/lua.bat")) - assert.truthy(lfs.attributes(myproject .. "/luarocks.bat")) - else - assert.truthy(lfs.attributes(myproject .. "/lua")) - assert.truthy(lfs.attributes(myproject .. "/luarocks")) - end - assert.truthy(lfs.attributes(myproject .. "/lua_modules")) - assert.truthy(lfs.attributes(myproject .. "/.luarocks")) - assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) - assert.truthy(lfs.attributes(myproject .. "/.gitignore")) - assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) - - lfs.chdir(olddir) + test_env.run_in_tmp(function(tmpdir) + local myproject = tmpdir .. "/myproject" + lfs.mkdir(myproject) + lfs.chdir(myproject) + + assert(run.luarocks("init")) + if is_win then + assert.truthy(lfs.attributes(myproject .. "/lua.bat")) + assert.truthy(lfs.attributes(myproject .. "/luarocks.bat")) + else + assert.truthy(lfs.attributes(myproject .. "/lua")) + assert.truthy(lfs.attributes(myproject .. "/luarocks")) + end + assert.truthy(lfs.attributes(myproject .. "/lua_modules")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) + assert.truthy(lfs.attributes(myproject .. "/.gitignore")) + assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) + end, finally) end) it("LuaRocks init with given arguments", function() - tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - local myproject = tmpdir .. "/myproject" - lfs.mkdir(myproject) - local olddir = lfs.currentdir() - lfs.chdir(myproject) - - assert(run.luarocks("init customname 1.0")) - assert.truthy(lfs.attributes(myproject .. "/customname-1.0-1.rockspec")) - - lfs.chdir(olddir) + test_env.run_in_tmp(function(tmpdir) + local myproject = tmpdir .. "/myproject" + lfs.mkdir(myproject) + lfs.chdir(myproject) + + assert(run.luarocks("init customname 1.0")) + assert.truthy(lfs.attributes(myproject .. "/customname-1.0-1.rockspec")) + end, finally) end) it("LuaRocks init in a git repo", function() - tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - local olddir = lfs.currentdir() - lfs.chdir(tmpdir) - local myproject = tmpdir .. "/myproject" - copy_dir(testing_paths.fixtures_dir .. "/git_repo", myproject) - lfs.chdir(myproject) - - assert(run.luarocks("init")) - local fd = assert(io.open(myproject .. "/myproject-dev-1.rockspec", "r")) - local content = assert(fd:read("*a")) - assert.truthy(content:find("summary = \"Test repo\"")) - assert.truthy(content:find("detailed = .+Test repo.+")) - assert.truthy(content:find("license = \"MIT\"")) - - fd = assert(io.open(myproject .. "/.gitignore", "r")) - content = assert(fd:read("*a")) - assert.truthy(content:find("/foo")) - assert.truthy(content:find("/lua")) - assert.truthy(content:find("/lua_modules")) - - lfs.chdir(olddir) + test_env.run_in_tmp(function(tmpdir) + local myproject = tmpdir .. "/myproject" + copy_dir(testing_paths.fixtures_dir .. "/git_repo", myproject) + lfs.chdir(myproject) + + assert(run.luarocks("init")) + local fd = assert(io.open(myproject .. "/myproject-dev-1.rockspec", "r")) + local content = assert(fd:read("*a")) + assert.truthy(content:find("summary = \"Test repo\"")) + assert.truthy(content:find("detailed = .+Test repo.+")) + assert.truthy(content:find("license = \"MIT\"")) + + fd = assert(io.open(myproject .. "/.gitignore", "r")) + content = assert(fd:read("*a")) + assert.truthy(content:find("/foo")) + assert.truthy(content:find("/lua")) + assert.truthy(content:find("/lua_modules")) + end, finally) end) end) diff --git a/spec/install_spec.lua b/spec/install_spec.lua index 8a5d8f09..0555155b 100644 --- a/spec/install_spec.lua +++ b/spec/install_spec.lua @@ -57,18 +57,12 @@ describe("luarocks install #integration", function() end) it("fails not a zip file", function() - local olddir = lfs.currentdir() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - - write_file("not_a_zipfile-1.0-1.src.rock", [[ - I am not a .zip file! - ]], finally) - assert.is_false(run.luarocks_bool("install not_a_zipfile-1.0-1.src.rock")) - - lfs.chdir(olddir) - lfs.rmdir(tmpdir) + test_env.run_in_tmp(function(tmpdir) + write_file("not_a_zipfile-1.0-1.src.rock", [[ + I am not a .zip file! + ]], finally) + assert.is_false(run.luarocks_bool("install not_a_zipfile-1.0-1.src.rock")) + end, finally) end) it("only-deps of lxsh show there is no lxsh", function() diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index 0218aa53..181f4693 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -101,6 +101,25 @@ function test_env.get_tmp_path() return path end +--- Helper function that runs the given function inside +-- a temporary directory, isolating it +-- @param f function: the function to be run +function test_env.run_in_tmp(f, finally) + local olddir = lfs.currentdir() + local tmpdir = test_env.get_tmp_path() + lfs.mkdir(tmpdir) + lfs.chdir(tmpdir) + + if finally then + finally(function() + lfs.chdir(olddir) + lfs.rmdir(tmpdir) + end) + end + + f(tmpdir) +end + --- Helper function for execute_bool and execute_output -- @param command string: command to execute -- @param print_command boolean: print command if 'true' -- cgit v1.2.3-55-g6feb