Fix ensure_dir: check that path is actually a directory path

Update #874 request, 
 comment and tests added, used develop branch