Closed UserProgrammer closed 4 years ago
(To understand the comments below, please refer to the repo linked in the OP)
I added the following line at the top of tests/spec/test2_spec.lua:
os.execute("pwd > /tmp/working_directory")
I expected it to be: <path_to_project_directory>/tests/spec
Instead, it was: <path_to_project_directory>/tests
I then changed the path to the required file from
require "../../main"
to
require "../main"
Unfortunately, this did not resolve my problem. I got the same error message.
no idea what your code does, but require "../../main"
is not valid. The module name has its own semantics, it is not a file-path, where you can navigate up and down using ..
.
Here's some explanation on how the paths work: http://www.thijsschreijer.nl/blog/?p=1025
@Tieske Thanks for the info. Is it possible to manage dependencies without adding my project's root directory path to the LUA_PATH environment variable, for a directory structure like this:
<project_root_dir>/src/main.lua
<project_root_dir>/tests/unit_test.lua
where unit_test.lua
requires main.lua
.
The reason I don't want to update LUA_PATH is because, from a design standpoint, I don't think it makes sense to introduce my project's root directory as a potential source of modules/dependencies to all Lua projects in my system, in order to make my project's source code files visible to the corresponding unit test files.
Furthermore, If I rely solely on Lua's default search paths, I would either have to organize my project's directory structure like this:
<project_root_directory>/tests/src
which I don't think makes sense either; or have two projects (i.e.: modules), one for my source code and another for my unit tests:
<some_default_lua_modules_path>/project/<...>
~/project_tests/<...>
which I don't want to do because I want to keep all of my project's code inside a single project root directory. Essentially, I'm trying to manage my project's dependencies, such that:
(1) Given a non-linear directory structure (like the example above), any file is visible to any other file within my project. (2) I don't have to modify Lua's global environment variables in order to accomplish (1).
If I can't accomplish this with Lua, I'm fine with exploring a different design philosophy/methodology. Having said that, it seems to me that the project structure I'm implementing is fairly standard, and I would be surprised if this isn't something that's supported out-of-the-box with Lua (with respect to dependency management).
see busted help:
-m, --lpath=PATH optional path to be prefixed to the Lua
module search path (default:
./src/?.lua;./src/?/?.lua;./src/?/init.lua)
--cpath=PATH optional path to be prefixed to the Lua C
module search path (default:
./csrc/?.so;./csrc/?/?.so;)
you can also specify those options in a .busted
configuration file
@Tieske That satisfies all I needed. Thank you for taking the time to help me out!
I setup a repository for reproducing the bug here: https://github.com/UserProgrammer/busted_bug Environment details and steps to reproduce bug can be found in the repo's README file; it's contents are duplicated below:
Lua Version: Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio Busted Version: 2.0.0-0 Operating System: Ubuntu 18.04.4 LTS (Bionic Beaver)
How to Reproduce Bug:
1) In the project's root directory, execute
busted --directory=./
. This will output the print statement from foo(), as expected.2) In the project's root directory, execute
busted --directory=./tests
.Expected output: To see print statement from foo().
Actual output: