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

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,
    )

    inputs = (
        bytes(hostname, "ascii"),
        bytes(str(port), "ascii"),
        b"sender@example.com",
        b"recipient@example.com",
        b"Subject: Hello World\n\nHi there.",
    )
    messages = (
        b"SMTP server hostname [localhost]:",
        b"SMTP server port [25]:",
        b"From:",
        b"To:",
        b"Enter message, end with ^D:",
    )

    output, errors = await proc.communicate(input=b"\n".join(inputs))

    assert errors is None
    for message in messages:
        assert message in output

    assert proc.returncode == 0

Fix deadlock in subprocess pipes
Fix deadlock in subprocess pipes

  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 = (
+     inputs = (
-         (b"hostname", bytes(hostname, "ascii")),
+         bytes(hostname, "ascii"),
-         (b"port", bytes(str(port), "ascii")),
+         bytes(str(port), "ascii"),
-         (b"From", b"sender@example.com"),
+         b"sender@example.com",
-         (b"To", b"recipient@example.com"),
+         b"recipient@example.com",
-         (b"message", b"Subject: Hello World\n\nHi there."),
+         b"Subject: Hello World\n\nHi there.",
+     )
+     messages = (
+         b"SMTP server hostname [localhost]:",
+         b"SMTP server port [25]:",
+         b"From:",
+         b"To:",
+         b"Enter message, end with ^D:",
      )
  
+     output, errors = await proc.communicate(input=b"\n".join(inputs))
-     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()
+     assert errors is None
+     for message in messages:
+         assert message in output
  
-     return_code = await proc.wait()
-     assert return_code == 0
+     assert proc.returncode == 0
  
Fix deadlock in subprocess pipes
## Code Before:
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

## Instruction:
Fix deadlock in subprocess pipes
## Code After:
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,
    )

    inputs = (
        bytes(hostname, "ascii"),
        bytes(str(port), "ascii"),
        b"sender@example.com",
        b"recipient@example.com",
        b"Subject: Hello World\n\nHi there.",
    )
    messages = (
        b"SMTP server hostname [localhost]:",
        b"SMTP server port [25]:",
        b"From:",
        b"To:",
        b"Enter message, end with ^D:",
    )

    output, errors = await proc.communicate(input=b"\n".join(inputs))

    assert errors is None
    for message in messages:
        assert message in output

    assert proc.returncode == 0
