11
11
from typing import Any , Tuple
12
12
13
13
import django
14
+ import pexpect
14
15
import pytest
15
16
import typer
16
17
from click .exceptions import UsageError
@@ -62,7 +63,20 @@ def get_named_arguments(function):
62
63
]
63
64
64
65
65
- def run_command (command , * args , parse_json = True ) -> Tuple [str , str ]:
66
+ def interact (command , * args , ** kwargs ):
67
+ cwd = os .getcwd ()
68
+ try :
69
+ os .chdir (manage_py .parent )
70
+ return pexpect .spawn (
71
+ " " .join ([sys .executable , f"./{ manage_py .name } " , command , * args ]),
72
+ env = os .environ ,
73
+ ** kwargs ,
74
+ )
75
+ finally :
76
+ os .chdir (cwd )
77
+
78
+
79
+ def run_command (command , * args , parse_json = True , ** kwargs ) -> Tuple [str , str ]:
66
80
# we want to use the same test database that was created for the test suite run
67
81
cwd = os .getcwd ()
68
82
try :
@@ -73,6 +87,7 @@ def run_command(command, *args, parse_json=True) -> Tuple[str, str]:
73
87
capture_output = True ,
74
88
text = True ,
75
89
env = env ,
90
+ ** kwargs ,
76
91
)
77
92
78
93
# Check the return code to ensure the script ran successfully
@@ -2753,3 +2768,93 @@ def test_handle_as_init_direct(self):
2753
2768
self .assertEqual (get_command ("handle_as_init" )(), "handle" )
2754
2769
self .assertEqual (get_command ("handle_as_init" , "subcommand" )(), "subcommand" )
2755
2770
self .assertEqual (get_command ("handle_as_init" ).subcommand (), "subcommand" )
2771
+
2772
+
2773
+ class TestPromptOptions (TestCase ):
2774
+
2775
+ def test_run_with_option_prompt (self ):
2776
+
2777
+ cmd = interact ("prompt" , "--no-color" , "cmd1" , "bckohan" )
2778
+ cmd .expect ("Password: " )
2779
+ cmd .sendline ("test_password" )
2780
+
2781
+ result = cmd .read ().decode ("utf-8" ).strip ().splitlines ()[0 ]
2782
+ self .assertEqual (result , "bckohan test_password" )
2783
+
2784
+ cmd = interact ("prompt" , "--no-color" , "cmd2" , "bckohan" )
2785
+ result = cmd .read ().decode ("utf-8" ).strip ().splitlines ()[0 ]
2786
+ self .assertEqual (result , "bckohan None" )
2787
+
2788
+ cmd = interact ("prompt" , "--no-color" , "cmd2" , "bckohan" , "-p" )
2789
+ cmd .expect ("Password: " )
2790
+ cmd .sendline ("test_password2" )
2791
+ result = cmd .read ().decode ("utf-8" ).strip ().splitlines ()[0 ]
2792
+ self .assertEqual (result , "bckohan test_password2" )
2793
+
2794
+ cmd = interact ("prompt" , "--no-color" , "cmd3" , "bckohan" )
2795
+ result = cmd .read ().decode ("utf-8" ).strip ().splitlines ()[0 ]
2796
+ self .assertEqual (result , "bckohan default" )
2797
+
2798
+ cmd = interact ("prompt" , "--no-color" , "cmd3" , "bckohan" , "-p" )
2799
+ cmd .expect (r"Password \[default\]: " )
2800
+ cmd .sendline ("test_password3" )
2801
+ result = cmd .read ().decode ("utf-8" ).strip ().splitlines ()[0 ]
2802
+ self .assertEqual (result , "bckohan test_password3" )
2803
+
2804
+ cmd = interact ("prompt" , "--no-color" , "group1" , "cmd4" , "bckohan" )
2805
+ cmd .expect (r"Flag: " )
2806
+ cmd .sendline ("test_flag" )
2807
+ cmd .expect (r"Password: " )
2808
+ cmd .sendline ("test_password4" )
2809
+ result = cmd .read ().decode ("utf-8" ).strip ().splitlines ()[0 ]
2810
+ self .assertEqual (result , "test_flag bckohan test_password4" )
2811
+
2812
+ def test_call_with_option_prompt (self ):
2813
+ self .assertEqual (
2814
+ call_command (
2815
+ "prompt" , "--no-color" , "cmd1" , "bckohan" , password = "test_password"
2816
+ ),
2817
+ "bckohan test_password" ,
2818
+ )
2819
+
2820
+ self .assertEqual (
2821
+ call_command ("prompt" , "--no-color" , "cmd2" , "bckohan" ), "bckohan None"
2822
+ )
2823
+
2824
+ self .assertEqual (
2825
+ call_command (
2826
+ "prompt" , "--no-color" , "cmd2" , "bckohan" , "-p" , "test_password2"
2827
+ ),
2828
+ "bckohan test_password2" ,
2829
+ )
2830
+
2831
+ self .assertEqual (
2832
+ call_command ("prompt" , "--no-color" , "cmd3" , "bckohan" ), "bckohan default"
2833
+ )
2834
+
2835
+ self .assertEqual (
2836
+ call_command (
2837
+ "prompt" , "--no-color" , "cmd3" , "bckohan" , password = "test_password3"
2838
+ ),
2839
+ "bckohan test_password3" ,
2840
+ )
2841
+
2842
+ self .assertEqual (
2843
+ call_command (
2844
+ "prompt" ,
2845
+ "--no-color" ,
2846
+ "group1" ,
2847
+ "-f" ,
2848
+ "test_flag" ,
2849
+ "cmd4" ,
2850
+ "bckohan" ,
2851
+ password = "test_password4" ,
2852
+ ),
2853
+ "test_flag bckohan test_password4" ,
2854
+ )
2855
+
2856
+ # this doesn't work!
2857
+ # self.assertEqual(
2858
+ # call_command('prompt', '--no-color', 'group1', 'cmd4', 'bckohan', flag='test_flag', password='test_password4'),
2859
+ # 'test_flag bckohan test_password4'
2860
+ # )
0 commit comments