coveragepy
coveragepy copied to clipboard
Add method purge_contexts to CoverageData
This method lets you purge all data for one or more contexts from coverage data, which is useful when updating with newly obtained data for those contexts. I'm using this to entirely replace coverage stats that originated from earlier runs of a particular test or tests, so that the lines reached by the most recent runs are the only ones in the coverage data for those contexts. Otherwise, I found that lines reached by prior runs of those tests might still be marked as reached, even though they no longer are.
This is in context of a fairly complex system that runs (and re-runs) many tests concurrently, collects coverage data from them independently, and then queues those results for merging into a master file, which is then used in various ways.
It seems like 'combine' in general would not remove old lines for a context, but arguably it should before merging in that data. This could possibly be added as an option for that command, but I didn't do it since I ended up calling the API directly and wasn't sure if others would ever need it.
Any insight on this @nedbat ?
I should note that I also ended up working up a version of CoverageData.update() that updates after first removing all contexts that are found in the dataset being merged into self. Doing this as an option in update() may be better, and is also faster because update() has to query the contexts again in any case. Doing purge_contexts() and then update() does gets the contexts twice on the incoming data set, and in some cases that's fairly significant.
Also, in case the motivation wasn't already clear, I'm using a context for each test, so this ends up making sure that a new test run completely replaces all the data for the old run.
I'm happy to revise this or replace it with the option-for-update() idea, as you prefer.