How to find the path of the file calling require? - ruby-on-rails

I have a large test_helper file which includes code for unit and integration test. I would like to split it into two, without having to change every require "test_helper".
To accomplish that, I want to conditionally call unit_test_helper or integration_test_helper based on the file path of the test file calling require "test_helper".
How can I find the path of the file calling require "test_helper"?

Related

how to load modules in different folders

I have following folder structure
--folder1
------main.lua
------sub_folder
---------func1.lua
--folder2
------func2.lua
I want to load func1.lua and func2.lua in main.lua by require command
PS:what does the init.lua in a folder do
In vanilla lua, you'd load them like this:
local func1 = loadfile "sub_folder/func1.lua"
local func2 = loadfile "../folder2/func2.lua"
See: Pil Chapter 8
If you're using Lua embedded in some other application (i.e, a game, software, etc.), then you need to let us know what this is, or we can't help you. This goes for your P.S question as well. But generally, init.lua is usually the first file to be executed, i.e, it'll load other files, etc.
The func1.lua is easily loaded via
require 'sub_folder.func1'
because sub_folder is in the same folder as main.lua.
For func2.lua, there is no way to tell require to search "one level up", so you must tell Lua how to find the required modules. This can be done at least two ways:
via LUA_PATH environment variable:
append path/to/folder2/?.lua to it, then main.lua can do require "func2".
OR append path/to/parent-of-folder2/?.lua to it, then main.lua can do require "folder2.func2".
by editing package.path in your script:
package.path = package.path .. ';../?.lua'
require 'func2'
or
package.path = package.path .. ';../../?.lua'
require 'folder2.func2'
The first method is more "permanent" since the setting is in the OS environment; it will work even if you move folder1 to some other place on your system, without moving folder2. The second method is dynamic so it will work regardless of where you place your folder structure, i.e., if folder2 is always sibling folder of folder1 the method 2 works, method 1 fails (or requires that you edit the LUA_PATH).

Lua: require fails to find submodule, but searchpath succeeds?

usually when I have a question about something remotely software related I find that someone else has already asked the very same thing, and gotten good answers that works for me too.
This time, though, I've failed to find an answer to my predicament.
Here we go:
I'm currently trying to move up my Lua-programming a notch or three and want to use modules. So, I've got a structure like this:
main.lua
foo/bar.lua
Now, in main.lua I do
require("foo.bar")
which fails,
main.lua:1 module 'foo.bar' not found:
no field package.preload['foo.bar']
no file 'foo.bar.lua'
no file 'foo.bar.lua'
no file 'foo.lua'
Ok, something might be wrong with my package.path so I use package.searchpath("foo.bar", package.path) to see what I', doing wrong.
The problem is that package.searchpath resolves foo.bar to foo/bar.lua which is exactly right.
As I've understood it, package.searchpath tries to find the module in the same way as require, but there seems to be som glitch in my case.
What strikes me as odd is the repetition of the no file 'foo.bar.lua' in the error output
Have I misunderstood the use of require?
I'm using LuaJIT-2.0.0 to run my chunks
Update:
I'm using LuaJIT-2.0.0 to run my chunks <- This was the reason for my problem, stock Lua-5.2.2 behaves as expected
package.path = debug.getinfo(1,"S").source:match[[^#?(.*[\/])[^\/]-$]] .."?.lua;".. package.path
require("foo.bar")
This line causes require to look in the same directory as the
current file when asked to load other files. If you want it to instead
search a directory relative to the current directory, insert the
relative path between " and ?.lua
Here is part of require description:
[...] Otherwise require searches for a Lua loader using the path stored in
package.path. If that also fails, it searches for a C loader using the
path stored in package.cpath. If that also fails, it tries an
all-in-one loader (see package.loaders).
Default path for package.path is always the .exe that executes specified script.

include file and it's content

How do I import or include a file into Lua code. I wanted to fetch the file's content which I am including into function.
What are the functions into Lua which will loads when lua file will get loaded.
Any help would be greatly appreciated
Thanks
You normally use either require or dofile, depending of your situation. It is also possible to call the lower level functions loadfile or load if you need more flexibility.
require is used to load library modules. The argument is the name of the module, and Lua then searches for the file in the package.path path. It also maintains a cache of loaded modules, so that the second time you call require for the same name it will only return the cached value.
dofile is simpler. It just take the file path as argument. It loads and executes the code every time it is called.

Lua Require function on a full path name

I need to call the require on a lua file that will not always be in the same place. I was trying to call require on the full path name but that doesn't seem to be working either. I even tried replacing one of my working normal requires with a correct full path name to the same file
example changing
require "foo"
to
require "C:\Users\Me\MyLuaProject\foo"
but when i switched it to the full path name it could no longer find it. So I am wondering if you can even call require on a full path and if not how would i achieve the same result differently?
If you just need to load a file, use dofile, which takes a path:
dofile("C:\\Users\\Me\\MyLuaProject\\foo")
Add the directory containing the file to package.path:
package.path = package.path .. ";C:\\Users\\Me\\MyLuaProject"
require "foo"
You can also add it to the LUA_PATH environment variable, but this is probably less easy to modify on the fly.
A common pattern for modules is to have abc.lua and abc/xyz.lua; to require files in a subdirectory like that, use the following:
require "abc"
require "abc.xyz"

Do file only once in Lua

I was wondering if there's a way to do a lua file only once and have any subsequent attempts to do that lua file will result in a no-op.
I've already thought about doing something akin to C++ header's #if/else/endif trick. I'm wondering if there's a standard way to implement this.
James
well, require pretty much does that.
require "file" -- runs "file.lua"
require "file" -- does not run the "file" again
The only problem with require is that it works on module names, not file names. In particular, require does not handle names with paths (although it does use package.path and package.cpath to locate modules in the file system).
If you want to handle names with paths you can write a simple wrapper to dofile as follows:
do
local cache={}
local olddofile=dofile
function dofile(x)
if cache[x]==nil then
olddofile(x)
cache[x]=true
end
end
end
based on lhf's answer, but utilising package, you can also do this once:
package.preload["something"]=dofile "/path/to/your/file.lua"
and then use:
local x=require "something"
to get the preloaded package again. but that's a bit abusive...

Resources