{"id":983,"date":"2016-04-19T16:38:01","date_gmt":"2016-04-19T23:38:01","guid":{"rendered":"http:\/\/angryweasel.com\/blog\/?p=983"},"modified":"2016-04-19T16:38:01","modified_gmt":"2016-04-19T23:38:01","slug":"filling-a-gap-in-istanbul-coverage","status":"publish","type":"post","link":"https:\/\/angryweasel.com\/blog\/filling-a-gap-in-istanbul-coverage\/","title":{"rendered":"Filling a gap in Istanbul coverage"},"content":{"rendered":"<p>I\u2019m at no loss for blog material, but have been short on time (that\u2019s not going to change, so I\u2019ll need to tweak priorities). But\u2026I wanted to write something a bit different&nbsp; from normal in case anyone else ever needs to solve this specific problem (or if anyone else knows that this problem already has an even better solution).<\/p>\n<p>Our team uses a tool called <a href=\"https:\/\/github.com\/gotwarlost\/istanbul\">Istanbul<\/a> to measure code coverage. It generates a report that looks sort of like this (minus the privacy scribbling).<a href=\"http:\/\/angryweasel.com\/blog\/wp-content\/uploads\/2016\/04\/image.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/angryweasel.com\/blog\/wp-content\/uploads\/2016\/04\/image_thumb.png\" width=\"601\" height=\"51\"\/><\/a><a href=\"http:\/\/angryweasel.com\/blog\/wp-content\/uploads\/2016\/04\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/angryweasel.com\/blog\/wp-content\/uploads\/2016\/04\/image_thumb-1.png\" width=\"597\" height=\"137\"\/><\/a><\/p>\n<blockquote>\n<p>For those who don\u2019t know me, I feel compelled to once again share that I think Code Coverage is a wonderful tool, but a horrible metric. Driving coverage numbers up purely for the sake of getting a higher number is idiotic and irresponsible. However, the value of discovering untested and unreachable code is invaluable, and dismissing the tool entirely can be worse than using the measurements incorrectly.<\/p>\n<\/blockquote>\n<h4>The Missing Piece<\/h4>\n<p>Istanbul shows all up coverage for our web app (about 600 files in 300 or so directories). What I wanted to do, was to break down coverage by feature team as well. The \u201celegant\u201d solution would be to create a map of files to features, then add code to the Istanbul reporter to add the feature team to each file \/ directory, and then modify the table output to include the ability to filter by team (or create separate reports by team).<\/p>\n<p>I don\u2019t have time for the elegant solution (but here\u2019s where someone can tell me if it already exists).<\/p>\n<h4>The (or \u201cMy\u201d) Solution<\/h4>\n<p>This seems like a job for Excel, so first, I looked to see if Istanbul had CSV as a reporter format (it doesn\u2019t). It does, however output json and xml, so I figured a quick and dirty solution was possible.<\/p>\n<p>The first thing I did was assign a team owner to each code directory. I pulled the list of directories from the Istanbul report (I copied from the html, but I could have pulled from the xml as well), and then used excel to create a CSV file with file and owner. I could figure out a team owner for over 90% of the files from the name (thanks to reasonable naming conventions!), and I used git log to discover the rest. I ended up with a format that looked like this:<\/p>\n<p><code><font size=\"2\">app.command-foo,SomeTeam<br \/>app.components.a,SomeTeam<br \/>app.components.b,AnotherTeam<br \/>app.components.c,SomeTeam<br \/>app.components.d,SomeOtherTeam<\/font><\/code>  <\/p>\n<p>Then it was a matter of parsing the coverage xml created by Istanbul and making a <em>new<\/em> CSV with the data I cared about (directory, coverage percentage, statements, and statements hit). The latter two are critical, because I would need to recalculate coverage per team.<\/p>\n<p>There was a time (like my first 20+ years in software) where a batch file was my answer for almost anything, but lately \u2013 and especially in this case \u2013 a bit of powershell was the right tool for the job.<\/p>\n<p>The pseudo code was pretty much:<\/p>\n<ul>\n<li>Load the xml file into a PS object<\/li>\n<li>Walk the xml nodes to get the coverage data for a node<\/li>\n<li>Load a map file from a csv<\/li>\n<li>Use the map and node information to create a new csv<\/li>\n<\/ul>\n<p>Hacky, yet effective.<\/p>\n<p>I posted the whole script on github <a href=\"https:\/\/github.com\/angryweasel\/IstanbulCoverageMap\/\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m at no loss for blog material, but have been short on time (that\u2019s not going to change, so I\u2019ll need to tweak priorities). But\u2026I wanted to write something a bit different&nbsp; from normal in case anyone else ever needs to solve this specific problem (or if anyone else knows that this problem already has&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-983","post","type-post","status-publish","format-standard","hentry","category-allposts"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/posts\/983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/comments?post=983"}],"version-history":[{"count":0,"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/posts\/983\/revisions"}],"wp:attachment":[{"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/media?parent=983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/categories?post=983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/angryweasel.com\/blog\/wp-json\/wp\/v2\/tags?post=983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}