|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import torch |
| 7 | +import torchcodec |
| 8 | + |
| 9 | + |
| 10 | +def test_cppapi_pkgconfig(tmp_path): |
| 11 | + cmake_args = [ |
| 12 | + "cmake", |
| 13 | + "-DCMAKE_BUILD_TYPE=Debug", |
| 14 | + "-DCMAKE_VERBOSE_MAKEFILE=ON", |
| 15 | + f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}", |
| 16 | + Path(__file__).parent / "cppapi", |
| 17 | + ] |
| 18 | + result = subprocess.run(cmake_args, cwd=tmp_path) |
| 19 | + assert result.returncode == 0 |
| 20 | + |
| 21 | + result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path) |
| 22 | + assert result.returncode == 0 |
| 23 | + |
| 24 | + ver = f"{torchcodec.ffmpeg_major_version}" |
| 25 | + result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path) |
| 26 | + assert result.returncode == 0 |
| 27 | + |
| 28 | + |
| 29 | +def test_cppapi_no_ffmpeg(tmp_path): |
| 30 | + cmake_args = [ |
| 31 | + "cmake", |
| 32 | + "-DCMAKE_BUILD_TYPE=Debug", |
| 33 | + "-DCMAKE_VERBOSE_MAKEFILE=ON", |
| 34 | + f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}", |
| 35 | + Path(__file__).parent / "cppapi", |
| 36 | + ] |
| 37 | + ver = f"{torchcodec.ffmpeg_major_version}" |
| 38 | + my_env = os.environ.copy() |
| 39 | + my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = ( |
| 40 | + Path(__file__).parent / "no-such-dir" |
| 41 | + ) |
| 42 | + |
| 43 | + # cmake config should fail as we've set ffmpeg install prefix to the not existing |
| 44 | + # directory |
| 45 | + result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env) |
| 46 | + assert result.returncode != 0 |
| 47 | + |
| 48 | + |
| 49 | +def test_cppapi_with_prefix(tmp_path): |
| 50 | + cmake_args = [ |
| 51 | + "cmake", |
| 52 | + "-DCMAKE_BUILD_TYPE=Debug", |
| 53 | + "-DCMAKE_VERBOSE_MAKEFILE=ON", |
| 54 | + f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}", |
| 55 | + Path(__file__).parent / "cppapi", |
| 56 | + ] |
| 57 | + |
| 58 | + # In this test we are calculating the prefix of installed ffmpeg version from the location |
| 59 | + # of its libavcodec.pc file. Potentially, on the custom ffmpeg install with custom layout |
| 60 | + # our calculation can be wrong and test might fail. |
| 61 | + result = subprocess.run( |
| 62 | + ["pkg-config", "--path", "libavcodec"], capture_output=True, text=True |
| 63 | + ) |
| 64 | + assert result.returncode == 0 |
| 65 | + |
| 66 | + ver = f"{torchcodec.ffmpeg_major_version}" |
| 67 | + my_env = os.environ.copy() |
| 68 | + my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = Path( |
| 69 | + f"{result.stdout}" |
| 70 | + ).parent.parent.parent |
| 71 | + |
| 72 | + result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env) |
| 73 | + assert result.returncode == 0 |
| 74 | + |
| 75 | + result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path) |
| 76 | + assert result.returncode == 0 |
| 77 | + |
| 78 | + ver = f"{torchcodec.ffmpeg_major_version}" |
| 79 | + result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path) |
| 80 | + assert result.returncode == 0 |
0 commit comments