|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 |
|
| 6 | +import six |
| 7 | + |
6 | 8 | import _pytest._code |
7 | 9 | import py |
8 | 10 | import pytest |
@@ -645,6 +647,69 @@ def join_pythonpath(*dirs): |
645 | 647 | "*1 passed*" |
646 | 648 | ]) |
647 | 649 |
|
| 650 | + @pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks") |
| 651 | + def test_cmdline_python_package_symlink(self, testdir, monkeypatch): |
| 652 | + """ |
| 653 | + test --pyargs option with packages with path containing symlink can |
| 654 | + have conftest.py in their package (#2985) |
| 655 | + """ |
| 656 | + monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', raising=False) |
| 657 | + |
| 658 | + search_path = ["lib", os.path.join("local", "lib")] |
| 659 | + |
| 660 | + dirname = "lib" |
| 661 | + d = testdir.mkdir(dirname) |
| 662 | + foo = d.mkdir("foo") |
| 663 | + foo.ensure("__init__.py") |
| 664 | + lib = foo.mkdir('bar') |
| 665 | + lib.ensure("__init__.py") |
| 666 | + lib.join("test_bar.py"). \ |
| 667 | + write("def test_bar(): pass\n" |
| 668 | + "def test_other(a_fixture):pass") |
| 669 | + lib.join("conftest.py"). \ |
| 670 | + write("import pytest\n" |
| 671 | + "@pytest.fixture\n" |
| 672 | + "def a_fixture():pass") |
| 673 | + |
| 674 | + d_local = testdir.mkdir("local") |
| 675 | + symlink_location = os.path.join(str(d_local), "lib") |
| 676 | + if six.PY2: |
| 677 | + os.symlink(str(d), symlink_location) |
| 678 | + else: |
| 679 | + os.symlink(str(d), symlink_location, target_is_directory=True) |
| 680 | + |
| 681 | + # The structure of the test directory is now: |
| 682 | + # . |
| 683 | + # ├── local |
| 684 | + # │ └── lib -> ../world |
| 685 | + # └── lib |
| 686 | + # └── foo |
| 687 | + # ├── __init__.py |
| 688 | + # └── bar |
| 689 | + # ├── __init__.py |
| 690 | + # ├── conftest.py |
| 691 | + # └── test_world.py |
| 692 | + |
| 693 | + def join_pythonpath(*dirs): |
| 694 | + cur = py.std.os.environ.get('PYTHONPATH') |
| 695 | + if cur: |
| 696 | + dirs += (cur,) |
| 697 | + return os.pathsep.join(str(p) for p in dirs) |
| 698 | + |
| 699 | + monkeypatch.setenv('PYTHONPATH', join_pythonpath(*search_path)) |
| 700 | + for p in search_path: |
| 701 | + monkeypatch.syspath_prepend(p) |
| 702 | + |
| 703 | + # module picked up in symlink-ed directory: |
| 704 | + result = testdir.runpytest("--pyargs", "-v", "foo.bar") |
| 705 | + testdir.chdir() |
| 706 | + assert result.ret == 0 |
| 707 | + result.stdout.fnmatch_lines([ |
| 708 | + "*lib/foo/bar/test_bar.py::test_bar*PASSED*", |
| 709 | + "*lib/foo/bar/test_bar.py::test_other*PASSED*", |
| 710 | + "*2 passed*" |
| 711 | + ]) |
| 712 | + |
648 | 713 | def test_cmdline_python_package_not_exists(self, testdir): |
649 | 714 | result = testdir.runpytest("--pyargs", "tpkgwhatv") |
650 | 715 | assert result.ret |
|
0 commit comments