|
| 1 | +package mill.pythonlib |
| 2 | + |
| 3 | +import mill._ |
| 4 | + |
| 5 | +/** |
| 6 | + * Code coverage via Python's [coverage](https://coverage.readthedocs.io/) |
| 7 | + * package. |
| 8 | + * |
| 9 | + * ** Note that this is a helper trait, and you are unlikely to use this |
| 10 | + * directly. If you're looking for including coverage across tests in your |
| 11 | + * project, please use [[CoverageTests]] instead! ** |
| 12 | + * |
| 13 | + * If you do want to use this module directly, please be aware that analyzing |
| 14 | + * code coverage introduces "non-linear" changes to the execution task flow, and |
| 15 | + * you will need to respect the following contract: |
| 16 | + * |
| 17 | + * 1. This trait defines a location where coverage data must be saved. |
| 18 | + * |
| 19 | + * 2. You need to define a `coverageTask` which is responsible for creating |
| 20 | + * coverage data in the before mentioned location. How this is done is up to |
| 21 | + * you. As an example, the [[CoverageTests]] module modifies `pythonOptions` |
| 22 | + * to prepend a `-m coverage` command line argument. |
| 23 | + * |
| 24 | + * 3. This trait defines methods that will a) invoke the coverage task b) assume |
| 25 | + * report data exists in the predefined location c) use that data to generate |
| 26 | + * coverage reports. |
| 27 | + */ |
| 28 | +trait CoverageModule extends PythonModule { |
| 29 | + |
| 30 | + override def pythonToolDeps = Task { |
| 31 | + super.pythonToolDeps() ++ Seq("coverage>=7.6.10") |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * The *location* (not the ref), where the coverage report lives. This |
| 36 | + * intentionally does not return a PathRef, since it will be populated from |
| 37 | + * other places. |
| 38 | + */ |
| 39 | + def coverageDataFile: T[os.Path] = Task { T.dest / "coverage" } |
| 40 | + |
| 41 | + /** |
| 42 | + * The task to run to generate the coverage report. |
| 43 | + * |
| 44 | + * This task must generate a coverage report into the output directory of |
| 45 | + * [[coverageDataFile]]. It is required that this file be readable as soon |
| 46 | + * as this task returns. |
| 47 | + */ |
| 48 | + def coverageTask: Task[_] |
| 49 | + |
| 50 | + private case class CoverageReporter( |
| 51 | + interp: os.Path, |
| 52 | + env: Map[String, String] |
| 53 | + ) { |
| 54 | + def run(command: String, args: Seq[String]): Unit = |
| 55 | + os.call( |
| 56 | + ( |
| 57 | + interp, |
| 58 | + "-m", |
| 59 | + "coverage", |
| 60 | + command, |
| 61 | + args |
| 62 | + ), |
| 63 | + env = env, |
| 64 | + stdout = os.Inherit |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + private def coverageReporter = Task.Anon { |
| 69 | + CoverageReporter( |
| 70 | + pythonExe().path, |
| 71 | + Map( |
| 72 | + "COVERAGE_FILE" -> coverageDataFile().toString |
| 73 | + ) |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Generate a coverage report. |
| 79 | + * |
| 80 | + * This command accepts arguments understood by `coverage report`. For |
| 81 | + * example, you can cause it to fail if a certain coverage threshold is not |
| 82 | + * met: `mill coverageReport --fail-under 90` |
| 83 | + */ |
| 84 | + def coverageReport(args: String*): Command[Unit] = Task.Command { |
| 85 | + coverageTask() |
| 86 | + coverageReporter().run("report", args) |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Generate a HTML version of the coverage report. |
| 91 | + */ |
| 92 | + def coverageHtml(args: String*): Command[Unit] = Task.Command { |
| 93 | + coverageTask() |
| 94 | + coverageReporter().run("html", args) |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Generate a JSON version of the coverage report. |
| 99 | + */ |
| 100 | + def coverageJson(args: String*): Command[Unit] = Task.Command { |
| 101 | + coverageTask() |
| 102 | + coverageReporter().run("json", args) |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Generate an XML version of the coverage report. |
| 107 | + */ |
| 108 | + def coverageXml(args: String*): Command[Unit] = Task.Command { |
| 109 | + coverageTask() |
| 110 | + coverageReporter().run("xml", args) |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Generate an LCOV version of the coverage report. |
| 115 | + */ |
| 116 | + def coverageLcov(args: String*): Command[Unit] = Task.Command { |
| 117 | + coverageTask() |
| 118 | + coverageReporter().run("lcov", args) |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +/** Analyze code coverage, starting from tests. */ |
| 124 | +trait CoverageTests extends CoverageModule with TestModule { |
| 125 | + |
| 126 | + override def pythonOptions = Task { |
| 127 | + Seq("-m", "coverage", "run", "--data-file", coverageDataFile().toString) ++ |
| 128 | + super.pythonOptions() |
| 129 | + } |
| 130 | + |
| 131 | + override def coverageTask = testCached |
| 132 | + |
| 133 | +} |
0 commit comments