summaryrefslogtreecommitdiffstats
path: root/tests/command.py
blob: 693bd3750d52f445a5efcbc80c5626b6eb2e8191 (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
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
122
123
124
125
126
127
128
129
130
# -*- coding: UTF-8 -*-
# Copyright 2014 Red Hat, Inc.
# Part of clufter project
# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
"""Testing command"""
__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"

import unittest
from os.path import dirname, join
from os import remove, stat
#from pprint import pprint

import _bootstrap  # known W402, required

from clufter.command import Command, CommandError

from clufter.filters.ccs2flatccs import ccs2flatccs
from clufter.filters.flatccs2pcs import flatccs2pcs
from clufter.filters.ccs2coro import ccs2needlexml

from clufter.format import formats
formats = formats.plugins


class ChainResolve(unittest.TestCase):
    def testShapeAndProtocolMatch(self):
        from tempfile import mktemp
        filters = dict(
            ccs2flatccs=ccs2flatccs(formats),
            flatccs2pcs=flatccs2pcs(formats),
            ccs2coroxml=ccs2needlexml(formats),
        )
        testfile = join(dirname(__file__), 'empty.conf')
        testoutput = mktemp(prefix='out', suffix='.conf',
                            dir=join(dirname(__file__), 'tmp'))

        @Command.deco(('ccs2flatccs',
                          ('flatccs2pcs'),
                          ('ccs2coroxml')))
        def cmd_chain_match_01(cmd_ctxt,
                               input=testfile,
                               output=testoutput,
                               coro='.coro'):
            return (
                ('file', input),
                (
                    ('file', output),
                    ('file', coro)
                )
            )
        @Command.deco(('ccs2flatccs'))
        def cmd_chain_match_02(cmd_ctxt,
                               input=testfile,
                               output=testoutput,
                               coro='.coro'):
            return (
                ('file', input),
                ('file', output)
            )
        @Command.deco(('ccs2flatccs'))
        def cmd_chain_nonmatch_01(cmd_ctxt,
                                  input=testfile,
                                  output=testoutput,
                                  coro='.coro'):
            return (
                ('file', input),
                (
                    ('file', output),
                    ('file', coro)
                )
            )
        @Command.deco(('ccs2flatccs',
                          ('flatccs2pcs'),
                          ('ccs2coroxml'),
                          ('ccs2coroxml')))
        def cmd_chain_nonmatch_02(cmd_ctxt,
                                  input=testfile,
                                  output=testoutput,
                                  coro='.coro'):
            return (
                ('file', input),
                (
                    ('file', output),
                    ('file', coro)
                )
            )
        # malformed protocol name
        @Command.deco(('ccs2flatccs',
                          ('flatccs2pcs'),
                          ('ccs2coroxml')))
        def cmd_chain_nonmatch_03(cmd_ctxt,
                                  input=testfile,
                                  output=testoutput,
                                  coro='.coro'):
            return (
                ('file', input),
                (
                    ('file', output),
                    ('life', coro)
                )
            )
        cmd_classes = (
            cmd_chain_match_01,
            cmd_chain_match_02,
            cmd_chain_nonmatch_01,
            cmd_chain_nonmatch_02,
            cmd_chain_nonmatch_03
        )
        split = cmd_classes.index(cmd_chain_nonmatch_01)
        for i, cmd_cls in enumerate(cmd_classes):
            try:
                ret = cmd_cls(filters)({}, [])  # no args/kwargs
                self.assertTrue(ret is not None)
            except CommandError as e:
                print "{0}: {1}".format(cmd_cls.name, e)
                self.assertFalse(i < split)
            except Exception as e:
                print "{0}: {1}".format(cmd_cls.name, e)
                self.assertTrue(i < split)
                raise
            else:
                self.assertTrue(i < split)
                # also test non-zero-size output whe
                self.assertTrue(stat(testoutput).st_size > 0)
                remove(testoutput)
            continue


if __name__ == '__main__':
    unittest.main()