|
1 | 1 | use insta_cmd::assert_cmd_snapshot; |
2 | 2 | use ruff_python_ast::PythonVersion; |
3 | 3 |
|
4 | | -use crate::CliTest; |
| 4 | +use crate::{CliTest, site_packages_filter}; |
5 | 5 |
|
6 | 6 | /// Specifying an option on the CLI should take precedence over the same setting in the |
7 | 7 | /// project's configuration. Here, this is tested for the Python version. |
@@ -1654,6 +1654,278 @@ home = ./ |
1654 | 1654 | Ok(()) |
1655 | 1655 | } |
1656 | 1656 |
|
| 1657 | +/// ty should include site packages from its own environment when no other environment is found. |
| 1658 | +#[test] |
| 1659 | +fn ty_environment_is_only_environment() -> anyhow::Result<()> { |
| 1660 | + let ty_venv_site_packages = if cfg!(windows) { |
| 1661 | + "ty-venv/Lib/site-packages" |
| 1662 | + } else { |
| 1663 | + "ty-venv/lib/python3.13/site-packages" |
| 1664 | + }; |
| 1665 | + |
| 1666 | + let ty_executable_path = if cfg!(windows) { |
| 1667 | + "ty-venv/Scripts/ty.exe" |
| 1668 | + } else { |
| 1669 | + "ty-venv/bin/ty" |
| 1670 | + }; |
| 1671 | + |
| 1672 | + let ty_package_path = format!("{ty_venv_site_packages}/ty_package/__init__.py"); |
| 1673 | + |
| 1674 | + let case = CliTest::with_files([ |
| 1675 | + (ty_package_path.as_str(), "class TyEnvClass: ..."), |
| 1676 | + ( |
| 1677 | + "ty-venv/pyvenv.cfg", |
| 1678 | + r" |
| 1679 | + home = ./ |
| 1680 | + version = 3.13 |
| 1681 | + ", |
| 1682 | + ), |
| 1683 | + ( |
| 1684 | + "test.py", |
| 1685 | + r" |
| 1686 | + from ty_package import TyEnvClass |
| 1687 | + ", |
| 1688 | + ), |
| 1689 | + ])?; |
| 1690 | + |
| 1691 | + let case = case.with_ty_at(ty_executable_path)?; |
| 1692 | + assert_cmd_snapshot!(case.command(), @r###" |
| 1693 | + success: true |
| 1694 | + exit_code: 0 |
| 1695 | + ----- stdout ----- |
| 1696 | + All checks passed! |
| 1697 | +
|
| 1698 | + ----- stderr ----- |
| 1699 | + "###); |
| 1700 | + |
| 1701 | + Ok(()) |
| 1702 | +} |
| 1703 | + |
| 1704 | +/// ty should include site packages from both its own environment and a local `.venv`. The packages |
| 1705 | +/// from ty's environment should take precedence. |
| 1706 | +#[test] |
| 1707 | +fn ty_environment_and_discovered_venv() -> anyhow::Result<()> { |
| 1708 | + let ty_venv_site_packages = if cfg!(windows) { |
| 1709 | + "ty-venv/Lib/site-packages" |
| 1710 | + } else { |
| 1711 | + "ty-venv/lib/python3.13/site-packages" |
| 1712 | + }; |
| 1713 | + |
| 1714 | + let ty_executable_path = if cfg!(windows) { |
| 1715 | + "ty-venv/Scripts/ty.exe" |
| 1716 | + } else { |
| 1717 | + "ty-venv/bin/ty" |
| 1718 | + }; |
| 1719 | + |
| 1720 | + let local_venv_site_packages = if cfg!(windows) { |
| 1721 | + ".venv/Lib/site-packages" |
| 1722 | + } else { |
| 1723 | + ".venv/lib/python3.13/site-packages" |
| 1724 | + }; |
| 1725 | + |
| 1726 | + let ty_unique_package = format!("{ty_venv_site_packages}/ty_package/__init__.py"); |
| 1727 | + let local_unique_package = format!("{local_venv_site_packages}/local_package/__init__.py"); |
| 1728 | + let ty_conflicting_package = format!("{ty_venv_site_packages}/shared_package/__init__.py"); |
| 1729 | + let local_conflicting_package = |
| 1730 | + format!("{local_venv_site_packages}/shared_package/__init__.py"); |
| 1731 | + |
| 1732 | + let case = CliTest::with_files([ |
| 1733 | + (ty_unique_package.as_str(), "class TyEnvClass: ..."), |
| 1734 | + (local_unique_package.as_str(), "class LocalClass: ..."), |
| 1735 | + (ty_conflicting_package.as_str(), "class FromTyEnv: ..."), |
| 1736 | + ( |
| 1737 | + local_conflicting_package.as_str(), |
| 1738 | + "class FromLocalVenv: ...", |
| 1739 | + ), |
| 1740 | + ( |
| 1741 | + "ty-venv/pyvenv.cfg", |
| 1742 | + r" |
| 1743 | + home = ./ |
| 1744 | + version = 3.13 |
| 1745 | + ", |
| 1746 | + ), |
| 1747 | + ( |
| 1748 | + ".venv/pyvenv.cfg", |
| 1749 | + r" |
| 1750 | + home = ./ |
| 1751 | + version = 3.13 |
| 1752 | + ", |
| 1753 | + ), |
| 1754 | + ( |
| 1755 | + "test.py", |
| 1756 | + r" |
| 1757 | + # Should resolve from ty's environment |
| 1758 | + from ty_package import TyEnvClass |
| 1759 | + # Should resolve from local .venv |
| 1760 | + from local_package import LocalClass |
| 1761 | + # Should resolve from ty's environment (takes precedence) |
| 1762 | + from shared_package import FromTyEnv |
| 1763 | + # Should NOT resolve (shadowed by ty's environment version) |
| 1764 | + from shared_package import FromLocalVenv |
| 1765 | + ", |
| 1766 | + ), |
| 1767 | + ])? |
| 1768 | + .with_ty_at(ty_executable_path)?; |
| 1769 | + |
| 1770 | + assert_cmd_snapshot!(case.command(), @r###" |
| 1771 | + success: false |
| 1772 | + exit_code: 1 |
| 1773 | + ----- stdout ----- |
| 1774 | + error[unresolved-import]: Module `shared_package` has no member `FromLocalVenv` |
| 1775 | + --> test.py:9:28 |
| 1776 | + | |
| 1777 | + 7 | from shared_package import FromTyEnv |
| 1778 | + 8 | # Should NOT resolve (shadowed by ty's environment version) |
| 1779 | + 9 | from shared_package import FromLocalVenv |
| 1780 | + | ^^^^^^^^^^^^^ |
| 1781 | + | |
| 1782 | + info: rule `unresolved-import` is enabled by default |
| 1783 | +
|
| 1784 | + Found 1 diagnostic |
| 1785 | +
|
| 1786 | + ----- stderr ----- |
| 1787 | + "###); |
| 1788 | + |
| 1789 | + Ok(()) |
| 1790 | +} |
| 1791 | + |
| 1792 | +/// When `VIRTUAL_ENV` is set, ty should *not* discover its own environment's site-packages. |
| 1793 | +#[test] |
| 1794 | +fn ty_environment_and_active_environment() -> anyhow::Result<()> { |
| 1795 | + let ty_venv_site_packages = if cfg!(windows) { |
| 1796 | + "ty-venv/Lib/site-packages" |
| 1797 | + } else { |
| 1798 | + "ty-venv/lib/python3.13/site-packages" |
| 1799 | + }; |
| 1800 | + |
| 1801 | + let ty_executable_path = if cfg!(windows) { |
| 1802 | + "ty-venv/Scripts/ty.exe" |
| 1803 | + } else { |
| 1804 | + "ty-venv/bin/ty" |
| 1805 | + }; |
| 1806 | + |
| 1807 | + let active_venv_site_packages = if cfg!(windows) { |
| 1808 | + "active-venv/Lib/site-packages" |
| 1809 | + } else { |
| 1810 | + "active-venv/lib/python3.13/site-packages" |
| 1811 | + }; |
| 1812 | + |
| 1813 | + let ty_package_path = format!("{ty_venv_site_packages}/ty_package/__init__.py"); |
| 1814 | + let active_package_path = format!("{active_venv_site_packages}/active_package/__init__.py"); |
| 1815 | + |
| 1816 | + let case = CliTest::with_files([ |
| 1817 | + (ty_package_path.as_str(), "class TyEnvClass: ..."), |
| 1818 | + ( |
| 1819 | + "ty-venv/pyvenv.cfg", |
| 1820 | + r" |
| 1821 | + home = ./ |
| 1822 | + version = 3.13 |
| 1823 | + ", |
| 1824 | + ), |
| 1825 | + (active_package_path.as_str(), "class ActiveClass: ..."), |
| 1826 | + ( |
| 1827 | + "active-venv/pyvenv.cfg", |
| 1828 | + r" |
| 1829 | + home = ./ |
| 1830 | + version = 3.13 |
| 1831 | + ", |
| 1832 | + ), |
| 1833 | + ( |
| 1834 | + "test.py", |
| 1835 | + r" |
| 1836 | + from ty_package import TyEnvClass |
| 1837 | + from active_package import ActiveClass |
| 1838 | + ", |
| 1839 | + ), |
| 1840 | + ])? |
| 1841 | + .with_ty_at(ty_executable_path)? |
| 1842 | + .with_filter(&site_packages_filter("3.13"), "<site-packages>"); |
| 1843 | + |
| 1844 | + assert_cmd_snapshot!( |
| 1845 | + case.command() |
| 1846 | + .env("VIRTUAL_ENV", case.root().join("active-venv")), |
| 1847 | + @r" |
| 1848 | + success: false |
| 1849 | + exit_code: 1 |
| 1850 | + ----- stdout ----- |
| 1851 | + error[unresolved-import]: Cannot resolve imported module `ty_package` |
| 1852 | + --> test.py:2:6 |
| 1853 | + | |
| 1854 | + 2 | from ty_package import TyEnvClass |
| 1855 | + | ^^^^^^^^^^ |
| 1856 | + 3 | from active_package import ActiveClass |
| 1857 | + | |
| 1858 | + info: Searched in the following paths during module resolution: |
| 1859 | + info: 1. <temp_dir>/ (first-party code) |
| 1860 | + info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) |
| 1861 | + info: 3. <temp_dir>/active-venv/<site-packages> (site-packages) |
| 1862 | + info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment |
| 1863 | + info: rule `unresolved-import` is enabled by default |
| 1864 | +
|
| 1865 | + Found 1 diagnostic |
| 1866 | +
|
| 1867 | + ----- stderr ----- |
| 1868 | + " |
| 1869 | + ); |
| 1870 | + |
| 1871 | + Ok(()) |
| 1872 | +} |
| 1873 | + |
| 1874 | +/// When ty is installed in a system environment rather than a virtual environment, it should |
| 1875 | +/// not include the environment's site-packages in its search path. |
| 1876 | +#[test] |
| 1877 | +fn ty_environment_is_system_not_virtual() -> anyhow::Result<()> { |
| 1878 | + let ty_system_site_packages = if cfg!(windows) { |
| 1879 | + "system-python/Lib/site-packages" |
| 1880 | + } else { |
| 1881 | + "system-python/lib/python3.13/site-packages" |
| 1882 | + }; |
| 1883 | + |
| 1884 | + let ty_executable_path = if cfg!(windows) { |
| 1885 | + "system-python/Scripts/ty.exe" |
| 1886 | + } else { |
| 1887 | + "system-python/bin/ty" |
| 1888 | + }; |
| 1889 | + |
| 1890 | + let ty_package_path = format!("{ty_system_site_packages}/system_package/__init__.py"); |
| 1891 | + |
| 1892 | + let case = CliTest::with_files([ |
| 1893 | + // Package in system Python installation (should NOT be discovered) |
| 1894 | + (ty_package_path.as_str(), "class SystemClass: ..."), |
| 1895 | + // Note: NO pyvenv.cfg - this is a system installation, not a venv |
| 1896 | + ( |
| 1897 | + "test.py", |
| 1898 | + r" |
| 1899 | + from system_package import SystemClass |
| 1900 | + ", |
| 1901 | + ), |
| 1902 | + ])? |
| 1903 | + .with_ty_at(ty_executable_path)?; |
| 1904 | + |
| 1905 | + assert_cmd_snapshot!(case.command(), @r###" |
| 1906 | + success: false |
| 1907 | + exit_code: 1 |
| 1908 | + ----- stdout ----- |
| 1909 | + error[unresolved-import]: Cannot resolve imported module `system_package` |
| 1910 | + --> test.py:2:6 |
| 1911 | + | |
| 1912 | + 2 | from system_package import SystemClass |
| 1913 | + | ^^^^^^^^^^^^^^ |
| 1914 | + | |
| 1915 | + info: Searched in the following paths during module resolution: |
| 1916 | + info: 1. <temp_dir>/ (first-party code) |
| 1917 | + info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) |
| 1918 | + info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment |
| 1919 | + info: rule `unresolved-import` is enabled by default |
| 1920 | +
|
| 1921 | + Found 1 diagnostic |
| 1922 | +
|
| 1923 | + ----- stderr ----- |
| 1924 | + "###); |
| 1925 | + |
| 1926 | + Ok(()) |
| 1927 | +} |
| 1928 | + |
1657 | 1929 | #[test] |
1658 | 1930 | fn src_root_deprecation_warning() -> anyhow::Result<()> { |
1659 | 1931 | let case = CliTest::with_files([ |
|
0 commit comments