test_safelint.py 5.25 KB
Newer Older
1
"""
2
Tests for paver safelint quality tasks
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
"""
from mock import patch

import pavelib.quality
from paver.easy import call_task

from .utils import PaverTestCase


class PaverSafeLintTest(PaverTestCase):
    """
    Test run_safelint with a mocked environment in order to pass in opts
    """

    def setUp(self):
        super(PaverSafeLintTest, self).setUp()
        self.reset_task_messages()

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
23 24
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_violation_number_not_found(self, _mock_counts, _mock_report_dir, _mock_write_metric):
25 26 27
        """
        run_safelint encounters an error parsing the safelint output log
        """
28
        _mock_counts.return_value = {}
29 30 31 32 33
        with self.assertRaises(SystemExit):
            call_task('pavelib.quality.run_safelint')

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
34 35
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_vanilla(self, _mock_counts, _mock_report_dir, _mock_write_metric):
36 37 38
        """
        run_safelint finds violations, but a limit was not set
        """
39
        _mock_counts.return_value = {'total': 0}
40 41 42 43
        call_task('pavelib.quality.run_safelint')

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_invalid_thresholds_option(self, _mock_counts, _mock_report_dir, _mock_write_metric):
        """
        run_safelint fails when thresholds option is poorly formatted
        """
        _mock_counts.return_value = {'total': 0}
        with self.assertRaises(SystemExit):
            call_task('pavelib.quality.run_safelint', options={"thresholds": "invalid"})

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_invalid_thresholds_option_key(self, _mock_counts, _mock_report_dir, _mock_write_metric):
        """
        run_safelint fails when thresholds option is poorly formatted
        """
        _mock_counts.return_value = {'total': 0}
        with self.assertRaises(SystemExit):
            call_task('pavelib.quality.run_safelint', options={"thresholds": '{"invalid": 3}'})

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_too_many_violations(self, _mock_counts, _mock_report_dir, _mock_write_metric):
68 69 70
        """
        run_safelint finds more violations than are allowed
        """
71
        _mock_counts.return_value = {'total': 4}
72
        with self.assertRaises(SystemExit):
73
            call_task('pavelib.quality.run_safelint', options={"thresholds": '{"total": 3}'})
74 75 76

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
77 78
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_under_limit(self, _mock_counts, _mock_report_dir, _mock_write_metric):
79 80 81
        """
        run_safelint finds fewer violations than are allowed
        """
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        _mock_counts.return_value = {'total': 4}
        # No System Exit is expected
        call_task('pavelib.quality.run_safelint', options={"thresholds": '{"total": 5}'})

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_rule_violation_number_not_found(self, _mock_counts, _mock_report_dir, _mock_write_metric):
        """
        run_safelint encounters an error parsing the safelint output log for a
        given rule threshold that was set.
        """
        _mock_counts.return_value = {'total': 4}
        with self.assertRaises(SystemExit):
            call_task('pavelib.quality.run_safelint', options={"thresholds": '{"rules": {"javascript-escape": 3}}'})

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_too_many_rule_violations(self, _mock_counts, _mock_report_dir, _mock_write_metric):
        """
        run_safelint finds more rule violations than are allowed
        """
        _mock_counts.return_value = {'total': 4, 'rules': {'javascript-escape': 4}}
        with self.assertRaises(SystemExit):
            call_task('pavelib.quality.run_safelint', options={"thresholds": '{"rules": {"javascript-escape": 3}}'})

    @patch.object(pavelib.quality, '_write_metric')
    @patch.object(pavelib.quality, '_prepare_report_dir')
    @patch.object(pavelib.quality, '_get_safelint_counts')
    def test_safelint_under_rule_limit(self, _mock_counts, _mock_report_dir, _mock_write_metric):
        """
        run_safelint finds fewer rule violations than are allowed
        """
        _mock_counts.return_value = {'total': 4, 'rules': {'javascript-escape': 4}}
117
        # No System Exit is expected
118
        call_task('pavelib.quality.run_safelint', options={"thresholds": '{"rules": {"javascript-escape": 5}}'})