Skip to content

Commit

Permalink
Fix pre-commit in two files
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannipizzi authored and sphuber committed Dec 12, 2019
1 parent 2eac63e commit 66b6f6a
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 217 deletions.
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
aiida/backends/tests/test_base_dataclasses.py|
aiida/backends/tests/cmdline/commands/test_code.py|
aiida/backends/tests/cmdline/commands/test_comment.py|
aiida/backends/tests/cmdline/commands/test_computer.py|
aiida/backends/tests/cmdline/commands/test_data.py|
aiida/backends/tests/cmdline/commands/test_export.py|
aiida/backends/tests/cmdline/commands/test_group.py|
Expand Down Expand Up @@ -151,7 +150,6 @@
aiida/tools/dbimporters/plugins/nninc.py|
aiida/tools/dbimporters/plugins/oqmd.py|
aiida/tools/dbimporters/plugins/pcod.py|
aiida/transports/plugins/ssh.py|
aiida/transports/plugins/test_all_plugins.py|
aiida/transports/plugins/test_local.py|
aiida/transports/plugins/test_ssh.py|
Expand Down
94 changes: 60 additions & 34 deletions aiida/backends/tests/cmdline/commands/test_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

from aiida import orm
from aiida.backends.testbase import AiidaTestCase
from aiida.cmdline.commands.cmd_computer import computer_disable, computer_enable, computer_setup
from aiida.cmdline.commands.cmd_computer import computer_setup
from aiida.cmdline.commands.cmd_computer import computer_show, computer_list, computer_rename, computer_delete
from aiida.cmdline.commands.cmd_computer import computer_test, computer_configure, computer_duplicate


def generate_setup_options_dict(replace_args={}, non_interactive=True):
def generate_setup_options_dict(replace_args=None, non_interactive=True):
"""
Return a OrderedDict with the key-value pairs for the command line.
Expand Down Expand Up @@ -53,8 +53,9 @@ def generate_setup_options_dict(replace_args={}, non_interactive=True):
valid_noninteractive_options['append-text'] = "env\necho '444'\necho 'third line'"

# I replace kwargs here, so that if they are known, they go at the right order
for k in replace_args:
valid_noninteractive_options[k] = replace_args[k]
if replace_args is not None:
for k in replace_args:
valid_noninteractive_options[k] = replace_args[k]

return valid_noninteractive_options

Expand All @@ -79,7 +80,7 @@ def generate_setup_options(ordereddict):
return options


def generate_setup_options_interactive(ordereddict):
def generate_setup_options_interactive(ordereddict): # pylint: disable=invalid-name
"""
Given an (ordered) dict, returns a list of options
Expand All @@ -91,7 +92,7 @@ def generate_setup_options_interactive(ordereddict):
:return: a list to be passed as command-line arguments.
"""
options = []
for key, value in ordereddict.items():
for value in ordereddict.values():
if value is None:
options.append(True)
else:
Expand All @@ -103,17 +104,21 @@ class TestVerdiComputerSetup(AiidaTestCase):
"""Tests for the 'verdi computer setup' command."""

def setUp(self):
"""Setup the class with a CliRunner."""
self.cli_runner = CliRunner()

def test_help(self):
"""Test the help of verdi computer setup."""
self.cli_runner.invoke(computer_setup, ['--help'], catch_exceptions=False)

def test_reachable(self):
"""Test if the verdi computer setup is reachable."""
import subprocess as sp
output = sp.check_output(['verdi', 'computer', 'setup', '--help'])
self.assertIn(b'Usage:', output)

def test_interactive(self):
"""Test verdi computer setup in interactive mode."""
os.environ['VISUAL'] = 'sleep 1; vim -cwq'
os.environ['EDITOR'] = 'sleep 1; vim -cwq'
label = 'interactive_computer'
Expand Down Expand Up @@ -143,6 +148,11 @@ def test_interactive(self):
self.assertEqual(new_computer.get_append_text(), '')

def test_mixed(self):
"""
Test verdi computer setup in mixed mode.
Some parts are given interactively and some non-interactively.
"""
os.environ['VISUAL'] = 'sleep 1; vim -cwq'
os.environ['EDITOR'] = 'sleep 1; vim -cwq'
label = 'mixed_computer'
Expand Down Expand Up @@ -175,8 +185,9 @@ def test_mixed(self):
self.assertEqual(new_computer.get_mpirun_command(), options_dict_full['mpirun-command'].split())
self.assertEqual(new_computer.get_shebang(), options_dict_full['shebang'])
self.assertEqual(new_computer.get_workdir(), options_dict_full['work-dir'])
self.assertEqual(new_computer.get_default_mpiprocs_per_machine(),
int(options_dict_full['mpiprocs-per-machine']))
self.assertEqual(
new_computer.get_default_mpiprocs_per_machine(), int(options_dict_full['mpiprocs-per-machine'])
)
# For now I'm not writing anything in them
self.assertEqual(new_computer.get_prepend_text(), options_dict_full['prepend-text'])
self.assertEqual(new_computer.get_append_text(), options_dict_full['append-text'])
Expand Down Expand Up @@ -210,7 +221,7 @@ def test_noninteractive(self):
self.assertIsInstance(result.exception, SystemExit)
self.assertIn('already exists', result.output)

def test_noninteractive_optional_default_mpiprocs(self):
def test_noninteractive_optional_default_mpiprocs(self): # pylint: disable=invalid-name
"""
Check that if is ok not to specify mpiprocs-per-machine
"""
Expand All @@ -225,7 +236,7 @@ def test_noninteractive_optional_default_mpiprocs(self):
self.assertIsInstance(new_computer, orm.Computer)
self.assertIsNone(new_computer.get_default_mpiprocs_per_machine())

def test_noninteractive_optional_default_mpiprocs_2(self):
def test_noninteractive_optional_default_mpiprocs_2(self): # pylint: disable=invalid-name
"""
Check that if is the specified value is zero, it means unspecified
"""
Expand All @@ -240,7 +251,7 @@ def test_noninteractive_optional_default_mpiprocs_2(self):
self.assertIsInstance(new_computer, orm.Computer)
self.assertIsNone(new_computer.get_default_mpiprocs_per_machine())

def test_noninteractive_optional_default_mpiprocs_3(self):
def test_noninteractive_optional_default_mpiprocs_3(self): # pylint: disable=invalid-name
"""
Check that it fails for a negative number of mpiprocs
"""
Expand Down Expand Up @@ -320,6 +331,7 @@ def test_noninteractive_from_config(self):


class TestVerdiComputerConfigure(AiidaTestCase):
"""Test the ``verdi computer configure`` command."""

def setUp(self):
"""Prepare computer builder with common properties."""
Expand All @@ -343,10 +355,10 @@ def test_top_help(self):
self.assertIn('ssh', result.output)
self.assertIn('local', result.output)

def test_reachable(self):
def test_reachable(self): # pylint: disable=no-self-use
"""Test reachability of top level and sub commands."""
import subprocess as sp
output = sp.check_output(['verdi', 'computer', 'configure', '--help'])
sp.check_output(['verdi', 'computer', 'configure', '--help'])
sp.check_output(['verdi', 'computer', 'configure', 'local', '--help'])
sp.check_output(['verdi', 'computer', 'configure', 'ssh', '--help'])
sp.check_output(['verdi', 'computer', 'configure', 'show', '--help'])
Expand Down Expand Up @@ -381,6 +393,7 @@ def test_local_ni_empty(self):
self.assertIn('local', result.output)

def test_local_interactive(self):
"""Test computer configuration for local transports."""
self.comp_builder.label = 'test_local_interactive'
self.comp_builder.transport = 'local'
comp = self.comp_builder.new()
Expand Down Expand Up @@ -411,15 +424,16 @@ def test_ssh_interactive(self):
# I just pass the first four arguments:
# the username, the port, look_for_keys, and the key_filename
# This testing also checks that an empty key_filename is ok
command_input = (
'{remote_username}\n{port}\n{look_for_keys}\n{key_filename}\n'
).format(
remote_username=remote_username, port=port,
command_input = ('{remote_username}\n{port}\n{look_for_keys}\n{key_filename}\n').format(
remote_username=remote_username,
port=port,
look_for_keys='yes' if look_for_keys else 'no',
key_filename=key_filename
)

result = self.cli_runner.invoke(computer_configure, ['ssh', comp.label], input=command_input, catch_exceptions=False)
result = self.cli_runner.invoke(
computer_configure, ['ssh', comp.label], input=command_input, catch_exceptions=False
)
self.assertTrue(comp.is_user_configured(self.user), msg=result.output)
new_auth_params = comp.get_authinfo(self.user).get_auth_params()
self.assertEqual(new_auth_params['username'], remote_username)
Expand Down Expand Up @@ -489,9 +503,10 @@ def test_ssh_ni_username(self):
options = ['ssh', comp.label, '--non-interactive', '--username={}'.format(username), '--safe-interval', '1']
result = self.cli_runner.invoke(computer_configure, options, catch_exceptions=False)
self.assertTrue(comp.is_user_configured(self.user), msg=result.output)
self.assertEqual(orm.AuthInfo.objects.get(
dbcomputer_id=comp.id, aiidauser_id=self.user.id).get_auth_params()['username'],
username)
self.assertEqual(
orm.AuthInfo.objects.get(dbcomputer_id=comp.id, aiidauser_id=self.user.id).get_auth_params()['username'],
username
)

def test_show(self):
"""Test verdi computer configure show <comp>."""
Expand All @@ -505,17 +520,19 @@ def test_show(self):
result = self.cli_runner.invoke(computer_configure, ['show', comp.label, '--defaults'], catch_exceptions=False)
self.assertIn('* username', result.output)

result = self.cli_runner.invoke(computer_configure, ['show', comp.label, '--defaults', '--as-option-string'],
catch_exceptions=False)
result = self.cli_runner.invoke(
computer_configure, ['show', comp.label, '--defaults', '--as-option-string'], catch_exceptions=False
)
self.assertIn('--username=', result.output)

config_cmd = ['ssh', comp.label, '--non-interactive']
config_cmd.extend(result.output.replace("'", '').split(' '))
result_config = self.cli_runner.invoke(computer_configure, config_cmd, catch_exceptions=False)
self.assertTrue(comp.is_user_configured(self.user), msg=result_config.output)

result_cur = self.cli_runner.invoke(computer_configure, ['show', comp.label, '--as-option-string'],
catch_exceptions=False)
result_cur = self.cli_runner.invoke(
computer_configure, ['show', comp.label, '--as-option-string'], catch_exceptions=False
)
self.assertIn('--username=', result.output)
self.assertEqual(result_cur.output, result.output)

Expand All @@ -537,7 +554,8 @@ def setUpClass(cls, *args, **kwargs):
hostname='localhost',
transport_type='local',
scheduler_type='direct',
workdir='/tmp/aiida')
workdir='/tmp/aiida'
)
cls.comp.set_default_mpiprocs_per_machine(1)
cls.comp.store()

Expand Down Expand Up @@ -593,8 +611,6 @@ def test_computer_show(self):
"""
Test if 'verdi computer show' command works
"""
import traceback

# See if we can display info about the test computer.
result = self.cli_runner.invoke(computer_show, ['comp_cli_test_computer'])

Expand Down Expand Up @@ -665,8 +681,13 @@ def test_computer_delete(self):
from aiida.common.exceptions import NotExistent

# Setup a computer to delete during the test
comp = orm.Computer(name='computer_for_test_delete', hostname='localhost',
transport_type='local', scheduler_type='direct', workdir='/tmp/aiida').store()
orm.Computer(
name='computer_for_test_delete',
hostname='localhost',
transport_type='local',
scheduler_type='direct',
workdir='/tmp/aiida'
).store()

# See if the command complains about not getting an invalid computer
options = ['non_existent_computer_name']
Expand All @@ -684,12 +705,14 @@ def test_computer_delete(self):
orm.Computer.objects.get(name='computer_for_test_delete')

def test_computer_duplicate_interactive(self):
"""Test 'verdi computer duplicate' in interactive mode."""
os.environ['VISUAL'] = 'sleep 1; vim -cwq'
os.environ['EDITOR'] = 'sleep 1; vim -cwq'
label = 'computer_duplicate_interactive'
user_input = label + '\n\n\n\n\n\n\n\n\nN'
result = self.cli_runner.invoke(computer_duplicate, [str(self.comp.pk)], input=user_input,
catch_exceptions=False)
result = self.cli_runner.invoke(
computer_duplicate, [str(self.comp.pk)], input=user_input, catch_exceptions=False
)
self.assertIsNone(result.exception, result.output)

new_computer = orm.Computer.objects.get(name=label)
Expand All @@ -705,9 +728,12 @@ def test_computer_duplicate_interactive(self):
self.assertEqual(self.comp.get_append_text(), new_computer.get_append_text())

def test_computer_duplicate_non_interactive(self):
"""Test if 'verdi computer duplicate' in non-interactive mode."""
label = 'computer_duplicate_noninteractive'
result = self.cli_runner.invoke(computer_duplicate,
['--non-interactive', '--label=' + label, str(self.comp.pk)])
result = self.cli_runner.invoke(
computer_duplicate,
['--non-interactive', '--label=' + label, str(self.comp.pk)]
)
self.assertIsNone(result.exception, result.output)

new_computer = orm.Computer.objects.get(name=label)
Expand Down
4 changes: 1 addition & 3 deletions aiida/transports/plugins/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,9 +806,7 @@ def gotocomputer_command(self, remotedir):
script = ' ; '.join([
'if [ -d {escaped_remotedir} ]', 'then cd {escaped_remotedir}', 'bash', "else echo ' ** The directory'",
"echo ' ** {remotedir}'", "echo ' ** seems to have been deleted, I logout...'", 'fi'
]).format(
escaped_remotedir="'{}'".format(remotedir), remotedir=remotedir
)
]).format(escaped_remotedir="'{}'".format(remotedir), remotedir=remotedir)
cmd = 'bash -c "{}"'.format(script)
return cmd

Expand Down
Loading

0 comments on commit 66b6f6a

Please sign in to comment.