From 586030d9702a03121e57a4326124b248732342e5 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Fri, 2 Jan 2015 21:04:36 +0100 Subject: Add old posts --- testing-with-lua.post | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 testing-with-lua.post (limited to 'testing-with-lua.post') diff --git a/testing-with-lua.post b/testing-with-lua.post new file mode 100644 index 0000000..96ac477 --- /dev/null +++ b/testing-with-lua.post @@ -0,0 +1,121 @@ +;;;;; +title: Testing with Lua +tags: projects, lua, testing, ci, avandu-lua +date: 2014-06-30 23:32 +format: md +;;;;; + +Last time I wrote about my project +[avandu-lua](https://ryuslash.org/blog/?p=51) 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: + +