|
| 1 | +classdef ExampleDrivenTesterTask < matlab.buildtool.Task |
| 2 | + % Buildtool task to run example scripts with optional test & coverage reports. |
| 3 | + % Inputs: |
| 4 | + % - Folders: string array of M-script locations |
| 5 | + % Optional Inputs: |
| 6 | + % - CreateTestReport (logical) |
| 7 | + % - TestReportFormat (string) |
| 8 | + % - ReportOutputFolder (string) |
| 9 | + % - CodeCoveragePlugin (object) |
| 10 | + |
| 11 | + properties |
| 12 | + Folders (1,:) string |
| 13 | + CreateTestReport (1,1) logical |
| 14 | + TestReportFormat (1,1) string |
| 15 | + OutputPath (1,1) string |
| 16 | + CodeCoveragePlugin |
| 17 | + end |
| 18 | + |
| 19 | + methods |
| 20 | + function task = ExampleDrivenTesterTask(folders, options) |
| 21 | + % Constructor |
| 22 | + arguments |
| 23 | + folders (1,:) string |
| 24 | + options.CreateTestReport (1,1) logical = true |
| 25 | + options.TestReportFormat (1,1) string {mustBeMember(options.TestReportFormat,["html", "pdf", "docx", "xml"])} = "html" |
| 26 | + options.OutputPath(1,1) string = "reports_" + char(datetime('now', 'Format', 'yyyyMMdd_HHmmss')) |
| 27 | + options.CodeCoveragePlugin = [] |
| 28 | + end |
| 29 | + |
| 30 | + task.Description = "Run published examples"; |
| 31 | + task.Inputs = folders; |
| 32 | + |
| 33 | + % Basic validation |
| 34 | + % mustBeMember(options.TestReportFormat, ["html", "pdf", "docx", "xml"]); |
| 35 | + for f = folders |
| 36 | + if ~isfolder(f) |
| 37 | + error("ExampleDrivenTesterTask:FolderNotFound", ... |
| 38 | + "Folder not found: %s", f); |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + task.Folders = folders; |
| 43 | + task.CreateTestReport = options.CreateTestReport; |
| 44 | + task.TestReportFormat = options.TestReportFormat; |
| 45 | + task.OutputPath= options.OutputPath; |
| 46 | + task.CodeCoveragePlugin= options.CodeCoveragePlugin; |
| 47 | + |
| 48 | + if task.CreateTestReport |
| 49 | + task.Outputs = task.OutputPath; |
| 50 | + else |
| 51 | + task.Outputs = string.empty; |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + methods (TaskAction, Sealed, Hidden) |
| 57 | + |
| 58 | + function runExampleTests(task, ~) |
| 59 | + if task.CreateTestReport && ~isfolder(task.OutputPath) |
| 60 | + mkdir(task.OutputPath); |
| 61 | + end |
| 62 | + |
| 63 | + if isempty(task.CodeCoveragePlugin) |
| 64 | + examplesRunner = examplesTester( ... |
| 65 | + task.Folders, ... |
| 66 | + CreateTestReport = task.CreateTestReport, ... |
| 67 | + TestReportFormat = task.TestReportFormat, ... |
| 68 | + OutputPath = task.OutputPath); |
| 69 | + else |
| 70 | + % Pass CodeCoveragePlugin through when provided |
| 71 | + examplesRunner = examplesTester( ... |
| 72 | + task.Folders, ... |
| 73 | + CreateTestReport = task.CreateTestReport, ... |
| 74 | + TestReportFormat = task.TestReportFormat, ... |
| 75 | + OutputPath = task.OutputPath, ... |
| 76 | + CodeCoveragePlugin = task.CodeCoveragePlugin); |
| 77 | + end |
| 78 | + examplesRunner.executeTests; |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments