Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some tweaks to colorization

  • Loading branch information...
commit 792cb8a55eba810f68d58c16f344275ea1a07382 1 parent d14ff49
@marick authored
View
2  src/midje/config.clj
@@ -35,7 +35,7 @@
(defonce ^{:dynamic true}
*config* {:print-level :print-normally
- :colorize nil
+ :colorize (not (ecosystem/on-windows?))

I was trying to do this but got stuck with testing. I had trouble mocking the ecosystem call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
:visible-deprecation true
:visible-future true
:visible-failure-namespace false
View
49 src/midje/emission/colorize.clj
@@ -3,35 +3,38 @@
midje.emission.colorize
(:require [colorize.core :as color]
[clojure.string :as str]
- [midje.config :as config])
- (:use [midje.util.ecosystem :only [getenv on-windows?]]))
+ [midje.config :as config]
+ [midje.util.ecosystem :as ecosystem]))
-(defn colorize-setting []
- (config/choice :colorize))
+;; This indirection is required so that the tests of this
+;; file can fake the prerequisite
+(def config-choice config/choice)
-(defn- colorize-config-as-str []
- (let [setting (colorize-setting)
- setting (if (keyword? setting) (name setting) setting)
- setting-as-str (str setting)]
- (when-not (str/blank? setting-as-str) setting-as-str)))
+(defn- config-choice-as-string []
+ (let [choice (config-choice :colorize)]
+ (if (keyword? choice)
+ (name choice)
+ (str choice))))
-(defn- colorize-choice []
- (str/upper-case (or (getenv "MIDJE_COLORIZE")
- (colorize-config-as-str)
- (str (not (on-windows?))))))
+(defn- colorize-string []
+ (str/upper-case (or (ecosystem/getenv "MIDJE_COLORIZE")
+ (config-choice-as-string))))
(defn init! []
- (case (colorize-choice)
- "TRUE" (do
- (def fail color/red)
- (def pass color/green)
- (def note color/cyan))
-
- "REVERSE" (do
- (def fail color/red-bg)
- (def pass color/green-bg)
- (def note color/cyan-bg))
+ (case (colorize-string)
+ "TRUE"
+ (do
+ (def fail color/red)
+ (def pass color/green)
+ (def note color/cyan))
+
+ "REVERSE"
+ (do
+ (def fail color/red-bg)
+ (def pass color/green-bg)
+ (def note color/cyan-bg))
+ ;; else
(do
(def fail str)
(def pass str)
View
2  src/midje/util/ecosystem.clj
@@ -58,7 +58,7 @@
(System/getenv var))
(defn on-windows? []
- (re-find #"[Ww]in" (System/getProperty "os.name")))
+ (boolean (re-find #"[Ww]in" (System/getProperty "os.name"))))
(def line-separator (System/getProperty "line.separator"))
View
83 test/midje/emission/t_colorize.clj
@@ -4,48 +4,57 @@
(:require [midje.emission.colorize :as color]
[midje.config :as config]))
+(tabular
+ (fact "wraps string in ascii color if the user so desires"
+ (prerequisites (color/config-choice :colorize) => ?config-choice
+ (getenv "MIDJE_COLORIZE") => ?env-choice)
+ (color/init!)
+ (?color-fn "string") => ?result)
+
+ ?color-fn ?env-choice ?config-choice ?result
-(tabular
- (fact "wraps string in ascii color when env variable is not explicitly set to FALSE"
- (do
- (color/init!)
- (?color-fn "string")) => ?result
- (provided
- (on-windows?) => ?on-windows :times (range)
- (color/colorize-setting) => ?config-var :times (range)
- (getenv "MIDJE_COLORIZE") => ?env-var))
-
- ?color-fn ?env-var ?config-var ?on-windows ?result
- color/fail "FALSE" nil false "string"
- color/fail "TRUE" nil false "\u001b[31mstring\u001b[0m"
- color/fail "reverse" nil false "\u001b[41mstring\u001b[0m"
- color/fail nil nil false "\u001b[31mstring\u001b[0m"
- color/fail nil nil true "string"
-
- ;Expore priority of config-var vs env var
- color/fail nil true false "\u001b[31mstring\u001b[0m"
- color/fail "FALSE" true false "string"
- color/fail nil false false "string"
- color/fail "TRUE" false false "\u001b[31mstring\u001b[0m"
- color/fail nil :reverse false "\u001b[41mstring\u001b[0m"
-
- color/note "FALSE" nil false "string"
- color/note "TRUE" nil false "\u001b[36mstring\u001b[0m"
- color/note "reverse" nil false "\u001b[46mstring\u001b[0m"
- color/note nil nil false "\u001b[36mstring\u001b[0m"
- color/note nil nil true "string")
+ ;; On windows, the config-choice defaults to false
+ color/fail "FALSE" false "string"
+ color/fail "TRUE" false "\u001b[31mstring\u001b[0m"
+ color/fail "reverse" false "\u001b[41mstring\u001b[0m"
+ color/fail nil false "string"
+
+ ;; On Unixoids, the config-choice defaults to true
+ color/fail "FALSE" true "string"
+ color/fail "TRUE" true "\u001b[31mstring\u001b[0m"
+ color/fail "reverse" true "\u001b[41mstring\u001b[0m"
+ color/fail nil true "\u001b[31mstring\u001b[0m"
+
+ ;; The user can set the config variable.
+ color/fail nil :reverse "\u001b[41mstring\u001b[0m"
+ ;; The environment variable takes precedence
+ color/fail "false" :reverse "string")
+
+
+(tabular
+ (fact "about the escape strings that apply to different choices"
+ (prerequisite (getenv "MIDJE_COLORIZE") => ?env-value)
+ (color/init!)
+ (?color-fn "string") => ?result)
+
+ ?color-fn ?env-value ?result
+ color/fail "FALSE" "string"
+ color/fail "TRUE" "\u001b[31mstring\u001b[0m"
+ color/fail "reverse" "\u001b[41mstring\u001b[0m"
+
+ color/note "FALSE" "string"
+ color/note "TRUE" "\u001b[36mstring\u001b[0m"
+ color/note "reverse" "\u001b[46mstring\u001b[0m")
;; Reset to user's default colorization.
(color/init!)
-(fact "access environment vars only when namespace is loaded"
- (do
- (color/fail "a")
- (color/note "b")
- (color/fail "c")
- (color/note "d")) => anything
- (provided
- (getenv "MIDJE_COLORIZE") => anything :times 0))
+(fact "access environment vars only when namespace is loaded, not on each report line"
+ (prerequisite (getenv "MIDJE_COLORIZE") => anything :times 0)
+
+ (color/fail "a") => anything
+ (color/note "b") => anything
+ (color/fail "c") => anything)
Please sign in to comment.
Something went wrong with that request. Please try again.