DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5350524bb654)

VCS Links

Line Code
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
# This file contains test code for the formatting of parsed statements back to
# make file "source." It essentially verifies to to_source() functions
# scattered across the tree.

import glob
import logging
import os.path
import unittest

from pymake.data import Expansion
from pymake.data import StringExpansion
from pymake.functions import BasenameFunction
from pymake.functions import SubstitutionRef
from pymake.functions import VariableRef
from pymake.functions import WordlistFunction
from pymake.parserdata import Include
from pymake.parserdata import SetVariable
from pymake.parser import parsestring
from pymake.parser import SyntaxError

class TestBase(unittest.TestCase):
    pass

class VariableRefTest(TestBase):
    def test_string_name(self):
        e = StringExpansion('foo', None)
        v = VariableRef(None, e)

        self.assertEqual(v.to_source(), '$(foo)')

    def test_special_variable(self):
        e = StringExpansion('<', None)
        v = VariableRef(None, e)

        self.assertEqual(v.to_source(), '$<')

    def test_expansion_simple(self):
        e = Expansion()
        e.appendstr('foo')
        e.appendstr('bar')

        v = VariableRef(None, e)

        self.assertEqual(v.to_source(), '$(foobar)')

class StandardFunctionTest(TestBase):
    def test_basename(self):
        e1 = StringExpansion('foo', None)
        v = VariableRef(None, e1)
        e2 = Expansion(None)
        e2.appendfunc(v)

        b = BasenameFunction(None)
        b.append(e2)

        self.assertEqual(b.to_source(), '$(basename $(foo))')

    def test_wordlist(self):
        e1 = StringExpansion('foo', None)
        e2 = StringExpansion('bar ', None)
        e3 = StringExpansion(' baz', None)

        w = WordlistFunction(None)
        w.append(e1)
        w.append(e2)
        w.append(e3)

        self.assertEqual(w.to_source(), '$(wordlist foo,bar , baz)')

    def test_curly_brackets(self):
        e1 = Expansion(None)
        e1.appendstr('foo')

        e2 = Expansion(None)
        e2.appendstr('foo ( bar')

        f = WordlistFunction(None)
        f.append(e1)
        f.append(e2)

        self.assertEqual(f.to_source(), '${wordlist foo,foo ( bar}')

class StringExpansionTest(TestBase):
    def test_simple(self):
        e = StringExpansion('foobar', None)
        self.assertEqual(e.to_source(), 'foobar')

        e = StringExpansion('$var', None)
        self.assertEqual(e.to_source(), '$var')

    def test_escaping(self):
        e = StringExpansion('$var', None)
        self.assertEqual(e.to_source(escape_variables=True), '$$var')

        e = StringExpansion('this is # not a comment', None)
        self.assertEqual(e.to_source(escape_comments=True),
                         'this is \# not a comment')

    def test_empty(self):
        e = StringExpansion('', None)
        self.assertEqual(e.to_source(), '')

        e = StringExpansion(' ', None)
        self.assertEqual(e.to_source(), ' ')

class ExpansionTest(TestBase):
    def test_single_string(self):
        e = Expansion()
        e.appendstr('foo')

        self.assertEqual(e.to_source(), 'foo')

    def test_multiple_strings(self):
        e = Expansion()
        e.appendstr('hello')
        e.appendstr('world')

        self.assertEqual(e.to_source(), 'helloworld')

    def test_string_escape(self):
        e = Expansion()
        e.appendstr('$var')
        self.assertEqual(e.to_source(), '$var')
        self.assertEqual(e.to_source(escape_variables=True), '$$var')

        e = Expansion()
        e.appendstr('foo')
        e.appendstr(' $bar')
        self.assertEqual(e.to_source(escape_variables=True), 'foo $$bar')

class SubstitutionRefTest(TestBase):
    def test_simple(self):
        name = StringExpansion('foo', None)
        c = StringExpansion('%.c', None)
        o = StringExpansion('%.o', None)
        s = SubstitutionRef(None, name, c, o)

        self.assertEqual(s.to_source(), '$(foo:%.c=%.o)')

class SetVariableTest(TestBase):
    def test_simple(self):
        v = SetVariable(StringExpansion('foo', None), '=', 'bar', None, None)
        self.assertEqual(v.to_source(), 'foo = bar')

    def test_multiline(self):
        s = 'hello\nworld'
        foo = StringExpansion('FOO', None)

        v = SetVariable(foo, '=', s, None, None)

        self.assertEqual(v.to_source(), 'define FOO\nhello\nworld\nendef')

    def test_multiline_immediate(self):
        source = 'define FOO :=\nhello\nworld\nendef'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements.to_source(), source)

    def test_target_specific(self):
        foo = StringExpansion('FOO', None)
        bar = StringExpansion('BAR', None)

        v = SetVariable(foo, '+=', 'value', None, bar)

        self.assertEqual(v.to_source(), 'BAR: FOO += value')

class IncludeTest(TestBase):
    def test_include(self):
        e = StringExpansion('rules.mk', None)
        i = Include(e, True, False)
        self.assertEqual(i.to_source(), 'include rules.mk')

        i = Include(e, False, False)
        self.assertEqual(i.to_source(), '-include rules.mk')

class IfdefTest(TestBase):
    def test_simple(self):
        source = 'ifdef FOO\nbar := $(value)\nendif'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements[0].to_source(), source)

    def test_nested(self):
        source = 'ifdef FOO\nifdef BAR\nhello = world\nendif\nendif'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements[0].to_source(), source)

    def test_negation(self):
        source = 'ifndef FOO\nbar += value\nendif'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements[0].to_source(), source)

class IfeqTest(TestBase):
    def test_simple(self):
        source = 'ifeq ($(foo),bar)\nhello = $(world)\nendif'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements[0].to_source(), source)

    def test_negation(self):
        source = 'ifneq (foo,bar)\nhello = world\nendif'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements.to_source(), source)

class ConditionBlocksTest(TestBase):
    def test_mixed_conditions(self):
        source = 'ifdef FOO\nifeq ($(FOO),bar)\nvar += $(value)\nendif\nendif'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements.to_source(), source)

    def test_extra_statements(self):
        source = 'ifdef FOO\nF := 1\nifdef BAR\nB += 1\nendif\nC = 1\nendif'

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements.to_source(), source)

    def test_whitespace_preservation(self):
        source = "ifeq ' x' 'x '\n$(error stripping)\nendif"

        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements.to_source(), source)

        source = 'ifneq (x , x)\n$(error stripping)\nendif'
        statements = parsestring(source, 'foo.mk')
        self.assertEqual(statements.to_source(),
                'ifneq (x,x)\n$(error stripping)\nendif')

class MakefileCorupusTest(TestBase):
    """Runs the make files from the pymake corpus through the formatter.

    All the above tests are child's play compared to this.
    """

    # Our reformatting isn't perfect. We ignore files with known failures until
    # we make them work.
    # TODO Address these formatting corner cases.
    _IGNORE_FILES = [
        # We are thrown off by backslashes at end of lines.
        'comment-parsing.mk',
        'escape-chars.mk',
        'include-notfound.mk',
    ]

    def _get_test_files(self):
        ourdir = os.path.dirname(os.path.abspath(__file__))

        for makefile in glob.glob(os.path.join(ourdir, '*.mk')):
            if os.path.basename(makefile) in self._IGNORE_FILES:
                continue

            source = None
            with open(makefile, 'rU') as fh:
                source = fh.read()

            try:
                yield (makefile, source, parsestring(source, makefile))
            except SyntaxError:
                continue

    def test_reparse_consistency(self):
        for filename, source, statements in self._get_test_files():
            reformatted = statements.to_source()

            # We should be able to parse the reformatted source fine.
            new_statements = parsestring(reformatted, filename)

            # If we do the formatting again, the representation shouldn't
            # change. i.e. the only lossy change should be the original
            # (whitespace and some semantics aren't preserved).
            reformatted_again = new_statements.to_source()
            self.assertEqual(reformatted, reformatted_again,
                '%s has lossless reformat.' % filename)

            self.assertEqual(len(statements), len(new_statements))

            for i in xrange(0, len(statements)):
                original = statements[i]
                formatted = new_statements[i]

                self.assertEqual(original, formatted, '%s %d: %s != %s' % (filename,
                    i, original, formatted))

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    unittest.main()