@@ -502,10 +502,12 @@ def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
502
502
** env ,
503
503
}
504
504
505
+ rex = re .compile
505
506
expected = [
506
- re .compile (r"\[y/N\]" ), # 0
507
- re .compile ("Aborted" ), # 1
508
- re .compile ("Installed" ), # 2
507
+ rex (r"Append the above contents to (?P<file>.*)\?" ), # 0
508
+ rex (r"Create (?P<file>.*) with the above contents\?" ), # 1
509
+ rex (r"Aborted shell completion installation." ), # 2
510
+ rex (rf"Installed autocompletion for { self .shell } " ), # 3
509
511
]
510
512
511
513
install_command = [
@@ -520,17 +522,23 @@ def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
520
522
521
523
install = pexpect .spawn (self .manage_script , install_command , env = env )
522
524
525
+ def wait_for_output (child ) -> t .Tuple [int , t .Optional [str ]]:
526
+ index = child .expect (expected )
527
+ if index in [0 , 1 ]:
528
+ return index , child .match .group ("file" ).decode ()
529
+ return index , None
530
+
523
531
# test an abort
524
- idx = install . expect ( expected )
525
- self .assertEqual (idx , 0 )
532
+ idx , _ = wait_for_output ( install )
533
+ self .assertLess (idx , 2 )
526
534
install .sendline ("N" )
527
535
528
536
while True :
529
- idx = install . expect ( expected )
530
- if idx == 0 :
537
+ idx , _ = wait_for_output ( install )
538
+ if idx < 2 :
531
539
install .sendline ("N" )
532
540
else :
533
- self .assertEqual (idx , 1 )
541
+ self .assertEqual (idx , 2 )
534
542
break
535
543
536
544
self .verify_remove (directory = directory )
@@ -539,83 +547,85 @@ def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
539
547
install = pexpect .spawn (self .manage_script , install_command , env = env )
540
548
541
549
while True :
542
- idx = install . expect ( expected )
543
- if idx < 1 :
550
+ idx , _ = wait_for_output ( install )
551
+ if idx < 2 :
544
552
install .sendline ("Y" )
545
553
else :
546
- self .assertEqual (idx , 2 )
554
+ self .assertEqual (idx , 3 )
547
555
break
548
556
549
557
self .verify_install (directory = directory )
550
558
551
- else :
552
-
553
- def test_prompt_install (self , env = {}, directory : t .Optional [Path ] = None ):
554
- env = {
555
- ** dict (os .environ ),
556
- "DJANGO_SETTINGS_MODULE" : "tests.settings.completion" ,
557
- "DJANGO_COLORS" : "nocolor" ,
558
- ** env ,
559
- }
560
-
561
- expected = [
562
- re .compile (r"\[y/N\]" ), # 0
563
- re .compile ("Aborted" ), # 1
564
- re .compile ("Installed" ), # 2
565
- ]
566
-
567
- install_command = [
568
- self .manage_script ,
569
- "shellcompletion" ,
570
- "--no-color" ,
571
- "--shell" ,
572
- self .shell ,
573
- "install" ,
574
- ]
575
- self .remove ()
576
- self .verify_remove (directory = directory )
577
-
578
- def run_with_response (responses : t .List [str ]):
579
- process = subprocess .Popen (
580
- install_command ,
581
- env = env ,
582
- cwd = directory ,
583
- stdin = subprocess .PIPE ,
584
- stdout = subprocess .PIPE ,
585
- stderr = subprocess .STDOUT ,
586
- text = True ,
587
- )
588
-
589
- output = ""
590
- for response in responses :
591
- while True :
592
- line = process .stdout .readline ()
593
- if not line :
594
- break
595
- output += line
596
-
597
- matched_index = match_output (line )
598
- if matched_index is not None :
599
- process .stdin .write (response + "\n " )
600
- process .stdin .flush ()
601
- break
602
-
603
- process .wait ()
604
- return output
605
-
606
- def match_output (line : str ) -> t .Optional [int ]:
607
- for i , pattern in enumerate (expected ):
608
- match = pattern .search (line )
609
- if match :
610
- return i
611
- return None
612
-
613
- # Test abort sequence
614
- abort_output = run_with_response (["N" , "N" ])
615
- self .assertIn ("Aborted shell completion installation." , abort_output )
616
- self .verify_remove (directory = directory )
617
-
618
- # Test install sequence
619
- install_output = run_with_response (["Y" , "Y" ])
620
- self .assertIn (f"Installed autocompletion for { self .shell } " , install_output )
621
- self .verify_install (directory = directory )
559
+ # else:
560
+
561
+ # def test_prompt_install(self, env={}, directory: t.Optional[Path] = None):
562
+ # env = {
563
+ # **dict(os.environ),
564
+ # "DJANGO_SETTINGS_MODULE": "tests.settings.completion",
565
+ # "DJANGO_COLORS": "nocolor",
566
+ # **env,
567
+ # }
568
+
569
+ # rex = re.compile
570
+ # expected_patterns = [
571
+ # rex(r"Append the above contents to (?P<file>.*)\?"), # 0
572
+ # rex(r"Create (?P<file>.*) with the above contents\?"), # 1
573
+ # rex(r"Aborted shell completion installation."), # 2
574
+ # rex(rf"Installed autocompletion for {self.shell}"), # 3
575
+ # ]
576
+
577
+ # install_command = [
578
+ # self.manage_script,
579
+ # "shellcompletion",
580
+ # "--no-color",
581
+ # "--shell",
582
+ # self.shell,
583
+ # "install",
584
+ # ]
585
+ # self.remove()
586
+ # self.verify_remove(directory=directory)
587
+
588
+ # def run_with_response(responses: t.List[str]):
589
+ # process = subprocess.Popen(
590
+ # install_command,
591
+ # env=env,
592
+ # cwd=directory,
593
+ # stdin=subprocess.PIPE,
594
+ # stdout=subprocess.PIPE,
595
+ # stderr=subprocess.STDOUT,
596
+ # text=True,
597
+ # )
598
+
599
+ # output = ""
600
+ # for response in responses:
601
+ # while True:
602
+ # line = process.stdout.readline()
603
+ # if not line:
604
+ # break
605
+ # output += line
606
+
607
+ # matched_index, matched_file = match_output(line)
608
+ # if matched_index is not None:
609
+ # process.stdin.write(response + "\n")
610
+ # process.stdin.flush()
611
+ # break
612
+
613
+ # process.wait()
614
+ # return output
615
+
616
+ # def match_output(line: str) -> t.Tuple[t.Optional[int], t.Optional[str]]:
617
+ # for i, pattern in enumerate(expected_patterns):
618
+ # match = pattern.search(line)
619
+ # if match:
620
+ # return i, match.groupdict().get("file")
621
+ # return None, None
622
+
623
+ # # Test abort sequence
624
+ # abort_output = run_with_response(["N", "N"])
625
+ # self.assertIn("Aborted shell completion installation.", abort_output)
626
+ # self.verify_remove(directory=directory)
627
+
628
+ # # Test install sequence
629
+ # install_output = run_with_response(["Y", "Y"])
630
+ # self.assertIn(f"Installed autocompletion for {self.shell}", install_output)
631
+ # self.verify_install(directory=directory)
0 commit comments