import asyncio
import sys

import pytest


pytestmark = pytest.mark.asyncio()


async def test_command_line_send(smtpd_server, hostname, port):
    proc = await asyncio.create_subprocess_exec(
        sys.executable,
        b"-m",
        b"aiosmtplib",
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE,
    )

    expected = (
        (b"hostname", bytes(hostname, "ascii")),
        (b"port", bytes(str(port), "ascii")),
        (b"From", b"sender@example.com"),
        (b"To", b"recipient@example.com"),
        (b"message", b"Subject: Hello World\n\nHi there."),
    )

    for expected_output, write_bytes in expected:
        output = await proc.stdout.readuntil(separator=b":")
        assert expected_output in output
        proc.stdin.write(write_bytes + b"\n")
        await proc.stdin.drain()

    proc.stdin.write_eof()
    await proc.stdin.drain()

    return_code = await proc.wait()
    assert return_code == 0

@@ -338,24 +338,22 @@
  )%0A%0A    
-expected
+inputs
  = (%0A   
@@ -360,22 +360,8 @@
     
- (b%22hostname%22,
  byt
@@ -373,33 +373,32 @@
 stname, %22ascii%22)
-)
 ,%0A        (b%22por
@@ -394,18 +394,8 @@
     
- (b%22port%22,
  byt
@@ -416,17 +416,16 @@
 %22ascii%22)
-)
 ,%0A      
@@ -429,18 +429,8 @@
     
- (b%22From%22,
  b%22s
@@ -439,33 +439,32 @@
 der@example.com%22
-)
 ,%0A        (b%22To%22
@@ -460,16 +460,8 @@
     
- (b%22To%22,
  b%22r
@@ -481,17 +481,16 @@
 ple.com%22
-)
 ,%0A      
@@ -494,21 +494,8 @@
     
- (b%22message%22,
  b%22S
@@ -531,17 +531,16 @@
 re.%22
-)
 ,%0A    )%0A
 %0A   
@@ -539,119 +539,70 @@
   )%0A
-%0A 
    
-for expected_output, write_bytes in expected:%0A        output = await proc.stdout.readuntil(separator=b%22:%22)
+ messages = (%0A        b%22SMTP server hostname %5Blocalhost%5D:%22,
 %0A   
@@ -610,213 +610,275 @@
     
-assert expected_output in output%0A        proc.stdin.write(write_bytes + b%22%5Cn%22)%0A        await proc.stdin.drain()%0A%0A    proc.stdin.write_eof()%0A    await proc.stdin.drain()%0A%0A    return_code = await proc.wait()
+b%22SMTP server port %5B25%5D:%22,%0A        b%22From:%22,%0A        b%22To:%22,%0A        b%22Enter message, end with %5ED:%22,%0A    )%0A%0A    output, errors = await proc.communicate(input=b%22%5Cn%22.join(inputs))%0A%0A    assert errors is None%0A    for message in messages:%0A        assert message in output%0A
 %0A   
@@ -889,15 +889,19 @@
 ert 
+proc.
 return
-_
 code

Fix deadlock in subprocess pipes