├── .gitignore ├── .travis.yml ├── LICENSE ├── README.markdown ├── project.clj ├── src └── bultitude │ └── core.clj ├── test-resources └── bultitude │ └── invalid.clj └── test ├── bulti_tude ├── cond.cljc └── test.clj └── bultitude └── core_test.clj /.gitignore: -------------------------------------------------------------------------------- 1 | pom.xml 2 | *jar 3 | /lib/ 4 | /classes/ 5 | .lein-deps-sum 6 | .lein-failures 7 | pom.xml* 8 | target/ 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: clojure 2 | lein: lein2 3 | script: lein2 test-all -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 4 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF 5 | THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and 12 | documentation distributed under this Agreement, and 13 | 14 | b) in the case of each subsequent Contributor: 15 | 16 | i) changes to the Program, and 17 | 18 | ii) additions to the Program; 19 | 20 | where such changes and/or additions to the Program originate from and 21 | are distributed by that particular Contributor. A Contribution 22 | 'originates' from a Contributor if it was added to the Program by such 23 | Contributor itself or anyone acting on such Contributor's 24 | behalf. Contributions do not include additions to the Program which: 25 | (i) are separate modules of software distributed in conjunction with 26 | the Program under their own license agreement, and (ii) are not 27 | derivative works of the Program. 28 | 29 | "Contributor" means any person or entity that distributes the Program. 30 | 31 | "Licensed Patents" mean patent claims licensable by a Contributor 32 | which are necessarily infringed by the use or sale of its Contribution 33 | alone or when combined with the Program. 34 | 35 | "Program" means the Contributions distributed in accordance with this 36 | Agreement. 37 | 38 | "Recipient" means anyone who receives the Program under this 39 | Agreement, including all Contributors. 40 | 41 | 2. GRANT OF RIGHTS 42 | 43 | a) Subject to the terms of this Agreement, each Contributor hereby 44 | grants Recipient a non-exclusive, worldwide, royalty-free copyright 45 | license to reproduce, prepare derivative works of, publicly display, 46 | publicly perform, distribute and sublicense the Contribution of such 47 | Contributor, if any, and such derivative works, in source code and 48 | object code form. 49 | 50 | b) Subject to the terms of this Agreement, each Contributor hereby 51 | grants Recipient a non-exclusive, worldwide, royalty-free patent 52 | license under Licensed Patents to make, use, sell, offer to sell, 53 | import and otherwise transfer the Contribution of such Contributor, if 54 | any, in source code and object code form. This patent license shall 55 | apply to the combination of the Contribution and the Program if, at 56 | the time the Contribution is added by the Contributor, such addition 57 | of the Contribution causes such combination to be covered by the 58 | Licensed Patents. The patent license shall not apply to any other 59 | combinations which include the Contribution. No hardware per se is 60 | licensed hereunder. 61 | 62 | c) Recipient understands that although each Contributor grants the 63 | licenses to its Contributions set forth herein, no assurances are 64 | provided by any Contributor that the Program does not infringe the 65 | patent or other intellectual property rights of any other entity. Each 66 | Contributor disclaims any liability to Recipient for claims brought by 67 | any other entity based on infringement of intellectual property rights 68 | or otherwise. As a condition to exercising the rights and licenses 69 | granted hereunder, each Recipient hereby assumes sole responsibility 70 | to secure any other intellectual property rights needed, if any. For 71 | example, if a third party patent license is required to allow 72 | Recipient to distribute the Program, it is Recipient's responsibility 73 | to acquire that license before distributing the Program. 74 | 75 | d) Each Contributor represents that to its knowledge it has sufficient 76 | copyright rights in its Contribution, if any, to grant the copyright 77 | license set forth in this Agreement. 78 | 79 | 3. REQUIREMENTS 80 | 81 | A Contributor may choose to distribute the Program in object code form 82 | under its own license agreement, provided that: 83 | 84 | a) it complies with the terms and conditions of this Agreement; and 85 | 86 | b) its license agreement: 87 | 88 | i) effectively disclaims on behalf of all Contributors all warranties 89 | and conditions, express and implied, including warranties or 90 | conditions of title and non-infringement, and implied warranties or 91 | conditions of merchantability and fitness for a particular purpose; 92 | 93 | ii) effectively excludes on behalf of all Contributors all liability 94 | for damages, including direct, indirect, special, incidental and 95 | consequential damages, such as lost profits; 96 | 97 | iii) states that any provisions which differ from this Agreement are 98 | offered by that Contributor alone and not by any other party; and 99 | 100 | iv) states that source code for the Program is available from such 101 | Contributor, and informs licensees how to obtain it in a reasonable 102 | manner on or through a medium customarily used for software exchange. 103 | 104 | When the Program is made available in source code form: 105 | 106 | a) it must be made available under this Agreement; and 107 | 108 | b) a copy of this Agreement must be included with each copy of the Program. 109 | 110 | Contributors may not remove or alter any copyright notices contained 111 | within the Program. 112 | 113 | Each Contributor must identify itself as the originator of its 114 | Contribution, if any, in a manner that reasonably allows subsequent 115 | Recipients to identify the originator of the Contribution. 116 | 117 | 4. COMMERCIAL DISTRIBUTION 118 | 119 | Commercial distributors of software may accept certain 120 | responsibilities with respect to end users, business partners and the 121 | like. While this license is intended to facilitate the commercial use 122 | of the Program, the Contributor who includes the Program in a 123 | commercial product offering should do so in a manner which does not 124 | create potential liability for other Contributors. Therefore, if a 125 | Contributor includes the Program in a commercial product offering, 126 | such Contributor ("Commercial Contributor") hereby agrees to defend 127 | and indemnify every other Contributor ("Indemnified Contributor") 128 | against any losses, damages and costs (collectively "Losses") arising 129 | from claims, lawsuits and other legal actions brought by a third party 130 | against the Indemnified Contributor to the extent caused by the acts 131 | or omissions of such Commercial Contributor in connection with its 132 | distribution of the Program in a commercial product offering. The 133 | obligations in this section do not apply to any claims or Losses 134 | relating to any actual or alleged intellectual property 135 | infringement. In order to qualify, an Indemnified Contributor must: a) 136 | promptly notify the Commercial Contributor in writing of such claim, 137 | and b) allow the Commercial Contributor tocontrol, and cooperate with 138 | the Commercial Contributor in, the defense and any related settlement 139 | negotiations. The Indemnified Contributor may participate in any such 140 | claim at its own expense. 141 | 142 | For example, a Contributor might include the Program in a commercial 143 | product offering, Product X. That Contributor is then a Commercial 144 | Contributor. If that Commercial Contributor then makes performance 145 | claims, or offers warranties related to Product X, those performance 146 | claims and warranties are such Commercial Contributor's responsibility 147 | alone. Under this section, the Commercial Contributor would have to 148 | defend claims against the other Contributors related to those 149 | performance claims and warranties, and if a court requires any other 150 | Contributor to pay any damages as a result, the Commercial Contributor 151 | must pay those damages. 152 | 153 | 5. NO WARRANTY 154 | 155 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 156 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 157 | KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY 158 | WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 159 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 160 | responsible for determining the appropriateness of using and 161 | distributing the Program and assumes all risks associated with its 162 | exercise of rights under this Agreement , including but not limited to 163 | the risks and costs of program errors, compliance with applicable 164 | laws, damage to or loss of data, programs or equipment, and 165 | unavailability or interruption of operations. 166 | 167 | 6. DISCLAIMER OF LIABILITY 168 | 169 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR 170 | ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 171 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 172 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 173 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 174 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 175 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 176 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 177 | 178 | 7. GENERAL 179 | 180 | If any provision of this Agreement is invalid or unenforceable under 181 | applicable law, it shall not affect the validity or enforceability of 182 | the remainder of the terms of this Agreement, and without further 183 | action by the parties hereto, such provision shall be reformed to the 184 | minimum extent necessary to make such provision valid and enforceable. 185 | 186 | If Recipient institutes patent litigation against any entity 187 | (including a cross-claim or counterclaim in a lawsuit) alleging that 188 | the Program itself (excluding combinations of the Program with other 189 | software or hardware) infringes such Recipient's patent(s), then such 190 | Recipient's rights granted under Section 2(b) shall terminate as of 191 | the date such litigation is filed. 192 | 193 | All Recipient's rights under this Agreement shall terminate if it 194 | fails to comply with any of the material terms or conditions of this 195 | Agreement and does not cure such failure in a reasonable period of 196 | time after becoming aware of such noncompliance. If all Recipient's 197 | rights under this Agreement terminate, Recipient agrees to cease use 198 | and distribution of the Program as soon as reasonably 199 | practicable. However, Recipient's obligations under this Agreement and 200 | any licenses granted by Recipient relating to the Program shall 201 | continue and survive. 202 | 203 | Everyone is permitted to copy and distribute copies of this Agreement, 204 | but in order to avoid inconsistency the Agreement is copyrighted and 205 | may only be modified in the following manner. The Agreement Steward 206 | reserves the right to publish new versions (including revisions) of 207 | this Agreement from time to time. No one other than the Agreement 208 | Steward has the right to modify this Agreement. The Eclipse Foundation 209 | is the initial Agreement Steward. The Eclipse Foundation may assign 210 | the responsibility to serve as the Agreement Steward to a suitable 211 | separate entity. Each new version of the Agreement will be given a 212 | distinguishing version number. The Program (including Contributions) 213 | may always be distributed subject to the version of the Agreement 214 | under which it was received. In addition, after a new version of the 215 | Agreement is published, Contributor may elect to distribute the 216 | Program (including its Contributions) under the new version. Except as 217 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives 218 | no rights or licenses to the intellectual property of any Contributor 219 | under this Agreement, whether expressly, by implication, estoppel or 220 | otherwise. All rights in the Program not expressly granted under this 221 | Agreement are reserved. 222 | 223 | This Agreement is governed by the laws of the State of New York and 224 | the intellectual property laws of the United States of America. No 225 | party to this Agreement will bring a legal action under this Agreement 226 | more than one year after the cause of action arose. Each party waives 227 | its rights to a jury trial in any resulting litigation. 228 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # Bultitude 2 | 3 | [![Build Status](https://secure.travis-ci.org/Raynes/bultitude.png)](http://travis-ci.org/Raynes/bultitude) 4 | 5 | Bultitude is a library for finding namespaces on the classpath. 6 | 7 | ## Usage 8 | 9 | ```clojure 10 | user=> (require '[bultitude.core :as b]) 11 | nil 12 | user=> (take 10 (b/namespaces-on-classpath)) 13 | (bultitude.core-test bultitude.core clojure.data clojure.string clojure.test clojure.xml clojure.inspector clojure.repl clojure.set clojure.test.junit) 14 | user=> (b/namespaces-on-classpath :prefix "bultitude") 15 | (bultitude.core-test bultitude.core) 16 | user=> (b/namespaces-on-classpath :prefix "bultitude" :classpath "src") 17 | (bultitude.core) 18 | user=> (b/namespaces-on-classpath :prefix "bultitude" :classpath "src:test") 19 | (bultitude.core bultitude.core-test) 20 | ``` 21 | 22 | Value for :classpath can either be a String containing paths (using the underlying operating system's path separator), or a collection of `File` objects. 23 | 24 | By default Bultitude will ignore files that have unreadable `ns` forms in them in order not to cause problems with projects that include [moustache templates](https://github.com/davidsantiago/stencil) on their classpath. Most functions take an `ignore-unreadable?` arg which you can set to false to make it propagate exceptions from the reader. 25 | 26 | ## The Name 27 | 28 | I don't know. You'd have to ask [Phil](http://technomancy.us) about that one. 29 | 30 | ## History 31 | 32 | This library is a library similar to `clojure.tools.namespace`. It is 33 | designed to find namespaces on the classpath. This one was ripped from 34 | Leiningen's core because we decided it should be publically available to 35 | everyone. 36 | 37 | This library was originally devised in Leiningen because Leiningen had a 38 | few specific needs that `clojure.tools.namespace` did not provide. 39 | Furthermore, the library's author seems to be ignoring some issues with 40 | the library (having declined a filed issue about it so far) that makes 41 | the library explode when ran across a namespace with a namespace form 42 | that the reader cannot read. We ran into this problem because 43 | lein-newnew has mustache templates with `.clj` extensions and namespace 44 | forms with mustache syntax inside of them, and it would break any 45 | project that was using tools.namespace. If you have this kind of 46 | problem, you can use this library instead. 47 | 48 | Furthermore, this library has a few useful features like being able to 49 | provide your own classpath as a string and for only looking for 50 | namespaces matching a certain prefix. 51 | 52 | Note that regarding the above, the author of tools.namespace did eventually fix 53 | the issue. This library is still necessary and maintained because: 54 | 55 | * Leiningen uses it internally 56 | * There have been some fairly complex classpath contributions that people rely on 57 | * This is not a contrib project so you can contribute without a CA or a Jira patch parade 58 | * I don't have a lot of confidence in tools.namespace after the issue reported above was declined to be fixed for several months for no good reason 59 | 60 | New features have been added to tools.namespace recently, and when I get some time I'll see 61 | about porting them over. 62 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject bultitude "0.2.8" 2 | :min-lein-version "2.0.0" 3 | :description "A library for find Clojure namespaces on the classpath." 4 | :url "https://github.com/Raynes/bultitude" 5 | :license {:name "Eclipse Public License 1.0"} 6 | :dependencies [[org.clojure/clojure "1.7.0"] 7 | [org.tcrawley/dynapath "0.2.3"]] 8 | :aliases {"test-all" ["with-profile" "dev,default:dev,1.6:dev,1.5:dev,1.4:dev,1.3,dev" "test"]} 9 | :profiles {:test {:resources ["test-resources"]} 10 | :1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]} 11 | :1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]} 12 | :1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]} 13 | :1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}}) 14 | -------------------------------------------------------------------------------- /src/bultitude/core.clj: -------------------------------------------------------------------------------- 1 | (ns bultitude.core 2 | (:require [clojure.java.io :as io] 3 | [clojure.string :as string] 4 | [dynapath.util :as dp]) 5 | (:import (java.util.jar JarFile JarEntry) 6 | (java.util.zip ZipException) 7 | (java.io File BufferedReader PushbackReader InputStreamReader) 8 | (clojure.lang DynamicClassLoader))) 9 | 10 | (declare namespace-forms-in-dir 11 | file->namespace-forms) 12 | 13 | (def ^:dynamic *read-cond* 14 | (if (or (< 1 (:major *clojure-version*)) 15 | (and (= 1 (:major *clojure-version*)) 16 | (<= 7 (:minor *clojure-version*)))) 17 | {:read-cond :allow} 18 | nil)) 19 | 20 | (defn- clojure-source-file? [^File f] 21 | (and (.isFile f) 22 | (re-matches #".*\.cljc?" (.getName f)))) 23 | 24 | (defn- clojure-source-jar-entry? [^JarEntry f] 25 | (re-matches #".*\.cljc?" (.getName f))) 26 | 27 | (defn- jar? [^File f] 28 | (and (.isFile f) (.endsWith (.getName f) ".jar"))) 29 | 30 | (defn- read-ns-form 31 | "Given a reader on a Clojure source file, read until an ns form is found." 32 | ([rdr] (read-ns-form rdr true)) 33 | ([rdr ignore-unreadable?] 34 | (let [form (try (if *read-cond* 35 | (read (assoc *read-cond* :eof ::done) rdr) 36 | (read rdr false ::done)) 37 | (catch Exception e 38 | (if ignore-unreadable? 39 | ::done 40 | (throw e))))] 41 | (if (and (list? form) (= 'ns (first form))) 42 | form 43 | (when-not (= ::done form) 44 | (recur rdr ignore-unreadable?)))))) 45 | 46 | (defn ns-form-for-file 47 | ([file] (ns-form-for-file file true)) 48 | ([file ignore-unreadable?] 49 | (with-open [r (PushbackReader. (io/reader file))] 50 | (read-ns-form r ignore-unreadable?)))) 51 | 52 | (defn namespaces-in-dir 53 | "Return a seq of all namespaces found in Clojure source files in dir." 54 | ([dir] (namespaces-in-dir dir true)) 55 | ([dir ignore-unreadable?] 56 | (map second (namespace-forms-in-dir dir ignore-unreadable?)))) 57 | 58 | (defn namespace-forms-in-dir 59 | "Return a seq of all namespace forms found in Clojure source files in dir." 60 | ([dir] (namespace-forms-in-dir dir true)) 61 | ([dir ignore-unreadable?] 62 | (for [^File f (file-seq (io/file dir)) 63 | :when (and (clojure-source-file? f) (.canRead f)) 64 | :let [ns-form (ns-form-for-file f ignore-unreadable?)] 65 | :when ns-form] 66 | ns-form))) 67 | 68 | (defn- ns-form-in-jar-entry 69 | ([jarfile entry] (ns-form-in-jar-entry jarfile entry true)) 70 | ([^JarFile jarfile ^JarEntry entry ignore-unreadable?] 71 | (with-open [rdr (-> jarfile 72 | (.getInputStream entry) 73 | InputStreamReader. 74 | BufferedReader. 75 | PushbackReader.)] 76 | (read-ns-form rdr ignore-unreadable?)))) 77 | 78 | (defn- namespace-forms-in-jar 79 | ([jar] (namespace-forms-in-jar jar true)) 80 | ([^File jar ignore-unreadable?] 81 | (try 82 | (let [jarfile (JarFile. jar)] 83 | (for [entry (enumeration-seq (.entries jarfile)) 84 | :when (clojure-source-jar-entry? entry) 85 | :let [ns-form (ns-form-in-jar-entry jarfile entry 86 | ignore-unreadable?)] 87 | :when ns-form] 88 | ns-form)) 89 | (catch ZipException e 90 | (throw (Exception. (str "jar file corrupt: " jar) e)))))) 91 | 92 | (defn- split-classpath [^String classpath] 93 | (.split classpath (System/getProperty "path.separator"))) 94 | 95 | (defn loader-classpath 96 | "Returns a sequence of File objects from a classloader." 97 | [loader] 98 | (map io/as-file (dp/classpath-urls loader))) 99 | 100 | (defn classpath-files 101 | "Returns a sequence of File objects of the elements on the classpath." 102 | ([classloader] 103 | (map io/as-file (dp/all-classpath-urls classloader))) 104 | ([] (classpath-files (clojure.lang.RT/baseLoader)))) 105 | 106 | (defn- classpath->collection [classpath] 107 | (if (coll? classpath) 108 | classpath 109 | (split-classpath classpath))) 110 | 111 | (defn- classpath->files [classpath] 112 | (map io/file classpath)) 113 | 114 | (defn file->namespaces 115 | "Map a classpath file to the namespaces it contains. `prefix` allows for 116 | reducing the namespace search space. For large directories on the classpath, 117 | passing a `prefix` can provide significant efficiency gains." 118 | [^String prefix ^File f] 119 | (map second (file->namespace-forms prefix f))) 120 | 121 | (defn file->namespace-forms 122 | "Map a classpath file to the namespace forms it contains. `prefix` allows for 123 | reducing the namespace search space. For large directories on the classpath, 124 | passing a `prefix` can provide significant efficiency gains." 125 | ([prefix f] (file->namespace-forms prefix f true)) 126 | ([^String prefix ^File f ignore-unreadable?] 127 | (cond 128 | (.isDirectory f) (namespace-forms-in-dir 129 | (if prefix 130 | (io/file f (-> prefix 131 | (.replaceAll "\\." "/") 132 | (.replaceAll "-" "_"))) 133 | f) ignore-unreadable?) 134 | (jar? f) (let [ns-list (namespace-forms-in-jar f ignore-unreadable?)] 135 | (if prefix 136 | (for [nspace ns-list 137 | :let [sym (second nspace)] 138 | :when (and sym (.startsWith (name sym) prefix))] 139 | nspace) 140 | ns-list))))) 141 | 142 | (defn namespace-forms-on-classpath 143 | "Returs the namespaces forms matching the given prefix both on disk and 144 | inside jar files. If :prefix is passed, only return namespaces that begin with 145 | this prefix. If :classpath is passed, it should be a seq of File objects or a 146 | classpath string. If it is not passed, default to java.class.path and the 147 | current classloader, assuming it is a dynamic classloader." 148 | [& {:keys [prefix classpath ignore-unreadable?] 149 | :or {classpath (classpath-files) ignore-unreadable? true}}] 150 | (mapcat 151 | #(file->namespace-forms prefix % ignore-unreadable?) 152 | (->> classpath 153 | classpath->collection 154 | classpath->files))) 155 | 156 | (defn namespaces-on-classpath 157 | "Return symbols of all namespaces matching the given prefix both on disk and 158 | inside jar files. If :prefix is passed, only return namespaces that begin with 159 | this prefix. If :classpath is passed, it should be a seq of File objects or a 160 | classpath string. If it is not passed, default to java.class.path and the 161 | current classloader, assuming it is a dynamic classloader." 162 | [& args] 163 | (map second (apply namespace-forms-on-classpath args))) 164 | 165 | (defn path-for 166 | "Transform a namespace into a file path relative to classpath root, using the given extension (.clj default)." 167 | ([namespace] 168 | (path-for namespace "clj")) 169 | ([namespace extension] 170 | (str (-> (str namespace) 171 | (.replace \- \_) 172 | (.replace \. \/)) 173 | "." extension))) 174 | 175 | (defn doc-from-ns-form 176 | "Extract the docstring from a given ns form without evaluating the form. The docstring returned should be the return value of (:doc (meta namespace-symbol)) if the ns-form were to be evaluated." 177 | [ns-form] 178 | (:doc (meta (second (second (second (macroexpand ns-form))))))) 179 | -------------------------------------------------------------------------------- /test-resources/bultitude/invalid.clj: -------------------------------------------------------------------------------- 1 | ;; This is an invalid clojure file, and is used to check bultitude has no 2 | ;; problems with malformed source, which may be on the classpath for use 3 | ;; as templates, etc 4 | 5 | ({{somthing}}) 6 | 7 | (ns bultitude.{{some node}}) 8 | 9 | (def x 1) 10 | -------------------------------------------------------------------------------- /test/bulti_tude/cond.cljc: -------------------------------------------------------------------------------- 1 | (ns bulti-tude.cond 2 | #?(:cljs (:require [cljs.test])) 3 | ) 4 | -------------------------------------------------------------------------------- /test/bulti_tude/test.clj: -------------------------------------------------------------------------------- 1 | (ns bulti-tude.test) 2 | -------------------------------------------------------------------------------- /test/bultitude/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns bultitude.core-test 2 | (:require [clojure.java.io :as io]) 3 | (:use clojure.test 4 | bultitude.core)) 5 | 6 | (deftest namespaces-in-dir-test 7 | (testing namespaces-in-dir 8 | (is (= (if *read-cond* 9 | '#{bulti-tude.cond bulti-tude.test} 10 | '#{bulti-tude.test}) 11 | (set (namespaces-in-dir "test/bulti_tude")))))) 12 | 13 | (deftest namespaces-forms-in-dir-test 14 | (testing namespace-forms-in-dir 15 | (is (= (if *read-cond* 16 | '#{(ns bulti-tude.cond) (ns bulti-tude.test)} 17 | '#{(ns bulti-tude.test)}) 18 | (set (namespace-forms-in-dir "test/bulti_tude")))))) 19 | 20 | (deftest file->namespaces-test 21 | (testing "on a directory with a clj in it" 22 | (is (= (if *read-cond* 23 | '#{bulti-tude.cond bulti-tude.test} 24 | '#{bulti-tude.test}) 25 | (set (file->namespaces nil (io/file "test/bulti_tude"))))))) 26 | 27 | (deftest file->namespace-forms-test 28 | (testing "on a directory with a clj in it" 29 | (is (= (if *read-cond* 30 | '#{(ns bulti-tude.cond) (ns bulti-tude.test)} 31 | '#{(ns bulti-tude.test)}) 32 | (set (file->namespace-forms nil (io/file "test/bulti_tude"))))))) 33 | 34 | (deftest namespaces-on-classpath-test 35 | (testing "find clojure.core" 36 | (is (seq (filter 37 | #(= 'clojure.core %) 38 | (namespaces-on-classpath))))) 39 | (testing "prefix" 40 | (is (seq (filter 41 | #(= 'clojure.core %) 42 | (namespaces-on-classpath :prefix "clojure.core")))) 43 | (is (every? 44 | #(.startsWith (name %) "clojure.core") 45 | (namespaces-on-classpath :prefix "clojure.core")))) 46 | (testing "directory" 47 | (is (= 48 | #{'bultitude.core 'bultitude.core-test} 49 | (set (namespaces-on-classpath :prefix "bultitude"))))) 50 | (testing "dash handling in prefixes" 51 | (is (= (if *read-cond* 52 | '#{bulti-tude.cond bulti-tude.test} 53 | '#{bulti-tude.test}) 54 | (set (namespaces-on-classpath :prefix "bulti-tude")))))) 55 | 56 | (deftest namespace-forms-on-classpath-test 57 | (testing namespace-forms-on-classpath 58 | (is (every? 59 | #(= 'ns (first %)) 60 | (namespace-forms-on-classpath))))) 61 | 62 | (defn test-doc-from-ns-form-helper 63 | [docstring ns-form] 64 | (eval ns-form) 65 | (is (= docstring 66 | (:doc (meta *ns*)) 67 | (doc-from-ns-form ns-form)))) 68 | 69 | (deftest doc-from-ns-form-test 70 | (testing "doc-from-ns-form" 71 | (let [callee-ns-name (ns-name *ns*)] 72 | (test-doc-from-ns-form-helper 73 | nil 74 | '(ns no-doc-namespace-name)) 75 | (test-doc-from-ns-form-helper 76 | "Docstring" 77 | '(ns regular-doc-namespace-name "Docstring")) 78 | (test-doc-from-ns-form-helper 79 | "Attribute-Docstring" 80 | '(ns attribute-doc-namepsace-name {:doc "Attribute-Docstring"})) 81 | (test-doc-from-ns-form-helper 82 | "Meta-Docstring" 83 | '(ns ^{:doc "Meta-Docstring"} meta-doc-namespace-name)) 84 | (test-doc-from-ns-form-helper 85 | "Docstring" 86 | '(ns ^{:doc "Meta-Docstring"} meta-and-reg-doc-namespace-name "Docstring")) 87 | (test-doc-from-ns-form-helper 88 | "Attribute-Docstring" 89 | '(ns reg-and-attribute-doc-namespace-name "Docstring" {:doc "Attribute-Docstring"})) 90 | (test-doc-from-ns-form-helper 91 | "Attribute-Docstring" 92 | '(ns ^{:doc "Meta-Docstring"} all-doc-namespace-name "Docstring" {:doc "Attribute-Docstring"})) 93 | (in-ns callee-ns-name)))) 94 | 95 | (deftest test-invalid-namespace 96 | (is (= nil (ns-form-for-file (io/file "test-resources/bultitude/invalid.clj") true))) 97 | (is (thrown-with-msg? RuntimeException #"Map literal must contain an even number of forms" 98 | (ns-form-for-file (io/file "test-resources/bultitude/invalid.clj") false)))) 99 | --------------------------------------------------------------------------------