# Path: scripnix/pybin/command_for_pid.py
# COMMAND_NAME = "command-for-pid"
# 
# def command_name(pid):
#     """ Return the command name for the given Process ID (PID).
#     """
#     if not pid:
#         pid = os.getpid()
# 
#     try:
#         return psutil.Process(pid).name()
#     except ValueError:
#         raise click.ClickException("Process ID '{}' must be a positive integer.".format(pid))
#     except psutil.NoSuchProcess:
#         raise click.ClickException("Process ID '{}' does not exist.".format(pid))
# 
# @common_command_and_options(command_name=COMMAND_NAME)
# @click.argument('pid', default=0)
# def main(pid):
#     """ Return the command name for the given Process ID (PID). If a PID is not specified, use the current process.
# 
#         The command-for-pid command is part of Scripnix.
#     """
#     click.echo(command_name(pid))
# 
# Path: scripnix/util/common.py
# def operating_system(translate=True):
#     """ Return the operating system platform name (e.g., linux, macos, windows).
#     """
#     os_name = platform.system().lower()
# 
#     if translate and os_name == "darwin":
#         return "macos"
# 
#     return os_name
# 
# Path: tests/command.py
# def common_help_option(command_entry, command_name):
#     """ Test help message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = HELP_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
# 
#     version_option_switch("-h")
#     version_option_switch("--help")
# 
# def common_version_option(command_entry, command_name):
#     """ Test version message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = VERSION_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
#         assert match.group(2) == __version__
# 
#     version_option_switch("-V")
#     version_option_switch("--version")


import os
import click
import pytest
from click.testing import CliRunner
from scripnix.pybin.command_for_pid import COMMAND_NAME, command_name, main
from scripnix.util.common import operating_system
from .command import common_help_option, common_version_option


def test_invalid_pid_command_name():
    with pytest.raises(click.ClickException) as excinfo:
        command_name(999999)

    assert str(excinfo).endswith("does not exist.")


def test_main():
    runner = CliRunner()
    result = runner.invoke(main)
    assert result.exit_code == 0
    command = result.output.strip()
    assert len(command) > 0
    assert "python" in command or "py.test" in command


def test_valid_command_name():
    command = command_name(0)
    assert "python" in command or "py.test" in command

    command = command_name(os.getpid())
    assert "python" in command or "py.test" in command

    command = command_name(1)
    assert command in (["launchd"] if operating_system() == "macos" else ["init", "systemd"])


def test_version_option():

# Path: scripnix/pybin/command_for_pid.py
# COMMAND_NAME = "command-for-pid"
# 
# def command_name(pid):
#     """ Return the command name for the given Process ID (PID).
#     """
#     if not pid:
#         pid = os.getpid()
# 
#     try:
#         return psutil.Process(pid).name()
#     except ValueError:
#         raise click.ClickException("Process ID '{}' must be a positive integer.".format(pid))
#     except psutil.NoSuchProcess:
#         raise click.ClickException("Process ID '{}' does not exist.".format(pid))
# 
# @common_command_and_options(command_name=COMMAND_NAME)
# @click.argument('pid', default=0)
# def main(pid):
#     """ Return the command name for the given Process ID (PID). If a PID is not specified, use the current process.
# 
#         The command-for-pid command is part of Scripnix.
#     """
#     click.echo(command_name(pid))
# 
# Path: scripnix/util/common.py
# def operating_system(translate=True):
#     """ Return the operating system platform name (e.g., linux, macos, windows).
#     """
#     os_name = platform.system().lower()
# 
#     if translate and os_name == "darwin":
#         return "macos"
# 
#     return os_name
# 
# Path: tests/command.py
# def common_help_option(command_entry, command_name):
#     """ Test help message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = HELP_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
# 
#     version_option_switch("-h")
#     version_option_switch("--help")
# 
# def common_version_option(command_entry, command_name):
#     """ Test version message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = VERSION_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
#         assert match.group(2) == __version__
# 
#     version_option_switch("-V")
#     version_option_switch("--version")


# Path: tests/test_command_for_pid.py
import os
import click
import pytest
from click.testing import CliRunner
from scripnix.pybin.command_for_pid import COMMAND_NAME, command_name, main
from scripnix.util.common import operating_system
from .command import common_help_option, common_version_option



def test_invalid_pid_command_name():
    with pytest.raises(click.ClickException) as excinfo:
        command_name(999999)

    assert str(excinfo).endswith("does not exist.")


def test_main():
    runner = CliRunner()
    result = runner.invoke(main)
    assert result.exit_code == 0
    command = result.output.strip()
    assert len(command) > 0
    assert "python" in command or "py.test" in command


def test_valid_command_name():
    command = command_name(0)
    assert "python" in command or "py.test" in command

    command = command_name(os.getpid())
    assert "python" in command or "py.test" in command

    command = command_name(1)
    assert command in (["launchd"] if operating_system() == "macos" else ["init", "systemd"])


def test_version_option():

    common_version_option(command_entry=main, command_name=COMMAND_NAME)