Video editing with Python
Add tests for configuration module (#1596)
Álvaro Mondéjar5b6085491cec
diff --git a/moviepy/config.py b/moviepy/config.pyindex f5e74838..9be17234 100644--- a/moviepy/config.py+++ b/moviepy/config.py@@ -47,7 +47,7 @@ elif FFMPEG_BINARY == "auto-detect":FFMPEG_BINARY = "ffmpeg"elif not IS_POSIX_OS and try_cmd(["ffmpeg.exe"])[0]:FFMPEG_BINARY = "ffmpeg.exe"- else:+ else: # pragma: no coverFFMPEG_BINARY = "unset"else:success, err = try_cmd([FFMPEG_BINARY])@@ -70,7 +70,7 @@ if IMAGEMAGICK_BINARY == "auto-detect":shell=True,encoding="utf-8",).split("\n")[0]- IMAGEMAGICK_BINARY = sp.check_output(+ IMAGEMAGICK_BINARY = sp.check_output( # pragma: no coverrf'dir /B /S "C:\Program Files\{imagemagick_path}\*convert.exe"',shell=True,encoding="utf-8",@@ -81,9 +81,9 @@ if IMAGEMAGICK_BINARY == "auto-detect":if IMAGEMAGICK_BINARY in ["unset", "auto-detect"]:if try_cmd(["convert"])[0]:IMAGEMAGICK_BINARY = "convert"- elif not IS_POSIX_OS and try_cmd(["convert.exe"])[0]:+ elif not IS_POSIX_OS and try_cmd(["convert.exe"])[0]: # pragma: no coverIMAGEMAGICK_BINARY = "convert.exe"- else:+ else: # pragma: no coverIMAGEMAGICK_BINARY = "unset"else:if not os.path.exists(IMAGEMAGICK_BINARY):@@ -100,15 +100,15 @@ else:def check():- """TODO: add documentation"""+ """Check if moviepy has found the binaries of FFmpeg and ImageMagick."""if try_cmd([FFMPEG_BINARY])[0]:print(f"MoviePy: ffmpeg successfully found in '{FFMPEG_BINARY}'.")- else:+ else: # pragma: no coverprint(f"MoviePy: can't find or access ffmpeg in '{FFMPEG_BINARY}'.")if try_cmd([IMAGEMAGICK_BINARY])[0]:print(f"MoviePy: ImageMagick successfully found in '{IMAGEMAGICK_BINARY}'.")- else:+ else: # pragma: no coverprint(f"MoviePy: can't find or access ImageMagick in '{IMAGEMAGICK_BINARY}'.")if DOTENV:@@ -116,5 +116,5 @@ def check():print(Path(DOTENV).read_text())-if __name__ == "__main__":+if __name__ == "__main__": # pragma: no covercheck()diff --git a/tests/test_tools.py b/tests/test_tools.pyindex b23ca229..450b831d 100644--- a/tests/test_tools.py+++ b/tests/test_tools.py@@ -1,9 +1,12 @@"""Tool tests meant to be run with pytest. Taken from PR #121 (grimley517)."""+import contextlibimport filecmpimport importlib+import ioimport osimport shutil+import sysimport pytest@@ -134,5 +137,142 @@ def test_download_webfile(url, expected_result):os.remove(filename)[email protected](os.name != "posix", reason="Doesn't works in Windows")+ ("ffmpeg_binary", "ffmpeg_binary_error"),+ (+ pytest.param("ffmpeg-imageio", None, id="FFMPEG_BINARY=ffmpeg-imageio"),+ pytest.param("auto-detect", None, id="FFMPEG_BINARY=auto-detect"),+ pytest.param(+ "foobarbazimpossible",+ (IOError, "No such file or directory:"),+ id="FFMPEG_BINARY=foobarbazimpossible",+ ),+ ),+)+ (+ "imagemagick_binary",+ "imagemagick_binary_error",+ "imagemagick_binary_isfile",+ "imagemagick_binary_isdir",+ ),+ (+ pytest.param(+ "auto-detect",+ None,+ False,+ False,+ id="IMAGEMAGICK_BINARY=auto-detect",+ ),+ pytest.param(+ "foobarbazimpossible",+ (IOError, "ImageMagick binary cannot be found"),+ False,+ False,+ id="IMAGEMAGICK_BINARY=foobarbazimpossiblefile",+ ),+ pytest.param(+ "foobarbazimpossiblefile",+ (+ IOError,+ "The path specified for the ImageMagick binary might be wrong",+ ),+ True,+ False,+ id="IMAGEMAGICK_BINARY=foobarbazimpossible(file)",+ ),+ pytest.param(+ "foobarbazimpossibledir",+ (IOError, "is not a file"),+ False,+ True,+ id="IMAGEMAGICK_BINARY=foobarbazimpossible(dir)",+ ),+ ),+)+def test_config(+ ffmpeg_binary,+ ffmpeg_binary_error,+ imagemagick_binary,+ imagemagick_binary_error,+ imagemagick_binary_isfile,+ imagemagick_binary_isdir,+):+ if "moviepy.config" in sys.modules:+ del sys.modules["moviepy.config"]++ if ffmpeg_binary_error is not None and os.path.isfile(ffmpeg_binary):+ os.remove(ffmpeg_binary)+ prev_ffmpeg_binary = os.environ.get("FFMPEG_BINARY")+ os.environ["FFMPEG_BINARY"] = ffmpeg_binary++ prev_imagemagick_binary = os.environ.get("IMAGEMAGICK_BINARY")+ if imagemagick_binary_isfile:+ imagemagick_binary = os.path.join(TMP_DIR, imagemagick_binary)+ with open(imagemagick_binary, "w") as f:+ f.write("foo")+ elif imagemagick_binary_isdir:+ imagemagick_binary = os.path.join(TMP_DIR, imagemagick_binary)+ if os.path.isdir(imagemagick_binary):+ shutil.rmtree(imagemagick_binary)+ os.mkdir(imagemagick_binary)+ os.environ["IMAGEMAGICK_BINARY"] = imagemagick_binary++ if ffmpeg_binary_error is not None:+ with pytest.raises(ffmpeg_binary_error[0]) as exc:+ importlib.import_module("moviepy.config")+ assert ffmpeg_binary_error[1] in str(exc.value)+ else:+ if imagemagick_binary_error is not None:+ with pytest.raises(imagemagick_binary_error[0]) as exc:+ importlib.import_module("moviepy.config")+ assert imagemagick_binary_error[1] in str(exc.value)+ else:+ importlib.import_module("moviepy.config")++ if prev_ffmpeg_binary is not None:+ os.environ["FFMPEG_BINARY"] = prev_ffmpeg_binary++ if prev_imagemagick_binary is not None:+ os.environ["IMAGEMAGICK_BINARY"] = prev_imagemagick_binary++ if "moviepy.config" in sys.modules:+ del sys.modules["moviepy.config"]+++def test_config_check():+ if "moviepy.config" in sys.modules:+ del sys.modules["moviepy.config"]++ try:+ dotenv_module = importlib.import_module("dotenv")+ except ImportError:+ dotenv_module = None+ else:+ with open(".env", "w") as f:+ f.write("")++ moviepy_config_module = importlib.import_module("moviepy.config")++ stdout = io.StringIO()+ with contextlib.redirect_stdout(stdout):+ moviepy_config_module.check()++ output = stdout.getvalue()++ assert "MoviePy: ffmpeg successfully found in" in output+ assert "MoviePy: ImageMagick successfully found in" in output++ if dotenv_module:+ assert os.path.isfile(".env")+ os.remove(".env")+ assert ".env file content at" in output+ del sys.modules["dotenv"]++ if "moviepy.config" in sys.modules:+ del sys.modules["moviepy.config"]++if __name__ == "__main__":pytest.main()