Repository: h2oai/wave
File: test/cypress.py (Python)

Code before:

def run_app_with_test(
    app_file: str,
    test_dir: str,
    sleep_time: int,
    browser: Optional[str],
    start_qd: bool,
    **kwargs
):
    def do_run():
        with TempDir(os.path.join(test_dir, "cypress/integration")) as spec_dir:
            with Process(
                cmd=["python", app_file],
                env={"CYPRESS_INTEGRATION_TEST_DIR": spec_dir},
                sleep_time=sleep_time,
            ):
                specs = [f for f in os.listdir(spec_dir) if f.endswith(".spec.js")]
                if not specs:
                    logging.warning(
                        f"No cypress specs generated in {spec_dir}, "
                        f"does {app_file} has any tests defined?"
                    )
                    return
                app_name = os.path.splitext(os.path.basename(app_file))[0]
                cypress = "./node_modules/cypress/bin/cypress"
                browser_arg = f"--browser {browser}" if browser else ""
                logging.info(f"Starting cypress to run spec(s): {specs}")
                os.chdir(test_dir)
                os.system(
                    f"{cypress} run --spec {spec_dir}/*.spec.js "
                    f"--reporter junit {browser_arg} "
                    f'--reporter-options "mochaFile=cypress/reports/{app_name}.xml"'
                )

    if start_qd:
        parent_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
        qd_path = os.path.join(parent_path, 'qd')
        web_dir = os.path.join(parent_path, 'ui/build')
        with Process(cmd=[qd_path, "-web-dir", web_dir], sleep_time=sleep_time):
            do_run()
    else:
        do_run()


def file_argument(file_name: str):
    if os.path.exists(file_name) and os.path.isfile(file_name):
        return os.path.abspath(file_name)
    raise ArgumentTypeError(f"file not found: {file_name}")


def dir_argument(dir_name: str):

Review comment (refactor):
Can we move this outside the `run_app_with_test()` scope as a constant? Something like `CYPRESS_PATH`.

Code after:

def run_app_with_test(
    app_file: str,
    test_dir: str,
    sleep_time: int,
    browser: Optional[str],
    qd_path: Optional[str],
    qd_web_dir: Optional[str],
    **kwargs
):
    def do_run():
        with TempDir(os.path.join(test_dir, "cypress/integration")) as spec_dir:
            with Process(
                cmd=["python", app_file],
                env={"CYPRESS_INTEGRATION_TEST_DIR": spec_dir},
                sleep_time=sleep_time,
            ):
                specs = [f for f in os.listdir(spec_dir) if f.endswith(".spec.js")]
                if not specs:
                    logging.warning(
                        f"No cypress specs generated in {spec_dir}, "
                        f"does {app_file} has any tests defined?"
                    )
                    return
                app_name = os.path.splitext(os.path.basename(app_file))[0]
                cypress = "./node_modules/cypress/bin/cypress"
                browser_arg = f"--browser {browser}" if browser else ""
                logging.info(f"Starting cypress to run spec(s): {specs}")
                os.chdir(test_dir)
                os.system(
                    f"{cypress} run --spec {spec_dir}/*.spec.js "
                    f"--reporter junit {browser_arg} "
                    f'--reporter-options "mochaFile=cypress/reports/{app_name}.xml"'
                )

    if qd_path:
        cmd = [qd_path, "-web-dir", qd_web_dir] if qd_web_dir else [qd_path]
        with Process(cmd=cmd, sleep_time=sleep_time):
            do_run()
    else:
        do_run()


def file_argument(file_name: str):
    if os.path.exists(file_name) and os.path.isfile(file_name):
        return os.path.abspath(file_name)
    raise ArgumentTypeError(f"file not found: {file_name}")


def dir_argument(dir_name: str):
    if os.path.exists(dir_name) and os.path.isdir(dir_name):