gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Please clean the following code to make it more aesthetically pleasing, readable, and…
- User
Please clean the following code to make it more aesthetically pleasing, readable, and maintainable. Ensure that no functionality or core logic of the code is altered. provide only the final version of the cleaned code, without any additional explanations or comments.
def pytest_setup(code, repo_path, test_dir, test_file_name): """Create necessary test directory and files.""" try: if not os.path.exists(test_dir): os.makedirs(test_dir) logger.info(f"Test directory created: {test_dir}") with open( f"{test_dir}/{test_file_name}", "w", encoding="utf-8" ) as script_file: script_file.write(code) logger.info(f"Test code saved in: {test_dir}/{test_file_name}") except OSError as e: raise PytestSetupError(8002, str(e)) try: with open(f"{repo_path}/pytest.ini", "w") as script_file: script_file.write("[pytest]\npythonpath = src") logger.info(f"pytest.ini created in location: {repo_path}/pytest.ini") except OSError as e: raise PytestSetupError(8003, str(e)) try: if not os.path.exists(f"env/{os.path.basename(repo_path)}"): packages = [ "pytest==7.4.4", "pytest-cov==5.0.0", "pytest-html==4.1.1", "pytest-json-report==1.5.0", "pytest-metadata==2.0.4", ] environment = os.path.abspath(f"env/{os.path.basename(repo_path)}") env_creation_success = create_environment( environment, f"{repo_path}/requirements.txt", packages ) if env_creation_success: logger.info("Pytest setup successful") return True else: raise PytestSetupError(8004) except Exception as e: logger.exception(f"Unexpected error during pytest setup: {e}") raise PytestSetupError(8004, f"Unexpected error: {str(e)}") - Assistant
def pytest_setup(code, repo_path, test_dir, test_file_name): """Create necessary test directory and files.""" try: if not os.path.exists(test_dir): os.makedirs(test_dir) logger.info(f"Test directory created: {test_dir}") with open(f"{test_dir}/{test_file_name}", "w", encoding="utf-8") as script_file: script_file.write(code) logger.info(f"Test code saved in: {test_dir}/{test_file_name}") except OSError as e: raise PytestSetupError(8002, str(e)) try: with open(f"{repo_path}/pytest.ini", "w") as script_file: script_file.write("[pytest]\npythonpath = src") logger.info(f"pytest.ini created in location: {repo_path}/pytest.ini") except OSError as e: raise PytestSetupError(8003, str(e)) try: env_path = f"env/{os.path.basename(repo_path)}" if not os.path.exists(env_path): packages = [ "pytest==7.4.4", "pytest-cov==5.0.0", "pytest-html==4.1.1", "pytest-json-report==1.5.0", "pytest-metadata==2.0.4", ] environment = os.path.abspath(env_path) env_creation_success = create_environment(environment, f"{repo_path}/requirements.txt", packages) if env_creation_success: logger.info("Pytest setup successful") return True else: raise PytestSetupError(8004) except Exception as e: logger.exception(f"Unexpected error during pytest setup: {e}") raise PytestSetupError(8004, f"Unexpected error: {str(e)}")