Testing rake tasks

Relying on rake task to do some repetitive work is a really common way in most
rails apps. Testing those tasks seems to not be as common so.

Why?

Personally I think this is mainly due to just not being present in the default
tests stack, so as a developer I assume they are not worth testing because nobody
else is. But actually I think a task which is essential to your application
should be tested never the less, so to ease my journey towards better test
coverage for rake tasks in my own apps I decided to create a little helper to
setup just the basic environment and about as important, be there as a reminder
that I should test those tasks.

Personally I come to like minitest to test rails apps, so the
gist is based on
minitest, but can easily be adapted for any other framework. Looking through the
code you’ll find some conventions I personally like to be enforced in my rake
tasks

  • namespace the task under the app name if it directly interacts with the app
  • put the tasks in sub directories under lib/tasks so it does not become a
    junk drawer of code
  • put each task in its own file
  • name the file after the task (common sense I guess but not always true)

so here we go, enjoy!

https://gist.github.com/sideshowcoder/5891019.js

Pressevent alpha, WordPress update tracker

Over my time running and administrating WordPress for various reasons I became
annoyed with always checking which instance is at which version, and should be
updated. Most of the time updating all at once is just not practical, but
keeping installations at an old version is dangerous, so here we go:

Pressevent watches all the WordPress
installs I have and sends me a report every night if updates are available for
any of my installations. Since there is no clean API for this it requires a
plugin to be installed but this should be easy for every WordPress user right
;).

Currently this is still early since it mainly was a quick weekend project to
make my own life easier, but maybe it’s helpful to others.

Enjoy!

ActiveRecordTranslatable is moving towards stable

When I extracted
ActiveRecordTranslatable
from an active project is was a pretty quick thing, but since then I found, since
it is just so barebones easy, that uses pop up outside of the initial
project.

Currently I am in the process of moving it towards a more stable release,
just a couple more things are left to do, but for now at version 0.0.9 is here
with a slimmed down interface and better docs. Find and read the
docs here

post-receive hook and branches

Installing a post-receive hook in a git repository can be used to automate a lot
of things, i.e. initiate deployment, precompile a static page, or similar. Using
a hook is quite easy, simply create an executable file named like the hook inside
the hooks folder. So here we go

Init a bare repository

$ mkdir ~/test.git && cd ~/test.git
$ git init --bare

Init a repository

$ mkdir ~/test && cd ~/test
$ git init
$ git remote add origin ~/test.git

Create a hook

$ touch ~/test.git/hooks/post-receive
$ echo 'echo "I am a post receive hook"' >> ~/test.git/hooks/post-receive
$ chmod +x ~/test.git/hook/post-recive

Pushing to the repository means the hook is executed

$ cd ~/test
$ touch foo.txt
$ git add foo.txt
$ git commit -m 'foo'
$ git push origin master
...
remote: I am a post receive hook
...

To do more fun stuff it is sometimes important to know which branch got pushed,
to do this a couple of things are passed to the hook via stdin, so

https://gist.github.com/sideshowcoder/5751530.js

to detect the branch simply check against the $ref.

Happy hooking ;)

Rails quick tip: generate and destroy

Rails generators are often known to be a one way street. Often people new to
rails think if they generate the wrong thing they are stuck. Not true! So here
we go, happens to me all the time:

$ rails g controller ReportsController index
...
app/controllers/reports_controller_controller.rb
...

Oh right generate just takes the name without the extension. No need to hunt
down the generated files:

$ rails d controller ReportsController index
...
$ rails g controller Reports index
...
app/controllers/reports_controller.rb
...

Just run d for destroy and generate again. Just thought to point this out!

Running minitest with zeus

Since minitest is supposed to be the new default in Rails 4, and I am really
fund of it I recently switched a pet project of mine over. One problem I ran
into was using zeus to speed up my test flow. So there are a couple of things to
look out for when using zeus with minitest-rails.

Rake and zeus don’t match

In most cases when using minitest people will run their tests via rake. This
actually makes a lot of sense normally since rake offers even a special test
task for it, but with zeus we have a different story here. Since rake will
actually load in the environment when running the tests, you don’t actually get
a lot of benefits when running via zeus, so make sure you run your tests via the
provided test task from zeus, which is base on m.

My tests run twice?

Minitest installs a test runner, at the exit hook of the ruby process, so your
tests will run, and then they run on exit again. This is weird, and should only
happen when autorun is required but somewhere deep in rails this is done, so you
need to make sure it doesn’t happen. There is a nice discussion around this
issue with some proposed fixes here
as well as a pull request by me.

So here you go enjoy minitest and zeus

Canned has been updated

I have used canned for some time now
to aid my development of apis, and over time some things arouse which made it
worth to add some features. If you want to know more about canned check out
the introduction

Changes canned 0.1.0

  • better error codes, a not found is actually 404 now not 400
  • better content type handling, html is now returned as text/html
  • support for comments in json, so documenting api responses can be done inline

interested install the new version via

$ npm install canned

And checkout the the repo

Daemons and output in ruby

Backgrounding a process in ruby is easy, just add

Process.daemon

This works great but by doing so you will actually lose any output since daemon
will redirect stdout and stderr to /dev/null by default. Depending on how you
handle logging this might actually not be a great idea, I personally prefer
redirecting the output of a daemon process myself so I can add logging easily.

This is easily solve so by passing both parameters for daemon, first to either
do or don’t change the working dir to / and the second to not redirect stdout
and stderr.

Process.daemon true, true

Just a nice thing which is quickly overlooked.

Using zeus without rails

At the last rubyshift the question came up if
zeus could be used without rails, and how to do
it. At the time I kind of expected it to be but wasn’t sure if there are any
complexities which I didn’t see at the moment because I never tried it. So
trying it out it actually works allmost as expected, the only thing needed is a
modification to the custom_plan.rb file which defines how the preloading
actually works. So just as a quick example I
setup a repository which
defines 2 tasks

$ zeus rake
$ zeus foo

This should get everybody started, as the tasks are pretty obvious and easily
modified for your needs.