File: test_loader.py

package info (click to toggle)
python-reno 4.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 776 kB
  • sloc: python: 5,263; makefile: 21; sh: 10
file content (121 lines) | stat: -rw-r--r-- 4,268 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
117
118
119
120
121
# -*- coding: utf-8 -*-

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import logging
import textwrap
from unittest import mock

import fixtures
import yaml

from reno import config
from reno import loader
from reno.tests import base


class TestValidate(base.TestCase):

    scanner_output = {
        '0.0.0': [('note', 'shaA')],
    }

    versions = ['0.0.0']

    def setUp(self):
        super(TestValidate, self).setUp()
        self.logger = self.useFixture(
            fixtures.FakeLogger(
                format='%(message)s',
                level=logging.WARNING,
            )
        )
        self.c = config.Config('reporoot')

    def _make_loader(self, note_bodies):
        def _load(ldr):
            ldr._scanner_output = self.scanner_output
            ldr._cache = {
                'file-contents': {'note1': note_bodies},
            }

        with mock.patch('reno.loader.Loader._load_data', _load):
            return loader.Loader(
                self.c,
                ignore_cache=False,
            )

    def test_note_with_non_prelude_string_converted_to_list(self):
        """Test behavior when a non-prelude note is not structured as a list.

        We should silently convert it to list.
        """
        note_bodies = yaml.safe_load(textwrap.dedent("""
        issues: |
          This is a single string. It should be converted to a list.
        """))
        self.assertIsInstance(note_bodies['issues'], str)
        with self._make_loader(note_bodies) as ldr:
            parse_results = ldr.parse_note_file('note1', None)
        self.assertIsInstance(parse_results['issues'], list)

    def test_invalid_note_with_prelude_as_list(self):
        note_bodies = yaml.safe_load(textwrap.dedent('''
        prelude:
          - The prelude should not be a list.
        '''))
        self.assertIsInstance(note_bodies['prelude'], list)
        with self._make_loader(note_bodies) as ldr:
            ldr.parse_note_file('note1', None)
        self.assertIn('does not parse as a single string', self.logger.output)

    def test_invalid_note_with_colon_as_dict(self):
        note_bodies = yaml.safe_load(textwrap.dedent('''
        issues:
          - This line is fine.
          - dict: But this is parsed as a mapping (dictionary), which is bad.
        '''))
        self.assertIsInstance(note_bodies['issues'][-1], dict)
        with self._make_loader(note_bodies) as ldr:
            ldr.parse_note_file('note1', None)
        self.assertIn('instead of a string', self.logger.output)

    def test_invalid_note_with_unrecognized_key(self):
        """Test behavior when note contains an unrecognized section."""
        note_bodies = yaml.safe_load(textwrap.dedent('''
        foobar:
        - |
          This is an issue but we're using an unrecognized section key.
        '''))
        self.assertIsInstance(note_bodies, dict)
        with self._make_loader(note_bodies) as ldr:
            ldr.parse_note_file('note1', None)
        self.assertIn(
            'The foobar section of note1 is not a recognized section.',
            self.logger.output)

    def test_invalid_note_with_missing_key(self):
        """Test behavior when note is not structured as a mapping.

        This one should be an error since we can't correct the input.
        """
        note_bodies = yaml.safe_load(textwrap.dedent('''
        - |
          This is an issue but we're missing the top-level 'issues' key.
        '''))
        self.assertIsInstance(note_bodies, list)
        with self._make_loader(note_bodies) as ldr:
            self.assertRaises(ValueError, ldr.parse_note_file, 'note1', None)
        self.assertIn(
            'does not appear to be structured as a YAML mapping',
            self.logger.output)