Commit 3cf2faf0 authored by carlosperate's avatar carlosperate

Save load sketch action in settings file to remember user's default.

compilersettings.py uses better python variables names.
Unit test for the ide load option added (still a lot of work left for unit testing in general).
parent fa5340b4
......@@ -150,9 +150,15 @@ Ardublockly.ideButtonLeftAction = Ardublockly.ideSendOpen;
/** Initialises the IDE buttons with the default option from the server. */
Ardublockly.initialiseIdeButtons = function() {
ArdublocklyServer.requestIdeOptions(function(data) {
Ardublockly.setIdeHtml(data);
Ardublockly.setIdeSettings();
ArdublocklyServer.requestIdeOptions(function(jsonResponse) {
if (jsonResponse != null) {
var parsedJson = JSON.parse(jsonResponse);
// "response_type" : "settings_board",
// "element" : "dropdown",
// "options" : [ {"value" : "XXX", "text" : "XXX"}, ...]
// "selected": "selected key"}
Ardublockly.changeIdeButtons(parsedJson.selected);
} // else Null: Ardublockly server is not running, do nothing
});
};
......
This diff is collapsed.
......@@ -207,12 +207,12 @@ def load_arduino_cli(sketch_path=None):
error = 'The compiler directory has not been set.\n\r' + \
'Please set it in the Settings.'
else:
if not ServerCompilerSettings().launch_IDE_option:
if not ServerCompilerSettings().load_ide_option:
success = False
conclusion = 'What should we do with the Sketch?'
error = 'The launch IDE option has not been set.n\r' + \
'Please select an IDE option in the Settings.'
elif ServerCompilerSettings().launch_IDE_option == 'upload':
elif ServerCompilerSettings().load_ide_option == 'upload':
if not ServerCompilerSettings().get_serial_port_flag():
success = False
conclusion = 'Serial Port unavailable'
......@@ -230,7 +230,7 @@ def load_arduino_cli(sketch_path=None):
if success:
# Concatenates the CLI command and execute if the flags are valid
cli_command = [ServerCompilerSettings().compiler_dir]
if ServerCompilerSettings().launch_IDE_option == 'upload':
if ServerCompilerSettings().load_ide_option == 'upload':
conclusion = 'Successfully Uploaded Sketch'
cli_command.append('--upload')
cli_command.append('--port')
......@@ -238,7 +238,7 @@ def load_arduino_cli(sketch_path=None):
cli_command.append('--board')
cli_command.append(
ServerCompilerSettings().get_arduino_board_flag())
elif ServerCompilerSettings().launch_IDE_option == 'verify':
elif ServerCompilerSettings().load_ide_option == 'verify':
conclusion = 'Successfully Verified Sketch'
cli_command.append('--verify')
cli_command.append(sketch_path)
......@@ -246,7 +246,7 @@ def load_arduino_cli(sketch_path=None):
print('\n\rCLI command:')
print(cli_command)
if ServerCompilerSettings().launch_IDE_option == 'open':
if ServerCompilerSettings().load_ide_option == 'open':
# Launch Arduino IDE in a subprocess without blocking server
subprocess.Popen(cli_command, shell=False)
conclusion = 'Sketch opened in IDE'
......@@ -465,7 +465,7 @@ def get_serial_ports():
# Launch IDE settings #
#######################
def set_load_ide_only(new_value):
ServerCompilerSettings().launch_IDE_option = new_value
ServerCompilerSettings().load_ide_option = new_value
return get_load_ide_only()
......@@ -484,9 +484,9 @@ def get_load_ide_only():
'element': 'dropdown',
'options': []}
#TODO: Check for None, however won't happen because static dict in settings
ide_options = ServerCompilerSettings().get_launch_ide_options()
ide_options = ServerCompilerSettings().get_load_ide_options()
for key in ide_options:
json_data['options'].append(
{'value': key, 'display_text': ide_options[key]})
json_data.update({'selected': ServerCompilerSettings().launch_IDE_option})
json_data.update({'selected': ServerCompilerSettings().load_ide_option})
return json.dumps(json_data)
......@@ -8,8 +8,9 @@
#
from __future__ import unicode_literals, absolute_import
import os
import unittest
import sys
import mock
import unittest
try:
from ardublocklyserver.compilersettings import ServerCompilerSettings
......@@ -25,9 +26,32 @@ class ServerCompilerSettingsTestCase(unittest.TestCase):
"""
Tests for ServerCompilerSettings
"""
#
# Helper functions
#
def get_default_settings_file_dir(self):
# The default location from the ServerCompilerSettings class is the
# following, so if that changes in the class it needs to change here !!!
default_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
# Accessing the class static variable does not initialise the singleton
return os.path.normpath(os.path.join(
default_dir,
ServerCompilerSettings._ServerCompilerSettings__settings_filename))
def delete_default_settings_file(self):
"""
Checks if there is a settings file in the default location and deletes
it if it finds it.
This will DELETE a file from the directory this script is called !!!
"""
settings_file = self.get_default_settings_file_dir()
if os.path.exists(settings_file):
print('Removing settings file from %s' % settings_file)
os.remove(settings_file)
#
# Testing the class singlentoness
# Testing the class singleton property
#
def test_singleton(self):
# Testing if singleton is working
......@@ -43,21 +67,36 @@ class ServerCompilerSettingsTestCase(unittest.TestCase):
instance_1._ServerCompilerSettings__singleton_instance)
#
# Testing the compiler_dir getter and setter
# Testing the compiler_dir accessors
#
def test_read_compiler_dir(self):
self.assertEqual(ServerCompilerSettings().compiler_dir,
ServerCompilerSettings().__compiler_dir__)
@mock.patch('ardublocklyserver.compilersettings.os.path.isfile')
def test_compiler_dir_valid_accesor(self, mock_os_path_isfile):
self.delete_default_settings_file()
# Creating the instance will create the settings file
instance = ServerCompilerSettings()
self.assertEqual(
instance.compiler_dir,
instance._ServerCompilerSettings__compiler_dir)
# Testing setter with valid input
mock_os_path_isfile.return_value = True
old_compiler_dir = instance.compiler_dir
new_compiler_dir = os.path.join(os.getcwd(), 'random.exe')
instance.compiler_dir = new_compiler_dir
self.assertEqual(
instance.compiler_dir,
instance._ServerCompilerSettings__compiler_dir)
self.assertEqual(instance.compiler_dir, new_compiler_dir)
self.assertNotEqual(instance.compiler_dir, old_compiler_dir)
@mock.patch('ardublocklyserver.compilersettings.os.path.isfile')
def test_write_compiler_dir_invalid(self, mock_os_path_isfile):
def test_compiler_dir_invalid_accesor(self, mock_os_path_isfile):
"""
Tests path doesn't get save if:
Tests path doesn't get saved if:
A file that does not exists
Just a folder
A non executable file
"""
# Test for failure, mock that the files does not exists
# Test for failure, mock that the file does not exists
mock_os_path_isfile.return_value = False
original_dir = ServerCompilerSettings().compiler_dir
new_dir = os.path.join(os.getcwd(), 'random.exe')
......@@ -65,7 +104,7 @@ class ServerCompilerSettingsTestCase(unittest.TestCase):
self.assertNotEqual(new_dir, ServerCompilerSettings().compiler_dir)
self.assertEqual(original_dir, ServerCompilerSettings().compiler_dir)
# No extension is accepted as a valid compiler directory
# Test for directory not accepted as a valid compiler file
mock_os_path_isfile.return_value = True
new_dir = os.getcwd()
ServerCompilerSettings().compiler_dir = new_dir
......@@ -73,21 +112,89 @@ class ServerCompilerSettingsTestCase(unittest.TestCase):
self.assertNotEqual(original_dir, ServerCompilerSettings().compiler_dir)
@mock.patch('ardublocklyserver.compilersettings.os.path.isfile')
def test_write_compiler_dir_valid(self, mock_os_path_isfile):
mock_os_path_isfile.return_value = True
new_dir = os.path.join(os.getcwd(), 'arduino.exe')
ServerCompilerSettings().compiler_dir = new_dir
self.assertTrue(new_dir in ServerCompilerSettings().compiler_dir)
@mock.patch('ardublocklyserver.compilersettings.sys.platform')
def test_compiler_dir_platform_specific(
self, mock_os_path_isfile, mock_sys_platform):
"""
Depending on the platform that the server is running, the Arduino
software has some specific quirks about which file has to be used.
:param mock_os_path_isfile: Mock for the isfile function.
:param mock_sys_platform: Mock for the sys.platform variable.
"""
# Set the system as MacOS to check for app bundle check
#mock_sys_platform.value = 'darwin'
#print(sys.platform)
# For now I'm unable to fake sys.platform without mocking entire sys, so
# we'll depend on the CI servers to do platform testing here
# Check that the final compiler dir is within the app bundle
if sys.platform == 'darwin':
mock_os_path_isfile.return_value = True
new_dir = os.path.join(os.getcwd(), 'arduino.app')
ServerCompilerSettings().compiler_dir = new_dir
self.assertNotEqual(new_dir, ServerCompilerSettings().compiler_dir)
self.assertTrue(new_dir in ServerCompilerSettings().compiler_dir)
final_dir = os.path.join(new_dir, 'Contents', 'MacOS',
'JavaApplicationStub')
self.assertEqual(final_dir, ServerCompilerSettings().compiler_dir)
#
# Testing the launch_IDE_option accessors
#
def test_ide_valid_accesor(self):
self.delete_default_settings_file()
# Creating the instance will create the settings file
instance = ServerCompilerSettings()
self.assertEqual(
instance.load_ide_option,
instance._ServerCompilerSettings__load_ide_option)
# Testing setter with valid inputs
for key in instance.get_load_ide_options():
old_load_ide_option = instance.load_ide_option
new_load_ide_option = key
instance.load_ide_option = new_load_ide_option
self.assertEqual(
instance.load_ide_option,
instance._ServerCompilerSettings__load_ide_option)
self.assertEqual(instance.load_ide_option, new_load_ide_option)
self.assertNotEqual(instance.load_ide_option, old_load_ide_option)
def test_ide_invalid_accesor(self):
# Creating the instance will create the settings file with defaults
self.delete_default_settings_file()
instance = ServerCompilerSettings()
# Testing setter with value instead of key inputs
ide_dict = instance.get_load_ide_options()
for key in ide_dict:
old_load_ide_option = instance.load_ide_option
new_load_ide_option = ide_dict[key]
instance.load_ide_option = new_load_ide_option
self.assertEqual(
instance.load_ide_option,
instance._ServerCompilerSettings__load_ide_option)
self.assertNotEqual(instance.load_ide_option, new_load_ide_option)
self.assertEqual(instance.load_ide_option, old_load_ide_option)
#
# Testing the settings file
#
def test_settings_file_creation(self):
""" Need to find a way to test this one """
"""
Tests if the settings file is created.
"""
self.delete_default_settings_file()
settings_file = self.get_default_settings_file_dir()
self.assertFalse(os.path.exists(settings_file))
ServerCompilerSettings().save_settings()
self.assertEqual(0,0)
self.assertTrue(os.path.exists(settings_file))
def test_settings_file_read(self):
"""
Simple test that checks for no exceptions happening while creating
and loading the created file.
"""
ServerCompilerSettings()
ServerCompilerSettings().set_default_settings()
ServerCompilerSettings().get_settings_file_data()
......
......@@ -47,7 +47,7 @@ class BlocklyRequestHandlerTestCase(unittest.TestCase):
test_compiler_dir = os.path.join(os.getcwd(), 'arduino.exe')
mock_settings = requesthandler.ServerCompilerSettings()
mock_settings.__compiler_dir__ = test_compiler_dir
mock_settings.__compiler_dir = test_compiler_dir
requesthandler.ServerCompilerSettings().launch_IDE_only = True
# Build expected string and run test
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment