def test_simple_template(): ctx = Context() ctx.source_folder = Path('.').absolute() template_file = Path('./template.tex').absolute() template_file.touch() template.get_template(ctx) assert ctx.template_source == template_file
def test_multiple_template(): ctx = Context() ctx.source_folder = Path('.').absolute() sub1 = Path('./sub1').absolute() sub1.mkdir() template_file1 = Path(sub1, 'template.tex').absolute() template_file1.touch() sub2 = Path(sub1, 'sub2').absolute() sub2.mkdir() template_file2 = Path(sub2, 'template.tex').absolute() template_file2.touch() sub3 = Path(sub2, 'sub3').absolute() sub3.mkdir() template_file3 = Path(sub3, 'template.tex').absolute() template_file3.touch() ctx.source_folder = Path(sub3).absolute() template.get_template(ctx) assert ctx.template_source == template_file3 template_file3.unlink() template.get_template(ctx) assert ctx.template_source == template_file2 template_file2.unlink() template.get_template(ctx) assert ctx.template_source == template_file1
def test_single_settings(): ctx = Context() ctx.source_folder = Path('.').absolute() settings_ = Path('./settings.yml') settings_.write_text('key: value') settings.get_settings(ctx) assert ctx.settings.data == {'key': 'value'}
def test_empty_settings(): ctx = Context() ctx.source_folder = Path('.').absolute() settings_ = Path('./settings.yml') settings_.write_text('', encoding='utf8') with pytest.raises(ConvertError): settings.get_settings(ctx)
def test_get_media_folder(): ctx = Context() ctx.source_folder = Path('.').absolute() media = Path('./media').absolute() media.mkdir() get_media_folders(ctx) assert ctx.media_folders == [media]
def test_get_image_full_path(): test_file = Path('./test.png') test_file.touch() ctx = Context() ctx.media_folders = [Path('.')] ctx.image_current = test_file.name assert image._get_image_full_path(ctx) == str(test_file.absolute())
def test_check_for_used_images_only_one_media_folder(caplog): caplog.set_level(30, 'EDLM') caplog.clear() ctx = Context() ctx.media_folders = ['.'] ctx.images_used = set() check_for_unused_images(ctx) assert not caplog.text
def test_template_loader_no_template(): ctx = Context() template = Path('./template.tex').absolute() ctx.template_source = template env = Environment(autoescape=True) loader = latex.TexTemplateLoader(ctx) with pytest.raises(TemplateNotFound): loader.get_source(env, 'template.tex')
def test_check_for_unused_images(caplog, media: Path): caplog.set_level(30, 'EDLM') caplog.clear() ctx = Context() ctx.media_folders = [str(media.absolute()), '.'] ctx.images_used = set() check_for_unused_images(ctx) for file in media.iterdir(): assert file.name in caplog.text
def test_template_loader(): ctx = Context() template = Path('./template.tex').absolute() template.write_text('moo', encoding='utf8') ctx.template_source = template env = Environment(autoescape=True) loader = latex.TexTemplateLoader(ctx) source, template_, reload = loader.get_source(env, 'template.tex') assert source == 'moo' assert template_ == template assert reload is False
def test_process_template_latex_error(): ctx = Context() template = Path('./template.tex').absolute() ctx.template_source = template ctx.media_folders = [] template.touch() when(latex.TexTemplateLoader).get_source(...).thenRaise( TemplateNotFound(ctx)) ctx.template_source = template with pytest.raises(TemplateNotFound): latex.process_latex(ctx)
def pdf(source_folder, keep_temp_dir, force): """ Converts content of SOURCE_FOLDER(s) recursively for folders containing "index.md" files and convert them to PDF """ ctx = Context() ctx.keep_temp_dir = keep_temp_dir or CFG.keep_temp_dir ctx.regen = force for folder in source_folder: make_pdf(ctx, folder)
def test_markdown_preprocessor(): ctx = Context() index_file = Path('./index.md') index_file.write_text('', encoding='utf8') ctx.index_file = index_file ctx.includes = [] ctx.markdown_text = '' when(_markdown).process_aliases(ctx) when(_markdown).process_images(ctx) when(_markdown).process_references(ctx) _markdown.process_markdown(ctx) verifyStubbedInvocationsAreUsed()
def _get_standard_build_folder() -> typing.Tuple[Context, Path]: ctx = Context() pandoc = Path('pandoc') pandoc.touch() src_folder = Path('./test').absolute() src_folder.mkdir() ctx.source_folder = src_folder source_file = Path('./test/index.md') source_file.touch() ctx.source_file = source_file ctx.markdown_text = '' return ctx, pandoc
def test_process_images(media): ctx = Context() ctx.image_max_width = 200 ctx.media_folders = [str(media.absolute()), '.'] pictures = list(media.iterdir()) ctx.markdown_text = f""" ![picture1](http://picture1.com) ![picture2]({pictures[0].name}) ![picture3](http://picture1.com) """ image.process_images(ctx) print(ctx.markdown_text) assert ctx.markdown_text == f"""
def test_process_references_no_ref(): ctx = Context() ctx.settings = Settings() ctx.markdown_text = """ This is some dummy markdown text. This should be ref1 //ref1. This should be ref3 //ref3. This should be ref1 //ref1 again. """ process_references(ctx) assert ctx.latex_refs == [] assert ctx.markdown_text == """
def test_process_references(): ctx = Context() ctx.settings.references = DUMMY_REFS ctx.markdown_text = """ This is some dummy markdown text. This should be ref1 //ref1. This should be ref3 //ref3. This should be ref1 //ref1 again. """ process_references(ctx) assert ctx.latex_refs == ['\\href{ref1_link}{ref1_name}', '\\href{ref3_link}{ref3_name}'] assert ctx.markdown_text == """
def test_process_image_http(): ctx = Context() ctx.images_used = set() match = mock() when(match).group('caption').thenReturn('caption') when(match).group('picture').thenReturn('http://something') when(match).group('extras').thenReturn('extras') when(image)._process_image_width(ctx) when(image)._get_image_full_path(ctx).thenReturn('image_current') result = image._process_image(ctx, match) assert result == '![caption](http://something)extras' assert ctx.image_current == 'http://something' verify(image)._process_image_width(ctx) when(image)._get_image_full_path(ctx)
def test_get_media_folder_multiple(): sub1 = Path('./sub1').absolute() sub1.mkdir() media1 = Path(sub1, 'media').absolute() media1.mkdir() sub2 = Path(sub1, 'sub2').absolute() sub2.mkdir() media2 = Path(sub2, 'media').absolute() media2.mkdir() sub3 = Path(sub2, 'sub3').absolute() sub3.mkdir() media3 = Path(sub3, 'media').absolute() media3.mkdir() ctx = Context() ctx.source_folder = sub3 get_media_folders(ctx) assert ctx.media_folders == [media3, media2, media1]
def test_nested_settings(): sub1 = Path('./sub1').absolute() sub1.mkdir() settings_1 = Path(sub1, 'settings.yml').absolute() settings_1.write_text( 'key1: value1\n' 'key2: value1\n' 'key3: value1\n' 'key4:\n' ' key1: value1\n' ' key2: value1\n' ' key3: value1\n', encoding='utf8') sub2 = Path(sub1, 'sub2').absolute() sub2.mkdir() settings_2 = Path(sub2, 'settings.yml').absolute() settings_2.write_text('key2: value2\n' 'key4:\n' ' key2: value2\n', encoding='utf8') sub3 = Path(sub2, 'sub3').absolute() sub3.mkdir() settings_3 = Path(sub3, 'settings.yml').absolute() settings_3.write_text('key3: value3\n' 'key4:\n' ' key3: value3\n', encoding='utf8') ctx = Context() ctx.source_folder = sub3 settings.get_settings(ctx) assert ctx.settings.data == { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3', }, }
def _download_existing_file(ctx: Context): if not ctx.out_file.exists(): ctx.info(f'trying to download {ctx.out_file.name}') url = BASE_URL + urllib.parse.quote(ctx.out_file.name) if elib.downloader.download(url, ctx.out_file): ctx.info('download successful') else: ctx.info('download failed')
def test_make_pdf_parent_folder(): ctx = Context() for sub_folder in [ Path('./sub1'), Path('./sub2'), Path('./sub3'), Path('./sub4'), ]: sub_folder.mkdir() Path(sub_folder, 'index.md').touch() Path('./sub5').mkdir() when(_make_pdf)._build_folder(ctx) _make_pdf.make_pdf(ctx, Path('.')) verify(_make_pdf, times=4)._build_folder(ctx)
def make_pdf(ctx: Context, source_folder: Path): """ Makes a PDF document from a source folder Args: ctx: Context source_folder: source folder """ _remove_artifacts() source_folder = elib.path.ensure_dir(source_folder).absolute() LOGGER.info(f'analyzing folder: "{source_folder}"') if _is_source_folder(source_folder): ctx.source_folder = source_folder _build_folder(ctx) else: for child in source_folder.iterdir(): if _is_source_folder(child): ctx.source_folder = child.absolute() _build_folder(ctx)
def test_process_template(): ctx = Context() template = Path('./template.tex').absolute() template_out = Path('./out.tex').absolute() ctx.template_file = template_out ctx.template_source = template ctx.media_folders = [] template.touch() ctx.template_source = template latex.process_latex(ctx) assert ctx.template_file.read_text(encoding='utf8') == ''
def skip_file(ctx: Context) -> bool: """ Checks if a file should be skipped Tests for: - EDLM version - index.md - template.tex - media folders content Args: ctx: Context Returns: True if file should be skipped """ if ctx.out_file.exists(): pdf = pdfrw.PdfReader(str(ctx.out_file.absolute())) creator = pdfstring.PdfString.to_unicode(pdf.Info.Creator) if creator != 'EDLM ' + __version__: ctx.info( 'document generated with an older version of EDLM, regenerating' ) return False producer = pdfstring.PdfString.to_unicode(pdf.Info.Producer) if producer != 'EDLM ' + _get_document_hash(ctx): ctx.info('document updated, regenerating') return False ctx.info('this document has not been modified, skipping it') if ctx.regen: ctx.info('forcing re-generation of all documents anyway') return False return True return False
def test_make_pdf_empty_folder(): ctx = Context() when(_make_pdf)._build_folder(ctx) _make_pdf.make_pdf(ctx, Path('.')) verify(_make_pdf, times=0)._build_folder(ctx)
def test_max_image_wrong_value(format_): ctx = Context() ctx.paper_size = format_ with pytest.raises(ValueError): _make_pdf._set_max_image_width(ctx)
def test_max_image_width(format_): ctx = Context() ctx.paper_size = format_ _make_pdf._set_max_image_width(ctx) assert ctx.image_max_width == int(_make_pdf.PAPER_FORMATS_WIDTH[format_])
def test_make_pdf_document_folder(): ctx = Context() Path('./index.md').touch() when(_make_pdf)._build_folder(ctx) _make_pdf.make_pdf(ctx, Path('.')) verify(_make_pdf, times=1)._build_folder(ctx)
def test_process_template_no_template(): ctx = Context() template = Path('./template.tex').absolute() ctx.template_source = template with pytest.raises(FileNotFoundError): latex.process_latex(ctx)