Will this work correctly if `dirname` already ends with a slash?

I think it will, but I should double-check before merging/releasing.  A unit test would perhaps be good.