2019-08-05 datefudge and agenda testing

Some time ago, a question was asked on the Org-mode mailing list about a specific kind of task in Org agenda. This made me think about debugging one’s agenda settings. I’ve already written about batch agenda, but one problem with agenda testing is that it is inherently stateful, in one of the worst ways – it depends on the notion of now.

Debugging time-related stuff is hard. (Well, time-related stuff is hard, after all.) It would be great if you could just manipulate Emacs into thinking that the time is some day in the future (or in the past)…

Well, actually, it can be done – and it’s easier than I thought.

One way would be to advise the current-time function. This is potentially dangerous, but should work for Org-related stuff (since Org-mode is written entirely in Emacs Lisp, there should be no danger of current-time being called by a primitive function written in C – but you can never be sure if Org doesn’t use some primitive calling current-time…)

Perhaps a better way would be just to fset the current-time function to something different. This is not very nice, since we usually only want to mess up with it temporarily – but we can use the cl-flet macro which does just that (or just fire a dedicated Emacs instance).

However, I found a completely different way of doing that, which should be much better than all of the above ideas. Why not change the system time? Of course, I do not want to mess up with system time on my machine – but there are two things I can do.

One is to run Emacs in a virtual machine, where I can do all sorts of system-related stuff and not worry about breaking my computer. For VirtualBox, changing the system time of the guest is easy (see here and here).

But there is even a better thing you can do, at least if you run a GNU/Linux system (as I do). There is this little utility called datefudge, which accepts any shell command and calls it, first modifying a few system calls responsible for getting the current time.

Even better, it has two modes of operation: “normal”, where the system clock appears to run as usual (but starting at the given time), and “static”, where the system clock appears to sit still on the time given.

Now I can easily (and reliably) test my agenda settings with a sample “fake” Org file, saying just something along the lines of datefudge 2019-08-05 emacs -Q --batch .... This way, we can even easily have automated tests for Org agenda.

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryOrgMode