summaryrefslogtreecommitdiffstats
path: root/tests/test_module.py
blob: 7f4f62bae123dae9c01dbabb4463a2e4b0ede104 (plain)
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
# Copyright (C) 2017 Adrien Vergé
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import shutil
import subprocess
import sys
import tempfile
import unittest

PYTHON = sys.executable or 'python'


class ModuleTestCase(unittest.TestCase):
    def setUp(self):
        self.wd = tempfile.mkdtemp(prefix='yamllint-tests-')

        # file with only one warning
        with open(os.path.join(self.wd, 'warn.yaml'), 'w') as f:
            f.write('key: value\n')

        # file in dir
        os.mkdir(os.path.join(self.wd, 'sub'))
        with open(os.path.join(self.wd, 'sub', 'nok.yaml'), 'w') as f:
            f.write('---\n'
                    'list: [  1, 1, 2, 3, 5, 8]  \n')

    def tearDown(self):
        shutil.rmtree(self.wd)

    def test_run_module_no_args(self):
        with self.assertRaises(subprocess.CalledProcessError) as ctx:
            subprocess.check_output([PYTHON, '-m', 'yamllint'],
                                    stderr=subprocess.STDOUT)
        self.assertEqual(ctx.exception.returncode, 2)
        self.assertRegex(ctx.exception.output.decode(), r'^usage: yamllint')

    def test_run_module_on_bad_dir(self):
        with self.assertRaises(subprocess.CalledProcessError) as ctx:
            subprocess.check_output([PYTHON, '-m', 'yamllint',
                                     '/does/not/exist'],
                                    stderr=subprocess.STDOUT)
        self.assertRegex(ctx.exception.output.decode(),
                         r'No such file or directory')

    def test_run_module_on_file(self):
        out = subprocess.check_output(
            [PYTHON, '-m', 'yamllint', os.path.join(self.wd, 'warn.yaml')])
        lines = out.decode().splitlines()
        self.assertIn('/warn.yaml', lines[0])
        self.assertEqual('\n'.join(lines[1:]),
                         '  1:1       warning  missing document start "---"'
                         '  (document-start)\n')

    def test_run_module_on_dir(self):
        with self.assertRaises(subprocess.CalledProcessError) as ctx:
            subprocess.check_output([PYTHON, '-m', 'yamllint', self.wd])
        self.assertEqual(ctx.exception.returncode, 1)

        files = ctx.exception.output.decode().split('\n\n')
        self.assertIn(
            '/warn.yaml\n'
            '  1:1       warning  missing document start "---"'
            '  (document-start)',
            files[0])
        self.assertIn(
            '/sub/nok.yaml\n'
            '  2:9       error    too many spaces inside brackets'
            '  (brackets)\n'
            '  2:27      error    trailing spaces  (trailing-spaces)',
            files[1])