1"""Test yarn registry replacement"""
2
3# Copyright (c) Jupyter Development Team.
4# Distributed under the terms of the Modified BSD License.
5
6import logging
7import subprocess
8from os.path import join as pjoin
9from unittest.mock import patch
10
11from jupyterlab import commands
12
13from .test_jupyterlab import AppHandlerTest
14
15
16class TestAppHandlerRegistry(AppHandlerTest):
17
18    def test_node_not_available(self):
19        # patch should be applied on `jupyterlab.commands` and not on `jupyterlab_server.process`
20        # See https://docs.python.org/3/library/unittest.mock.html#where-to-patch
21        with patch("jupyterlab.commands.which") as which:
22            which.side_effect = ValueError("Command not found")
23
24            logger = logging.getLogger('jupyterlab')
25            config = commands._yarn_config(logger)
26
27            which.assert_called_once_with('node')
28            self.assertDictEqual(config,
29                {"yarn config": {},
30                "npm config": {}}
31            )
32
33    def test_yarn_config(self):
34        with patch("subprocess.check_output") as check_output:
35            yarn_registry = "https://private.yarn/manager"
36            check_output.return_value = b'\n'.join([
37                b'{"type":"info","data":"yarn config"}',
38                b'{"type":"inspect","data":{"registry":"' + bytes(yarn_registry, 'utf-8') + b'"}}',
39                b'{"type":"info","data":"npm config"}',
40                b'{"type":"inspect","data":{"registry":"' + bytes(yarn_registry, 'utf-8') + b'"}}'
41            ])
42            logger = logging.getLogger('jupyterlab')
43            config = commands._yarn_config(logger)
44
45            self.assertDictEqual(config,
46                {"yarn config": {"registry": yarn_registry},
47                "npm config": {"registry": yarn_registry}}
48            )
49
50    def test_yarn_config_failure(self):
51        with patch("subprocess.check_output") as check_output:
52            check_output.side_effect = subprocess.CalledProcessError(1, ['yarn', 'config', 'list'], stderr=b"yarn config failed.")
53
54            logger = logging.getLogger('jupyterlab')
55            config = commands._yarn_config(logger)
56
57            self.assertDictEqual(config,
58                {"yarn config": {},
59                "npm config": {}}
60            )
61
62    def test_get_registry(self):
63        with patch("subprocess.check_output") as check_output:
64            yarn_registry = "https://private.yarn/manager"
65            check_output.return_value = b'\n'.join([
66                b'{"type":"info","data":"yarn config"}',
67                b'{"type":"inspect","data":{"registry":"' + bytes(yarn_registry, 'utf-8') + b'"}}',
68                b'{"type":"info","data":"npm config"}',
69                b'{"type":"inspect","data":{"registry":"' + bytes(yarn_registry, 'utf-8') + b'"}}'
70            ])
71
72            handler = commands.AppOptions()
73
74            self.assertEqual(handler.registry, yarn_registry)
75
76    def test_populate_staging(self):
77        with patch("subprocess.check_output") as check_output:
78            yarn_registry = "https://private.yarn/manager"
79            check_output.return_value = b'\n'.join([
80                b'{"type":"info","data":"yarn config"}',
81                b'{"type":"inspect","data":{"registry":"' + bytes(yarn_registry, 'utf-8') + b'"}}',
82                b'{"type":"info","data":"npm config"}',
83                b'{"type":"inspect","data":{"registry":"' + bytes(yarn_registry, 'utf-8') + b'"}}'
84            ])
85
86            staging = pjoin(self.app_dir, 'staging')
87            handler = commands._AppHandler(commands.AppOptions())
88            handler._populate_staging()
89
90            lock_path = pjoin(staging, 'yarn.lock')
91            with open(lock_path) as f:
92                lock = f.read()
93
94            self.assertNotIn(commands.YARN_DEFAULT_REGISTRY, lock)
95            self.assertIn(yarn_registry, lock)
96