summaryrefslogtreecommitdiff
path: root/tests/test_npm.py
blob: 7bed337beda82a1a5916ec1bd9d35f39852ecb43 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) The python-semanticversion project
# This code is distributed under the two-clause BSD License.

"""Test NPM-style specifications."""

import unittest
import sys

from semantic_version import base


class NpmSpecTests(unittest.TestCase):
    if sys.version_info[0] <= 2:
        import contextlib
        @contextlib.contextmanager
        def subTest(self, **kwargs):
            yield

    examples = {
        # range: [matchings], [failings]
        '>=1.2.7': (
            ['1.2.7', '1.2.8', '1.3.9'],
            ['1.2.6', '1.1.0'],
        ),
        '>=1.2.7 <1.3.0': (
            ['1.2.7', '1.2.8', '1.2.99'],
            ['1.2.6', '1.3.0', '1.1.0'],
        ),
        '1.2.7 || >=1.2.9 <2.0.0': (
            ['1.2.7', '1.2.9', '1.4.6'],
            ['1.2.8', '2.0.0'],
        ),
        '>1.2.3-alpha.3': (
            ['1.2.3-alpha.7', '3.4.5'],
            ['1.2.3-alpha.3', '3.4.5-alpha.9'],
        ),
        '>=1.2.3-alpha.3': (
            ['1.2.3-alpha.3', '1.2.3-alpha.7', '3.4.5'],
            ['1.2.3-alpha.2', '3.4.5-alpha.9'],
        ),
        '1.2.3 - 2.3.4': (
            ['1.2.3', '1.2.99', '2.2.0', '2.3.4', '2.3.4+b42'],
            ['1.2.0', '1.2.3-alpha.1', '2.3.5'],
        ),
        '~1.2.3-beta.2': (
            ['1.2.3-beta.2', '1.2.3-beta.4', '1.2.4'],
            ['1.2.4-beta.2', '1.3.0'],
        ),
    }

    def test_spec(self):
        for spec, lists in self.examples.items():
            matching, failing = lists
            for version in matching:
                with self.subTest(spec=spec, version=version):
                    self.assertIn(base.Version(version), base.NpmSpec(spec))
            for version in failing:
                with self.subTest(spec=spec, version=version):
                    self.assertNotIn(base.Version(version), base.NpmSpec(spec))

    expansions = {
        # Hyphen ranges
        '1.2.3 - 2.3.4': '>=1.2.3 <=2.3.4',
        '1.2 - 2.3.4': '>=1.2.0 <=2.3.4',
        '1.2.3 - 2.3': '>=1.2.3 <2.4.0',
        '1.2.3 - 2': '>=1.2.3 <3',

        # X-Ranges
        '*': '>=0.0.0',
        '1.x': '>=1.0.0 <2.0.0',
        '1.2.x': '>=1.2.0 <1.3.0',
        '': '*',
        '1': '1.x.x',
        '1.x.x': '>=1.0.0 <2.0.0',
        '1.2': '1.2.x',

        # Tilde ranges
        '~1.2.3': '>=1.2.3 <1.3.0',
        '~1.2': '>=1.2.0 <1.3.0',
        '~1': '>=1.0.0 <2.0.0',
        '~0.2.3': '>=0.2.3 <0.3.0',
        '~0.2': '>=0.2.0 <0.3.0',
        '~0': '>=0.0.0 <1.0.0',
        '~1.2.3-beta.2': '>=1.2.3-beta.2 <1.3.0',

        # Caret ranges
        '^1.2.3': '>=1.2.3 <2.0.0',
        '^0.2.3': '>=0.2.3 <0.3.0',
        '^0.0.3': '>=0.0.3 <0.0.4',
        '^1.2.3-beta.2': '>=1.2.3-beta.2 <2.0.0',
        '^0.0.3-beta': '>=0.0.3-beta <0.0.4',
        '^1.2.x': '>=1.2.0 <2.0.0',
        '^0.0.x': '>=0.0.0 <0.1.0',
        '^0.0': '>=0.0.0 <0.1.0',
        '^1.x': '>=1.0.0 <2.0.0',
        '^0.x': '>=0.0.0 <1.0.0',
    }

    def test_expand(self):
        for source, expanded in self.expansions.items():
            with self.subTest(source=source):
                self.assertEqual(
                    base.NpmSpec(source).clause,
                    base.NpmSpec(expanded).clause,
                )