def test_should_raise_if_file_not_found(): """ Test docstring extraction handle file not found """ with pytest.raises(MlVToolException) as e: extract_docstring_from_file(f'./{uuid4()}.py') assert isinstance(e.value.__cause__, FileNotFoundError)
def test_should_raise_if_no_method_found(work_dir): """ Test docstring extraction fail if no method found """ file_docstring = '""" Not the docstring to extract """\n' python_script = join(work_dir, 'test.py') with open(python_script, 'w') as fd: fd.write(file_docstring) fd.write('import os\n') with pytest.raises(MlVToolException): extract_docstring_from_file(python_script)
def test_should_raise_if_syntax_error(work_dir): """ Test docstring extraction fail if python script syntax error """ python_script = join(work_dir, 'test.py') with open(python_script, 'w') as fd: fd.write('import os\n') fd.write('def my_method(my-param):\n') fd.write('pass\n') with pytest.raises(MlVToolException) as e: extract_docstring_from_file(python_script) assert isinstance(e.value.__cause__, SyntaxError)
def gen_dvc_command(input_path: str, dvc_output_path: str, conf: MlVToolConf, docstring_conf: dict = None): logging.info( f'Generate DVC command "{dvc_output_path}" from "{input_path}"') logging.debug(f'Global configuration {conf}') logging.debug(f'Docstring configuration {docstring_conf}') docstring_info = extract_docstring_from_file(input_path, docstring_conf) python_cmd_rel_path = relpath(input_path, conf.top_directory) extra_var = { conf.dvc_var_python_cmd_path: python_cmd_rel_path, conf.dvc_var_python_cmd_name: basename(python_cmd_rel_path) } info = get_dvc_template_data( docstring_info, conf.top_directory, python_cmd_rel_path, conf.dvc_var_meta_filename, conf.path.dvc_metadata_root_dir if conf.path else '', extra_var) templates_path = join(CURRENT_DIR, 'templates', DVC_CMD_TEMPLATE_NAME) write_template(dvc_output_path, templates_path, info=info) logging.log( logging.WARNING + 1, f'DVC bash command successfully generated in {dvc_output_path}')
def test_should_extract_resolved_docstring(work_dir): """Test jinja template is applied on extracted docstring""" docstring = '""":dvc-out param-one: {{ conf.out_path }}"""' docstring_file = join(work_dir, 'test_file') with open(docstring_file, 'w') as fd: fd.write('def my_method():\n') fd.write(f'\t{docstring}\n') fd.write('\tpass') user_conf = {'out_path': 'path/to/other'} docstring_info = extract_docstring_from_file(docstring_file, user_conf) assert docstring_info.repr == ':dvc-out param-one: path/to/other'
def test_should_extract_docstring_from_python_file(work_dir): """ Test docstring is extracted from python file with a unique method """ file_docstring = '""" Not the docstring to extract """\n' docstring = '""" This is a docstring\n On multilines\n"""' python_script = join(work_dir, 'test.py') with open(python_script, 'w') as fd: fd.write(file_docstring) fd.write('import os\n') fd.write('def my_method():\n') fd.write(f'\t{docstring}\n') fd.write('\tpass') docstring_info = extract_docstring_from_file(python_script) assert docstring_info.file_path == python_script assert docstring_info.repr == 'This is a docstring\nOn multilines' assert docstring_info.method_name == 'my_method' assert docstring_info.docstring