Content tagged lua

Testing with Lua

Published on:

Last time I wrote about my project avandu-lua and I mentioned how I was having some trouble testing the different types of functions. I've since found a way to mock the functions in such a way that I can safely test functions with IO operations without having to actually perform them. It seems that Lua modules are mutable. Perhaps this isn't strange given that Lua modules are basically tables, but I hadn't considered it before. I'm not entirely sure if it is a language feature or just something that happens to be true right now, so this method of mine might soon become useless.

Testing operations

So, to test these functions that would normally have side-effects or would require a lot of extra work setting up to work correctly, we basically have to replace the existing functions. Normally in a running program you really wouldn't want to do this, save for when you have dynamic scope, which I haven't yet found in Lua.

So I want to test that everything works properly when the io.access function reports it can't access a certain file, I'd change the function like so:

-- You must first require it, so you have the same module.
local posix = require 'posix'

-- ...

posix.access = function ()
   return false
end

This way I know what the function will do, when it eventually gets called.

Travis-CI

After finally getting some tests in my project and making sure that I have full test coverage of my module, I thought it would be fun to see if I could automatically test everything with travis-ci. It was a little challenging because I don't normally run Ubuntu or Debian, so I don't know what they name their packages, and one of my dependencies (luasec) had some trouble finding the libssl library.

After a little poking around, a few retries and a false-success, it's now finally running:

My .travis.yml is pretty simple:

language: erlang

env:
  - LUA="lua"

branches:
  except:
    - gh-pages

install:
  - sudo apt-get update
  - sudo apt-get install luarocks libssl1.0.0
  - sudo luarocks install busted
  - sudo luarocks install luasocket
  - sudo luarocks install luasec OPENSSL_LIBDIR=/usr/lib/x86_64-linux-gnu
  - sudo luarocks install luajson
  - sudo luarocks install luaposix

script: "busted -m 'lua/?.lua' -o TAP"

# ...

I'm using the erlang environment, because there isn't a Lua one (yet). I've written my library for lua, not luajit, so busted needs to know which to run, it always runs luajit first it seems. I don't need the tests to be run again when the gh-pages branch is updated, since that has nothing to do with the code itself, I would actually like to have tests run on all other branches.

Then we get to the dependencies. Nothing major except for the luasec dependency. I'm not entirely sure how long that OPENSSL_LIBDIR will remain where it is now, but it works for now. I didn't discover the location myself, I found it on someone else's .travis.yml as a comment.

Lastly we have the script. Since the tests live in /spec and the code lives in /lua I run the tests from the project root and include the lua directory in the module path. I use TAP output because with the default output failures also return 0, when a failure occurs with the TAP output, a non-0 exit status is returned and travis knows they didn't pass. That is why build 6 passed even though there was still a failed test.

The rest is notification settings which isn't interesting enough to duplicate here.

Still to do

Now I should start expanding it a little. Well, actually I still need to add the proper license information.


Avandu in Lua

Published on:

A little while ago I started using the Awesome window manager again. I've started to play some PC games (such as Rogue Legacy) and have to use some more graphical applications again. Stumpwm just wasn't quite cutting it and suddenly it seemed that my workflow didn't quite fit with a completely-manual tiling experience.

So now that I'm back with Awesome I wanted to have a count of the number of unread articles in Tiny Tiny RSS in my wibox. I already have a project named avandu, which is an Emacs interface for Tiny Tiny RSS, and for a little while I used that in combination with Emacs' daemon mode to get the number of unread RSS items. This halt my Emacs for a few seconds every minute, though, so that was unpleasant. It also caused a lot of "Connecting to host: ..." messages to appear in my Emacs' echo area, which is a little annoying.

So I decided to write a lua module to get this count, since I didn't have a lua project yet. That is avandu-lua. Right now it only implements a login and unread functions, which allow you to log-in to get a session key (so you can do other things), and get the number of unread articles (what a shock!).

I've written a bit of documentation for it, hosted by github. There isn't much to document of course, but I try.

I still need to add tests, but I'm having difficulty deciding on how to do this. busted looks really nice, but their idea of stubs and mocks doesn't seem to be very useful if you're testing a function that calls, for example, http.call, which returns 4 distinct values, not none. I can't decide if I should keep looking, (try to) write something or use _TEST checks everywhere. I'm leaning towards that last one, perhaps I'll add that.

I don't currently have any concrete plans of extending it to have more functions than the ones I've added so far, I might do it for fun at some point, or if you'd really like to be able to call Tiny Tiny RSS from lua let me know and I might put some more effort into it.

It's been nice to work with lua. I don't particularly love the language and it certainly doesn't beat Lisp on any front, but it has its moments and niceties.

Some things that still need to be done:

  • As I said, I need to add tests.

  • I think I should try to see if coroutines can be used, it seems to hang Awesome now on occasion.

  • Add license info. Yeah I really should almost do this before I do anything else when I start a new project. It's going to be LLGPL in any case.

  • Release an actual version. Always installing from just a git checkout can be a little annoying.


Notstumpwm

Published on:

I have just returned from an excursion into the land of exherbo, which is an awesome source-based distro, and I found that while I was gone, something changed that made stumpwm cause a segmentation fault in X11 a few seconds after starting up.

I have tried everything I can think of to get it running again, but alas, to no avail. So I started looking at alternatives again. Feeling a little crazy I decided to give notion another try. And it fits strangely well.

It's configured/extended in lua, which I'm not particularly fond of, and it has a (in my opinion) crazy default configuration. But it also allows Emacs-like key combinations out-of-the-box, which is a very big plus in my book. So the quest to bring it closer to my stumpwm setup has begun.

Window layout

One of the nicest additions to my stumpwm configuration I made in the last few weeks was a loaded window configuration which put my Emacs frames in a big chunk of my left monitor, my terminals on my left monitor with just enough space for 80 columns and my web browser filling my right screen. I had also set-up some rules to always place them in the correct spots.

I have not yet tried to automatically place the windows in the right spots, but I do have the proportions right. I just had to delete the right frames and resize the one for terminals and, by default, notion remembers this set-up and automatically restores it when I log in.

I will look at creating a special layout for this so I don't have to worry about (accidentally) changing things.

run-or-raise

I found this interesting page about run-or-raise-like functionality for Ion3, which notion is a fork of. This is a little outdated, though, since notion has changed (apparently) the workings of some functions and lua 5.2 introduced the goto keyword, so I had to change it to this:

function oni_match_class(class)
   local result = {}
   ioncore.clientwin_i(
      function (win)
         if class `` win:get_ident().class then
            table.insert(result, win)
            return false
         end
         return true
      end
   )
   return result
end

function xsteve_run_byclass(prog, class)
   local win = oni_match_class(class)[1]
   if win then
      win:goto_()
   else
      ioncore.exec(prog)
   end
end

There is no function to get a list of all the client windows, only a function to iterate over them. For the moment I am only interested in finding the first window with class CLASS, so I return false when a match is found, this stops the iteration process. I also had to use the WRegion.goto_ function, instead of WRegion.goto because of the mentioned change in lua 5.2, but they are the same.

I then only have to bind it:

defbindings("WScreen", {
    -- ...
    submap("Control+Z", {
        -- ...
        kpress("E", "xsteve_run_byclass('emacsclient -ca emacs', 'Emacs')"),
        kpress("W", "xsteve_run_byclass('conkeror', 'Conkeror')"),
        kpress("C", "xsteve_run_byclass('urxvt', 'URxvt')"),
    }),
})

Quoting C-z

One of the coolest things about using a prefix in stumpwm that I have been able to find in precious few other solutions is the ability to send the prefix key to the applications you use, so you don't entirely miss its functionality. In stumpwm this is easy, but in notion its a little more work:

defbindings("WClientWin", {
    -- ...
    submap("Control+Z", {
        -- ...
        kpress("Q", "WClientWin.quote_next(_)"),
    }),
})

This means that I have to type C-z q C-z to send the C-z key to, for instance, Emacs. That a few more keys than I was used to in stumpwm, but at least it's possible.


This blog covers archlinux, avandu, avandu-lua, cask, ci, clark, common-lisp, config, conkeror, diff, dispass, dispass.el, editors, elisp, emacs, eshell, evil, exherbo, experiments, file-synchronization, games, git, github, gnus, hla, html, javascript, lisp, lua, markam, meta, mpd, notion, org-mode, ox-coleslaw, projects, rc, sbcl, small-recent-posts, software, stumpwm, systemd, tasks, tekuti, testing, tips, todo, ttrss, utility, vagrant, vc, vim, visual, wdocker docker docker-compose, wm, wordpress, yoshi-theme

View content from 2016-02, 2015-09, 2015-08, 2014-12, 2014-10, 2014-08, 2014-07, 2014-06, 2014-04, 2014-01, 2013-11, 2013-10, 2013-08, 2013-05, 2013-04, 2013-02, 2013-01