11import os
2+ import io
3+ import json
24import unittest
5+ import unittest .mock
36from pysqa .cmd import command_line
47
58
@@ -8,13 +11,27 @@ class TestCMD(unittest.TestCase):
811 def setUpClass (cls ):
912 cls .test_dir = os .path .abspath (os .path .dirname (__file__ ))
1013
14+ @unittest .mock .patch ('sys.stdout' , new_callable = io .StringIO )
15+ def assert_stdout_command_line (self , cmd_args , execute_command , expected_output , mock_stdout ):
16+ command_line (
17+ argv = cmd_args ,
18+ execute_command = execute_command
19+ )
20+ self .assertEqual (mock_stdout .getvalue (), expected_output )
21+
1122 def test_help (self ):
12- with self .assertRaises (SystemExit ):
13- command_line (["--help" ])
23+ self .assert_stdout_command_line (
24+ ["--help" ],
25+ None ,
26+ "python -m pysqa --help ... coming soon.\n " ,
27+ )
1428
1529 def test_wrong_option (self ):
16- with self .assertRaises (SystemExit ):
17- command_line (["--error" ])
30+ self .assert_stdout_command_line (
31+ ["--error" ],
32+ None ,
33+ "python -m pysqa --help\n " ,
34+ )
1835
1936 def test_submit (self ):
2037 def execute_command (
@@ -26,21 +43,21 @@ def execute_command(
2643 ):
2744 return "1\n "
2845
29- with self .assertRaises ( SystemExit ):
30- command_line (
31- [
32- "--config_directory" , os . path . join ( self . test_dir , "config" , "slurm" ) ,
33- "--submit " ,
34- "--queue " , "slurm " ,
35- "--job_name " , "test " ,
36- "--working_directory " , ". " ,
37- "--cores " , "2 " ,
38- "--memory " , "1GB " ,
39- "--run_time " , "10" ,
40- "--command" , "echo hello"
41- ] ,
42- execute_command = execute_command
43- )
46+ self .assert_stdout_command_line (
47+ [
48+ "--config_directory" , os . path . join ( self . test_dir , "config" , "slurm" ),
49+ "--submit" ,
50+ "--queue" , "slurm " ,
51+ "--job_name " , "test " ,
52+ "--working_directory " , ". " ,
53+ "--cores " , "2 " ,
54+ "--memory " , "1GB " ,
55+ "--run_time " , "10 " ,
56+ "--command " , "echo hello"
57+ ],
58+ execute_command ,
59+ "1 \n " ,
60+ )
4461 with open ("run_queue.sh" ) as f :
4562 output = f .readlines ()
4663 content = [
@@ -69,18 +86,17 @@ def execute_command(
6986 ):
7087 return "Success\n "
7188
72- with self .assertRaises ( SystemExit ):
73- command_line (
74- [
75- "--config_directory" , os . path . join ( self . test_dir , "config" , "slurm" ) ,
76- "--delete" ,
77- "--id" , "1" ,
78- ] ,
79- execute_command = execute_command
80- )
89+ self .assert_stdout_command_line (
90+ [
91+ "--config_directory" , os . path . join ( self . test_dir , "config" , "slurm" ),
92+ "--delete" ,
93+ "--id" , "1"
94+ ] ,
95+ execute_command ,
96+ "S \n "
97+ )
8198
8299 def test_status (self ):
83-
84100 def execute_command (
85101 commands ,
86102 working_directory = None ,
@@ -91,14 +107,25 @@ def execute_command(
91107 with open (os .path .join (self .test_dir , "config" , "slurm" , "squeue_output" )) as f :
92108 return f .read ()
93109
94- with self .assertRaises (SystemExit ):
95- command_line (
96- [
97- "--config_directory" , os .path .join (self .test_dir , "config" , "slurm" ),
98- "--status"
99- ],
100- execute_command = execute_command
101- )
110+ self .assert_stdout_command_line (
111+ [
112+ "--config_directory" , os .path .join (self .test_dir , "config" , "slurm" ),
113+ "--status"
114+ ],
115+ execute_command ,
116+ json .dumps ({
117+ "jobid" : [5322019 , 5322016 , 5322017 , 5322018 , 5322013 ], "user" : ["janj" , "janj" , "janj" , "janj" , "maxi" ],
118+ "jobname" : ["pi_19576488" , "pi_19576485" , "pi_19576486" , "pi_19576487" , "pi_19576482" ],
119+ "status" : ["running" , "running" , "running" , "running" , "running" ],
120+ "working_directory" : [
121+ "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_1" ,
122+ "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_2" ,
123+ "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_3" ,
124+ "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_4" ,
125+ "/cmmc/u/janj/pyiron/projects/2023/2023-04-19-dft-test/job_5"
126+ ]
127+ }) + "\n "
128+ )
102129
103130 def test_list (self ):
104131 def execute_command (
@@ -110,13 +137,21 @@ def execute_command(
110137 ):
111138 pass
112139
113- with self .assertRaises (SystemExit ):
114- command_line (
115- [
116- "--config_directory" , os .path .join (self .test_dir , "config" , "slurm" ),
117- "--list" ,
118- "--working_directory" , os .path .join (self .test_dir , "config" , "slurm" ),
140+ self .assert_stdout_command_line (
141+ [
142+ "--config_directory" , os .path .join (self .test_dir , "config" , "slurm" ),
143+ "--list" ,
144+ "--working_directory" , os .path .join (self .test_dir , "config" , "slurm" ),
119145
120- ],
121- execute_command = execute_command
122- )
146+ ],
147+ execute_command ,
148+ json .dumps ({
149+ "dirs" : [os .path .join (self .test_dir , "config" , "slurm" )],
150+ "files" : sorted ([
151+ os .path .join (self .test_dir , "config" , "slurm" , "squeue_output" ),
152+ os .path .join (self .test_dir , "config" , "slurm" , "slurm_extra.sh" ),
153+ os .path .join (self .test_dir , "config" , "slurm" , "slurm.sh" ),
154+ os .path .join (self .test_dir , "config" , "slurm" , "queue.yaml" ),
155+ ])
156+ }) + "\n "
157+ )
0 commit comments