1(ns leiningen.test.new.templates
2  (:use clojure.test
3        leiningen.new.templates)
4  (:require [leiningen.test.helper :refer [abort-msg] :as lthelper]
5            [leiningen.core.user :as user]
6            [clojure.java.io :as io])
7  (:import [java.io File]))
8
9(defn- getenv [s]
10  (System/getenv s))
11
12(deftest line-separators
13  (testing "that nothing changes when we're on unix systems"
14    (with-redefs [user/getprop (constantly "\n")]
15      (is (= (fix-line-separators "foo") "foo"))
16      (is (= (fix-line-separators "bar\nbaz") "bar\nbaz"))
17      (is (= (fix-line-separators "quux\n\n\nsycorax") "quux\n\n\nsycorax"))))
18
19  (testing "that newlines are correctly converted on '\\r\\n' systems"
20    (with-redefs [user/getprop (constantly "\r\n")]
21      (is (= (fix-line-separators "foo") "foo"))
22      (is (= (fix-line-separators "bar\nbaz") "bar\r\nbaz"))
23      (is (= (fix-line-separators "quux\n\n\nsycorax")
24             "quux\r\n\r\n\r\nsycorax"))))
25
26  (testing "that other bizarre systems get same treatment"
27    (with-redefs [user/getprop (constantly "\t\t")]
28      (is (= (fix-line-separators "foo") "foo"))
29      (is (= (fix-line-separators "bar\nbaz") "bar\t\tbaz"))
30      (is (= (fix-line-separators "quux\n\n\nsycorax")
31             "quux\t\t\t\t\t\tsycorax"))))
32
33  (testing "that one can override the normal system newline"
34    (with-redefs [user/getprop (constantly "\r\n")
35                  user/getenv (fn [s] (if (= s "LEIN_NEW_UNIX_NEWLINES")
36                                      "y"
37                                      (getenv s)))]
38      (is (= (fix-line-separators "foo") "foo"))
39      (is (= (fix-line-separators "bar\nbaz") "bar\nbaz"))
40      (is (= (fix-line-separators "quux\n\n\nsycorax") "quux\n\n\nsycorax")))))
41
42(deftest project-names
43  (is (= (project-name "org.example/foo.bar") "foo.bar"))
44  (is (= (project-name "example") "example"))
45  (is (= (sanitize-ns "org.example/foo-bar") "org.example.foo-bar"))
46  (is (= (sanitize-ns "foo-bar") "foo-bar"))
47  (is (= (sanitize-ns "foo_bar") "foo-bar")))
48
49(deftest namespaces
50  (is (= (multi-segment "foo") "foo.core"))
51  (is (= (multi-segment "foo" "api") "foo.api"))
52  (is (= (multi-segment "multi.segment" "last") "multi.segment")))
53
54(deftest paths
55  (is (= (name-to-path "foo-bar.baz") (lthelper/fix-path-delimiters "foo_bar/baz"))))
56
57(deftest renderers
58  (is (.contains (abort-msg (renderer "my-template") "boom" {})
59                 "Template resource 'leiningen/new/my_template/boom' not found."))
60  (is (.contains (abort-msg (renderer "my-template") "boom")
61                 "Template resource 'leiningen/new/my_template/boom' not found.")))
62
63(deftest slurp-resource-compatibility ; can be removed in 3.0.0
64  (is (= (slurp-resource "leiningen/new/template/temp.clj")
65         (slurp-resource (io/resource "leiningen/new/template/temp.clj")))))
66
67
68(deftest files
69  (testing "that files marked as executable are set executable"
70    (let [file (File/createTempFile "lein" "template")
71          path [(.getName file) (.getAbsolutePath file) :executable true]]
72      (binding [*dir* (.getParentFile file)
73                *force?* true]
74        (.deleteOnExit file)
75        (->files {} path)
76        (is (.canExecute file))))))
77