|
| 1 | +# Copyright lowRISC contributors (OpenTitan project). |
| 2 | +# Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +"""Test flow configuration checking.""" |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import hjson |
| 10 | +from hamcrest import assert_that, equal_to |
| 11 | + |
| 12 | +from dvsim.check.flow import check_flow_config, detect_flow_type |
| 13 | + |
| 14 | + |
| 15 | +def test_check_valid_lint_config() -> None: |
| 16 | + """Test checking a valid lint flow configuration.""" |
| 17 | + fixtures_dir = Path(__file__).parent.parent / "flow" / "fixtures" |
| 18 | + config_file = fixtures_dir / "example_lint.hjson" |
| 19 | + |
| 20 | + success, message, flow_type = check_flow_config(config_file) |
| 21 | + |
| 22 | + assert_that(success, equal_to(True)) |
| 23 | + assert_that(flow_type, equal_to("lint")) |
| 24 | + assert_that("aes_dv_lint" in message, equal_to(True)) |
| 25 | + |
| 26 | + |
| 27 | +def test_check_invalid_lint_config(tmp_path: Path) -> None: |
| 28 | + """Test checking an invalid lint flow configuration.""" |
| 29 | + config_file = tmp_path / "invalid_lint.hjson" |
| 30 | + config_data = { |
| 31 | + "flow": "lint", |
| 32 | + # Missing required 'name' field |
| 33 | + } |
| 34 | + config_file.write_text(hjson.dumps(config_data)) |
| 35 | + |
| 36 | + success, message, flow_type = check_flow_config(config_file) |
| 37 | + |
| 38 | + assert_that(success, equal_to(False)) |
| 39 | + assert_that(flow_type, equal_to("lint")) |
| 40 | + assert_that("Invalid" in message, equal_to(True)) |
| 41 | + |
| 42 | + |
| 43 | +def test_check_missing_flow_field(tmp_path: Path) -> None: |
| 44 | + """Test checking a config without a flow field.""" |
| 45 | + config_file = tmp_path / "no_flow.hjson" |
| 46 | + config_data = {"name": "test"} |
| 47 | + config_file.write_text(hjson.dumps(config_data)) |
| 48 | + |
| 49 | + success, message, flow_type = check_flow_config(config_file) |
| 50 | + |
| 51 | + assert_that(success, equal_to(False)) |
| 52 | + assert_that(flow_type, equal_to(None)) |
| 53 | + assert_that("No 'flow' field" in message, equal_to(True)) |
| 54 | + |
| 55 | + |
| 56 | +def test_check_nonexistent_file(tmp_path: Path) -> None: |
| 57 | + """Test checking a file that doesn't exist.""" |
| 58 | + config_file = tmp_path / "nonexistent.hjson" |
| 59 | + |
| 60 | + success, message, flow_type = check_flow_config(config_file) |
| 61 | + |
| 62 | + assert_that(success, equal_to(False)) |
| 63 | + assert_that(flow_type, equal_to(None)) |
| 64 | + assert_that("not found" in message.lower(), equal_to(True)) |
| 65 | + |
| 66 | + |
| 67 | +def test_check_invalid_hjson(tmp_path: Path) -> None: |
| 68 | + """Test checking a file with invalid hjson syntax.""" |
| 69 | + config_file = tmp_path / "invalid.hjson" |
| 70 | + config_file.write_text("{invalid hjson content") |
| 71 | + |
| 72 | + success, message, flow_type = check_flow_config(config_file) |
| 73 | + |
| 74 | + assert_that(success, equal_to(False)) |
| 75 | + assert_that(flow_type, equal_to(None)) |
| 76 | + assert_that("parse" in message.lower(), equal_to(True)) |
| 77 | + |
| 78 | + |
| 79 | +def test_check_unsupported_flow_type(tmp_path: Path) -> None: |
| 80 | + """Test checking a config with an unsupported flow type.""" |
| 81 | + config_file = tmp_path / "sim.hjson" |
| 82 | + config_data = { |
| 83 | + "name": "test_sim", |
| 84 | + "flow": "sim", |
| 85 | + } |
| 86 | + config_file.write_text(hjson.dumps(config_data)) |
| 87 | + |
| 88 | + success, message, flow_type = check_flow_config(config_file) |
| 89 | + |
| 90 | + assert_that(success, equal_to(False)) |
| 91 | + assert_that(flow_type, equal_to("sim")) |
| 92 | + assert_that("not yet implemented" in message, equal_to(True)) |
| 93 | + |
| 94 | + |
| 95 | +def test_detect_flow_type() -> None: |
| 96 | + """Test detecting flow type from config file.""" |
| 97 | + fixtures_dir = Path(__file__).parent.parent / "flow" / "fixtures" |
| 98 | + config_file = fixtures_dir / "example_lint.hjson" |
| 99 | + |
| 100 | + flow_type = detect_flow_type(config_file) |
| 101 | + |
| 102 | + assert_that(flow_type, equal_to("lint")) |
| 103 | + |
| 104 | + |
| 105 | +def test_detect_flow_type_missing(tmp_path: Path) -> None: |
| 106 | + """Test detecting flow type when field is missing.""" |
| 107 | + config_file = tmp_path / "no_flow.hjson" |
| 108 | + config_data = {"name": "test"} |
| 109 | + config_file.write_text(hjson.dumps(config_data)) |
| 110 | + |
| 111 | + flow_type = detect_flow_type(config_file) |
| 112 | + |
| 113 | + assert_that(flow_type, equal_to(None)) |
0 commit comments