Skip to content

Commit

Permalink
fix some path errors in windows tests
Browse files Browse the repository at this point in the history
lein test still has 2 failures and 1 error after this.
normalize-path should now handle absolute *nix paths correctly under
windows
addresses technomancy#1176
  • Loading branch information
AtKaaZ committed May 13, 2013
1 parent 0ddd6ca commit a8e20e9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
7 changes: 6 additions & 1 deletion leiningen-core/src/leiningen/core/classpath.clj
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,14 @@
(get project dependencies-key)
(apply get-dependencies dependencies-key project options)))

;http://stackoverflow.com/questions/5154754/why-is-file-isabsolute-platform-dependent-when-the-file-class-is-platform-inde
(defn- normalize-path [root path]
(let [f (io/file path)]
(.getAbsolutePath (if (.isAbsolute f) f (io/file root path)))))
(.getAbsolutePath (cond
;(.isAbsolute f) f
(or (.isAbsolute f) (.startsWith (.getPath f) "\\")) f
:else
(io/file root path)))))

(defn ext-dependency?
"Should the given dependency be loaded in the extensions classloader?"
Expand Down
27 changes: 27 additions & 0 deletions leiningen-core/src/leiningen/core/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,30 @@
(io/file (if (= :windows (get-os))
"NUL"
"/dev/null")))

(defn fix-path-delimiters [input-str]
(clojure.string/replace input-str "/" java.io.File/separator))

;so paths would work under windows too which adds a drive letter and changes path separator
(defn pathify
"
pass only absolute paths, will throw if not
because if not absolute then .getAbsolutePath will resolve them relative to current directory
"
[in-str-or-file]
(cond (or
(nil? in-str-or-file)
(not (or
(.startsWith in-str-or-file "/")
(and
(>= (.length in-str-or-file) 3)
(= ":\\" (.substring in-str-or-file 1 3))
)
)
)
)
(throw (new RuntimeException (str "bad usage, passed: `" in-str-or-file "`")))
:else
(.getAbsolutePath (io/as-file in-str-or-file))))


10 changes: 6 additions & 4 deletions leiningen-core/test/leiningen/core/test/classpath.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
(:require [clojure.java.io :as io]
[clojure.set :as set]
[leiningen.core.user :as user]
[leiningen.core.project :as project]))
[leiningen.core.project :as project]
[leiningen.core.utils :as utils]))

(use-fixtures :once
(fn [f]
Expand All @@ -20,7 +21,7 @@
[ring/ring-core "1.0.0"
:exclusions [commons-codec]]]
:checkout-deps-shares [:source-paths :resource-paths
:compile-path #(str (:root %) "/foo")]
:compile-path #(utils/pathify (str (:root %) "/foo"))]
:repositories project/default-repositories
:root "/tmp/lein-sample-project"
:target-path "/tmp/lein-sample-project/target"
Expand Down Expand Up @@ -51,9 +52,10 @@
(dependency-hierarchy :dependencies project))))

(def directories
(vec (map utils/pathify
["/tmp/lein-sample-project/test"
"/tmp/lein-sample-project/src"
"/tmp/lein-sample-project/resources"])
"/tmp/lein-sample-project/resources"])))

(def libs
#{(str (m2-file "commons-io/commons-io/1.4/commons-io-1.4.jar"))
Expand All @@ -75,7 +77,7 @@
(spit (io/file d1 "project.clj")
(pr-str '(defproject hello "1.0")))
(is (= (for [path ["src" "resources" "target/classes" "foo"]]
(format "/tmp/lein-sample-project/checkouts/d1/%s" path))
(utils/pathify (format "/tmp/lein-sample-project/checkouts/d1/%s" path)))
(#'leiningen.core.classpath/checkout-deps-paths project)))
(finally
;; can't recur from finally
Expand Down
8 changes: 4 additions & 4 deletions leiningen-core/test/leiningen/core/test/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
(is (= v (k actual))))
(doseq [[k path] paths
:when (string? path)]
(is (= (str (:root actual) "/" path)
(is (= (utils/pathify (str (:root actual) "/" path))
(k actual))))
(doseq [[k path] paths
:when (coll? path)]
(is (= (for [p path] (str (:root actual) "/" p))
(is (= (for [p path] (utils/pathify (str (:root actual) "/" p)))
(k actual))))))

;; TODO: test omit-default
Expand Down Expand Up @@ -267,14 +267,14 @@
(project-with-profiles-meta
p
(merge @test-profiles (:profiles p))))]
(is (= ["/etc/myapp" "test/hi" "blue-resources" "resources"]
(is (= (vec (map utils/fix-path-delimiters ["/etc/myapp" "test/hi" "blue-resources" "resources"]))
(-> (make
(test-project
{:resource-paths ["resources"]
:profiles {:blue {:resource-paths ["blue-resources"]}}}))
(merge-profiles [:blue :tes :qa])
:resource-paths)))
(is (= ["/etc/myapp" "test/hi" "blue-resources"]
(is (= (vec (map utils/fix-path-delimiters ["/etc/myapp" "test/hi" "blue-resources"]))
(-> (make
(test-project
{:resource-paths ^:displace ["resources"]
Expand Down
2 changes: 1 addition & 1 deletion src/leiningen/new/plugin.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

(defn plugin
"A leiningen plugin project template."
[name]
[^String name]
(let [render (renderer "plugin")
unprefixed (if (.startsWith name "lein-")
(subs name 5)
Expand Down
3 changes: 2 additions & 1 deletion test/leiningen/test/new/templates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
leiningen.new.templates)
(:require [leiningen.test.helper :refer [abort-msg]]
[leiningen.core.user :as user]
[leiningen.core.utils :as utils]
[clojure.java.io :as io])
(:import [java.io File]))

Expand Down Expand Up @@ -52,7 +53,7 @@
(is (= (multi-segment "multi.segment" "last") "multi.segment")))

(deftest paths
(is (= (name-to-path "foo-bar.baz") "foo_bar/baz")))
(is (= (name-to-path "foo-bar.baz") (utils/fix-path-delimiters "foo_bar/baz"))))

(deftest renderers
(is (.contains (abort-msg (renderer "my-template") "boom" {})
Expand Down
5 changes: 3 additions & 2 deletions test/leiningen/test/pom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[leiningen.test.helper :only [sample-project]])
(:require [clojure.data.xml :as xml]
[leiningen.core.project :as project]
[leiningen.core.utils :as utils]
[leiningen.core.main :as main]))

(use-fixtures :once (fn [f]
Expand Down Expand Up @@ -96,7 +97,7 @@
"target directory is included")
(is (= nil (first-in xml [:project :build :extensions]))
"no extensions")
(is (= "target/classes" (first-in xml [:project :build :outputDirectory]))
(is (= (utils/fix-path-delimiters "target/classes") (first-in xml [:project :build :outputDirectory]))
"classes directory is included")
(is (= ["org.clojure" "rome" "ring" "org.clojure" "clojure-complete"]
(map #(first-in % [:dependency :groupId])
Expand Down Expand Up @@ -275,7 +276,7 @@
(first-in [:project :classifier])))))

(deftest test-pom-adds-java-source-paths
(is (= ["java/src" "java/another"]
(is (= (vec (map utils/fix-path-delimiters ["java/src" "java/another"]))
(-> (make-pom (with-profile sample-project
{:java-source-paths ["java/src" "java/another"]}))
xml/parse-str
Expand Down
6 changes: 3 additions & 3 deletions test/leiningen/test/repl.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns leiningen.test.repl
(:require [clojure.test :refer :all]
[leiningen.repl :refer :all]
(leiningen.core [user :as user] [project :as project])))
(leiningen.core [user :as user] [project :as project] [utils :as utils])))

(deftest test-merge-repl-profile
(is (= (-> {:repl-options {:ack-port 4}}
Expand Down Expand Up @@ -61,13 +61,13 @@
"http://localhost:20" "http://localhost:20"))

(deftest test-options-for-reply
(is (= "/home/user/.lein-repl-history"
(is (= (utils/fix-path-delimiters "/home/user/.lein-repl-history")
(:history-file (options-for-reply {:root "/home/user"}))))
(let [prompt-fn (fn [ns] "hi ")]
(are
[in exp]
(= (merge
{:history-file (str (user/leiningen-home) "/repl-history")
{:history-file (utils/pathify (str (user/leiningen-home) "/repl-history"))
:input-stream System/in}
exp)
(let [[prj-k prj-v arg-k arg-v] in]
Expand Down

0 comments on commit a8e20e9

Please sign in to comment.