def test_command_multiple_shell_failing(self): @command(command_type=Type.SHELL) def foo(*args, **kwargs): return [['foo'], ['bar']] args = ['foo'] main = Main(args) with patch('clinner.run.base.Popen') as popen_mock: popen_mock.return_value.returncode = 1 main.run() self.assertEqual(popen_mock.call_count, 1) self.assertEqual(popen_mock.call_args_list[0][1]['args'], ['foo'])
def test_command_multiple_shell_failing(self, cli): @command(command_type=Type.SHELL) def foo(*args, **kwargs): return [["foo"], ["bar"]] args = ["foo"] main = Main(args) with patch("clinner.run.base.Popen") as popen_mock: popen_mock.return_value.returncode = 1 main.run() assert popen_mock.call_count == 1 assert popen_mock.call_args_list[0][1]["args"] == ["foo"] del command.register["foo"]
def test_command_wrong_type(self): @command(command_type='foo') def foo(*args, **kwargs): pass args = ['foo'] self.assertRaises(CommandTypeError, Main(args).run, args)
def test_command_wrong_type(self, cli): @command(command_type="foo") def foo(*args, **kwargs): pass args = ["foo"] with pytest.raises(CommandTypeError): Main(args).run(args) del command.register["foo"]
def test_command_with_wrong_args_number(self, cli): @command(command_type=Type.PYTHON, args=(((1, 2, 3),))) # wrong args number def foo(*args, **kwargs): kwargs["q"].put(kwargs["bar"]) args = ["foo"] with pytest.raises(CommandArgParseError): Main(args) del command.register["foo"]
def test_command_python(self): @command(command_type=Type.PYTHON) def foo(*args, **kwargs): kwargs['q'].put(42) queue = Queue() args = ['foo'] Main(args).run(q=queue) self.assertEqual(queue.get(), 42)
def test_command(self): @command def foo(*args, **kwargs): kwargs['q'].put(42) queue = Queue() args = ['foo'] Main(args).run(q=queue) self.assertEqual(queue.get(), 42)
def test_command_with_unknown_args(self): @command # args def foo(*args, **kwargs): kwargs['q'].put(args[0]) queue = Queue() args = ['foo'] Main(args).run('foobar', q=queue) self.assertEqual(queue.get(), 'foobar')
def test_command_with_args_and_opts(self): @command(command_type=Type.PYTHON, args=((('-b', '--bar'), {'type': int, 'help': 'bar argument'}),)) # args and opts def foo(*args, **kwargs): kwargs['q'].put(kwargs['bar']) queue = Queue() args = ['foo'] Main(args).run(bar=3, q=queue) self.assertEqual(queue.get(), 3)
def test_command_with_args(self): @command(command_type=Type.PYTHON, args=((('-b', '--bar'),),)) # args def foo(*args, **kwargs): kwargs['q'].put(kwargs['bar']) queue = Queue() args = ['foo'] Main(args).run(bar='foobar', q=queue) self.assertEqual(queue.get(), 'foobar')
def test_command(self, cli): @command def foo(*args, **kwargs): kwargs["q"].put(42) queue = Queue() args = ["foo"] Main(args).run(q=queue) assert queue.get() == 42 del command.register["foo"]
def test_command_with_unknown_args(self, cli): @command # args def foo(*args, **kwargs): kwargs["q"].put(args[0]) queue = Queue() args = ["foo"] Main(args).run("foobar", q=queue) assert queue.get() == "foobar" del command.register["foo"]
def test_command_with_args(self, cli): @command(command_type=Type.PYTHON, args=((("-b", "--bar"),),)) # args def foo(*args, **kwargs): kwargs["q"].put(kwargs["bar"]) queue = Queue() args = ["foo"] Main(args).run(bar="foobar", q=queue) assert queue.get() == "foobar" del command.register["foo"]
def test_command_python_async(self, cli): @command(command_type=Type.PYTHON) async def foo(*args, **kwargs): kwargs["q"].put(42) queue = Queue() args = ["foo"] Main(args).run(q=queue) assert queue.get() == 42 del command.register["foo"]
def test_command_with_callable_args(self): def add_arguments(parser): parser.add_argument('-b', '--bar', type=int, help='bar argument') @command(args=add_arguments) def foo(*args, **kwargs): kwargs['q'].put(kwargs['bar']) queue = Queue() args = ['foo'] Main(args).run(bar=3, q=queue) self.assertEqual(queue.get(), 3)
def test_command_with_args_and_opts(self, cli): @command( command_type=Type.PYTHON, args=((("-b", "--bar"), {"type": int, "help": "bar argument"}),) ) # args and opts def foo(*args, **kwargs): kwargs["q"].put(kwargs["bar"]) queue = Queue() args = ["foo"] Main(args).run(bar=3, q=queue) assert queue.get() == 3 del command.register["foo"]
def test_command_with_callable_args(self, cli): def add_arguments(parser): parser.add_argument("-b", "--bar", type=int, help="bar argument") @command(args=add_arguments) def foo(*args, **kwargs): kwargs["q"].put(kwargs["bar"]) queue = Queue() args = ["foo"] Main(args).run(bar=3, q=queue) assert queue.get() == 3 del command.register["foo"]
condition = Condition() if kwargs["unlock"]: unlock_thread = Thread(name="unlock", target=unlock, args=(condition,)) unlock_thread.start() socat_cmd = shlex.split("socat tcp-listen:8000,reuseaddr,fork tcp:localhost:9980") siad_cmd = shlex.split(f"siad --sia-directory {os.getcwd()}") + list(args) try: with subprocess.Popen(socat_cmd) as socat_process: with subprocess.Popen( siad_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True ) as siad_process: redirect_output_thread = Thread( name="redirect_output", target=redirect_output, args=(siad_process, condition) ) redirect_output_thread.start() siad_process.wait() socat_process.terminate() except KeyboardInterrupt: pass finally: logger.info("Siad command finished with code %d", siad_process.returncode) logger.info("Socat command finished with code %d", socat_process.returncode) if __name__ == "__main__": sys.exit(Main().run())