|
| 1 | +import json |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +def test_resolve_from_manifest_found(tmp_path: Path) -> None: |
| 10 | + """When manifest has yosys entry and binary exists, resolve it.""" |
| 11 | + from chipcompiler.tools.yosys.utility import _resolve_from_manifest |
| 12 | + |
| 13 | + # Set up fake tool installation |
| 14 | + tool_dir = tmp_path / "yosys" / "0.61" |
| 15 | + bin_dir = tool_dir / "bin" |
| 16 | + bin_dir.mkdir(parents=True) |
| 17 | + yosys_bin = bin_dir / "yosys" |
| 18 | + yosys_bin.write_text("#!/bin/sh\necho yosys") |
| 19 | + yosys_bin.chmod(0o755) |
| 20 | + |
| 21 | + manifest = { |
| 22 | + "schema_version": 1, |
| 23 | + "installed": { |
| 24 | + "yosys": { |
| 25 | + "version": "0.61", |
| 26 | + "path": str(tool_dir), |
| 27 | + "sha256": "abc123", |
| 28 | + } |
| 29 | + }, |
| 30 | + } |
| 31 | + manifest_path = tmp_path / "manifest.json" |
| 32 | + manifest_path.write_text(json.dumps(manifest)) |
| 33 | + |
| 34 | + with patch( |
| 35 | + "chipcompiler.tools.yosys.utility._MANIFEST_PATH", |
| 36 | + manifest_path, |
| 37 | + ): |
| 38 | + cmd, tool_path = _resolve_from_manifest("yosys") |
| 39 | + assert cmd == [str(yosys_bin)] |
| 40 | + assert tool_path == tool_dir |
| 41 | + |
| 42 | + |
| 43 | +def test_resolve_from_manifest_no_file(tmp_path: Path) -> None: |
| 44 | + """When manifest doesn't exist, return empty.""" |
| 45 | + from chipcompiler.tools.yosys.utility import _resolve_from_manifest |
| 46 | + |
| 47 | + with patch( |
| 48 | + "chipcompiler.tools.yosys.utility._MANIFEST_PATH", |
| 49 | + tmp_path / "nonexistent.json", |
| 50 | + ): |
| 51 | + cmd, tool_path = _resolve_from_manifest("yosys") |
| 52 | + assert cmd == [] |
| 53 | + assert tool_path is None |
| 54 | + |
| 55 | + |
| 56 | +def test_resolve_from_manifest_tool_not_installed(tmp_path: Path) -> None: |
| 57 | + """When manifest exists but tool not in it, return empty.""" |
| 58 | + from chipcompiler.tools.yosys.utility import _resolve_from_manifest |
| 59 | + |
| 60 | + manifest = {"schema_version": 1, "installed": {}} |
| 61 | + manifest_path = tmp_path / "manifest.json" |
| 62 | + manifest_path.write_text(json.dumps(manifest)) |
| 63 | + |
| 64 | + with patch( |
| 65 | + "chipcompiler.tools.yosys.utility._MANIFEST_PATH", |
| 66 | + manifest_path, |
| 67 | + ): |
| 68 | + cmd, tool_path = _resolve_from_manifest("yosys") |
| 69 | + assert cmd == [] |
| 70 | + assert tool_path is None |
| 71 | + |
| 72 | + |
| 73 | +def test_resolve_from_manifest_binary_missing(tmp_path: Path) -> None: |
| 74 | + """When manifest has entry but binary doesn't exist, return empty.""" |
| 75 | + from chipcompiler.tools.yosys.utility import _resolve_from_manifest |
| 76 | + |
| 77 | + tool_dir = tmp_path / "yosys" / "0.61" |
| 78 | + tool_dir.mkdir(parents=True) |
| 79 | + # Don't create the binary |
| 80 | + |
| 81 | + manifest = { |
| 82 | + "schema_version": 1, |
| 83 | + "installed": { |
| 84 | + "yosys": { |
| 85 | + "version": "0.61", |
| 86 | + "path": str(tool_dir), |
| 87 | + "sha256": "abc123", |
| 88 | + } |
| 89 | + }, |
| 90 | + } |
| 91 | + manifest_path = tmp_path / "manifest.json" |
| 92 | + manifest_path.write_text(json.dumps(manifest)) |
| 93 | + |
| 94 | + with patch( |
| 95 | + "chipcompiler.tools.yosys.utility._MANIFEST_PATH", |
| 96 | + manifest_path, |
| 97 | + ): |
| 98 | + cmd, tool_path = _resolve_from_manifest("yosys") |
| 99 | + assert cmd == [] |
| 100 | + assert tool_path is None |
| 101 | + |
| 102 | + |
| 103 | +def test_resolve_yosys_command_checks_manifest_first(tmp_path: Path) -> None: |
| 104 | + """_resolve_yosys_command should check manifest before env var and PATH.""" |
| 105 | + from chipcompiler.tools.yosys.utility import _resolve_yosys_command |
| 106 | + |
| 107 | + # Set up fake manifest tool |
| 108 | + tool_dir = tmp_path / "yosys" / "0.61" |
| 109 | + bin_dir = tool_dir / "bin" |
| 110 | + bin_dir.mkdir(parents=True) |
| 111 | + yosys_bin = bin_dir / "yosys" |
| 112 | + yosys_bin.write_text("#!/bin/sh\necho yosys") |
| 113 | + yosys_bin.chmod(0o755) |
| 114 | + |
| 115 | + manifest = { |
| 116 | + "schema_version": 1, |
| 117 | + "installed": { |
| 118 | + "yosys": { |
| 119 | + "version": "0.61", |
| 120 | + "path": str(tool_dir), |
| 121 | + "sha256": "abc123", |
| 122 | + } |
| 123 | + }, |
| 124 | + } |
| 125 | + manifest_path = tmp_path / "manifest.json" |
| 126 | + manifest_path.write_text(json.dumps(manifest)) |
| 127 | + |
| 128 | + with patch( |
| 129 | + "chipcompiler.tools.yosys.utility._MANIFEST_PATH", |
| 130 | + manifest_path, |
| 131 | + ): |
| 132 | + cmd, oss_path = _resolve_yosys_command() |
| 133 | + assert cmd == [str(yosys_bin)] |
| 134 | + assert oss_path == tool_dir |
0 commit comments