Skip to content

Commit

Permalink
Improves the custom file conflict warning (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenNneji authored Feb 18, 2025
1 parent b8779bf commit e3c5909
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
26 changes: 18 additions & 8 deletions API/projectClass/customFileClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
% models, backgrounds or resolutions.
properties (SetAccess = private, Hidden = true)
wrappers = {}
canShowWarning = false; % ensures conflict warning only shows once if no change occurs
end

properties(Access = private, Constant, Hidden)
invalidLanguageMessage = sprintf('Language must be a supportedLanguages enum or one of the following strings (%s)', ...
strjoin(supportedLanguages.values(), ', '))
strjoin(supportedLanguages.values(), ', '))

end

methods
Expand Down Expand Up @@ -66,14 +68,14 @@ function delete(obj)
newName = inputs{1};

if ~isText(newName)
throw(exceptions.invalidType('First value must be unique name identifer (text)'));
throw(exceptions.invalidType('First value must be unique name identifier (text)'));
end
% Check length of added data
switch length(inputs)
case 1
case 2

% Two inputs suppled - assume both name and filename supplied;
% Two inputs supplied - assume both name and filename supplied;
newName = inputs{1};
newFile = inputs{2};

Expand Down Expand Up @@ -125,7 +127,8 @@ function delete(obj)
newLang = validateOption(newLang, 'supportedLanguages', obj.invalidLanguageMessage).value;
newFile = obj.addFileExtension(newFile, newLang);
newFunc = obj.validateFunctionName(newFile, newFunc, newLang);
obj.addRow(newName, newFile, newFunc, newLang, obj.validatePath(newPath));
obj.addRow(newName, newFile, newFunc, newLang, obj.validatePath(newPath));
obj.canShowWarning = true;
end

function obj = removeCustomFile(obj, row)
Expand All @@ -145,7 +148,7 @@ function delete(obj)
% "Name", "Filename", "Language", "Path" and "functionName" if
% applicable.
%
% customFiles.setcustomFile(1, 'Name', 'New Name',...
% customFiles.setCustomFile(1, 'Name', 'New Name',...
% 'Language', 'Octave')
customNames = obj.getNames;

Expand Down Expand Up @@ -183,6 +186,7 @@ function delete(obj)
obj.varTable{row, 4} = {results.language};
obj.varTable{row, 5} = {obj.validatePath(results.path)};
obj.varTable{row, 3} = {obj.validateFunctionName(results.filename, results.functionName, results.language)};
obj.canShowWarning = true;
end

function displayTable(obj)
Expand Down Expand Up @@ -268,10 +272,16 @@ function displayTable(obj)
if isempty(foundPath)
msg = 'The Matlab custom file (%s) is not on the search path. Add the file to path and check using "which(%s)".';
throw(exceptions.invalidPath(sprintf(msg, strrep(libpath, '\', '/'), functionName)));
elseif ~isempty(thisPath) && ~strcmp(foundPath, libpath)
elseif ~isempty(thisPath) && ~strcmp(foundPath, libpath) && isfile(libpath) && obj.canShowWarning
% This warning should only show if there is a difference between the specified custom file and
% the file on the search path. If the specified file does not exist the one found in the search path will
% be used with no warning.
msg = ['The Matlab custom file (%s) on the search path does not match the specified path (%s). ' ...
'The file on the serach path will be used, if this is expected ignore this warning'];
warning(msg, strrep(foundPath, '\', '/'), strrep(libpath, '\', '/'));
'The file on the search path will be used, if this is expected ignore this warning, ' ...
'otherwise set the current directory to the directory with the desired file i.e. ' ...
'cd("%s")'];
warning(msg, strrep(foundPath, '\', '/'), strrep(libpath, '\', '/'), strrep(what(thisPath).path, '\', '/'));
obj.canShowWarning = false;
end
else
if ~exist(libpath, 'file')
Expand Down
14 changes: 11 additions & 3 deletions tests/testCustomFileClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,17 @@ function testToStruct(testCase)
[~, handle, ~] = fileparts(testCase.exampleClass.varTable{i, 2});
testCase.verifyEqual(string(fileStruct.files{i}), handle);
end
old = testCase.exampleClass.varTable{1, 5};
testCase.exampleClass.varTable{1, 5} = {'../..'};
testCase.verifyWarning(@() testCase.exampleClass.toStruct(), '');
import matlab.unittest.fixtures.TemporaryFolderFixture
import matlab.unittest.fixtures.CurrentFolderFixture
fixture = testCase.applyFixture(TemporaryFolderFixture);
filename = fullfile(fixture.Folder, 'DPPCCustomXY.m');
fid = fopen(filename, 'w');
testCase.addTeardown(@fclose, fid)

old = testCase.exampleClass.varTable{1, 5};
testCase.exampleClass.setCustomFile(1, 'path', fixture.Folder);
testCase.verifyWarning(@() testCase.exampleClass.toStruct(), '', 'CustomFile conflict warning should be triggered');
testCase.verifyWarningFree(@() testCase.exampleClass.toStruct(), 'CustomFile conflict warning should be triggered only once');
testCase.exampleClass.varTable{1, 5} = old;
testCase.exampleClass.varTable{1, 3} = {'randomName'};
testCase.verifyError(@() testCase.exampleClass.toStruct(), exceptions.invalidPath.errorID);
Expand Down

0 comments on commit e3c5909

Please sign in to comment.