test_registry.py 4.2 KB

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