def test_unify_tex_with_relative_input(): with TemporaryDirectory() as tex_dir: file_contents = {} tex_dir_basename = os.path.basename(tex_dir) file_contents[ "main.tex"] = rf"\input{{../{tex_dir_basename}/other.tex}}" file_contents["other.tex"] = "Hello world!" create_temp_files(tex_dir, file_contents) assert expand_tex(tex_dir, "main.tex") == "Hello world!"
def test_unify_tex_with_input_without_braces(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": r"\input other.tex", "other.tex": "Hello world!" }, ) assert expand_tex(tex_dir, "main.tex") == "Hello world!"
def test_do_not_unify_with_tex_files_outside_of_directory(): with TemporaryDirectory() as tex_dir, TemporaryDirectory() as other_dir: forbidden_input_path = os.path.join(other_dir, "other.tex") forbidden_posix_input_path = forbidden_input_path.replace( os.sep, posixpath.sep) create_temp_files(other_dir, {"other.tex": "Hello world!"}) create_temp_files( tex_dir, {"main.tex": rf"\input{{{forbidden_posix_input_path}}}"}) assert (expand_tex( tex_dir, "main.tex") == rf"\input{{{forbidden_posix_input_path}}}")
def test_file_name_with_spaces_and_quotes(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": r'\input "do expand"', "do expand.tex": "Hello world!", }, ) assert expand_tex(tex_dir, "main.tex") == "Hello world!"
def test_unify_tex_with_input_with_spaces_in_path(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": r"\input{other file.tex}", "other file.tex": "Hello world!" }, ) assert expand_tex(tex_dir, "main.tex") == "Hello world!"
def test_unify_tex_with_include_ignore_non_tex_files_with_same_filename(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": r"\include{other}", "other.aux": "Junk contents" }, ) assert expand_tex(tex_dir, "main.tex") == r"\include{other}"
def test_unify_tex_leave_reference_to_input_that_was_not_found(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { # In this test case, there is no other.tex file. So the macro won't be expanded. "main.tex": r"\input{other.tex}" }, ) assert expand_tex(tex_dir, "main.tex") == r"\input{other.tex}"
def process(self, item: NormalizationTask) -> Iterator[None]: if item.compiled_tex_file.path not in os.listdir( item.normalized_sources_dir): logging.warning( # pylint: disable=logging-not-lazy "TeX file %s could not be found in sources directory for paper %s. " + "This may mean that the TeX file is specified using a relative path (e.g., " + "with '../'). To avoid unanticipated side effects on the file system, this " + "TeX file will not be normalized.", item.compiled_tex_file.path, item.arxiv_id, ) yield None tex_path = os.path.join(item.normalized_sources_dir, item.compiled_tex_file.path) if not os.path.isfile(tex_path): logging.warning( # pylint: disable=logging-not-lazy "TeX file at path %s for paper %s is not an actual file (i.e., it might be a " + "link). This file will not be normalized, in case there's something fishy " + "with this directory of sources that could cause files outside of the TeX " + "project to be modified.", item.compiled_tex_file.path, item.arxiv_id, ) yield None expanded = expand_tex(item.normalized_sources_dir, item.compiled_tex_file.path) if expanded is None: logging.warning( "Could not expand TeX file %s for paper %s.", item.compiled_tex_file.path, item.arxiv_id, ) return # Overwrite the original file with the unified file. This is so that as little as # possible will be changed in the sources directory, in the hopes that it will # cause AutoTeX to process the unified file the same way as the original file. with open(tex_path, "w", encoding="utf-8") as tex_file: tex_file.write(expanded) logging.debug( "TeX file %s for paper %s has been expanded.", item.compiled_tex_file.path, item.arxiv_id, ) yield None
def test_unify_tex_with_nested_input(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": r"\input{other1.tex}", "other1.tex": r"\input{other2.tex}", "other2.tex": "Hello world!", }, ) assert expand_tex(tex_dir, "main.tex") == "Hello world!"
def test_unify_tex_with_input_with_absolute_path(): with TemporaryDirectory() as tex_dir: file_contents = {} # TeX paths are expected in Unix convention. This conversion of the path makes it # more likely that this test code will run correctly on Windows computers. absolute_input_path = os.path.join(tex_dir, "other.tex") absolute_posix_input_path = absolute_input_path.replace( os.sep, posixpath.sep) file_contents["main.tex"] = rf"\input{{{absolute_posix_input_path}}}" file_contents[absolute_input_path] = "Hello world!" create_temp_files(tex_dir, file_contents) assert expand_tex(tex_dir, "main.tex") == "Hello world!"
def test_unify_with_inputs_from_inputs_in_child_directory(): with TemporaryDirectory() as tex_dir: child_dir = os.path.join(tex_dir, "child_dir") os.makedirs(child_dir) create_temp_files( tex_dir, { "main.tex": r"\input{child_dir/other1}", "other2.tex": "Hello world!" }, ) create_temp_files(child_dir, {"other1.tex": r"\input{other2.tex}"}) assert expand_tex(tex_dir, "main.tex") == "Hello world!"
def test_unify_tex_with_include(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": r"\include{other}", "other.tex": "Hello world!" }, ) expanded = expand_tex(tex_dir, "main.tex") assert "Hello world!" in expanded assert r"\clearpage" in expanded assert r"\@input{other.aux}" in expanded
def test_expand_input_on_same_line_after_endinput(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": "\\input{other1.tex}", "other1.tex": "other1 content.\\endinput\\input{other2.tex}\\endinput", "other2.tex": "other2 content.", }, ) assert expand_tex(tex_dir, "main.tex") == "other1 content.other2 content."
def test_unify_tex_stop_input_after_line_with_endinput(): with TemporaryDirectory() as tex_dir: create_temp_files( tex_dir, { "main.tex": ("\\input{other.tex}\n" + "Text after input macro."), "other.tex": ("Hello world!\n" + "Text before endinput.\\endinput Text after endinput. % Comment\n" + "Text on line after endinput."), }, ) assert expand_tex(tex_dir, "main.tex") == ( "Hello world!\n" + "Text before endinput.Text after endinput. % Comment\n" + "Text after input macro.")