diff --git a/pysqa/cmd.py b/pysqa/cmd.py index b8f6bb10..126f68dd 100644 --- a/pysqa/cmd.py +++ b/pysqa/cmd.py @@ -48,8 +48,7 @@ def command_line(argv, execute_command=execute_command): ], ) except getopt.GetoptError: - print("cmd.py help") - sys.exit() + print("python -m pysqa --help") else: mode_submit = False mode_delete = False @@ -92,9 +91,6 @@ def command_line(argv, execute_command=execute_command): dependency_list = [arg] else: dependency_list.append(arg) - elif opt in ("-h", "--help"): - print("cmd.py help ... coming soon.") - sys.exit() if mode_submit or mode_delete or mode_reservation or mode_status: qa = QueueAdapter(directory=directory, execute_command=execute_command) if mode_submit: @@ -122,9 +118,8 @@ def command_line(argv, execute_command=execute_command): for p, folder, files in os.walk(working_directory): remote_dirs.append(p) remote_files += [os.path.join(p, f) for f in files] - print(json.dumps({"dirs": remote_dirs, "files": remote_files})) - sys.exit() - - -if __name__ == "__main__": - command_line(sys.argv[1:]) + print( + json.dumps({"dirs": sorted(remote_dirs), "files": sorted(remote_files)}) + ) + else: + print("python -m pysqa --help ... coming soon.") diff --git a/pysqa/ext/remote.py b/pysqa/ext/remote.py index ef9e7b33..8b321915 100644 --- a/pysqa/ext/remote.py +++ b/pysqa/ext/remote.py @@ -117,7 +117,7 @@ def get_job_from_remote(self, working_directory): ) remote_dict = json.loads( self._execute_remote_command( - command="python -m pysqa.cmd --list --working_directory " + command="python -m pysqa --list --working_directory " + remote_working_directory ) ) @@ -191,11 +191,7 @@ def _open_ssh_connection(self): return ssh def _remote_command(self): - return ( - "python -m pysqa.cmd --config_directory " - + self._ssh_remote_config_dir - + " " - ) + return "python -m pysqa --config_directory " + self._ssh_remote_config_dir + " " def _get_queue_status_command(self): return self._remote_command() + "--status" diff --git a/tests/test_cmd.py b/tests/test_cmd.py index 4d19c3e2..c1b7286e 100644 --- a/tests/test_cmd.py +++ b/tests/test_cmd.py @@ -1,5 +1,8 @@ import os +import io +import json import unittest +import unittest.mock from pysqa.cmd import command_line @@ -8,13 +11,27 @@ class TestCMD(unittest.TestCase): def setUpClass(cls): cls.test_dir = os.path.abspath(os.path.dirname(__file__)) + @unittest.mock.patch('sys.stdout', new_callable=io.StringIO) + def assert_stdout_command_line(self, cmd_args, execute_command, expected_output, mock_stdout): + command_line( + argv=cmd_args, + execute_command=execute_command + ) + self.assertEqual(mock_stdout.getvalue(), expected_output) + def test_help(self): - with self.assertRaises(SystemExit): - command_line(["--help"]) + self.assert_stdout_command_line( + ["--help"], + None, + "python -m pysqa --help ... coming soon.\n", + ) def test_wrong_option(self): - with self.assertRaises(SystemExit): - command_line(["--error"]) + self.assert_stdout_command_line( + ["--error"], + None, + "python -m pysqa --help\n", + ) def test_submit(self): def execute_command( @@ -26,21 +43,21 @@ def execute_command( ): return "1\n" - with self.assertRaises(SystemExit): - command_line( - [ - "--config_directory", os.path.join(self.test_dir, "config", "slurm"), - "--submit", - "--queue", "slurm", - "--job_name", "test", - "--working_directory", ".", - "--cores", "2", - "--memory", "1GB", - "--run_time", "10", - "--command", "echo hello" - ], - execute_command=execute_command - ) + self.assert_stdout_command_line( + [ + "--config_directory", os.path.join(self.test_dir, "config", "slurm"), + "--submit", + "--queue", "slurm", + "--job_name", "test", + "--working_directory", ".", + "--cores", "2", + "--memory", "1GB", + "--run_time", "10", + "--command", "echo hello" + ], + execute_command, + "1\n", + ) with open("run_queue.sh") as f: output = f.readlines() content = [ @@ -69,18 +86,17 @@ def execute_command( ): return "Success\n" - with self.assertRaises(SystemExit): - command_line( - [ - "--config_directory", os.path.join(self.test_dir, "config", "slurm"), - "--delete", - "--id", "1", - ], - execute_command=execute_command - ) + self.assert_stdout_command_line( + [ + "--config_directory", os.path.join(self.test_dir, "config", "slurm"), + "--delete", + "--id", "1" + ], + execute_command, + "S\n" + ) def test_status(self): - def execute_command( commands, working_directory=None, @@ -91,14 +107,25 @@ def execute_command( with open(os.path.join(self.test_dir, "config", "slurm", "squeue_output")) as f: return f.read() - with self.assertRaises(SystemExit): - command_line( - [ - "--config_directory", os.path.join(self.test_dir, "config", "slurm"), - "--status" - ], - execute_command=execute_command - ) + self.assert_stdout_command_line( + [ + "--config_directory", os.path.join(self.test_dir, "config", "slurm"), + "--status" + ], + execute_command, + json.dumps({ + "jobid": [5322019, 5322016, 5322017, 5322018, 5322013], "user": ["janj", "janj", "janj", "janj", "maxi"], + "jobname": ["pi_19576488", "pi_19576485", "pi_19576486", "pi_19576487", "pi_19576482"], + "status": ["running", "running", "running", "running", "running"], + "working_directory": [ + "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_1", + "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_2", + "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_3", + "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_4", + "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_5" + ] + }) +"\n" + ) def test_list(self): def execute_command( @@ -110,13 +137,21 @@ def execute_command( ): pass - with self.assertRaises(SystemExit): - command_line( - [ - "--config_directory", os.path.join(self.test_dir, "config", "slurm"), - "--list", - "--working_directory", os.path.join(self.test_dir, "config", "slurm"), + self.assert_stdout_command_line( + [ + "--config_directory", os.path.join(self.test_dir, "config", "slurm"), + "--list", + "--working_directory", os.path.join(self.test_dir, "config", "slurm"), - ], - execute_command=execute_command - ) + ], + execute_command, + json.dumps({ + "dirs": [os.path.join(self.test_dir, "config", "slurm")], + "files": sorted([ + os.path.join(self.test_dir, "config", "slurm", "squeue_output"), + os.path.join(self.test_dir, "config", "slurm", "slurm_extra.sh"), + os.path.join(self.test_dir, "config", "slurm", "slurm.sh"), + os.path.join(self.test_dir, "config", "slurm", "queue.yaml"), + ]) + }) + "\n" + ) diff --git a/tests/test_remote.py b/tests/test_remote.py index c82f6bd3..6f3c5e06 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -43,13 +43,13 @@ def test_convert_path_to_remote(self): def test_delete_command(self): self.assertEqual( - "python -m pysqa.cmd --config_directory /u/share/pysqa/resources/queues/ --delete --id 123", + "python -m pysqa --config_directory /u/share/pysqa/resources/queues/ --delete --id 123", self.remote._adapter._delete_command(job_id=123) ) def test_reservation_command(self): self.assertEqual( - "python -m pysqa.cmd --config_directory /u/share/pysqa/resources/queues/ --reservation --id 123", + "python -m pysqa --config_directory /u/share/pysqa/resources/queues/ --reservation --id 123", self.remote._adapter._reservation_command(job_id=123) )