def test_after_first_reusable(): inserter = AfterFirst("^foo=") assert (add_line_to_string( "foo=bar\n" "bar=baz\n" "baz=quux\n", "gnusto=cleesh", inserter=inserter, ) == ("foo=bar\n" "gnusto=cleesh\n" "bar=baz\n" "baz=quux\n")) assert (add_line_to_string( "food=yummy\n" "foo=icky\n" "fo=misspelled\n", "gnusto=cleesh", inserter=inserter, ) == ("food=yummy\n" "foo=icky\n" "gnusto=cleesh\n" "fo=misspelled\n"))
from lineinfile import AfterFirst input_file = "input-noeol.txt" line = "gnusto=cleesh" args = {"inserter": AfterFirst(r"^spaced\s*=")} options = ["-a", r"^spaced\s*="]
from lineinfile import AfterFirst input_file = "input-crlf.txt" line = "gnusto=cleesh" args = {"inserter": AfterFirst(r"=stuff\r$")} options = ["-a", r"=stuff\r$"] nonuniversal_lines = True
import re from lineinfile import AfterFirst line = "gnusto=cleesh" args = {"inserter": AfterFirst(re.compile(r"^foo="))} options = ["--after-first", "^foo="]
from lineinfile import AfterFirst line = r"gnusto=\1:\2" args = { "regexp": r"^(notinfile)=(\w+)", "backrefs": True, "inserter": AfterFirst(r"^foo="), } options = ["-e", r"^(notinfile)=(\w+)", "--backrefs", "-a", "^foo="]
from lineinfile import AfterFirst line = "gnusto=cleesh" args = {"regexp": "notinfile", "inserter": AfterFirst(r"^foo=")} options = ["-e", "notinfile", "-a", "^foo="]
from lineinfile import AfterFirst line = "gnusto=cleesh" args = {"inserter": AfterFirst(r"^foo=")} options = ["-a", "^foo="]
r = runner.invoke( main, ["add"] + backup_opts + ["gnusto=cleesh", "file.txt"], standalone_mode=False, ) assert r.exit_code == 0, show_result(r) assert r.output == "" args = {**CLI_DEFAULTS, **backup_args} add_line_mock.assert_called_once_with("file.txt", "gnusto=cleesh", **args) @pytest.mark.parametrize( "opts,inserter", [ ([], None), (["--after-first", "foo"], AfterFirst("foo")), (["--after-first", "foo", "--before-last", "bar"], BeforeLast("bar")), (["-B", "bar", "-a", "foo"], AfterFirst("foo")), (["--bof"], AtBOF()), (["--bof", "-A", "foo"], AfterLast("foo")), (["-A", "foo", "--bof"], AtBOF()), (["--bof", "--eof"], AtEOF()), (["--eof", "--bof"], AtBOF()), (["--bof", "--eof", "-b", "foo"], BeforeFirst("foo")), (["-a", "foo", "-A", "bar", "-b", "baz", "-B", "quux" ], BeforeLast("quux")), ( ["-a", "foo", "-A", "bar", "--bof", "-b", "baz", "-B", "quux"], BeforeLast("quux"), ), (["-a", "foo", "-a", "bar"], AfterFirst("bar")),
from lineinfile import AfterFirst line = "gnusto=cleesh" args = {"inserter": AfterFirst(r"notinfile")} options = ["-a", "notinfile"]