Skip to content
Merged
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
80 changes: 80 additions & 0 deletions plotly/Test_plotlysetup.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
classdef Test_plotlysetup < PlotlyTestCase
methods
function testPlotlysetupOffline(tc)
% finds plotly/ from an unrelated folder and writes no startup
% file; the URL cannot resolve, so the real bundle is never replaced
sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok<NASGU>
if is_octave()
pathdefs = {};
else
pathdefs = which('pathdef.m', '-all');
end
before = Test_plotlysetup.fileContents(pathdefs);

output = evalc(['try, plotlysetup_offline(', ...
'''https://plotly.invalid/plotly.min.js''); catch, end']);

tc.verifyTrue(isempty(strfind(output, 'plotly:notFound')), output);
tc.verifyTrue(~isempty(strfind(output, 'Now downloading')), output);
tc.verifyTrue(isequal(Test_plotlysetup.fileContents(pathdefs), before), ...
'a pathdef.m on the search path changed');
tc.verifyNumElements(dir(fullfile(getenv('HOME'), '.octaverc')), 0);
end

function testPrintPlotlyPathHint(tc)
if is_octave()
startupFile = '~/.octaverc';
else
startupFile = 'startup.m (in the folder that userpath returns)';
end

output = evalc('printplotlypathhint(''/opt/it''''s here/plotly'')');

tc.verifyTrue(~isempty(strfind(output, ...
['add this line to your ' startupFile ':'])), output);
tc.verifyTrue(~isempty(strfind(output, ...
'addpath(genpath(''/opt/it''''s here/plotly''));')), output);
end
end

methods (Static)
function sandbox = enterSetupSandbox()
%-run the setup scripts, which live in the repository root,
%-from an empty folder that is also HOME; the returned object
%-restores folder, HOME and the repository root's absence from
%-the path when it goes out of scope (restoring the whole path
%-with path() breaks the running test object in Octave)-%
oldFolder = pwd();
oldHome = getenv('HOME');
repoRoot = fileparts(fileparts(which('fig2plotly')));
rootWasOnPath = ~isempty(strfind([pathsep path() pathsep], ...
[pathsep repoRoot pathsep]));
home = tempname();
mkdir(home);
cd(home);
setenv('HOME', home);
addpath(repoRoot);
sandbox = onCleanup(@() Test_plotlysetup.leaveSetupSandbox( ...
oldFolder, oldHome, home, repoRoot, rootWasOnPath));
end

function leaveSetupSandbox(oldFolder, oldHome, home, repoRoot, rootWasOnPath)
if ~rootWasOnPath
rmpath(repoRoot);
end
cd(oldFolder);
setenv('HOME', oldHome);
if is_octave()
confirm_recursive_rmdir(false, 'local');
end
try rmdir(home, 's'); catch; end
end

function contents = fileContents(files)
contents = cell(size(files));
for i = 1:numel(files)
contents{i} = fileread(files{i});
end
end
end
end
59 changes: 0 additions & 59 deletions plotly/plotly_setup_aux/addplotlystartup.m

This file was deleted.

96 changes: 0 additions & 96 deletions plotly/plotly_setup_aux/plotlycleanup.m

This file was deleted.

13 changes: 13 additions & 0 deletions plotly/plotly_setup_aux/printplotlypathhint.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function printplotlypathhint(plotlyFolderPath)
% prints the line that keeps plotlyFolderPath on the search path in
% future sessions; setup leaves editing startup files to the user
if is_octave
startupFile = '~/.octaverc';
else
startupFile = 'startup.m (in the folder that userpath returns)';
end
fprintf(['\nPlotly is on the search path for this session. To keep it ', ...
'there in future sessions,\nadd this line to your %s:\n\n', ...
' addpath(genpath(''%s''));\n'], ...
startupFile, strrep(plotlyFolderPath, '''', ''''''));
end
3 changes: 2 additions & 1 deletion plotly/testing/runplotlytests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function runplotlytests(varargin)
%-every test method of the class. Without arguments the default
%-suites run-%
if nargin == 0
args = {'Test_m2json', 'Test_plotlyfig', 'Test_plotlyfig_perf'};
args = {'Test_m2json', 'Test_plotlyfig', 'Test_plotlyfig_perf', ...
'Test_plotlysetup'};
else
args = varargin;
end
Expand Down
13 changes: 4 additions & 9 deletions plotlysetup_offline.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin)
% CALL: plotlysetup_offline(plotly_bundle_url);
% WHERE: plotly_bundle_url is the plotly bundle url, e.g. https://cdn.plot.ly/plotly-2.35.3.min.js
% If no argument is provided, the default https://cdn.plot.ly/plotly-2.35.3.min.js is used.
% [1] adds plotly api to search path and saves it via savepath
% [1] adds plotly api to search path and prints how to keep it there

%DEFAULT OUTPUT
exception.message = '';
Expand All @@ -24,14 +24,13 @@ function plotlysetup_offline(plotly_bundle_url, varargin)

try
%check to see if plotly is in the searchpath
plotlysetupPath = which('plotlysetup');
plotlyFolderPath = fullfile(fileparts(plotlysetupPath),'plotly');
plotlyFolderPath = fullfile(fileparts(mfilename('fullpath')),'plotly');
%if it was not found
if (strcmp(genpath(plotlyFolderPath),''))
error('plotly:notFound',...
['\n\nShoot! It looks like MATLAB is having trouble finding the current version ' ...
'\nof Plotly. Please make sure that the Plotly API folder is in the same ' ...
'\ndirectory as plotlysetup.m. Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']);
'\ndirectory as plotlysetup_offline.m. Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']);
end
%add Plotly API MATLAB Library to search path
addpath(genpath(plotlyFolderPath));
Expand All @@ -40,11 +39,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin)
return
end

% Save search path so Plotly remains available in future sessions
try
savepath;
catch
end
printplotlypathhint(plotlyFolderPath);

%get offline bundle
fprintf('\nNow downloading the plotly offline bundle ...');
Expand Down
Loading
Loading