Debian Codegraph @

reno/tests/test_loader.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import logging
16 import textwrap
17 from unittest import mock
18
19 import fixtures
20 import yaml
21
22 from reno import config
23 from reno import loader
24 from reno.tests import base
25
26
27 class TestValidate(base.TestCase):
28
29 scanner_output = {
30 '0.0.0': [('note', 'shaA')],
31 }
32
33 versions = ['0.0.0']
34
35 def setUp(self):
36 super(TestValidate, self).setUp()
37 self.logger = self.useFixture(
38 fixtures.FakeLogger(
39 format='%(message)s',
40 level=logging.WARNING,
41 )
42 )
43 self.c = config.Config('reporoot')
44
45 def _make_loader(self, note_bodies):
46 def _load(ldr):
47 ldr._scanner_output = self.scanner_output
48 ldr._cache = {
49 'file-contents': {'note1': note_bodies},
50 }
51
52 with mock.patch('reno.loader.Loader._load_data', _load):
53 return loader.Loader(
54 self.c,
55 ignore_cache=False,
56 )
57
58 def test_note_with_non_prelude_string_converted_to_list(self):
59 """Test behavior when a non-prelude note is not structured as a list.
60
61 We should silently convert it to list.
62 """
63 note_bodies = yaml.safe_load(textwrap.dedent("""
64 issues: |
65 This is a single string. It should be converted to a list.
66 """))
67 self.assertIsInstance(note_bodies['issues'], str)
68 with self._make_loader(note_bodies) as ldr:
69 parse_results = ldr.parse_note_file('note1', None)
70 self.assertIsInstance(parse_results['issues'], list)
71
72 def test_invalid_note_with_prelude_as_list(self):
73 note_bodies = yaml.safe_load(textwrap.dedent('''
74 prelude:
75 - The prelude should not be a list.
76 '''))
77 self.assertIsInstance(note_bodies['prelude'], list)
78 with self._make_loader(note_bodies) as ldr:
79 ldr.parse_note_file('note1', None)
80 self.assertIn('does not parse as a single string', self.logger.output)
81
82 def test_invalid_note_with_colon_as_dict(self):
83 note_bodies = yaml.safe_load(textwrap.dedent('''
84 issues:
85 - This line is fine.
86 - dict: But this is parsed as a mapping (dictionary), which is bad.
87 '''))
88 self.assertIsInstance(note_bodies['issues'][-1], dict)
89 with self._make_loader(note_bodies) as ldr:
90 ldr.parse_note_file('note1', None)
91 self.assertIn('instead of a string', self.logger.output)
92
93 def test_invalid_note_with_unrecognized_key(self):
94 """Test behavior when note contains an unrecognized section."""
95 note_bodies = yaml.safe_load(textwrap.dedent('''
96 foobar:
97 - |
98 This is an issue but we're using an unrecognized section key.
99 '''))
100 self.assertIsInstance(note_bodies, dict)
101 with self._make_loader(note_bodies) as ldr:
102 ldr.parse_note_file('note1', None)
103 self.assertIn(
104 'The foobar section of note1 is not a recognized section.',
105 self.logger.output)
106
107 def test_invalid_note_with_missing_key(self):
108 """Test behavior when note is not structured as a mapping.
109
110 This one should be an error since we can't correct the input.
111 """
112 note_bodies = yaml.safe_load(textwrap.dedent('''
113 - |
114 This is an issue but we're missing the top-level 'issues' key.
115 '''))
116 self.assertIsInstance(note_bodies, list)
117 with self._make_loader(note_bodies) as ldr:
118 self.assertRaises(ValueError, ldr.parse_note_file, 'note1', None)
119 self.assertIn(
120 'does not appear to be structured as a YAML mapping',
121 self.logger.output)