From 734a483ee23b58ab81442d9d3368fd0d2f1c8b3e Mon Sep 17 00:00:00 2001
From: Lukas Holecek <lholecek@redhat.com>
Date: Jul 19 2018 14:41:54 +0000
Subject: Fix bad summary messages for "invalid gating.yaml"


Fixes crashing when there is only one result which is "invalid
gating.yaml".

Fixes #260

---

diff --git a/greenwave/policies.py b/greenwave/policies.py
index 9c4b53c..2bfd272 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -230,9 +230,13 @@ def summarize_answers(answers):
     if not answers:
         return 'no tests are required'
 
-    failure_count = len([answer for answer in answers if isinstance(answer, TestResultFailed)])
+    failure_count = len([answer for answer in answers if isinstance(answer, RuleNotSatisfied)])
     missing_count = len([answer for answer in answers if isinstance(answer, TestResultMissing)])
 
+    # Missing results are also failures but we will distinguish between those
+    # two in summary message.
+    failure_count -= missing_count
+
     if failure_count and missing_count:
         return '{} of {} required tests failed, {} result{} missing'.format(
             failure_count, len(answers), missing_count, 's' if missing_count > 1 else '')
diff --git a/greenwave/tests/test_summary.py b/greenwave/tests/test_summary.py
new file mode 100644
index 0000000..2543770
--- /dev/null
+++ b/greenwave/tests/test_summary.py
@@ -0,0 +1,110 @@
+# SPDX-License-Identifier: GPL-2.0+
+from greenwave.policies import (
+    summarize_answers,
+    RuleSatisfied,
+    TestResultFailed,
+    TestResultMissing,
+    TestResultMissingWaived,
+    InvalidGatingYaml,
+)
+
+
+testResultPassed = RuleSatisfied()
+testResultFailed = TestResultFailed(
+    'koji_build', 'nethack-1.2.3-1.el9000', 'test', None, 1)
+testResultMissing = TestResultMissing(
+    'koji_build', 'nethack-1.2.3-1.el9000', 'test', None)
+testResultMissingWaived = TestResultMissingWaived(
+    'koji_build', 'nethack-1.2.3-1.el9000', 'test', None)
+testInvalidGatingYaml = InvalidGatingYaml(
+    'koji_build', 'nethack-1.2.3-1.el9000', 'test', 'Missing !Policy tag')
+
+
+def test_summary_passed():
+    answers = [
+        testResultPassed,
+    ]
+    assert summarize_answers(answers) == 'all required tests passed'
+
+
+def test_summary_empty():
+    answers = []
+    assert summarize_answers(answers) == 'no tests are required'
+
+
+def test_summary_failed():
+    answers = [
+        testResultFailed,
+    ]
+    assert summarize_answers(answers) == '1 of 1 required tests failed'
+
+
+def test_summary_missing():
+    answers = [
+        testResultMissing,
+    ]
+    assert summarize_answers(answers) == '1 of 1 required test results missing'
+
+
+def test_summary_missing_waived():
+    answers = [
+        testResultMissingWaived,
+    ]
+    assert summarize_answers(answers) == 'all required tests passed'
+
+
+def test_summary_one_passed_one_failed():
+    answers = [
+        testResultPassed,
+        testResultFailed,
+    ]
+    assert summarize_answers(answers) == '1 of 2 required tests failed'
+
+
+def test_summary_one_passed_one_missing():
+    answers = [
+        testResultPassed,
+        testResultMissing,
+    ]
+    assert summarize_answers(answers) == '1 of 2 required test results missing'
+
+
+def test_summary_one_passed_one_missing_waived():
+    answers = [
+        testResultPassed,
+        testResultMissingWaived,
+    ]
+    assert summarize_answers(answers) == 'all required tests passed'
+
+
+def test_summary_one_failed_one_missing():
+    answers = [
+        testResultFailed,
+        testResultMissing,
+    ]
+    assert summarize_answers(answers) == '1 of 2 required tests failed, 1 result missing'
+
+
+def test_summary_one_passed_one_failed_one_missing():
+    answers = [
+        testResultPassed,
+        testResultFailed,
+        testResultMissing,
+    ]
+    assert summarize_answers(answers) == '1 of 3 required tests failed, 1 result missing'
+
+
+def test_summary_invalid_gating_yaml():
+    answers = [
+        testInvalidGatingYaml,
+    ]
+    assert summarize_answers(answers) == '1 of 1 required tests failed'
+
+
+def test_summary_one_passed_one_invalid_gating_yaml_one_missing():
+    answers = [
+        testResultPassed,
+        testResultMissing,
+        testInvalidGatingYaml,
+    ]
+    assert summarize_answers(answers) == '1 of 3 required tests failed, 1 result missing'

