lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.4k stars 185 forks source link

'require'd source file not found for test file despite correct file path #628

Closed UserProgrammer closed 4 years ago

UserProgrammer commented 4 years ago

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:

0 successes / 0 failures / 1 error / 0 pending : 0.000969 seconds

Error → spec/test2_spec.lua @ 1
suite spec/test2_spec.lua
spec/test2_spec.lua:1: module '../../main' not found:No LuaRocks module found for ../../main
    no field package.preload['../../main']
    no file './src///////main.lua'
    no file './src///////main///////main.lua'
    no file './src///////main/init.lua'
    no file '/home/michael/.luarocks/share/lua/5.3///////main.lua'
    no file '/home/michael/.luarocks/share/lua/5.3///////main/init.lua'
    no file '/usr/local/share/lua/5.3///////main.lua'
    no file '/usr/local/share/lua/5.3///////main/init.lua'
    no file '/usr/local/lib/lua/5.3///////main.lua'
    no file '/usr/local/lib/lua/5.3///////main/init.lua'
    no file '/usr/share/lua/5.3///////main.lua'
    no file '/usr/share/lua/5.3///////main/init.lua'
    no file './//////main.lua'
    no file './//////main/init.lua'
    no file './csrc///////main.so'
    no file './csrc///////main///////main.so'
    no file '/home/michael/.luarocks/lib/lua/5.3///////main.so'
    no file '/usr/local/lib/lua/5.3///////main.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.3///////main.so'
    no file '/usr/lib/lua/5.3///////main.so'
    no file '/usr/local/lib/lua/5.3/loadall.so'
    no file './//////main.so'
    no file './csrc/.so'
    no file './csrc//.so'
    no file '/home/michael/.luarocks/lib/lua/5.3/.so'
    no file '/usr/local/lib/lua/5.3/.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.3/.so'
    no file '/usr/lib/lua/5.3/.so'
    no file '/usr/local/lib/lua/5.3/loadall.so'
    no file './.so'
UserProgrammer commented 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.

Tieske commented 4 years ago

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

UserProgrammer commented 4 years ago

@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).

Tieske commented 4 years ago

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

UserProgrammer commented 4 years ago

@Tieske That satisfies all I needed. Thank you for taking the time to help me out!