Пример #1
0
def test_section_exists_empty_file(temporary_file):
    expected_file = Path(temporary_file.name)
    expected_file.write_text("")

    rule = SectionExists(
        name="Ensure section exists",
        path=expected_file,
        section="test_section",
        target=r"^$",
        options=(("option_1", "some value"), ("option_2", True)),
    )

    rule.pre_task_hook()
    rule.task()

    assert (expected_file.read_text() ==
            "[test_section]\noption_1 = some value\noption_2 = True\n")
    expected_file.unlink()
Пример #2
0
def test_section_exists_add_before(temporary_file):
    expected_file = Path(temporary_file.name)
    expected_file.write_text("[main]")

    rule = SectionExists(
        name="Ensure section exists",
        path=expected_file,
        section="test_section",
        target="main",
        add_after=False,
        options=(("option_1", "some value"), ("option_2", True)),
    )

    rule.pre_task_hook()
    rule.task()

    assert (expected_file.read_text() ==
            "[test_section]\noption_1 = some value\noption_2 = True\n[main]")
    expected_file.unlink()
Пример #3
0
def test_section_exists_keeping_comment(temporary_file):
    expected_file = Path(temporary_file.name)
    expected_file.write_text(";commenting some thing\n[main]")

    rule = SectionExists(
        name="Ensure section exists",
        path=expected_file,
        section="test_section",
        target="main",
        options=(("option_1", "some value"), ("option_2", True)),
    )

    rule.pre_task_hook()
    rule.task()

    assert (
        expected_file.read_text() ==
        ";commenting some thing\n[main]\n[test_section]\noption_1 = some value\noption_2 = True\n"
    )
    expected_file.unlink()