Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/badges/tests.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 14 additions & 12 deletions code/+matbox/+tasks/codecheckToolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
65 changes: 65 additions & 0 deletions tools/tests/+matboxtools/+unittest/TasksTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<NASGU> 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)
Expand Down Expand Up @@ -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));
Expand Down