speccpu/x264: add recompile_mc_src to simplify code

This commit is contained in:
2025-04-27 17:27:26 +08:00
parent 516d525ef4
commit 4c0c722616
3 changed files with 113 additions and 11 deletions

View File

@@ -1,9 +1,11 @@
from pathlib import Path
from unittest import mock
from unittest.mock import mock_open, patch
import pytest
from speccpu import SPEC, check_bench_dir, find_build
from speccpu.x264 import recompile_mc_src
class TestFindBuild:
@@ -169,3 +171,98 @@ class TestCheckBenchDir:
(bench_dir / "RUN").mkdir()
assert check_bench_dir(bench_dir) is False
@pytest.fixture
def mock_build_path():
return Path("/mock/build/path")
@pytest.fixture
def mock_spec_env():
return {"CC": "gcc", "CFLAGS": "-O2"}
@pytest.fixture
def mock_mc_path(mock_build_path):
return Path("/mock/build/path/x264_src/common/mc.c")
def test_recompile_mc_src(mock_build_path, mock_spec_env, mock_mc_path):
test_mc_src = "test source code"
with (
patch("speccpu.x264.get_mc_path", return_value=mock_mc_path) as mock_get_path,
patch("builtins.open", mock_open()) as mock_file,
patch("speccpu.x264.recompile_mc") as mock_recompile,
patch.object(Path, "exists", return_value=True),
patch.object(Path, "is_file", return_value=True),
):
# Configure recompile_mc mock to return a path
mock_exe_path = Path("/mock/build/path/x264_s")
mock_recompile.return_value = mock_exe_path
# Call the function under test
result = recompile_mc_src(test_mc_src, mock_build_path, mock_spec_env)
# Verify the function behavior
mock_get_path.assert_called_once_with(mock_build_path)
mock_file.assert_called_once_with(mock_mc_path, "w")
mock_file().write.assert_called_once_with(test_mc_src)
mock_recompile.assert_called_once_with(
mock_mc_path, mock_build_path, mock_spec_env
)
def test_recompile_mc_src_file_not_exists(mock_build_path, mock_spec_env, mock_mc_path):
test_mc_src = "test source code"
with (
patch("speccpu.x264.get_mc_path", return_value=mock_mc_path),
patch.object(Path, "exists", return_value=False),
):
# Test that the function raises an assertion error when file doesn't exist
with pytest.raises(AssertionError):
recompile_mc_src(test_mc_src, mock_build_path, mock_spec_env)
def test_recompile_mc_src_exe_not_exists(mock_build_path, mock_spec_env, mock_mc_path):
test_mc_src = "test source code"
with (
patch("speccpu.x264.get_mc_path", return_value=mock_mc_path) as mock_get_path,
patch("builtins.open", mock_open()) as mock_file,
patch("speccpu.x264.recompile_mc") as mock_recompile,
patch.object(Path, "exists", side_effect=[True, False]),
patch.object(Path, "is_file", return_value=True),
):
# Configure recompile_mc mock to return a path
mock_exe_path = Path("/mock/build/path/x264_s")
mock_recompile.return_value = mock_exe_path
# Test that the function raises an assertion error when exe doesn't exist
with pytest.raises(AssertionError):
recompile_mc_src(test_mc_src, mock_build_path, mock_spec_env)
def test_recompile_mc_src_exe_not_file(mock_build_path, mock_spec_env, mock_mc_path):
test_mc_src = "test source code"
with (
patch("speccpu.x264.get_mc_path", return_value=mock_mc_path) as mock_get_path,
patch("builtins.open", mock_open()) as mock_file,
patch("speccpu.x264.recompile_mc") as mock_recompile,
patch.object(Path, "exists", return_value=True),
patch.object(Path, "is_file", return_value=False),
):
# Configure recompile_mc mock to return a path
mock_exe_path = Path("/mock/build/path/x264_s")
mock_recompile.return_value = mock_exe_path
# Test that the function raises an assertion error when exe is not a file
with pytest.raises(AssertionError):
recompile_mc_src(test_mc_src, mock_build_path, mock_spec_env)