| 
 | 1 | +"""Tests for simple Pixi.toml generation."""  | 
 | 2 | + | 
 | 3 | +from __future__ import annotations  | 
 | 4 | + | 
 | 5 | +import textwrap  | 
 | 6 | +from typing import TYPE_CHECKING  | 
 | 7 | + | 
 | 8 | +from unidep._pixi import generate_pixi_toml  | 
 | 9 | + | 
 | 10 | +if TYPE_CHECKING:  | 
 | 11 | +    from pathlib import Path  | 
 | 12 | + | 
 | 13 | + | 
 | 14 | +def test_simple_pixi_generation(tmp_path: Path) -> None:  | 
 | 15 | +    """Test basic pixi.toml generation from a single requirements.yaml."""  | 
 | 16 | +    req_file = tmp_path / "requirements.yaml"  | 
 | 17 | +    req_file.write_text(  | 
 | 18 | +        textwrap.dedent(  | 
 | 19 | +            """\  | 
 | 20 | +            channels:  | 
 | 21 | +              - conda-forge  | 
 | 22 | +            dependencies:  | 
 | 23 | +              - numpy >=1.20  | 
 | 24 | +              - pandas  | 
 | 25 | +              - pip: requests  | 
 | 26 | +            platforms:  | 
 | 27 | +              - linux-64  | 
 | 28 | +              - osx-arm64  | 
 | 29 | +            """,  | 
 | 30 | +        ),  | 
 | 31 | +    )  | 
 | 32 | + | 
 | 33 | +    output_file = tmp_path / "pixi.toml"  | 
 | 34 | +    generate_pixi_toml(  | 
 | 35 | +        req_file,  | 
 | 36 | +        project_name="test-project",  | 
 | 37 | +        output_file=output_file,  | 
 | 38 | +        verbose=False,  | 
 | 39 | +    )  | 
 | 40 | + | 
 | 41 | +    assert output_file.exists()  | 
 | 42 | +    content = output_file.read_text()  | 
 | 43 | + | 
 | 44 | +    # Check basic structure  | 
 | 45 | +    assert "[project]" in content  | 
 | 46 | +    assert 'name = "test-project"' in content  | 
 | 47 | +    assert "conda-forge" in content  | 
 | 48 | +    assert "linux-64" in content  | 
 | 49 | +    assert "osx-arm64" in content  | 
 | 50 | + | 
 | 51 | +    # Check dependencies  | 
 | 52 | +    assert "[dependencies]" in content  | 
 | 53 | +    assert 'numpy = ">=1.20"' in content  | 
 | 54 | +    assert 'pandas = "*"' in content  | 
 | 55 | + | 
 | 56 | +    assert "[pypi-dependencies]" in content  | 
 | 57 | +    assert 'requests = "*"' in content  | 
 | 58 | + | 
 | 59 | + | 
 | 60 | +def test_monorepo_pixi_generation(tmp_path: Path) -> None:  | 
 | 61 | +    """Test pixi.toml generation with features for multiple requirements files."""  | 
 | 62 | +    # Create project1  | 
 | 63 | +    project1_dir = tmp_path / "project1"  | 
 | 64 | +    project1_dir.mkdir()  | 
 | 65 | +    req1 = project1_dir / "requirements.yaml"  | 
 | 66 | +    req1.write_text(  | 
 | 67 | +        textwrap.dedent(  | 
 | 68 | +            """\  | 
 | 69 | +            channels:  | 
 | 70 | +              - conda-forge  | 
 | 71 | +            dependencies:  | 
 | 72 | +              - numpy  | 
 | 73 | +              - conda: scipy  | 
 | 74 | +            """,  | 
 | 75 | +        ),  | 
 | 76 | +    )  | 
 | 77 | + | 
 | 78 | +    # Create project2  | 
 | 79 | +    project2_dir = tmp_path / "project2"  | 
 | 80 | +    project2_dir.mkdir()  | 
 | 81 | +    req2 = project2_dir / "requirements.yaml"  | 
 | 82 | +    req2.write_text(  | 
 | 83 | +        textwrap.dedent(  | 
 | 84 | +            """\  | 
 | 85 | +            channels:  | 
 | 86 | +              - conda-forge  | 
 | 87 | +            dependencies:  | 
 | 88 | +              - pandas  | 
 | 89 | +              - pip: requests  | 
 | 90 | +            """,  | 
 | 91 | +        ),  | 
 | 92 | +    )  | 
 | 93 | + | 
 | 94 | +    output_file = tmp_path / "pixi.toml"  | 
 | 95 | +    generate_pixi_toml(  | 
 | 96 | +        req1,  | 
 | 97 | +        req2,  | 
 | 98 | +        project_name="monorepo",  | 
 | 99 | +        output_file=output_file,  | 
 | 100 | +        verbose=False,  | 
 | 101 | +    )  | 
 | 102 | + | 
 | 103 | +    assert output_file.exists()  | 
 | 104 | +    content = output_file.read_text()  | 
 | 105 | + | 
 | 106 | +    # Check project section  | 
 | 107 | +    assert "[project]" in content  | 
 | 108 | +    assert 'name = "monorepo"' in content  | 
 | 109 | + | 
 | 110 | +    # Check feature dependencies (TOML writes them directly without parent section)  | 
 | 111 | +    assert "[feature.project1.dependencies]" in content  | 
 | 112 | +    assert 'numpy = "*"' in content  | 
 | 113 | +    assert 'scipy = "*"' in content  | 
 | 114 | + | 
 | 115 | +    assert "[feature.project2.dependencies]" in content  | 
 | 116 | +    assert 'pandas = "*"' in content  | 
 | 117 | + | 
 | 118 | +    assert "[feature.project2.pypi-dependencies]" in content  | 
 | 119 | +    assert 'requests = "*"' in content  | 
 | 120 | + | 
 | 121 | +    # Check environments (be flexible with TOML formatting)  | 
 | 122 | +    assert "[environments]" in content  | 
 | 123 | +    assert "default =" in content  | 
 | 124 | +    assert "project1" in content  | 
 | 125 | +    assert "project2" in content  | 
 | 126 | +    # Verify that default includes both projects  | 
 | 127 | +    assert content.count('"project1"') >= 2  # In default and individual env  | 
 | 128 | +    assert content.count('"project2"') >= 2  # In default and individual env  | 
 | 129 | + | 
 | 130 | + | 
 | 131 | +def test_pixi_with_version_pins(tmp_path: Path) -> None:  | 
 | 132 | +    """Test that version pins are passed through without resolution."""  | 
 | 133 | +    req_file = tmp_path / "requirements.yaml"  | 
 | 134 | +    req_file.write_text(  | 
 | 135 | +        textwrap.dedent(  | 
 | 136 | +            """\  | 
 | 137 | +            channels:  | 
 | 138 | +              - conda-forge  | 
 | 139 | +            dependencies:  | 
 | 140 | +              - numpy >=1.20,<2.0  | 
 | 141 | +              - conda: scipy =1.9.0  | 
 | 142 | +              - pip: requests >2.20  | 
 | 143 | +            """,  | 
 | 144 | +        ),  | 
 | 145 | +    )  | 
 | 146 | + | 
 | 147 | +    output_file = tmp_path / "pixi.toml"  | 
 | 148 | +    generate_pixi_toml(  | 
 | 149 | +        req_file,  | 
 | 150 | +        output_file=output_file,  | 
 | 151 | +        verbose=False,  | 
 | 152 | +    )  | 
 | 153 | + | 
 | 154 | +    content = output_file.read_text()  | 
 | 155 | + | 
 | 156 | +    # Check that pins are preserved exactly  | 
 | 157 | +    assert 'numpy = ">=1.20,<2.0"' in content  | 
 | 158 | +    assert 'scipy = "=1.9.0"' in content  | 
 | 159 | +    assert 'requests = ">2.20"' in content  | 
 | 160 | + | 
 | 161 | + | 
 | 162 | +def test_pixi_empty_dependencies(tmp_path: Path) -> None:  | 
 | 163 | +    """Test handling of requirements file with no dependencies."""  | 
 | 164 | +    req_file = tmp_path / "requirements.yaml"  | 
 | 165 | +    req_file.write_text(  | 
 | 166 | +        textwrap.dedent(  | 
 | 167 | +            """\  | 
 | 168 | +            channels:  | 
 | 169 | +              - conda-forge  | 
 | 170 | +            platforms:  | 
 | 171 | +              - linux-64  | 
 | 172 | +            """,  | 
 | 173 | +        ),  | 
 | 174 | +    )  | 
 | 175 | + | 
 | 176 | +    output_file = tmp_path / "pixi.toml"  | 
 | 177 | +    generate_pixi_toml(  | 
 | 178 | +        req_file,  | 
 | 179 | +        output_file=output_file,  | 
 | 180 | +        verbose=False,  | 
 | 181 | +    )  | 
 | 182 | + | 
 | 183 | +    assert output_file.exists()  | 
 | 184 | +    content = output_file.read_text()  | 
 | 185 | + | 
 | 186 | +    # Should have project section but no dependencies sections  | 
 | 187 | +    assert "[project]" in content  | 
 | 188 | +    assert "[dependencies]" not in content  | 
 | 189 | +    assert "[pypi-dependencies]" not in content  | 
0 commit comments