Debian Codegraph @

tests/u/parsec/test_include.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """Basic tests for the include module and its functions. Some functions
18 rely on file system operations, and are probably better tested by functional
19 tests. So this suite of unit tests should not cover all the module features.
20 """
21
22 import os
23 import tempfile
24 import unittest
25
26 from cylc.flow.parsec.exceptions import ParsecError
27 from cylc.flow.parsec.include import inline, IncludeFileNotFoundError
28
29
30 class TestInclude(unittest.TestCase):
31
32 def test_include_file_not_found_error(self):
33 dir_temp = tempfile.mkdtemp()
34 with tempfile.NamedTemporaryFile(dir=dir_temp) as tf:
35 file_list = [tf.name, tf.name, tf.name]
36 error = IncludeFileNotFoundError(file_list)
37 self.assertTrue(" via " in str(error))
38
39 def test_inline_error_mismatched_quotes(self):
40 """The inline function throws an error when you have the
41 %include statement with a value without the correct balance
42 for quotes, e.g. %include "abc.txt
43 """
44 with self.assertRaises(ParsecError):
45 inline(
46 lines=["%include 'abc.txt"],
47 dir_=None,
48 filename=None,
49 level=None)
50
51 def test_inline(self):
52 with tempfile.NamedTemporaryFile() as tf:
53 filename = tf.name
54 file_lines = [
55 "#!jinja2",
56 "[section]",
57 "value 1"
58 ]
59 file_lines_with_include = file_lines + [
60 "%include '{0}'".format(filename)
61 ]
62
63 # same as before as there was no include lines
64 r = inline(lines=file_lines,
65 dir_=os.path.dirname(tf.name),
66 filename=filename)
67 self.assertEqual(file_lines, r)
68
69 # here the include line is removed, so the value returned
70 # is still file_lines, not file_lines_with_include
71 r = inline(lines=file_lines_with_include,
72 dir_=os.path.dirname(tf.name),
73 filename=filename)
74 self.assertEqual(file_lines, r)
75
76 # the for_grep adds some marks helpful for when grep'ing the file
77 r = inline(lines=file_lines_with_include,
78 dir_=os.path.dirname(tf.name),
79 filename=filename, for_grep=True)
80 expected = file_lines + [
81 "#++++ START INLINED INCLUDE FILE {0}".format(filename),
82 "#++++ END INLINED INCLUDE FILE {0}".format(filename)
83 ]
84 self.assertEqual(expected, r)
85
86 # for_edit would call the backup function, which triggers file
87 # system operations. So we avoid testing that option here.
88
89 # test that whatever is in the included file appears in the output
90 tf.write("[section2]".encode())
91 tf.flush()
92 r = inline(lines=file_lines_with_include,
93 dir_=os.path.dirname(tf.name),
94 filename=filename)
95 expected = file_lines + [
96 "[section2]"
97 ]
98 self.assertEqual(expected, r)