Unit testing a moonscript awesome config

283 views Asked by At

What I am trying to do to learn some lua/moonscript is migrate my awesome configuration file (rc.lua) to moonscript and unit-test a few things along the way. For this I set up rc.lua to require the moonscript config file like this

package.path = pathsToAdd .. package.path
-- a bit of a hassle to amend the lua require paths
-- correctly; I boldly assume for now that these are not the
-- cause of the problem
require('moonscript')
require('config')

For a first unit test to check if my config calls a specific function of the module 'gears' all went reasonably well. I ended up mocking the gears module of every subsequent call to

require('gears')

by setting up the unit test like so

package.loaded.gears = myMockVersion

fast forward to when my config file under test needs to require the 'awful' module: its init.lua is called, immediately executing

return
{
    client = require("awful.client");
    ...
}

which leads to client.lua doing

...
local tag = require("awful.tag")
...
local client = {}
-- define lots of functions, register some signal handlers
return client

and now, for everyone still reading, the problem in tag.lua:

...
local capi =
{
    ...
    client = client,
    ...
}
...
capi.client.connect_signal(...)

That last call throws a good old

attempt to index a nil value (field 'client')

Which I assume is because client.lua did not yet run past the first few require calls and therefore is not available globally at all or, at least, did not define its functionality yet.
Which leads me, at last, to the question:

Why does this even run during your everyday awesome startup (awful is pretty much the core module) in the first place and what do I miss when trying to replicate the environment in which it does.

Thank your very much in advance.
Yours truly

1

There are 1 answers

2
Uli Schlachter On BEST ANSWER

The C core of awesome exports some objects for lua to use. Awful (and lots of others) use these directly. These are in awesome 3.5 (see https://awesome.naquadah.org/doc/api/):

  • tag
  • timer
  • drawin
  • keygrabber
  • drawable
  • root
  • mouse
  • client
  • screen
  • awesome
  • mousegrabber
  • selection
  • key
  • dbus
  • button

Most of these have wrappers in awful which add useful stuff (e.g. key vs awful.key, same for tag, keygrabber, button). Other stuff is completely hidden from "the average user" (e.g. drawin, drawable).

You should be able to mock these as well, but you will have to set global variables with the same name.

Edit: by the way, this is why you cannot require("awful") in a normal lua promt. The same built-in objects are missing.