File: test_markers.py

package info (click to toggle)
distlib 0.3.9-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 2,228 kB
  • sloc: python: 12,347; ansic: 820; sh: 106; makefile: 3
file content (96 lines) | stat: -rw-r--r-- 4,188 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2023 The Python Software Foundation.
# See LICENSE.txt and CONTRIBUTORS.txt.
#
"""Tests for distlib.markers."""
import os
import sys
import platform

from compat import unittest
from support import DistlibTestCase

from distlib.compat import python_implementation
from distlib.markers import interpret
from distlib.util import in_venv


class MarkersTestCase(DistlibTestCase):

    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 = python_implementation()

        self.assertTrue(interpret("sys_platform == '%s'" % 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))

        self.assertTrue(interpret('platform_in_venv == "%s"' % in_venv()))

        # stuff that need to raise a syntax error
        ops = ('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
        FALSEOP = 'os_name == "buuuu"'
        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 + FALSEOP))
        self.assertTrue(interpret(OP + OR + OP + OR + FALSEOP))
        self.assertTrue(interpret(OP + OR + FALSEOP + OR + FALSEOP))
        self.assertTrue(interpret(FALSEOP + OR + OP))
        self.assertFalse(interpret(FALSEOP + AND + FALSEOP))
        self.assertFalse(interpret(FALSEOP + OR + FALSEOP))

        # 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'"))
        if sys.version_info < (3, 10):
            self.assertTrue(interpret('python_version < "3.10"'))
            self.assertFalse(interpret('python_version >= "3.10"'))
        if sys.version_info >= (3, 10):
            self.assertFalse(interpret('python_version < "3.10"'))
            self.assertTrue(interpret('python_version >= "3.10"'))
        self.assertTrue(interpret("'%s' in os_name" % os_name))
        self.assertTrue(interpret("'buuuu' not in os_name"))
        self.assertTrue(interpret("'buu' in os_name", {'os_name': 'buuu'}))
        self.assertTrue(interpret("'buuuu' not in os_name and '%s' in os_name" % os_name))

        # normalized version comparison correctness
        self.assertTrue(interpret('python_version > "5.0"', {'python_version': '10.0'}))
        self.assertTrue(interpret('python_version == "5.0"', {'python_version': '5.0'}))
        self.assertTrue(interpret('python_version < "5.0"', {'python_version': '5.0b0'}))
        self.assertTrue(interpret('python_full_version > "5.0"', {'python_full_version': '10.0'}))

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

        # parentheses and extra
        if sys.platform != 'win32':
            relop = '!='
        else:
            relop = '=='
        expression = ("(sys_platform %s 'win32' or python_version == '2.4') "
                      "and extra == 'quux'" % relop)
        self.assertTrue(interpret(expression, {'extra': 'quux'}))


if __name__ == '__main__':  # pragma: no cover
    unittest.main()