From dd4c7ca7850f166af4997912a3ded5639bd14a56 Mon Sep 17 00:00:00 2001 From: eltrac Date: Fri, 30 Oct 2026 11:35:45 +0800 Subject: test, downloader: tidy up downloader and add tests - simplify (.mkdirs parent-dir) and with (io/make-parents file) - should return false when HTTP request fails, not nil - check if corrputed file exitsts before deleting it - add tests --- src/weepinbell/http/downloader.clj | 14 +++++++------- test/weepinbell/downloader_test.clj | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/weepinbell/http/downloader.clj b/src/weepinbell/http/downloader.clj index 3b5bdfb..e2d0fba 100644 --- a/src/weepinbell/http/downloader.clj +++ b/src/weepinbell/http/downloader.clj @@ -7,10 +7,8 @@ "Download file to destination. Return true if success, false if failed." [url dest-path] (try - (let [file (io/file dest-path) - parent-dir (.getParentFile file)] - (when (and parent-dir (not (.exists parent-dir))) - (.mkdirs parent-dir)) + (let [file (io/file dest-path)] + (io/make-parents file) (let [response (client/get! url {:as :stream}) status (:status response)] (if (and status (> status 199) (< status 300)) @@ -25,11 +23,13 @@ (do (timbre/warn "Download fail. Status: " status) (when-let [error (:error response)] - (timbre/error error)))))) + (timbre/error error)) + false)))) ;; when download failed - (catch Exception e + (catch Throwable e (timbre/error "Download error:" (.getMessage e)) (timbre/debug e) - (io/delete-file (io/file dest-path)) + (when (.exists (io/file dest-path)) + (io/delete-file (io/file dest-path))) false))) diff --git a/test/weepinbell/downloader_test.clj b/test/weepinbell/downloader_test.clj index 7a2d6b3..469c9ba 100644 --- a/test/weepinbell/downloader_test.clj +++ b/test/weepinbell/downloader_test.clj @@ -1,6 +1,7 @@ (ns weepinbell.downloader-test (:require [weepinbell.http.downloader :refer [download-file]] [clojure.java.io :as io] + [weepinbell.http.client] [org.httpkit.server :refer [run-server]] [clojure.test :refer [deftest testing is]])) @@ -29,5 +30,15 @@ (testing "Downloading a non-existant file" (let [file-dst (io/file "tmp/should-not-exist.txt")] (is (not (download-file "https://localhost:18888" file-dst))) - (is (not (.exists file-dst)))))) - + (is (not (.exists file-dst))))) + (testing "Exception/Error thrown while downloading" + (with-redefs [weepinbell.http.client/get! + (fn [& _] (throw (.Error "Test error")))] + (is (false? (download-file "" ""))))) + (testing "Deletes possibly corrupted file when failure is caught" + (with-redefs [weepinbell.http.client/get! + (fn [& _] (throw (.Error "Test error")))] + (let [dest "dest"] + (spit dest "CORRUPTED") + (is (false? (download-file "" dest))) + (is (not (.exists (io/file dest)))))))) -- cgit v1.2.3