I wrote this function to ascend upwards from a path to find the root of a cookbook: https://github.com/berkshelf/berkshelf/blob/master/lib/berkshelf/core_ext/pathname.rb#L14

I think it would be best to figure out where the cookbook is by writing a descending function like this that finds the metadata.json or metadata.rb