# Path: tests/fixture_data.py
# CONFIG_YAML = """
# ---
# 
# - step:
#     name: Train model
#     image: busybox
#     command: "false"
#     inputs:
#       - name: in1
#         default: http://example.com/
#     parameters:
#       - name: max_steps
#         pass-as: --max_steps={v}
#         description: Number of steps to run the trainer
#         type: integer
#         default: 300
#       - name: learning_rate
#         type: float
#         default: 0.1337
#       - name: enable_mega_boost
#         type: flag
#     environment-variables:
#       - name: testenvvar
#         default: 'test'
# 
# """
# 
# PYTHON_SOURCE = """
# import os
# 
# print("I'm just a poor boy without any utils.")
# """
# 
# PYTHON_SOURCE_USING_VALOHAI_UTILS = """
# import os
# 
# import valohai
# 
# params = {
#     "param1": True,
#     "param2": "asdf",
#     "param3": 123,
#     "param4": 0.0001,
# }
# 
# inputs = {"input1": "asdf", "input2": ["yolol", "yalala"]}
# 
# 
# def prepare(a, b):
#     print("this is fake method %s %s" % (a, b))
# 
# 
# valohai.prepare(step="foobar1", parameters=params, inputs=inputs)
# """
# 
# Path: valohai_cli/commands/yaml/step.py
# @click.command()
# @click.argument('filenames', nargs=-1, type=click.Path(file_okay=True, exists=True, dir_okay=False), required=True)
# def step(filenames: List[str]) -> None:
#     """
#     Update a step config(s) in valohai.yaml based on Python source file(s).
# 
#     Example:
# 
#         vh yaml step hello.py
# 
#     :param filenames: Path(s) of the Python source code files.
#     """
#     project = get_project()
#     if project is None:
#         info("no project linked - assuming files are in current directory.")
#         project = Project(data={}, directory=os.getcwd())
#         project.name = "YAML command simulated project"
# 
#     config_path = project.get_config_filename()
# 
#     for source_path in filenames:
#         if not os.path.isfile(config_path):
#             update_yaml_from_source(source_path, project)
#             info("valohai.yaml generated.")
#             create_or_update_requirements(project)
#         elif yaml_needs_update(source_path, project):
#             update_yaml_from_source(source_path, project)
#             info("valohai.yaml updated.")
#             create_or_update_requirements(project)
#         else:
#             info("valohai.yaml already up-to-date.")
# 
# Path: valohai_cli/ctx.py
# @overload
# def get_project(dir: Optional[str] = None, require: Literal[True] = True) -> ProjectOrRemoteProject:
#     ...


import os
from tests.fixture_data import CONFIG_YAML, PYTHON_SOURCE, PYTHON_SOURCE_USING_VALOHAI_UTILS
from valohai_cli.commands.yaml.step import step
from valohai_cli.ctx import get_project



def test_yaml(runner, logged_in_and_linked):
    # Try to generate YAML from random .py source code
    source_path = os.path.join(get_project().directory, 'mysnake.py')
    with open(source_path, 'w') as python_fp:
        python_fp.write(PYTHON_SOURCE)
    args = ([source_path])
    rv = runner.invoke(step, args, catch_exceptions=True)
    assert isinstance(rv.exception, ValueError)

    # Generate YAML from .py source code that is using valohai-utils

# Path: tests/fixture_data.py
# CONFIG_YAML = """
# ---
# 
# - step:
#     name: Train model
#     image: busybox
#     command: "false"
#     inputs:
#       - name: in1
#         default: http://example.com/
#     parameters:
#       - name: max_steps
#         pass-as: --max_steps={v}
#         description: Number of steps to run the trainer
#         type: integer
#         default: 300
#       - name: learning_rate
#         type: float
#         default: 0.1337
#       - name: enable_mega_boost
#         type: flag
#     environment-variables:
#       - name: testenvvar
#         default: 'test'
# 
# """
# 
# PYTHON_SOURCE = """
# import os
# 
# print("I'm just a poor boy without any utils.")
# """
# 
# PYTHON_SOURCE_USING_VALOHAI_UTILS = """
# import os
# 
# import valohai
# 
# params = {
#     "param1": True,
#     "param2": "asdf",
#     "param3": 123,
#     "param4": 0.0001,
# }
# 
# inputs = {"input1": "asdf", "input2": ["yolol", "yalala"]}
# 
# 
# def prepare(a, b):
#     print("this is fake method %s %s" % (a, b))
# 
# 
# valohai.prepare(step="foobar1", parameters=params, inputs=inputs)
# """
# 
# Path: valohai_cli/commands/yaml/step.py
# @click.command()
# @click.argument('filenames', nargs=-1, type=click.Path(file_okay=True, exists=True, dir_okay=False), required=True)
# def step(filenames: List[str]) -> None:
#     """
#     Update a step config(s) in valohai.yaml based on Python source file(s).
# 
#     Example:
# 
#         vh yaml step hello.py
# 
#     :param filenames: Path(s) of the Python source code files.
#     """
#     project = get_project()
#     if project is None:
#         info("no project linked - assuming files are in current directory.")
#         project = Project(data={}, directory=os.getcwd())
#         project.name = "YAML command simulated project"
# 
#     config_path = project.get_config_filename()
# 
#     for source_path in filenames:
#         if not os.path.isfile(config_path):
#             update_yaml_from_source(source_path, project)
#             info("valohai.yaml generated.")
#             create_or_update_requirements(project)
#         elif yaml_needs_update(source_path, project):
#             update_yaml_from_source(source_path, project)
#             info("valohai.yaml updated.")
#             create_or_update_requirements(project)
#         else:
#             info("valohai.yaml already up-to-date.")
# 
# Path: valohai_cli/ctx.py
# @overload
# def get_project(dir: Optional[str] = None, require: Literal[True] = True) -> ProjectOrRemoteProject:
#     ...


# Path: tests/commands/yaml/test_step.py
import os
from tests.fixture_data import CONFIG_YAML, PYTHON_SOURCE, PYTHON_SOURCE_USING_VALOHAI_UTILS
from valohai_cli.commands.yaml.step import step
from valohai_cli.ctx import get_project




def test_yaml(runner, logged_in_and_linked):
    # Try to generate YAML from random .py source code
    source_path = os.path.join(get_project().directory, 'mysnake.py')
    with open(source_path, 'w') as python_fp:
        python_fp.write(PYTHON_SOURCE)
    args = ([source_path])
    rv = runner.invoke(step, args, catch_exceptions=True)
    assert isinstance(rv.exception, ValueError)

    # Generate YAML from .py source code that is using valohai-utils

    source_path = os.path.join(get_project().directory, 'mysnake.py')