예제 #1
0
def test_section_exists_no_sections(mocked_updater_class):
    mock_file = Mock()
    expected_path = Mock()
    expected_path.open.return_value.__enter__ = Mock(return_value=mock_file)
    expected_path.open.return_value.__exit__ = Mock()

    expected_section = Mock()
    expected_match = Mock()

    mocked_updater = MagicMock()
    mocked_updater.sections.return_value = []
    mocked_updater.has_section.side_effect = [False, False]

    mocked_updater_class.return_value = mocked_updater

    rule = SectionExists(
        name="Section exists rule",
        path=expected_path,
        section=expected_section,
        match=expected_match,
    )

    rule.task()

    mocked_updater.sections.assert_called_once_with()
    mocked_updater.add_section.assert_called_once_with(expected_section)
예제 #2
0
def test_section_exists_already_exists(mocked_updater_class):
    mock_file = Mock()
    expected_path = Mock()
    expected_path.open.return_value.__enter__ = Mock(return_value=mock_file)
    expected_path.open.return_value.__exit__ = Mock()

    expected_section = Mock()

    mock_add_before_return = Mock()
    mock_add_after_return = Mock()
    expected_match = Mock()

    mocked_before = PropertyMock(return_value=mock_add_before_return)
    mocked_after = PropertyMock(return_value=mock_add_after_return)
    type(expected_match).add_before = mocked_before
    type(expected_match).add_after = mocked_after

    mocked_updater = MagicMock()
    mocked_updater.__getitem__.return_value = expected_match
    mocked_updater.sections.return_value = [expected_match]
    mocked_updater.has_section.side_effect = [True, True]

    mocked_updater_class.return_value = mocked_updater

    rule = SectionExists(
        name="Section exists rule",
        path=expected_path,
        section=expected_section,
        match=expected_match,
    )

    result = rule.task()

    mocked_updater.has_section.has_calls(
        [call(expected_match), call(expected_section)])

    assert mocked_updater.sections.called is False
    assert mocked_before.called is False
    assert mocked_after.called is False
    assert result == expected_path
예제 #3
0
def test_section_exists_add_before(mocked_updater_class):
    mock_file = Mock()
    expected_path = Mock()
    expected_path.open.return_value.__enter__ = Mock(return_value=mock_file)
    expected_path.open.return_value.__exit__ = Mock()

    expected_section = Mock()

    mock_add_before_return = Mock()
    expected_match = Mock()

    mocked_prop = PropertyMock(return_value=mock_add_before_return)
    type(expected_match).add_before = mocked_prop

    mocked_updater = MagicMock()
    mocked_updater.__getitem__.return_value = expected_match
    mocked_updater.sections.return_value = [expected_match]
    mocked_updater.has_section.side_effect = [False, True]

    mocked_updater_class.return_value = mocked_updater

    rule = SectionExists(
        name="Section exists rule",
        path=expected_path,
        section=expected_section,
        match=expected_match,
        add_after=False,
    )

    result = rule.task()

    mocked_updater.sections.assert_called_once_with()
    mocked_updater.has_section.has_calls(call(expected_section),
                                         call(expected_match))
    mocked_prop.assert_called_once_with()
    mock_add_before_return.section.assert_called_once_with(expected_section)
    assert mock_add_before_return.section.return_value.space.called is False
    assert result == expected_path
예제 #4
0
def test_section_exists_no_match(mocked_updater_class):
    mock_file = Mock()
    expected_path = Mock()
    expected_path.open.return_value.__enter__ = Mock(return_value=mock_file)
    expected_path.open.return_value.__exit__ = Mock()

    expected_section = Mock()

    mock_add_after_return = Mock()
    expected_match = Mock()

    mocked_prop = PropertyMock(return_value=mock_add_after_return)
    type(expected_match).add_after = mocked_prop

    mocked_updater = MagicMock()
    mocked_updater.sections.return_value = [expected_match]
    mocked_updater.sections_blocks.return_value = mocked_updater.sections.return_value
    mocked_updater.has_section.side_effect = [False, False]

    mocked_updater_class.return_value = mocked_updater

    rule = SectionExists(
        name="Section exists rule",
        path=expected_path,
        section=expected_section,
        match=Mock(),
    )

    result = rule.task()

    mocked_updater.sections.assert_called_once_with()
    mocked_updater.has_section.has_calls(call(expected_section),
                                         call(expected_match))
    mocked_prop.assert_called_once_with()
    mock_add_after_return.space.assert_called_once_with(rule.space)
    mock_add_after_return.space.return_value.section.assert_called_once_with(
        expected_section)
    assert result == expected_path
예제 #5
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()
예제 #6
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()
예제 #7
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()