Is it possible to dynamically generate groups?
Hi friends, this isn't a bug, but a question;
I'm curious if it's possible to dynamically generate groups, such that we'd be able to generate a name for a group at execution time, based on available file, project, path, and coverage data?
Ideally I'd like to, for example, create sub-groups based on a specific file naming scheme or directory structure, e.g.:
Controllers: app/controllers
Controllers/$1 : app/controllers/$1/
Which could result in a final groups list similar to the following:
Controllers
Controllers/BlogController
Controllers/JobController
Controllers/UserController
etc.
I've attempted to do this by placing additional add_group calls within the "parent" add_group block, but Ruby explicitly blocks adding keys to the hash as it's being iterated upon, and I'm not aware of another facility to access the "current" file outside of an add_group's do block.
Ironically I immediately realized that while I can't add groups from add_group, I can add them from add_filter, e.g.,
add_filter do |src_file|
if src_file.project_filename.include? "app/controllers"
group_name = src_file.project_filename.split(%r{/}, 5)[3]
add_group("Controllers/#{group_name}", "app/controllers/#{group_name}")
end
false
end
The only remaining issue I see is that groups can't be reordered ( basically, sorted ) before final render.
Wait what, what's the problem that means you can't call add_group dynamically in the Simplecov.start block?
I'm not aware of anything that would keep you from:
my_dynamic_groups = # magic
SimpleCov.start do
add_group "Models", "app/models"
add_group "Controllers", "app/controllers"
my_dynamic_groups.each { |name| add_group name }
end
what doesn't work here? :thinking:
That would work, yes, but I don't know the groups ahead of time without manually indexing the folder structure first.
The idea was to be able to leverage SimpleCov's own parsing logic to do the heavy lifting, and be able to do things like making a group for Controllers, and making additional groups for every child folder under the primary Controllers directory
I see.
I'm not sure that's something we want to support. I think building this yourself is rather easy as you can just say like: Dir.entries("app/controllers").select { |name| File.directory?("app/controllers/{#name}")} - and then add those.
I don't think that qualifies as "heavy lifting" - I wouldn't know where to add it in simplecov and how to deal with it.
The only thing I could think of is to give it an option like sub_folders_own_use_case: true or something to automaticcaly generate these but I'm not sure if:
- this happens often enough so that we'd wanna add it
- I really wanna add all sub folders.. should it just be one level deep? 2 levels deep? That one folder should be ignored?
So my guess is that a more explicit approach where people add them themselves is better.
I both agree and disagree - I filed this more for exploration on how it might be possible to implement any part of it, likewise, the usefulness of drilling down heavily depends on the representation of coverage at the end - it's not very useful if your chosen coverage visualizer doesn't support logical grouping or directory style navigation ( which is what I was looking to do, in a way )
The end goal there being to allow for something like a report to let you list out and drill down into subfolders rather than listing a ( potentially large ) amount of all files recursively descendant of the current group's root
@Fire- well what's more important to you - seeing the coverage by group or the better file navigation or both?
@Fire- so do I understand you correctly that ideally you'd want to have a view that basically display you coverage per folder on every level (sub folders) and then also files?