aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/lib/nots/package_manager/base/tests/package_json.py
blob: b50f4273d54fd7252a2f36fc9cc70d6c9ed6906e (plain) (blame)
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
import os

import pytest

from build.plugins.lib.nots.package_manager.base.package_json import PackageJson, PackageJsonWorkspaceError


def test_get_name_exist():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "name": "package-name",
    }

    name = pj.get_name()

    assert name == "package-name"


def test_get_name_none():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {}

    name = pj.get_name()

    assert name == "packages-foo"


def test_get_workspace_dep_spec_paths_ok():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "@yandex-int/bar": "workspace:../bar",
        },
        "devDependencies": {
            "@yandex-int/baz": "workspace:../baz",
        },
    }

    ws_dep_spec_paths = pj.get_workspace_dep_spec_paths()

    assert ws_dep_spec_paths == [
        ("@yandex-int/bar", "../bar"),
        ("@yandex-int/baz", "../baz"),
    ]


def test_get_workspace_dep_spec_paths_invalid_path():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "@yandex-int/bar": "workspace:*",
        },
    }

    with pytest.raises(PackageJsonWorkspaceError) as e:
        pj.get_workspace_dep_spec_paths()

    assert (
        str(e.value)
        == "Expected relative path specifier for workspace dependency, but got 'workspace:*' for @yandex-int/bar in /packages/foo/package.json"
    )


def test_get_workspace_dep_paths_ok():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "@yandex-int/bar": "workspace:../bar",
        },
        "devDependencies": {
            "@yandex-int/baz": "workspace:../baz",
        },
    }

    ws_dep_paths = pj.get_workspace_dep_paths()

    assert ws_dep_paths == [
        "/packages/bar",
        "/packages/baz",
    ]


def test_get_dep_specifier():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "jestify": "0.0.1",
            "eslint": ">= 7.27.0",
        },
        "devDependencies": {
            "jest": "27.1.0",
            "eslinting": "0.0.2",
        },
    }

    jest_spec = pj.get_dep_specifier("jest")
    assert jest_spec == "27.1.0", "Got unexpected jest specifier: {}".format(jest_spec)

    eslint_spec = pj.get_dep_specifier("eslint")
    assert eslint_spec == ">= 7.27.0", "Got unexpected eslint specifier: {}".format(eslint_spec)


def test_get_workspace_dep_paths_with_custom_base_path():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "@yandex-int/bar": "workspace:../bar",
        },
        "devDependencies": {
            "@yandex-int/baz": "workspace:../baz",
        },
    }

    ws_dep_paths = pj.get_workspace_dep_paths(base_path="custom/dir")

    assert ws_dep_paths == [
        "custom/bar",
        "custom/baz",
    ]


def test_get_workspace_deps_ok():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "@yandex-int/bar": "workspace:../bar",
        },
        "devDependencies": {
            "@yandex-int/baz": "workspace:../baz",
        },
    }

    def load_mock(cls, path):
        p = PackageJson(path)
        p.data = {
            "name": "@yandex-int/{}".format(os.path.basename(os.path.dirname(path))),
        }
        return p

    PackageJson.load = classmethod(load_mock)

    ws_deps = pj.get_workspace_deps()

    assert len(ws_deps) == 2
    assert ws_deps[0].path == "/packages/bar/package.json"
    assert ws_deps[1].path == "/packages/baz/package.json"


def test_get_workspace_deps_with_wrong_name():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "@yandex-int/bar": "workspace:../bar",
        },
    }

    def load_mock(cls, path):
        p = PackageJson(path)
        p.data = {
            "name": "@shouldbe/{}".format(os.path.basename(os.path.dirname(path))),
        }
        return p

    PackageJson.load = classmethod(load_mock)

    with pytest.raises(PackageJsonWorkspaceError) as e:
        pj.get_workspace_deps()

    assert (
        str(e.value)
        == "Workspace dependency name mismatch, found '@yandex-int/bar' instead of '@shouldbe/bar' in /packages/foo/package.json"
    )


def test_get_workspace_map_ok():
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "dependencies": {
            "@yandex-int/bar": "workspace:../bar",
        },
    }

    def load_mock(cls, path):
        name = os.path.basename(os.path.dirname(path))
        p = PackageJson(path)
        p.data = {
            "name": "@yandex-int/{}".format(name),
            "dependencies": ({"@yandex-int/qux": "workspace:../qux"} if name == "bar" else {}),
        }
        return p

    PackageJson.load = classmethod(load_mock)

    ws_map = pj.get_workspace_map()

    assert len(ws_map) == 3
    assert ws_map["/packages/foo"][0].path == "/packages/foo/package.json"
    assert ws_map["/packages/foo"][1] == 0
    assert ws_map["/packages/bar"][0].path == "/packages/bar/package.json"
    assert ws_map["/packages/bar"][1] == 1
    assert ws_map["/packages/qux"][0].path == "/packages/qux/package.json"
    assert ws_map["/packages/qux"][1] == 2


def test_get_bin_path_string():
    # arrange
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "name": "next",
        "bin": "./dist/bin/next",
    }

    # act + assert
    assert pj.get_bin_path() == "./dist/bin/next"


def test_get_bin_path_same_name():
    # arrange
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "name": "next",
        "bin": {"next": "./dist/bin/next"},
    }

    # act + assert
    assert pj.get_bin_path() == "./dist/bin/next"


def test_get_bin_path_custom_bin_name():
    # arrange
    pj = PackageJson("/packages/foo/package.json")
    pj.data = {
        "name": "typescript",
        "bin": {"tsc": "./bin/tsc"},
    }

    # act + assert
    assert pj.get_bin_path("tsc") == "./bin/tsc"