Repository: texworld/betterbib (language: python)

Issue:
Multiple runs of betterbib-format create ugly `\\\\`-chains
eg for 

```sh
@article{foobar,
title={Foo \& Bar on \LaTeX\ @TheBridge},
}
```

The problem seems to be a missing try to decode the original value beforehand


Discussion / hints:

Fix patch:
diff --git a/src/betterbib/cli/doi2bibtex.py b/src/betterbib/cli/doi2bibtex.py
index cb61c75..bbd1160 100644
--- a/src/betterbib/cli/doi2bibtex.py
+++ b/src/betterbib/cli/doi2bibtex.py
@@ -22,9 +22,9 @@ def _create_citekey_for_entry(entry):
         entry.persons
         and "author" in entry.persons
         and entry.persons["author"]
-        and entry.persons["author"][0].last()
+        and entry.persons["author"][0].last_names
     ):
-        bibtex_key += unidecode(entry.persons["author"][0].last()[0].lower())
+        bibtex_key += unidecode(entry.persons["author"][0].last_names[0].lower())
     if "year" in entry.fields and entry.fields["year"]:
         bibtex_key += str(entry.fields["year"])
     if not bibtex_key:
diff --git a/src/betterbib/tools.py b/src/betterbib/tools.py
index 43d1d11..0c76841 100644
--- a/src/betterbib/tools.py
+++ b/src/betterbib/tools.py
@@ -234,7 +234,15 @@ def pybtex_to_bibtex_string(
 
         try:
             if key not in ["url", "doi"]:
-                value = codecs.encode(value, "ulatex")
+                # Parse the original value to get a unified version
+                value = codecs.decode(value, "ulatex", errors="replace")
+                value = codecs.encode(value, "ulatex", errors="replace")
+
+                # Replace doubled spaces
+                value = value.replace("  ", " ")
+                # Remove trailing spaces
+                value = value.rstrip()
+
         except TypeError:
             # expected unicode for encode input, but got int instead
             pass


Test patch:
diff --git a/tests/test_bibtex_title.py b/tests/test_bibtex_title.py
index ea02b03..03cf736 100644
--- a/tests/test_bibtex_title.py
+++ b/tests/test_bibtex_title.py
@@ -1,7 +1,7 @@
 import betterbib
 
 
-def test():
+def test_translate_title_capitalized():
     assert (
         betterbib.tools._translate_title(
             "The Magnus expansion and some of its applications"
@@ -10,22 +10,22 @@ def test():
     )
 
     assert (
-        betterbib.tools._translate_title("VODE: a variable-coefficient ODE solver")
-        == "{VODE:} {A} variable-coefficient {ODE} solver"
+        betterbib.tools._translate_title(
+            "On generalized averaged Gaussian formulas, II"
+        )
+        == "On generalized averaged {Gaussian} formulas, {II}"
     )
 
     assert (
-        betterbib.tools._translate_title(
-            "Peano's kernel theorem for vector-valued functions"
-        )
-        == "{Peano's} kernel theorem for vector-valued functions"
+        betterbib.tools._translate_title("Gaussian Hermitian Jacobian")
+        == "{Gaussian} {Hermitian} {Jacobian}"
     )
 
+
+def test_translate_title_ACRONYM():
     assert (
-        betterbib.tools._translate_title(
-            "Exponential Runge-Kutta methods for parabolic problems"
-        )
-        == "Exponential {Runge}-{Kutta} methods for parabolic problems"
+        betterbib.tools._translate_title("VODE: a variable-coefficient ODE solver")
+        == "{VODE:} {A} variable-coefficient {ODE} solver"
     )
 
     assert (
@@ -35,28 +35,42 @@ def test():
         == "{GMRES:} {A} generalized minimal residual algorithm"
     )
 
+
+def test_translate_title_apostrophe():
     assert (
         betterbib.tools._translate_title(
-            "On generalized averaged Gaussian formulas, II"
+            "Peano's kernel theorem for vector-valued functions"
         )
-        == "On generalized averaged {Gaussian} formulas, {II}"
+        == "{Peano's} kernel theorem for vector-valued functions"
     )
 
+
+def test_translate_title_dashed_names():
     assert (
-        betterbib.tools._translate_title("Gaussian Hermitian Jacobian")
-        == "{Gaussian} {Hermitian} {Jacobian}"
+        betterbib.tools._translate_title(
+            "Exponential Runge-Kutta methods for parabolic problems"
+        )
+        == "Exponential {Runge}-{Kutta} methods for parabolic problems"
     )
 
+
+def test_translate_title_dashes():
     assert (
         betterbib.tools._translate_title("Dash-Dash Double--Dash Triple---Dash")
         == "Dash-Dash Double--Dash Triple---Dash"
     )
 
+
+def test_translate_title_variable():
     assert betterbib.tools._translate_title("x: {X}") == "x: {X}"
 
+
+def test_translate_title_escape_dollar():
     assert (
         betterbib.tools._translate_title("{Aaa ${\\text{Pt/Co/AlO}}_{x}$ aaa bbb}")
         == "{Aaa {${\\text{Pt/Co/AlO}}_{x}$} aaa bbb}"
     )
 
+
+def test_translate_title_star():
     assert betterbib.tools._translate_title("z*") == "z*"
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 6f8a96c..88cb743 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -3,76 +3,68 @@
 
 import betterbib
 
+ref_test_cli_dedup_doi_in = (
+    "@article{foobar,\n" "doi={foobar},\n" "url = {https://doi.org/foobar}\n" "}"
+)
+
+ref_test_cli_dedup_doi_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{foobar,\n"
+    " url = {https://doi.org/foobar},\n"
+    "}\n"
+)
+
 
 def test_cli_dedup_doi():
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{foobar,\n"
-            "doi={foobar},\n"
-            "url = {https://doi.org/foobar}\n"
-            "}"
-        )
+        f.write(ref_test_cli_dedup_doi_in)
 
     betterbib.cli.dedup_doi(["--in-place", infile])
     with open(infile) as f:
-        assert f.read() == (
-            f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-            "\n"
-            "\n"
-            "@article{foobar,\n"
-            " url = {https://doi.org/foobar},\n"
-            "}\n"
-        )
+        assert f.read() == (ref_test_cli_dedup_doi_out)
     os.remove(infile)
 
 
 def test_cli_dedup_doi_stdout(capsys):
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{foobar,\n"
-            "doi={foobar},\n"
-            "url = {https://doi.org/foobar}\n"
-            "}"
-        )
+        f.write(ref_test_cli_dedup_doi_in)
 
     betterbib.cli.dedup_doi([infile])
     captured = capsys.readouterr()
 
-    assert captured.out == "".join(
-        f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-        "\n"
-        "\n"
-        "@article{foobar,\n"
-        " url = {https://doi.org/foobar},\n"
-        "}\n"
-    )
+    assert captured.out == "".join(ref_test_cli_dedup_doi_out)
     os.remove(infile)
 
 
+ref_test_cli_format_in = (
+    "@article{foobar,\n" "doi={foobar},\n" "url = {https://doi.org/foobar}\n" "}"
+)
+
+
+ref_test_cli_format_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{foobar,\n"
+    " doi = {foobar},\n"
+    " url = {https://doi.org/foobar},\n"
+    "}\n"
+)
+
+
 def test_cli_format():
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{foobar,\n"
-            "doi={foobar},\n"
-            "url = {https://doi.org/foobar}\n"
-            "}"
-        )
+        f.write(ref_test_cli_format_in)
 
     betterbib.cli.format(["--in-place", infile])
 
     with open(infile) as f:
-        assert f.read() == (
-            f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-            "\n"
-            "\n"
-            "@article{foobar,\n"
-            " doi = {foobar},\n"
-            " url = {https://doi.org/foobar},\n"
-            "}\n"
-        )
+        assert f.read() == ref_test_cli_format_out
 
     os.remove(infile)
 
@@ -80,45 +72,267 @@ def test_cli_format():
 def test_cli_format_stdout(capsys):
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{foobar,\n"
-            "doi={foobar},\n"
-            "url = {https://doi.org/foobar}\n"
-            "}"
-        )
+        f.write(ref_test_cli_format_in)
+
+    betterbib.cli.format([infile])
+    captured = capsys.readouterr()
+
+    assert captured.out == "".join(ref_test_cli_format_out)
+
+    os.remove(infile)
+
+
+ref_test_cli_format_escape_ampersand_in = (
+    "@article{foobar,\n"
+    "doi={foobar},\n"
+    "url = {https://doi.org/foobar},\n"
+    r"title = {Foo & Bar}," + "\n"
+    "}"
+)
+
+
+ref_test_cli_format_escape_ampersand_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{foobar,\n"
+    " doi = {foobar},\n"
+    " url = {https://doi.org/foobar},\n"
+    r" title = {Foo \& Bar}," + "\n"
+    "}\n"
+)
+
+
+def test_cli_format_escape_ampersand():
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_ampersand_in)
+
+    betterbib.cli.format(["--in-place", infile])
+
+    with open(infile) as f:
+        assert f.read() == ref_test_cli_format_escape_ampersand_out
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_ampersand_stdout(capsys):
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_ampersand_in)
 
     betterbib.cli.format([infile])
     captured = capsys.readouterr()
 
-    assert captured.out == "".join(
-        f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-        "\n"
-        "\n"
-        "@article{foobar,\n"
-        " doi = {foobar},\n"
-        " url = {https://doi.org/foobar},\n"
-        "}\n"
-    )
+    assert captured.out == "".join(ref_test_cli_format_escape_ampersand_out)
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_ampersand_repeated():
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_ampersand_in)
+
+    for i in range(10):
+        betterbib.cli.format(["--in-place", infile])
+
+        with open(infile) as f:
+            assert f.read() == ref_test_cli_format_escape_ampersand_out
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_ampersand_repeated_stdout(capsys):
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_ampersand_in)
+
+    for i in range(10):
+        betterbib.cli.format(["--in-place", infile])
+        betterbib.cli.format([infile])
+        captured = capsys.readouterr()
+
+        assert captured.out == "".join(ref_test_cli_format_escape_ampersand_out)
+
+    os.remove(infile)
+
+
+ref_test_cli_format_escape_command_in = (
+    "@article{foobar,\n"
+    "doi={foobar},\n"
+    "url = {https://doi.org/foobar},\n"
+    r"title = {Foo on \LaTeX}," + "\n"
+    "}"
+)
+
+
+ref_test_cli_format_escape_command_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{foobar,\n"
+    " doi = {foobar},\n"
+    " url = {https://doi.org/foobar},\n"
+    r" title = {Foo on \LaTeX}," + "\n"
+    "}\n"
+)
+
+
+def test_cli_format_escape_command():
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_command_in)
+
+    betterbib.cli.format(["--in-place", infile])
+
+    with open(infile) as f:
+        assert f.read() == ref_test_cli_format_escape_command_out
 
     os.remove(infile)
 
 
+def test_cli_format_escape_command_stdout(capsys):
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_command_in)
+
+    betterbib.cli.format([infile])
+    captured = capsys.readouterr()
+
+    assert captured.out == "".join(ref_test_cli_format_escape_command_out)
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_command_repeated():
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_command_in)
+
+    for i in range(10):
+        betterbib.cli.format(["--in-place", infile])
+
+        with open(infile) as f:
+            assert f.read() == ref_test_cli_format_escape_command_out
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_command_repeated_stdout(capsys):
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_command_in)
+
+    for i in range(10):
+        betterbib.cli.format(["--in-place", infile])
+        betterbib.cli.format([infile])
+        captured = capsys.readouterr()
+
+        assert captured.out == "".join(ref_test_cli_format_escape_command_out)
+
+    os.remove(infile)
+
+
+ref_test_cli_format_escape_in = (
+    "@article{foobar,\n"
+    "doi={foobar},\n"
+    "url = {https://doi.org/foobar},\n"
+    r"title = {Foo & Bar on \LaTeX\ @TheBridge}," + "\n"
+    "}"
+)
+
+
+ref_test_cli_format_escape_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{foobar,\n"
+    " doi = {foobar},\n"
+    " url = {https://doi.org/foobar},\n"
+    r" title = {Foo \& Bar on \LaTeX @TheBridge}," + "\n"
+    "}\n"
+)
+
+
+def test_cli_format_escape():
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_in)
+
+    betterbib.cli.format(["--in-place", infile])
+
+    with open(infile) as f:
+        assert f.read() == ref_test_cli_format_escape_out
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_stdout(capsys):
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_in)
+
+    betterbib.cli.format([infile])
+    captured = capsys.readouterr()
+
+    assert captured.out == "".join(ref_test_cli_format_escape_out)
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_repeated():
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_in)
+
+    for i in range(10):
+        betterbib.cli.format(["--in-place", infile])
+
+        with open(infile) as f:
+            assert f.read() == ref_test_cli_format_escape_out
+
+    os.remove(infile)
+
+
+def test_cli_format_escape_repeated_stdout(capsys):
+    infile = tempfile.NamedTemporaryFile().name
+    with open(infile, "w") as f:
+        f.write(ref_test_cli_format_escape_in)
+
+    for i in range(10):
+        betterbib.cli.format(["--in-place", infile])
+        betterbib.cli.format([infile])
+        captured = capsys.readouterr()
+
+        assert captured.out == "".join(ref_test_cli_format_escape_out)
+
+    os.remove(infile)
+
+
+ref_test_cli_journal_abbrev_in = (
+    "@article{foobar,\n" "journal={SIAM Journal on Scientific Computing}\n" "}"
+)
+
+ref_test_cli_journal_abbrev_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{foobar,\n"
+    " journal = {SIAM J. Sci. Comput.},\n"
+    "}\n"
+)
+
+
 def test_cli_journal_abbrev():
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write("@article{foobar,\njournal={SIAM Journal on Scientific Computing}\n}")
+        f.write(ref_test_cli_journal_abbrev_in)
 
     betterbib.cli.journal_abbrev(["--in-place", infile])
 
     with open(infile) as f:
-        assert f.read() == (
-            f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-            "\n"
-            "\n"
-            "@article{foobar,\n"
-            " journal = {SIAM J. Sci. Comput.},\n"
-            "}\n"
-        )
+        assert f.read() == (ref_test_cli_journal_abbrev_out)
 
     os.remove(infile)
 
@@ -126,152 +340,136 @@ def test_cli_journal_abbrev():
 def test_cli_journal_abbrev_stdout(capsys):
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write("@article{foobar,\njournal={SIAM Journal on Scientific Computing}\n}")
+        f.write(ref_test_cli_journal_abbrev_in)
 
     betterbib.cli.journal_abbrev([infile])
     captured = capsys.readouterr()
 
-    assert captured.out == "".join(
-        f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-        "\n"
-        "\n"
-        "@article{foobar,\n"
-        " journal = {SIAM J. Sci. Comput.},\n"
-        "}\n"
-    )
+    assert captured.out == "".join(ref_test_cli_journal_abbrev_out)
 
     os.remove(infile)
 
 
+ref_test_cli_sync_in = (
+    "@article{stockman,\n"
+    " author = {Stockman},\n"
+    "title = {A New Equation to Estimate Glomerular Filtration Rate}\n"
+    "}"
+)
+
+ref_test_cli_sync_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{stockman,\n"
+    " author = {Stockman, J.A.},\n"
+    " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
+    " doi = {10.1016/s0084-3954(09)79550-8},\n"
+    " pages = {193-194},\n"
+    " source = {Crossref},\n"
+    " url = {http://dx.doi.org/10.1016/s0084-3954(09)79550-8},\n"
+    " volume = {2011},\n"
+    " journal = {Yearbook of Pediatrics},\n"
+    " publisher = {Elsevier BV},\n"
+    " issn = {0084-3954},\n"
+    " year = {2011},\n"
+    " month = jan,\n"
+    "}\n"
+)
+
+
 def test_cli_sync():
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{stockman,\n"
-            " author = {Stockman},\n"
-            "title = {A New Equation to Estimate Glomerular Filtration Rate}\n"
-            "}"
-        )
+        f.write(ref_test_cli_sync_in)
 
     betterbib.cli.sync(["--in-place", infile])
 
     with open(infile) as f:
-        assert f.read() == (
-            f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-            "\n"
-            "\n"
-            "@article{stockman,\n"
-            " author = {Stockman, J.A.},\n"
-            " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
-            " doi = {10.1016/s0084-3954(09)79550-8},\n"
-            " pages = {193-194},\n"
-            " source = {Crossref},\n"
-            " url = {http://dx.doi.org/10.1016/s0084-3954(09)79550-8},\n"
-            " volume = {2011},\n"
-            " journal = {Yearbook of Pediatrics},\n"
-            " publisher = {Elsevier BV},\n"
-            " issn = {0084-3954},\n"
-            " year = {2011},\n"
-            " month = jan,\n"
-            "}\n"
-        )
+        assert f.read() == (ref_test_cli_sync_out)
     os.remove(infile)
 
 
 def test_cli_sync_stdout(capsys):
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{stockman,\n"
-            " author = {Stockman},\n"
-            "title = {A New Equation to Estimate Glomerular Filtration Rate}\n"
-            "}"
-        )
+        f.write(ref_test_cli_sync_in)
 
     betterbib.cli.sync([infile])
     captured = capsys.readouterr()
 
-    assert captured.out == "".join(
-        f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-        "\n"
-        "\n"
-        "@article{stockman,\n"
-        " author = {Stockman, J.A.},\n"
-        " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
-        " doi = {10.1016/s0084-3954(09)79550-8},\n"
-        " pages = {193-194},\n"
-        " source = {Crossref},\n"
-        " url = {http://dx.doi.org/10.1016/s0084-3954(09)79550-8},\n"
-        " volume = {2011},\n"
-        " journal = {Yearbook of Pediatrics},\n"
-        " publisher = {Elsevier BV},\n"
-        " issn = {0084-3954},\n"
-        " year = {2011},\n"
-        " month = jan,\n"
-        "}\n"
-    )
+    assert captured.out == "".join(ref_test_cli_sync_out)
     os.remove(infile)
 
 
+ref_test_cli_doi2bibtex_args = ["10.1016/s0084-3954(09)79550-8"]
+
+ref_test_cli_doi2bibtex_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{stockman2011,\n"
+    " author = {Stockman, J.A.},\n"
+    " doi = {10.1016/s0084-3954(09)79550-8},\n"
+    " pages = {193-194},\n"
+    " source = {Crossref},\n"
+    " url = {http://dx.doi.org/10.1016/s0084-3954(09)79550-8},\n"
+    " volume = {2011},\n"
+    " journal = {Yearbook of Pediatrics},\n"
+    " publisher = {Elsevier BV},\n"
+    " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
+    " issn = {0084-3954},\n"
+    " year = {2011},\n"
+    " month = jan,\n"
+    "}\n"
+)
+
+
 def test_cli_doi2bibtex(capsys):
-    betterbib.cli.doi2bibtex(["10.1016/s0084-3954(09)79550-8"])
+    betterbib.cli.doi2bibtex(ref_test_cli_doi2bibtex_args)
+
     captured = capsys.readouterr()
-    assert captured.out == "".join(
-        f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-        "\n"
-        "\n"
-        "@article{stockman2011,\n"
-        " author = {Stockman, J.A.},\n"
-        " doi = {10.1016/s0084-3954(09)79550-8},\n"
-        " pages = {193-194},\n"
-        " source = {Crossref},\n"
-        " url = {http://dx.doi.org/10.1016/s0084-3954(09)79550-8},\n"
-        " volume = {2011},\n"
-        " journal = {Yearbook of Pediatrics},\n"
-        " publisher = {Elsevier BV},\n"
-        " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
-        " issn = {0084-3954},\n"
-        " year = {2011},\n"
-        " month = jan,\n"
-        "}\n"
-    )
+    assert captured.out == "".join(ref_test_cli_doi2bibtex_out)
+
+
+ref_test_cli_full_in = (
+    "@article{stockman,\n"
+    " author = {Stockman},\n"
+    "title = {A New Equation to Estimate Glomerular Filtration Rate}\n"
+    "}"
+)
+
+ref_test_cli_full_out = (
+    f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
+    "\n"
+    "\n"
+    "@article{stockman,\n"
+    " author = {Stockman, J.A.},\n"
+    " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
+    " doi = {10.1016/s0084-3954(09)79550-8},\n"
+    " pages = {193-194},\n"
+    " source = {Crossref},\n"
+    " url = {https://doi.org/10.1016/s0084-3954(09)79550-8},\n"
+    " volume = {2011},\n"
+    " journal = {Yearbook of Pediatrics},\n"
+    " publisher = {Elsevier BV},\n"
+    " issn = {0084-3954},\n"
+    " year = {2011},\n"
+    " month = jan,\n"
+    "}\n"
+)
 
 
 def test_cli_full():
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{stockman,\n"
-            " author = {Stockman},\n"
-            "title = {A New Equation to Estimate Glomerular Filtration Rate}\n"
-            "}"
-        )
+        f.write(ref_test_cli_full_in)
 
     betterbib.cli.full(["--in-place", infile])
 
-    ref = (
-        f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-        "\n"
-        "\n"
-        "@article{stockman,\n"
-        " author = {Stockman, J.A.},\n"
-        " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
-        " doi = {10.1016/s0084-3954(09)79550-8},\n"
-        " pages = {193-194},\n"
-        " source = {Crossref},\n"
-        " url = {https://doi.org/10.1016/s0084-3954(09)79550-8},\n"
-        " volume = {2011},\n"
-        " journal = {Yearbook of Pediatrics},\n"
-        " publisher = {Elsevier BV},\n"
-        " issn = {0084-3954},\n"
-        " year = {2011},\n"
-        " month = jan,\n"
-        "}\n"
-    )
-
     with open(infile) as f:
         data = f.read()
-        assert data == ref
+        assert data == ref_test_cli_full_out
 
     os.remove(infile)
 
@@ -279,36 +477,11 @@ def test_cli_full():
 def test_cli_full_stdout(capsys):
     infile = tempfile.NamedTemporaryFile().name
     with open(infile, "w") as f:
-        f.write(
-            "@article{stockman,\n"
-            " author = {Stockman},\n"
-            "title = {A New Equation to Estimate Glomerular Filtration Rate}\n"
-            "}"
-        )
+        f.write(ref_test_cli_full_in)
 
     betterbib.cli.full([infile])
     captured = capsys.readouterr()
 
-    ref = (
-        f"%comment{{This file was created with betterbib v{betterbib.__version__}.}}\n"
-        "\n"
-        "\n"
-        "@article{stockman,\n"
-        " author = {Stockman, J.A.},\n"
-        " title = {A New Equation to Estimate Glomerular Filtration Rate},\n"
-        " doi = {10.1016/s0084-3954(09)79550-8},\n"
-        " pages = {193-194},\n"
-        " source = {Crossref},\n"
-        " url = {https://doi.org/10.1016/s0084-3954(09)79550-8},\n"
-        " volume = {2011},\n"
-        " journal = {Yearbook of Pediatrics},\n"
-        " publisher = {Elsevier BV},\n"
-        " issn = {0084-3954},\n"
-        " year = {2011},\n"
-        " month = jan,\n"
-        "}\n"
-    )
-
-    assert captured.out == "".join(ref)
+    assert captured.out == "".join(ref_test_cli_full_out)
 
     os.remove(infile)