From 227d06446f2873954fd4c7cb7755a188a26dd3ac Mon Sep 17 00:00:00 2001 From: esteban Date: Sun, 15 Oct 2017 02:16:39 -0600 Subject: [PATCH] Completed coverage and refactored to make testable --- project.clj | 2 +- src/ffmpeg_clj/core.clj | 41 ++++++++++++++++++----------------- test/ffmpeg_clj/core_test.clj | 38 +++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/project.clj b/project.clj index f81eb0f..8912803 100644 --- a/project.clj +++ b/project.clj @@ -3,4 +3,4 @@ :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} - :dependencies [[org.clojure/clojure "1.5.1"]]) + :dependencies [[org.clojure/clojure "1.8.0"]]) diff --git a/src/ffmpeg_clj/core.clj b/src/ffmpeg_clj/core.clj index 89ae914..c664daa 100644 --- a/src/ffmpeg_clj/core.clj +++ b/src/ffmpeg_clj/core.clj @@ -1,27 +1,28 @@ (ns ffmpeg-clj.core - (:require - [clojure.java.shell :refer [sh]] - [clojure.string :as s])) + (:require + [clojure.java.shell :refer [sh]] + [clojure.string :as s])) (def ^:dynamic *bin* "ffmpeg") -(defn cmd [& argv] - (->> argv - (map #(if (keyword? %) (str "-" (name %)) (str %))) - (into [*bin*]))) +(defn create-cmd-string-seq [& argv] + (->> argv + (map #(if (keyword? %) (str "-" (name %)) (str %))) + (into [*bin*]))) -(defn bin [] *bin*) +(defn ffmpeg-factory [shell-call-fn] + (fn [& args] + (let [cmd! (apply shell-call-fn (apply create-cmd-string-seq args)) + {:keys [exit out err]} cmd!] + (when-not (zero? exit) + (throw + (Exception. err))) + out))) -(defn ffmpeg! [& args] - (let [cmd! (apply sh (apply cmd args)) - {:keys [exit out err]} cmd!] - (when-not (zero? exit) - (throw - (Exception. err))) - out)) +(def ffmpeg! (ffmpeg-factory sh)) -(defn version [] - (as-> (ffmpeg! "-version") o - (re-find #"version \S+" o) - (s/split o #" ") - (last o))) +(defn version [] + (as-> (ffmpeg! "-version") o + (re-find #"version \S+" o) + (s/split o #" ") + (last o))) diff --git a/test/ffmpeg_clj/core_test.clj b/test/ffmpeg_clj/core_test.clj index 8d112c0..f72a099 100644 --- a/test/ffmpeg_clj/core_test.clj +++ b/test/ffmpeg_clj/core_test.clj @@ -1,7 +1,35 @@ (ns ffmpeg-clj.core-test - (:require [clojure.test :refer :all] - [ffmpeg-clj.core :refer :all])) + (:require [clojure.test :refer :all] + [ffmpeg-clj.core :refer :all] + [clojure.string :as s])) -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1)))) +(deftest cmd-creates-command-with-only-ffmpeg-when-no-args-provided + (testing "If no args are provided to (cmd) it should create a list with only the command in it (\"ffmpeg\")." + (is (= (create-cmd-string-seq) ["ffmpeg"])))) + +(deftest cmd-creates-command-with-one-option-and-one-value + (testing "A ffmpeg call with an option (using a - before) from a keyword and a value from a string should be created." + (is (= (create-cmd-string-seq :codec "h264") ["ffmpeg", "-codec", "h264"])))) + +(deftest cmd-combination-of-many-keywords-and-values + (testing "Keywords must be seen as options for ffmpeg and string values as parameters" + (is (= (create-cmd-string-seq "value1" :option1 "value2" "value3" :option2 "value4" :option3 :option4) ["ffmpeg", "value1", "-option1", "value2", "value3", "-option2", "value4", "-option3", "-option4"])))) + + +(defn mocked-sh + "A mocked sh function that return a map with the result values as in the arguments" + [exit-code stdout err] + (fn [& arguments-ignored-in-this-mock] + {:exit exit-code :out stdout :err err})) + +(deftest ffmpeg-clj-when-exit-code-from-sh-call-is-not-zero-throws-err-as-exception + (testing "Should throw the error of the shell call if the exit code is not 0" + (is (thrown? Exception ((ffmpeg-factory (mocked-sh 1 nil "Error")) "irrelevant_value"))))) + +(deftest ffmpeg-clj-when-exit-code-from-the-sh-is-zero-returns-the-out-value-call + (testing "Should return the output of the shell call if the exit code is 0" + (is (= ((ffmpeg-factory (mocked-sh 0 "Test stdout value" nil)) "irrelevant_value") "Test stdout value")))) + +(deftest ffmpeg-version + (testing "Test the version of ffmpeg" + (is (not (s/blank? (version)))))) \ No newline at end of file