summaryrefslogtreecommitdiff
path: root/Lib/packaging/tests/test_markers.py
blob: dec04297622b36abfe66045ae383d8251a4d8e3b (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
"""Tests for packaging.markers."""
import os
import sys
import platform
from packaging.markers import interpret

from packaging.tests import unittest
from packaging.tests.support import LoggingCatcher


class MarkersTestCase(LoggingCatcher,
                      unittest.TestCase):

    def test_interpret(self):
        sys_platform = sys.platform
        version = sys.version.split()[0]
        os_name = os.name
        platform_version = platform.version()
        platform_machine = platform.machine()
        platform_python_implementation = platform.python_implementation()

        self.assertTrue(interpret("sys.platform == '%s'" % sys_platform))
        self.assertTrue(interpret(
            "sys.platform == '%s' or python_version == '2.4'" % sys_platform))
        self.assertTrue(interpret(
            "sys.platform == '%s' and python_full_version == '%s'" %
            (sys_platform, version)))
        self.assertTrue(interpret("'%s' == sys.platform" % sys_platform))
        self.assertTrue(interpret('os.name == "%s"' % os_name))
        self.assertTrue(interpret(
            'platform.version == "%s" and platform.machine == "%s"' %
            (platform_version, platform_machine)))
        self.assertTrue(interpret('platform.python_implementation == "%s"' %
            platform_python_implementation))

        # stuff that need to raise a syntax error
        ops = ('os.name == os.name', 'os.name == 2', "'2' == '2'",
               'okpjonon', '', 'os.name ==', 'python_version == 2.4')
        for op in ops:
            self.assertRaises(SyntaxError, interpret, op)

        # combined operations
        OP = 'os.name == "%s"' % os_name
        AND = ' and '
        OR = ' or '
        self.assertTrue(interpret(OP + AND + OP))
        self.assertTrue(interpret(OP + AND + OP + AND + OP))
        self.assertTrue(interpret(OP + OR + OP))
        self.assertTrue(interpret(OP + OR + OP + OR + OP))

        # other operators
        self.assertTrue(interpret("os.name != 'buuuu'"))
        self.assertTrue(interpret("python_version > '1.0'"))
        self.assertTrue(interpret("python_version < '5.0'"))
        self.assertTrue(interpret("python_version <= '5.0'"))
        self.assertTrue(interpret("python_version >= '1.0'"))
        self.assertTrue(interpret("'%s' in os.name" % os_name))
        self.assertTrue(interpret("'buuuu' not in os.name"))
        self.assertTrue(interpret(
            "'buuuu' not in os.name and '%s' in os.name" % os_name))

        # execution context
        self.assertTrue(interpret('python_version == "0.1"',
                                  {'python_version': '0.1'}))


def test_suite():
    return unittest.makeSuite(MarkersTestCase)

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