def test_init_install_sqlalchemy(caplog, tmp_path_factory, monkeypatch): """WARNING: THIS TEST IS AWFUL AND WE HATE IT.""" # This test is as much about changing the entire test environment with side effects as it is about actually testing # the observed behavior. library_import_name = "sqlalchemy" library_name = "sqlalchemy" cli_input = "\n\n2\nn\n" basedir = tmp_path_factory.mktemp("test_cli_init_diff") runner = CliRunner(mix_stderr=False) monkeypatch.chdir(basedir) result = runner.invoke(cli, ["--v3-api", "init", "--no-view"], input=cli_input, catch_exceptions=False) stdout = result.output assert "Always know what to expect from your data" in stdout assert "What data would you like Great Expectations to connect to" in stdout assert ( f"""Great Expectations relies on the library `{library_import_name}` to connect to your data, \ but the package `{library_name}` containing this library is not installed. Would you like Great Expectations to try to execute `pip install {library_name}` for you?""" in stdout) # NOW, IN AN EVIL KNOWN ONLY TO SLEEPLESS PROGRAMMERS, WE USE OUR UTILITY TO INSTALL SQLALCHEMY _ = execute_shell_command_with_progress_polling("pip install sqlalchemy")
def library_install_load_check( python_import_name: str, pip_library_name: str ) -> Union[int, None]: """ Dynamically load a module from strings, attempt a pip install or raise a helpful error. :return: True if the library was loaded successfully, False otherwise Args: pip_library_name: name of the library to load python_import_name (str): a module to import to verify installation """ if is_library_loadable(library_name=python_import_name): return None confirm_prompt: str = f"""Great Expectations relies on the library `{python_import_name}` to connect to your data, \ but the package `{pip_library_name}` containing this library is not installed. Would you like Great Expectations to try to execute `pip install {pip_library_name}` for you?""" continuation_message: str = f"""\nOK, exiting now. - Please execute `pip install {pip_library_name}` before trying again.""" pip_install_confirmed = toolkit.confirm_proceed_or_exit( confirm_prompt=confirm_prompt, continuation_message=continuation_message, exit_on_no=True, exit_code=1, ) if not pip_install_confirmed: cli_message(continuation_message) sys.exit(1) status_code: int = execute_shell_command_with_progress_polling( f"pip install {pip_library_name}" ) # project_distribution: Distribution = get_project_distribution() # if project_distribution: # project_name: str = project_distribution.metadata['Name'] # version: str = project_distribution.metadata['Version'] # # pkg_resources.working_set = pkg_resources.WorkingSet._build_master() working_set: WorkingSet = pkg_resources.working_set # noinspection SpellCheckingInspection distr: Distribution = pkg_resources.get_distribution(dist=pip_library_name) pkg_resources.WorkingSet.add_entry(self=working_set, entry=distr.key) library_loadable: bool = is_library_loadable(library_name=python_import_name) if status_code == 0 and library_loadable: return 0 if not library_loadable: cli_message( f"""<red>ERROR: Great Expectations relies on the library `{pip_library_name}` to connect to your data.</red> - Please execute `pip install {pip_library_name}` before trying again.""" ) return 1 return status_code