diff --git a/.github/badges/tests.svg b/.github/badges/tests.svg index 0261c9f..865d413 100644 --- a/.github/badges/tests.svg +++ b/.github/badges/tests.svg @@ -1 +1 @@ -tests: 110 passedtests110 passed +tests: 115 passedtests115 passed diff --git a/code/+matbox/+tasks/codecheckToolbox.m b/code/+matbox/+tasks/codecheckToolbox.m index d7d4918..ee6b350 100644 --- a/code/+matbox/+tasks/codecheckToolbox.m +++ b/code/+matbox/+tasks/codecheckToolbox.m @@ -64,21 +64,23 @@ % R2022b and later, just display issues displayIssuesTable(issues.Issues) end + end - % Throw error if unresolved issues are present at specified severity - if options.RequireIssuesResolved - if options.SeverityThreshold == "info" && issueCount.Total > 0 - throwUnresolvedCodeIssuesException() - elseif options.SeverityThreshold == "warning" && (issueCount.Warning > 0 || issueCount.Error > 0) - throwUnresolvedCodeIssuesException() - elseif options.SeverityThreshold == "error" && issueCount.Error > 0 - throwUnresolvedCodeIssuesException() - end + % Throw error if unresolved issues are present at specified severity + if options.RequireIssuesResolved + if options.SeverityThreshold == "info" && issueCount.Total > 0 + throwUnresolvedCodeIssuesException() + elseif options.SeverityThreshold == "warning" && (issueCount.Warning > 0 || issueCount.Error > 0) + throwUnresolvedCodeIssuesException() + elseif options.SeverityThreshold == "error" && issueCount.Error > 0 + throwUnresolvedCodeIssuesException() end + end - if ~nargout - clear issues - end + % Without this, a call with no output and no semicolon displays the + % issues as ans + if ~nargout + clear issues end end diff --git a/tools/tests/+matboxtools/+unittest/TasksTest.m b/tools/tests/+matboxtools/+unittest/TasksTest.m index 8a609bc..d0f04e1 100644 --- a/tools/tests/+matboxtools/+unittest/TasksTest.m +++ b/tools/tests/+matboxtools/+unittest/TasksTest.m @@ -66,6 +66,50 @@ function testCodecheckToolboxCreatesBadgeJson(testCase) str2double(badgeInfo.message)) end + function testCodecheckToolboxThrowsOnUnresolvedIssues(testCase) + sourceFolder = createFolderWithCodeWarning(pwd); + + testCase.verifyError( ... + @() runCodecheck(pwd, sourceFolder, "RequireIssuesResolved", true), ... + "MatBox:CodeIssues:UnresolvedIssues") + end + + function testCodecheckToolboxThrowsWhenShowingIssues(testCase) + sourceFolder = createFolderWithCodeWarning(pwd); + + testCase.verifyError( ... + @() runCodecheck(pwd, sourceFolder, ... + "RequireIssuesResolved", true, "ShowIssues", true), ... + "MatBox:CodeIssues:UnresolvedIssues") + end + + function testCodecheckToolboxIgnoresIssuesBelowThreshold(testCase) + sourceFolder = createFolderWithCodeWarning(pwd); + + testCase.verifyWarningFree( ... + @() runCodecheck(pwd, sourceFolder, ... + "RequireIssuesResolved", true, "SeverityThreshold", "error")) + end + + function testCodecheckToolboxAllowsIssuesByDefault(testCase) + sourceFolder = createFolderWithCodeWarning(pwd); + + testCase.verifyWarningFree(@() runCodecheck(pwd, sourceFolder)) + end + + function testCodecheckToolboxHidesIssuesByDefault(testCase) + sourceFolder = createFolderWithCodeWarning(pwd); + codecheckOptions = {"FoldersToCheck", sourceFolder, ... + "CreateBadge", false, "SaveReport", false}; %#ok Used in evalc + + % No semicolon: an output left in ans would be displayed + displayedText = evalc("matbox.tasks.codecheckToolbox(pwd, codecheckOptions{:})"); + + testCase.verifySubstring(displayedText, "Warnings: 1") + testCase.verifyTrue(~contains(displayedText, "hasCodeWarning.m"), ... + "Expected the issues not to be displayed when ShowIssues is false.") + end + function testWriteBadgeJSONFileOmitsCountsByDefault(testCase) matbox.utility.writeBadgeJSONFile("tests", "1 passed", "green", ... "OutputFolder", pwd) @@ -211,6 +255,27 @@ function testTestedWithBadgeErrorsWhenNoReleasePassed(testCase) end end +function sourceFolder = createFolderWithCodeWarning(projectRootDirectory) +% createFolderWithCodeWarning - Create a folder with one file that has one warning +% +% The file assigns a variable that is never used, which the Code Analyzer +% reports as a single issue of severity "warning". + + sourceFolder = fullfile(projectRootDirectory, "src"); + mkdir(sourceFolder) + + code = sprintf("function hasCodeWarning()\n unusedValue = 1;\nend\n"); + matbox.utility.filewrite(fullfile(sourceFolder, "hasCodeWarning.m"), code); +end + +function runCodecheck(projectRootDirectory, sourceFolder, varargin) +% runCodecheck - Check the given folder without writing a badge or a report + + matbox.tasks.codecheckToolbox(projectRootDirectory, ... + "FoldersToCheck", sourceFolder, "CreateBadge", false, ... + "SaveReport", false, varargin{:}); +end + function setMltbxFileName(projectRootDirectory, mltbxFileName) toolboxInfoFile = fullfile(projectRootDirectory, 'tools', 'MLToolboxInfo.json'); toolboxInfo = jsondecode(fileread(toolboxInfoFile));