 (ns incise.deploy.workflows.git-branch-spec
-  (:require stefon.util
+  (:require [stefon.util :refer [temp-dir]]
             [speclj.core :refer :all]
             (clojure.java [io :refer [file]]
                          [shell :refer [with-sh-dir]])

            [incise.deploy.workflows.git-branch :refer :all])
  (:import [org.eclipse.jgit.api Git]))

(defn- create-dummy-repo
  []
  (let [tmp-dir (stefon.util/temp-dir "incise-deploy-git-branch-spec")
        repo (git-init tmp-dir)]
    (git-commit repo "Initial commit.")
    tmp-dir))

(defn- same-file?
  "Compare the canonical paths of the two file like values."
  [afile-like bfile-like]
  (= (.getCanonicalPath (file afile-like))
     (.getCanonicalPath (file bfile-like))))

(describe "with-repo"
  (with repo-dir (create-dummy-repo))

  (it "binds *repo* to a Git instance"
    (should (instance? Git *repo*)))
  (it "binds *work-dir* to the working tree directory"
    (should= @repo-dir *work-dir*))
  (it "binds *out-dir* to a directory nested within the git dir."
    (should= (file @repo-dir ".git/_incise") *out-dir*)))

(describe "move-to-work-dir:"
  (with-all work-dir (spec-temp-dir))
  (with-all out-dir (spec-temp-dir))
  (with-all file-name "my-cool-file")
  (with ex-file (file @out-dir @file-name))
  (around [it] (binding [*work-dir* @work-dir
                         *out-dir* @out-dir]
                 (it)))
  (context "remove-out-dir"
    (it "removes the prefixed out-dir returning a relative path"
      (should= @file-name (#'incise.deploy.workflows.git-branch/remove-out-dir
                            (.getCanonicalPath (file @out-dir @file-name))))))
  (context "move-to-work-dir"
    (it "moves a file from out-dir to work-dir"
      (should= (file @work-dir @file-name)
               (move-to-work-dir @ex-file)))))

(run-specs)
