Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.

[refactor-prompt] add config nep8 #771

Merged
merged 1 commit into from
Dec 24, 2018
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
35 changes: 33 additions & 2 deletions neo/Prompt/Commands/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from neo.Prompt.Utils import get_arg
from neo.Settings import settings
from neo.Network.NodeLeader import NodeLeader
from distutils import util
import logging


Expand All @@ -17,6 +18,7 @@ def __init__(self):
self.register_sub_command(CommandConfigVMLog())
self.register_sub_command(CommandConfigNodeRequests())
self.register_sub_command(CommandConfigMaxpeers())
self.register_sub_command(CommandConfigNEP8())

def command_desc(self):
return CommandDesc('config', 'configure internal settings')
Expand Down Expand Up @@ -155,7 +157,36 @@ def command_desc(self):

def handle_help(self, arguments):
super().handle_help(arguments)
print(f"\nCurrent settings {self.command_desc().params[0].name}: {NodeLeader.Instance().BREQPART} {self.command_desc().params[1].name}: {NodeLeader.Instance().BREQMAX}")
print(f"\nCurrent settings {self.command_desc().params[0].name}:"
f" {NodeLeader.Instance().BREQPART} {self.command_desc().params[1].name}: {NodeLeader.Instance().BREQMAX}")


class CommandConfigNEP8(CommandBase):
def __init__(self):
super().__init__()

def execute(self, arguments):
if len(arguments) != 1:
print("Please specify the required parameter")
return False

try:
flag = bool(util.strtobool(arguments[0]))
except ValueError:
print("Invalid option")
return False

settings.COMPILER_NEP_8 = flag
if flag:
print("NEP-8 compiler instruction usage is ON")
else:
print("NEP-8 compiler instruction usage is OFF")

return True

def command_desc(self):
p1 = ParameterDesc('attribute', 'either "on"|"off" or 1|0')
return CommandDesc('nep8', 'toggle using NEP-8 compiler instructions', [p1])


class CommandConfigMaxpeers(CommandBase):
Expand All @@ -171,7 +202,7 @@ def execute(self, arguments):
return c1
except ValueError:
print("Please supply a positive integer for maxpeers")
return
return
else:
print(f"Maintaining maxpeers at {settings.CONNECTED_PEER_MAX}")
return
Expand Down
46 changes: 46 additions & 0 deletions neo/Prompt/Commands/tests/test_config_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,49 @@ def test_config_maxpeers(self):
args = ['maxpeers', "-1"]
res = CommandConfig().execute(args)
self.assertFalse(res)

def test_config_nep8(self):
# test with missing flag argument
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['nep8']
res = CommandConfig().execute(args)
self.assertFalse(res)
self.assertIn("Please specify the required parameter", mock_print.getvalue())

# test with invalid option
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['nep8', 'blah']
res = CommandConfig().execute(args)
self.assertFalse(res)
self.assertIn("Invalid option", mock_print.getvalue())

# ideally for the next tests we should compile some SC and validate if NEP-8 instructions are used or not
# for now the effort required to do so does not seem justified and we'll just rely on applying the setting

# test turning on - 1
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['nep8', 'on']
res = CommandConfig().execute(args)
self.assertTrue(res)
self.assertIn("NEP-8 compiler instruction usage is ON", mock_print.getvalue())

# test turning on - 2
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['nep8', '1']
res = CommandConfig().execute(args)
self.assertTrue(res)
self.assertIn("NEP-8 compiler instruction usage is ON", mock_print.getvalue())

# test turning off - 1
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['nep8', 'off']
res = CommandConfig().execute(args)
self.assertTrue(res)
self.assertIn("NEP-8 compiler instruction usage is OFF", mock_print.getvalue())

# test turning off - 2
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['nep8', '0']
res = CommandConfig().execute(args)
self.assertTrue(res)
self.assertIn("NEP-8 compiler instruction usage is OFF", mock_print.getvalue())