Esempio n. 1
0
def test_generate_pull_request_body():
    mocked_pillar = Mock()
    mocked_pillar.laws = [
        Law(
            name="Test law 1",
            description="Test description 1",
            rules=[ExampleRule(name="Test rule 1", param=Mock())],
        ),
        Law(
            name="Test law 2",
            description="Test description 2",
            rules=[ExampleRule(name="Test rule 2", param=Mock())],
        ),
        Law(
            name="Test law 3",
            description="Test description 3",
            rules=[
                ExampleRule(name="Test rule 3", param=Mock()),
                ExampleRule(name="Test rule 4", param=Mock()),
            ],
        ),
    ]

    expected_body = """## Description
Below you can find the executed laws and information about them.

### Test law 1
Test description 1

<details>
<summary>Passed rules</summary>

* Test rule 1
</details>

### Test law 2
Test description 2

<details>
<summary>Passed rules</summary>

* Test rule 2
</details>

### Test law 3
Test description 3

<details>
<summary>Passed rules</summary>

* Test rule 3
* Test rule 4
</details>"""

    pr_helper = get_pull_request_helper_mixin_consumer()

    body = pr_helper.generate_pull_request_body(mocked_pillar)

    assert body == expected_body
Esempio n. 2
0
def test_generate_pull_request_body_no_changes_no_law():
    failing_rule = ExampleRule(name="Test failed rule 1", param=Mock())
    failing_rule.made_changes = False

    law_1 = Law(
        name="Test law 1",
        description="Test description 1",
        rules=[ExampleRule(name="Test rule 1", param=Mock())],
    )

    law_2 = Law(
        name="Test law 2",
        description="Test description 2",
        rules=[ExampleRule(name="Test rule 2", param=Mock())],
    )

    law_3 = Law(
        name="Test law 3",
        description="Test description 3",
        rules=[ExampleRule(name="Test rule 3", param=Mock()), failing_rule],
    )

    law_1.rules[0].made_changes = False
    law_2.rules[0].made_changes = False

    law_3.rules[0].made_changes = True
    law_3._failed_rules = (failing_rule,)

    mocked_pillar = Mock()
    mocked_pillar.laws = [law_1, law_2, law_3]

    expected_body = """## Description
Below you can find the executed laws and information about them.

### Test law 3
Test description 3

<details>
<summary>Passed rules</summary>

* Test rule 3
</details>

<details open>
<summary>Failed rules (manual fix needed)</summary>

* Test failed rule 1
</details>"""

    pr_helper = get_pull_request_helper_mixin_consumer()

    body = pr_helper.generate_pull_request_body(mocked_pillar)

    assert body == expected_body
Esempio n. 3
0
def test_repr():
    expected = 'Law(name="Empty", description="empty law", rules=(), preconditions=())'
    law = Law(name="Empty", description="empty law", rules=())

    assert repr(law) == expected
    # The repr should be used with eval
    assert repr(eval(repr(law))) == expected
Esempio n. 4
0
def test_documentation(name, description):
    law = Law(name="Empty", description="empty law", rules=())
    law.name = name
    law.description = description
    expected_documentation = f"{name}\n{description}"

    assert law.documentation == expected_documentation
Esempio n. 5
0
def test_executes_no_task():
    law = Law(name="Empty", description="empty law", rules=())
    law.commit = Mock()

    law.enforce()

    assert len(law.rules) == 0
    assert law.commit.called is True
Esempio n. 6
0
def test_executes_task():
    rule = get_passing_rule()
    rule.execute = Mock()

    law = Law(name="Passing", description="passing law", rules=(rule,))
    law.commit = Mock()

    law.enforce()

    rule.execute.assert_called_once_with()
    law.commit.assert_called_once_with()
Esempio n. 7
0
def test_commit_no_changes():
    rule = get_passing_rule()
    rule.execute = Mock()
    rule.made_changes = False

    law = Law(name="Passing", description="passing law", rules=(rule,))
    law.git_commit = Mock()
    law.get_execution_order = Mock(return_value=[rule])

    law.enforce()

    rule.execute.assert_called_once_with()
    assert law.git_commit.called is False
Esempio n. 8
0
def test_execution_order():
    rule_1 = ExampleRule(name="rule_1", param="rule_1")
    rule_2 = ExampleRule(name="rule_2", param="rule_2")
    rule_3 = ExampleRule(name="rule_3", param="rule_3")

    rule_1.get_execution_order = Mock(return_value=[])
    rule_2.get_execution_order = Mock(return_value=[])
    rule_3.get_execution_order = Mock(return_value=[])

    law = Law(name="Passing", description="passing law", rules=(rule_1, rule_2, rule_3))
    law.get_execution_order()

    rule_1.get_execution_order.assert_called_once_with()
    rule_2.get_execution_order.assert_called_once_with()
    rule_3.get_execution_order.assert_called_once_with()
Esempio n. 9
0
def test_executes_task_failed_preconditions():
    rule = get_passing_rule()
    rule.execute = Mock()

    law = Law(
        name="Passing",
        description="passing law",
        rules=(rule,),
        preconditions=[ExamplePrecondition(param=False)],
    )
    law.commit = Mock()

    law.enforce()

    assert rule.execute.called is False
    assert law.commit.called is False
Esempio n. 10
0
def test_executes_task_preconditions():
    rule = get_passing_rule()
    rule.execute = Mock()

    law = Law(
        name="Passing",
        description="passing law",
        rules=(rule,),
        preconditions=[ExamplePrecondition(param=True)],
    )
    law.commit = Mock()

    law.enforce()

    rule.execute.assert_called_once_with()
    law.commit.assert_called_once_with()
Esempio n. 11
0
def test_commit_changes():
    rule = get_passing_rule()
    rule.execute = Mock()
    rule.made_changes = True

    law = Law(name="Passing", description="passing law", rules=(rule,))
    law.git_commit = Mock()
    law.get_execution_order = Mock(return_value=[rule])

    expected_commit_message = f"{law.documentation}\n\n* {rule.name}"

    law.enforce()

    rule.execute.assert_called_once_with()
    law.get_execution_order.assert_called_once_with()
    law.git_commit.assert_called_once_with(expected_commit_message)
Esempio n. 12
0
def test_rule_execution_failed_no_abort(mocked_config, mocked_logging):
    mocked_config.settings.rule_can_abort = False
    mocked_logging.error = Mock()
    expected_exception = "failed"

    rule = get_failing_rule()
    rule.made_changes = False
    rule.param = expected_exception
    rule.get_rule_chain = Mock(return_value=[get_passing_rule()])

    law = Law(name="Failing", description="failing law", rules=(rule,))
    law.commit = Mock()

    law.enforce()

    assert mocked_logging.error.called
    rule.get_rule_chain.assert_called_once_with(rule)
    assert law.commit.called is True
Esempio n. 13
0
def test_rule_execution_aborted(mocked_logging, law_config, base_rule_config):
    law_config.settings.rule_can_abort = True
    base_rule_config.settings.dry_run = False
    mocked_logging.error = Mock()
    expected_exception = "failed"

    rule = get_failing_rule()
    rule.param = expected_exception
    rule.get_rule_chain = Mock(return_value=[get_passing_rule()])

    law = Law(name="Passing", description="passing law", rules=(rule,))
    law.commit = Mock()

    with pytest.raises(Exception) as exc:
        law.enforce()

    assert mocked_logging.error.called
    assert str(exc.value) == expected_exception
    rule.get_rule_chain.assert_called_once_with(rule)
    assert law.commit.called is False
Esempio n. 14
0
def test_rule_execution_failed_precondition_no_abort(mocked_config, mocked_logging):
    mocked_config.settings.rule_can_abort = False
    mocked_logging.warning = Mock()
    expected_exception = "failed"

    rule = get_passing_rule()
    rule.made_changes = False

    rule.preconditions = [ExamplePrecondition(param=False)]

    rule.param = expected_exception
    rule.get_rule_chain = Mock(return_value=[get_passing_rule()])

    law = Law(name="Failing", description="failing law", rules=(rule,))
    law.commit = Mock()

    law.enforce()

    assert mocked_logging.warning.called
    assert law.commit.called is True
Esempio n. 15
0
def test_str():
    expected = "Empty law"
    law = Law(name="Empty", description="empty law", rules=())

    assert str(law) == expected
Esempio n. 16
0
def test_generate_pull_request_body_with_chained_rules():
    failed_rule = ExampleRule(
        name="Test failed rule 1",
        param=Mock(),
        preconditions=[PASSING_PRECONDITION],
        children=[get_passing_rule("Child rule")],
    )

    failed_rule.made_changes = False

    passing_rule = ExampleRule(
        name="Test rule 1",
        param=Mock(),
        preconditions=[PASSING_PRECONDITION],
        children=[get_passing_rule("Passing child rule")],
    )

    failed_execution_law = Law(
        name="Test law 1",
        description="Test description 1",
        rules=[passing_rule, failed_rule],
    )

    failed_execution_law._failed_rules = (failed_rule,)

    mocked_pillar = Mock()
    mocked_pillar.laws = [
        failed_execution_law,
        Law(
            name="Test law 2",
            description="Test description 2",
            rules=[ExampleRule(name="Test rule 2", param=Mock())],
        ),
        Law(
            name="Test law 3",
            description="Test description 3",
            rules=[
                ExampleRule(name="Test rule 3", param=Mock()),
                ExampleRule(name="Test rule 4", param=Mock()),
            ],
        ),
    ]

    expected_body = """## Description
Below you can find the executed laws and information about them.

### Test law 1
Test description 1

<details>
<summary>Passed rules</summary>

* Test rule 1
  * Passing child rule
</details>

<details open>
<summary>Failed rules (manual fix needed)</summary>

* Test failed rule 1
  * Child rule
</details>

### Test law 2
Test description 2

<details>
<summary>Passed rules</summary>

* Test rule 2
</details>

### Test law 3
Test description 3

<details>
<summary>Passed rules</summary>

* Test rule 3
* Test rule 4
</details>"""

    pr_helper = get_pull_request_helper_mixin_consumer()

    body = pr_helper.generate_pull_request_body(mocked_pillar)

    assert body == expected_body