sh_cpl/unittests/unittests_shared/cli_commands.py

89 lines
2.5 KiB
Python
Raw 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
2022-05-27 18:07:12 +02:00
env_vars['CPL_IS_UNITTEST'] = 'NO' if output else 'YES'
2022-05-26 15:29:49 +02: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:
2022-06-26 00:39:16 +02: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
2022-12-01 23:34:00 +01:00
env_vars['CPL_IS_UNITTEST'] = 'NO'
2022-06-26 19:51:18 +02:00
command = ['python', CLI_PATH, cmd]
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):
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):
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):
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:
cls._run('install', output=output)
return
2022-06-27 20:16:37 +02: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):
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):
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):
cls._run('remove', project, output=output)
@classmethod
def run(cls, project: str = None, output=False):
if project is None:
cls._run('run', output=output)
return
cls._run('run', project, output=output)
2022-06-21 09:24:56 +02:00
2022-06-26 00:39:16 +02:00
@classmethod
def start(cls, output=False):
cls._run('start', output=output)
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):
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):
cls._run('update', output=output)
2022-06-26 19:51:18 +02:00
@classmethod
def version(cls) -> str:
return cls._run_with_output('version')