You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.9KB

  1. import json
  2. from pathlib import Path
  3. from functools import partial
  4. from concurrent.futures import ThreadPoolExecutor
  5. from http.server import SimpleHTTPRequestHandler, HTTPServer
  6. import time
  7. import pytest
  8. from tests import dds, DDS
  9. from tests.fileutil import ensure_dir
  10. class DirectoryServingHTTPRequestHandler(SimpleHTTPRequestHandler):
  11. def __init__(self, *args, **kwargs) -> None:
  12. self.dir = kwargs.pop('dir')
  13. super().__init__(*args, **kwargs)
  14. def translate_path(self, path) -> str:
  15. abspath = Path(super().translate_path(path))
  16. relpath = abspath.relative_to(Path.cwd())
  17. return self.dir / relpath
  18. def test_import_json(dds: DDS):
  19. dds.scope.enter_context(ensure_dir(dds.build_dir))
  20. dds.catalog_create()
  21. json_fpath = dds.build_dir / 'data.json'
  22. import_data = {
  23. 'version': 2,
  24. 'packages': {
  25. 'foo': {
  26. '1.2.4': {
  27. 'url': 'git+http://example.com#master',
  28. 'depends': [],
  29. },
  30. '1.2.5': {
  31. 'url': 'git+http://example.com#master',
  32. },
  33. },
  34. 'bar': {
  35. '1.5.1': {
  36. 'url': 'http://example.com/bar-1.5.2.tgz'
  37. },
  38. }
  39. },
  40. }
  41. dds.scope.enter_context(
  42. dds.set_contents(json_fpath,
  43. json.dumps(import_data).encode()))
  44. dds.catalog_import(json_fpath)
  45. @pytest.yield_fixture
  46. def http_import_server():
  47. handler = partial(
  48. DirectoryServingHTTPRequestHandler,
  49. dir=Path.cwd() / 'data/http-test-1')
  50. addr = ('0.0.0.0', 8000)
  51. pool = ThreadPoolExecutor()
  52. with HTTPServer(addr, handler) as httpd:
  53. pool.submit(lambda: httpd.serve_forever(poll_interval=0.1))
  54. try:
  55. yield
  56. finally:
  57. httpd.shutdown()
  58. @pytest.yield_fixture
  59. def http_repo_server():
  60. handler = partial(
  61. DirectoryServingHTTPRequestHandler,
  62. dir=Path.cwd() / 'data/test-repo-1')
  63. addr = ('0.0.0.0', 4646)
  64. pool = ThreadPoolExecutor()
  65. with HTTPServer(addr, handler) as httpd:
  66. pool.submit(lambda: httpd.serve_forever(poll_interval=0.1))
  67. try:
  68. yield
  69. finally:
  70. httpd.shutdown()
  71. def test_import_http(dds: DDS, http_import_server):
  72. dds.repo_dir.mkdir(parents=True, exist_ok=True)
  73. dds.run(
  74. [
  75. 'repo',
  76. dds.repo_dir_arg,
  77. 'import',
  78. 'http://localhost:8000/neo-buffer-0.4.2.tar.gz',
  79. ],
  80. cwd=dds.repo_dir,
  81. )
  82. assert dds.repo_dir.joinpath('neo-buffer@0.4.2').is_dir()
  83. def test_repo_add(dds: DDS, http_repo_server):
  84. dds.repo_dir.mkdir(parents=True, exist_ok=True)
  85. dds.run([
  86. 'repo',
  87. dds.repo_dir_arg,
  88. 'add',
  89. dds.catalog_path_arg,
  90. 'http://localhost:4646',
  91. '--update',
  92. ])
  93. # dds.build_deps(['neo-url@0.2.1'])