sh_cpl/unittests/unittests_shared/cli_commands.py

98 lines
2.7 KiB
Python
Raw Permalink Normal View History

2022-05-26 14:47:36 +02:00
import os
import subprocess
2022-12-01 23:23:48 +01:00
import sys
2022-05-26 14:47:36 +02:00
2022-05-26 15:29:49 +02:00
from unittests_cli.constants import CLI_PATH
2022-05-26 14:47:36 +02:00
class CLICommands:
@staticmethod
2022-05-27 18:07:12 +02:00
def _run(cmd: str, *args, output=False):
2022-05-26 14:47:36 +02:00
env_vars = os.environ
2023-02-20 15:55:20 +01:00
env_vars["CPL_IS_UNITTEST"] = "NO" if output else "YES"
2022-05-27 18:07:12 +02:00
2023-02-20 15:55:20 +01:00
command = ["python", CLI_PATH, cmd]
2022-05-26 14:47:36 +02:00
for arg in args:
command.append(arg)
2022-06-26 00:15:05 +02:00
if output:
subprocess.run(command, env=env_vars)
else:
2023-02-20 15:55:20 +01:00
subprocess.run(
command, env=env_vars, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL
)
2022-05-26 14:47:36 +02:00
2022-06-26 19:51:18 +02:00
@staticmethod
def _run_with_output(cmd: str, *args) -> str:
env_vars = os.environ
2023-02-20 15:55:20 +01:00
env_vars["CPL_IS_UNITTEST"] = "NO"
2022-06-26 19:51:18 +02:00
2023-02-20 15:55:20 +01:00
command = ["python", CLI_PATH, cmd]
2022-06-26 19:51:18 +02:00
for arg in args:
command.append(arg)
2022-12-01 23:34:00 +01:00
return subprocess.run(command, env=env_vars, check=True, capture_output=True, text=True).stdout
2022-06-26 19:51:18 +02:00
2022-06-03 23:35:07 +02:00
@classmethod
2022-06-26 00:15:05 +02:00
def add(cls, source: str, target: str, output=False):
2023-02-20 15:55:20 +01:00
cls._run("add", source, target, output=output)
2022-06-03 23:35:07 +02:00
2022-06-23 21:49:44 +02:00
@classmethod
2022-06-26 00:15:05 +02:00
def build(cls, output=False):
2023-02-20 15:55:20 +01:00
cls._run("build", output=output)
2022-06-23 21:49:44 +02:00
2022-05-26 14:47:36 +02:00
@classmethod
2022-05-27 18:07:12 +02:00
def generate(cls, schematic: str, name: str, output=False):
2023-02-20 15:55:20 +01:00
cls._run("generate", schematic, name, output=output)
2022-05-26 14:47:36 +02:00
2022-06-21 08:56:26 +02:00
@classmethod
2022-06-27 20:16:37 +02:00
def install(cls, package: str = None, is_dev=False, output=False):
2022-06-23 23:39:41 +02:00
if package is None:
2023-02-20 15:55:20 +01:00
cls._run("install", output=output)
2022-06-23 23:39:41 +02:00
return
2023-02-20 15:55:20 +01:00
cls._run("install", package, "--dev" if is_dev else "", output=output)
2022-06-21 08:56:26 +02:00
2022-05-26 14:47:36 +02:00
@classmethod
2022-05-27 18:07:12 +02:00
def new(cls, project_type: str, name: str, *args, output=False):
2023-02-20 15:55:20 +01:00
cls._run("new", project_type, name, *args, output=output)
2022-06-03 23:44:26 +02:00
2022-06-23 23:47:29 +02:00
@classmethod
2022-06-26 00:15:05 +02:00
def publish(cls, output=False):
2023-02-20 15:55:20 +01:00
cls._run("publish", output=output)
2022-06-23 23:47:29 +02:00
2022-06-03 23:44:26 +02:00
@classmethod
2022-06-26 00:15:05 +02:00
def remove(cls, project: str, output=False):
2023-02-20 15:55:20 +01:00
cls._run("remove", project, output=output)
2022-06-26 00:15:05 +02:00
@classmethod
def run(cls, project: str = None, is_dev=False, output=False):
args = []
if is_dev:
2023-02-20 15:55:20 +01:00
args.append("--dev")
2022-06-26 00:15:05 +02:00
if project is None:
2023-02-20 15:55:20 +01:00
cls._run("run", *args, output=output)
2022-06-26 00:15:05 +02:00
return
2023-02-20 15:55:20 +01:00
cls._run("run", project, *args, output=output)
2022-06-21 09:24:56 +02:00
2022-06-26 00:39:16 +02:00
@classmethod
def start(cls, is_dev=False, output=False):
args = []
if is_dev:
2023-02-20 15:55:20 +01:00
args.append("--dev")
2023-02-20 15:55:20 +01:00
cls._run("start", *args, output=output)
2022-06-26 00:39:16 +02:00
2022-06-21 09:24:56 +02:00
@classmethod
2022-06-27 20:16:37 +02:00
def uninstall(cls, package: str, is_dev=False, output=False):
2023-02-20 15:55:20 +01:00
cls._run("uninstall", package, "--dev" if is_dev else "", output=output)
2022-06-26 19:51:18 +02:00
2022-06-27 09:10:21 +02:00
@classmethod
def update(cls, output=False):
2023-02-20 15:55:20 +01:00
cls._run("update", output=output)
2022-06-27 09:10:21 +02:00
2022-06-26 19:51:18 +02:00
@classmethod
def version(cls) -> str:
2023-02-20 15:55:20 +01:00
return cls._run_with_output("version")