@@ -29,6 +29,49 @@ def __init__(self, message=None):
2929 super ().__init__ (message )
3030
3131
32+ def is_jupyter_testing_enabled ():
33+ """
34+ Checks if testing mode is enabled
35+
36+ Returns:
37+ bool: True if MWI_JUPYTER_TEST environment variable is set to 'true'
38+ else False
39+ """
40+
41+ return os .environ .get ("MWI_JUPYTER_TEST" , "false" ).lower () == "true"
42+
43+
44+ def start_matlab_proxy_for_testing ():
45+ """
46+ Only used for testing purposes. Gets the matlab-proxy server configuration
47+ from environment variables and mocks the 'start_matlab_proxy' function
48+
49+ Returns:
50+ Tuple (string, string, dict):
51+ url (string): Complete URL to send HTTP requests to matlab-proxy
52+ base_url (string): Complete base url for matlab-proxy obtained from tests
53+ headers (dict): Empty dictionary
54+ """
55+
56+ import matlab_proxy .util .mwi .environment_variables as mwi_env
57+
58+ # These environment variables are being set by tests, using dictionary lookup
59+ # instead of '.getenv' to make sure that the following line fails with the
60+ # Exception 'KeyError' in case the environment variables are not set
61+ matlab_proxy_base_url = os .environ [mwi_env .get_env_name_base_url ()]
62+ matlab_proxy_app_port = os .environ [mwi_env .get_env_name_app_port ()]
63+
64+ # '127.0.0.1' is used instead 'localhost' for testing since Windows machines consume
65+ # some time to resolve 'localhost' hostname
66+ url = "{protocol}://127.0.0.1:{port}{base_url}" .format (
67+ protocol = "http" ,
68+ port = matlab_proxy_app_port ,
69+ base_url = matlab_proxy_base_url ,
70+ )
71+ headers = {}
72+ return url , matlab_proxy_base_url , headers
73+
74+
3275def start_matlab_proxy ():
3376 """
3477 Start matlab-proxy registered with the jupyter server which started the
@@ -45,6 +88,12 @@ def start_matlab_proxy():
4588 headers (dict): HTTP headers required while sending HTTP requests to matlab-proxy
4689 """
4790
91+ # If jupyter testing is enabled, then a standalone matlab-proxy server would be
92+ # launched by the tests and kernel would expect the configurations of this matlab-proxy
93+ # server which is provided through environment variables to 'start_matlab_proxy_for_testing'
94+ if is_jupyter_testing_enabled ():
95+ return start_matlab_proxy_for_testing ()
96+
4897 nb_server_list = []
4998
5099 # The matlab-proxy server, if running, could have been started by either
@@ -176,7 +225,7 @@ def __init__(self, *args, **kwargs):
176225 # Call superclass constructor to initialize ipykernel infrastructure
177226 super (MATLABKernel , self ).__init__ (* args , ** kwargs )
178227 try :
179- # Start matlab-proxy using the jupyter-matlab-proxy registered endpoint
228+ # Start matlab-proxy using the jupyter-matlab-proxy registered endpoint.
180229 self .murl , self .server_base_url , self .headers = start_matlab_proxy ()
181230 (
182231 self .is_matlab_licensed ,
0 commit comments