|
6 | 6 | import json |
7 | 7 | import os |
8 | 8 | import pathlib |
9 | | -import tempfile |
10 | 9 | from unittest.mock import patch |
11 | 10 |
|
12 | 11 | import pytest |
@@ -422,88 +421,87 @@ def test_to_css_class(s, expected): |
422 | 421 | assert utils.to_css_class(s) == expected |
423 | 422 |
|
424 | 423 |
|
425 | | -def test_temporary_docker_directory_uses_hard_link(): |
426 | | - with tempfile.TemporaryDirectory() as td: |
427 | | - os.chdir(td) |
428 | | - with open("hello", "w") as fp: |
429 | | - fp.write("world") |
430 | | - # Default usage of this should use symlink |
431 | | - with utils.temporary_docker_directory( |
432 | | - files=["hello"], |
433 | | - name="t", |
434 | | - metadata=None, |
435 | | - extra_options=None, |
436 | | - branch=None, |
437 | | - template_dir=None, |
438 | | - plugins_dir=None, |
439 | | - static=[], |
440 | | - install=[], |
441 | | - spatialite=False, |
442 | | - version_note=None, |
443 | | - secret="secret", |
444 | | - ) as temp_docker: |
445 | | - hello = os.path.join(temp_docker, "hello") |
446 | | - with open(hello) as fp: |
447 | | - assert fp.read() == "world" |
448 | | - # It should be a hard link |
449 | | - assert os.stat(hello).st_nlink == 2 |
| 424 | +def test_temporary_docker_directory_uses_hard_link(tmp_path, monkeypatch): |
| 425 | + monkeypatch.chdir(tmp_path) |
| 426 | + with open("hello", "w") as fp: |
| 427 | + fp.write("world") |
| 428 | + # Default usage of this should use symlink |
| 429 | + with utils.temporary_docker_directory( |
| 430 | + files=["hello"], |
| 431 | + name="t", |
| 432 | + metadata=None, |
| 433 | + extra_options=None, |
| 434 | + branch=None, |
| 435 | + template_dir=None, |
| 436 | + plugins_dir=None, |
| 437 | + static=[], |
| 438 | + install=[], |
| 439 | + spatialite=False, |
| 440 | + version_note=None, |
| 441 | + secret="secret", |
| 442 | + ) as temp_docker: |
| 443 | + hello = os.path.join(temp_docker, "hello") |
| 444 | + with open(hello) as fp: |
| 445 | + assert fp.read() == "world" |
| 446 | + # It should be a hard link |
| 447 | + assert os.stat(hello).st_nlink == 2 |
450 | 448 |
|
451 | 449 |
|
452 | 450 | @patch("os.link") |
453 | | -def test_temporary_docker_directory_uses_copy_if_hard_link_fails(mock_link): |
| 451 | +def test_temporary_docker_directory_uses_copy_if_hard_link_fails( |
| 452 | + mock_link, tmp_path, monkeypatch |
| 453 | +): |
454 | 454 | # Copy instead if os.link raises OSError (normally due to different device) |
455 | 455 | mock_link.side_effect = OSError |
456 | | - with tempfile.TemporaryDirectory() as td: |
457 | | - os.chdir(td) |
458 | | - with open("hello", "w") as fp: |
459 | | - fp.write("world") |
460 | | - # Default usage of this should use symlink |
461 | | - with utils.temporary_docker_directory( |
462 | | - files=["hello"], |
463 | | - name="t", |
464 | | - metadata=None, |
465 | | - extra_options=None, |
466 | | - branch=None, |
467 | | - template_dir=None, |
468 | | - plugins_dir=None, |
469 | | - static=[], |
470 | | - install=[], |
471 | | - spatialite=False, |
472 | | - version_note=None, |
473 | | - secret=None, |
474 | | - ) as temp_docker: |
475 | | - hello = os.path.join(temp_docker, "hello") |
476 | | - with open(hello) as fp: |
477 | | - assert fp.read() == "world" |
478 | | - # It should be a copy, not a hard link |
479 | | - assert os.stat(hello).st_nlink == 1 |
480 | | - |
481 | | - |
482 | | -def test_temporary_docker_directory_quotes_args(): |
483 | | - with tempfile.TemporaryDirectory() as td: |
484 | | - os.chdir(td) |
485 | | - with open("hello", "w") as fp: |
486 | | - fp.write("world") |
487 | | - with utils.temporary_docker_directory( |
488 | | - files=["hello"], |
489 | | - name="t", |
490 | | - metadata=None, |
491 | | - extra_options="--$HOME", |
492 | | - branch=None, |
493 | | - template_dir=None, |
494 | | - plugins_dir=None, |
495 | | - static=[], |
496 | | - install=[], |
497 | | - spatialite=False, |
498 | | - version_note="$PWD", |
499 | | - secret="secret", |
500 | | - ) as temp_docker: |
501 | | - df = os.path.join(temp_docker, "Dockerfile") |
502 | | - with open(df) as fp: |
503 | | - df_contents = fp.read() |
504 | | - assert "'$PWD'" in df_contents |
505 | | - assert "'--$HOME'" in df_contents |
506 | | - assert "ENV DATASETTE_SECRET 'secret'" in df_contents |
| 456 | + monkeypatch.chdir(tmp_path) |
| 457 | + with open("hello", "w") as fp: |
| 458 | + fp.write("world") |
| 459 | + # Default usage of this should use symlink |
| 460 | + with utils.temporary_docker_directory( |
| 461 | + files=["hello"], |
| 462 | + name="t", |
| 463 | + metadata=None, |
| 464 | + extra_options=None, |
| 465 | + branch=None, |
| 466 | + template_dir=None, |
| 467 | + plugins_dir=None, |
| 468 | + static=[], |
| 469 | + install=[], |
| 470 | + spatialite=False, |
| 471 | + version_note=None, |
| 472 | + secret=None, |
| 473 | + ) as temp_docker: |
| 474 | + hello = os.path.join(temp_docker, "hello") |
| 475 | + with open(hello) as fp: |
| 476 | + assert fp.read() == "world" |
| 477 | + # It should be a copy, not a hard link |
| 478 | + assert os.stat(hello).st_nlink == 1 |
| 479 | + |
| 480 | + |
| 481 | +def test_temporary_docker_directory_quotes_args(tmp_path, monkeypatch): |
| 482 | + monkeypatch.chdir(tmp_path) |
| 483 | + with open("hello", "w") as fp: |
| 484 | + fp.write("world") |
| 485 | + with utils.temporary_docker_directory( |
| 486 | + files=["hello"], |
| 487 | + name="t", |
| 488 | + metadata=None, |
| 489 | + extra_options="--$HOME", |
| 490 | + branch=None, |
| 491 | + template_dir=None, |
| 492 | + plugins_dir=None, |
| 493 | + static=[], |
| 494 | + install=[], |
| 495 | + spatialite=False, |
| 496 | + version_note="$PWD", |
| 497 | + secret="secret", |
| 498 | + ) as temp_docker: |
| 499 | + df = os.path.join(temp_docker, "Dockerfile") |
| 500 | + with open(df) as fp: |
| 501 | + df_contents = fp.read() |
| 502 | + assert "'$PWD'" in df_contents |
| 503 | + assert "'--$HOME'" in df_contents |
| 504 | + assert "ENV DATASETTE_SECRET 'secret'" in df_contents |
507 | 505 |
|
508 | 506 |
|
509 | 507 | def test_compound_keys_after_sql(): |
|
0 commit comments