From f946baf6c52bfbe6e89114cf3f2bd3538af36dd8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 19 Jan 2021 14:52:27 +0100
Subject: [PATCH] Testsuite: add workaround for crash of MATLAB R2014a with TeX
 option
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The TeX option crashes MATLAB R2014a run with "-nodisplay" option
(as is done from the testsuite). We disable the TeX option in that case.

Since we can’t directly test whether "-nodisplay" has been passed, we test for
the "TOP_TEST_DIR" environment variable, which is set by the testsuite.

Note that it was not tested whether the crash happens with more recent MATLAB
versions, so when OLD_MATLAB_VERSION is increased, one should make a test
before removing this workaround.
---
 matlab/dynare_estimation.m                   | 15 ++++++++++++++-
 matlab/dynare_identification.m               | 17 +++++++++++++++--
 matlab/method_of_moments/method_of_moments.m | 18 ++++++++++++++++++
 matlab/stoch_simul.m                         | 15 ++++++++++++++-
 4 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/matlab/dynare_estimation.m b/matlab/dynare_estimation.m
index 07f1300e16..1f9d804acd 100644
--- a/matlab/dynare_estimation.m
+++ b/matlab/dynare_estimation.m
@@ -12,7 +12,7 @@ function oo_recursive_=dynare_estimation(var_list,dname)
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright (C) 2003-2020 Dynare Team
+% Copyright (C) 2003-2021 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -43,6 +43,19 @@ if ~isempty(strmatch('dsge_prior_weight',M_.param_names))
     options_.dsge_var = 1;
 end
 
+% The TeX option crashes MATLAB R2014a run with "-nodisplay" option
+% (as is done from the testsuite).
+% Since we can’t directly test whether "-nodisplay" has been passed,
+% we test for the "TOP_TEST_DIR" environment variable, which is set
+% by the testsuite.
+% Note that it was not tested whether the crash happens with more
+% recent MATLAB versions, so when OLD_MATLAB_VERSION is increased,
+% one should make a test before removing this workaround.
+if options_.TeX && ~isoctave && matlab_ver_less_than('8.4') && ~isempty(getenv('TOP_TEST_DIR'))
+    warning('Disabling TeX option due to a bug in MATLAB R2014a with -nodisplay')
+    options_.TeX = false;
+end
+
 var_list = check_list_of_variables(options_, M_, var_list);
 options_.varlist = var_list;
 
diff --git a/matlab/dynare_identification.m b/matlab/dynare_identification.m
index c843f1b808..196ae7d5e3 100644
--- a/matlab/dynare_identification.m
+++ b/matlab/dynare_identification.m
@@ -46,7 +46,7 @@ function [pdraws, STO_REDUCEDFORM, STO_MOMENTS, STO_DYNAMIC, STO_si_dDYNAMIC, ST
 %    * skipline
 %    * vnorm
 % =========================================================================
-% Copyright (C) 2010-2020 Dynare Team
+% Copyright (C) 2010-2021 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -66,6 +66,19 @@ function [pdraws, STO_REDUCEDFORM, STO_MOMENTS, STO_DYNAMIC, STO_si_dDYNAMIC, ST
 
 global M_ options_ oo_ bayestopt_ estim_params_
 
+% The TeX option crashes MATLAB R2014a run with "-nodisplay" option
+% (as is done from the testsuite).
+% Since we can’t directly test whether "-nodisplay" has been passed,
+% we test for the "TOP_TEST_DIR" environment variable, which is set
+% by the testsuite.
+% Note that it was not tested whether the crash happens with more
+% recent MATLAB versions, so when OLD_MATLAB_VERSION is increased,
+% one should make a test before removing this workaround.
+if options_.TeX && ~isoctave && matlab_ver_less_than('8.4') && ~isempty(getenv('TOP_TEST_DIR'))
+    warning('Disabling TeX option due to a bug in MATLAB R2014a with -nodisplay')
+    options_.TeX = false;
+end
+
 store_options_ = options_; % store options to restore them at the end
 fname = M_.fname; %model name
 dname = M_.dname; %model name
@@ -949,4 +962,4 @@ end
 
 fprintf('\n==== Identification analysis completed ====\n\n')
 
-options_ = store_options_; %restore options set
\ No newline at end of file
+options_ = store_options_; %restore options set
diff --git a/matlab/method_of_moments/method_of_moments.m b/matlab/method_of_moments/method_of_moments.m
index 5ea218c293..c83bae21be 100644
--- a/matlab/method_of_moments/method_of_moments.m
+++ b/matlab/method_of_moments/method_of_moments.m
@@ -94,6 +94,24 @@ function [oo_, options_mom_, M_] = method_of_moments(bayestopt_, options_, oo_,
 % - [ ] improve check for duplicate moments by using the cellfun and unique functions
 % - [ ] dirname option to save output to different directory not yet implemented
 % - [ ] add analytic_jacobian option for mode_compute 4 and 101
+
+% The TeX option crashes MATLAB R2014a run with "-nodisplay" option
+% (as is done from the testsuite).
+% Since we can’t directly test whether "-nodisplay" has been passed,
+% we test for the "TOP_TEST_DIR" environment variable, which is set
+% by the testsuite.
+% Note that it was not tested whether the crash happens with more
+% recent MATLAB versions, so when OLD_MATLAB_VERSION is increased,
+% one should make a test before removing this workaround.
+if options_.TeX && ~isoctave && matlab_ver_less_than('8.4') && ~isempty(getenv('TOP_TEST_DIR'))
+    warning('Disabling TeX option due to a bug in MATLAB R2014a with -nodisplay')
+    options_.TeX = false;
+end
+if isfield(options_mom_, 'TeX') && options_mom_.TeX && ~isoctave && matlab_ver_less_than('8.4') && ~isempty(getenv('TOP_TEST_DIR'))
+    warning('Disabling TeX option due to a bug in MATLAB R2014a with -nodisplay')
+    options_mom_.TeX = false;
+end
+
 % -------------------------------------------------------------------------
 % Step 0: Check if required structures and options exist
 % -------------------------------------------------------------------------
diff --git a/matlab/stoch_simul.m b/matlab/stoch_simul.m
index 218d081791..3ec1c93392 100644
--- a/matlab/stoch_simul.m
+++ b/matlab/stoch_simul.m
@@ -1,6 +1,6 @@
 function [info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list)
 
-% Copyright (C) 2001-2020 Dynare Team
+% Copyright (C) 2001-2021 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -22,6 +22,19 @@ if isequal(options_.order,0)
     error('stoch_simul:: The order of the Taylor approximation cannot be 0!')
 end
 
+% The TeX option crashes MATLAB R2014a run with "-nodisplay" option
+% (as is done from the testsuite).
+% Since we can’t directly test whether "-nodisplay" has been passed,
+% we test for the "TOP_TEST_DIR" environment variable, which is set
+% by the testsuite.
+% Note that it was not tested whether the crash happens with more
+% recent MATLAB versions, so when OLD_MATLAB_VERSION is increased,
+% one should make a test before removing this workaround.
+if options_.TeX && ~isoctave && matlab_ver_less_than('8.4') && ~isempty(getenv('TOP_TEST_DIR'))
+    warning('Disabling TeX option due to a bug in MATLAB R2014a with -nodisplay')
+    options_.TeX = false;
+end
+
 if M_.exo_nbr==0
     error('stoch_simul:: does not support having no varexo in the model. As a workaround you could define a dummy exogenous variable.')
 end
-- 
GitLab