Skip to content

Commit

Permalink
Migrated to leiningen 2 and latest cucumber-jvm version.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilswloka committed Mar 21, 2012
1 parent e458228 commit e6dee91
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 62 deletions.
24 changes: 17 additions & 7 deletions README.md
Expand Up @@ -4,21 +4,31 @@ This is a very simple leiningen plugin for use with [cucumber-jvm](https://githu

## Usage

1. Add `[lein-cucumber "0.2.0"]` to `:dev-dependencies` in your project.clj
1. Add `[lein-cucumber "1.0.0.M1-SNAPSHOT"]` to `:plugins` in your project.clj
2. Run `lein deps` to fetch all dependencies.
3. Run all Cucumber features with `lein cucumber`

## Please note

In the current version, you cannot specify any configuration options. The following settings are hard-coded into the plugin:
lein-cucumber requires Leiningen 2.

* A progress report will be printed to the console.
* The complete report (formatted with `PrettyFormatter`) will be written to `test-reports/cucumber.out`. Consider adding `:extra-files-to-clean ["test-reports"]` to your project.clj.
## Configuration

You can configure feature paths like this:

* Add a `:cucumber-feature-paths` parameter to your project.clj (e.g. `:cucumber-feature-path ["test/features/"]`).
* By default, lein-cucumber looks for your `.feature` files in the `features/` directory.
* Step definitions will be loaded from the `step_definitions/` directories inside your feature directories.

## Other settings

The following settings are hard-coded into the plugin:

* A summary report will be printed to the console.
* The complete report (formatted with `CucumberPrettyFormatter`) will be written to `test-reports/cucumber.out` inside your project's target directory (usually `target/`).
* Leiningen will exit with the exit status of the cucumber-jvm [runtime](https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/runtime/Runtime.java).
* Put your `.feature` files into `features/` (feature-paths can be configured in project.clj using the `:cucumber-feature-path` parameter, e.g `:cucumber-feature-path ["test/features/"]`.)
* Put your step definitions into `features/step_definitions/`

See https://github.com/cucumber/cucumber-jvm/tree/master/clojure/src/test/resources/cucumber/runtime/clojure for an example specification.
See https://github.com/cucumber/cucumber-jvm/tree/master/examples/clojure_cukes for an example project.

## Unlicense

Expand Down
5 changes: 3 additions & 2 deletions project.clj
@@ -1,7 +1,8 @@
(defproject lein-cucumber "1.0.0.M1-SNAPSHOT"
:description "Run cucumber-jvm specifications with leiningen"
:dependencies [[info.cukes/cucumber-clojure "1.0.0.RC23-SNAPSHOT"]]
:eval-in-leiningen true
:dependencies [[info.cukes/cucumber-clojure "1.0.0.RC23-SNAPSHOT"]
[leiningen-core "2.0.0-preview2"]]
:eval-in :leiningen
:license {:name "Unlicense"
:url "http://unlicense.org/"
:distribution :repo})
24 changes: 13 additions & 11 deletions src/leiningen/cucumber.clj
@@ -1,19 +1,21 @@
(ns leiningen.cucumber
(:use [leiningen.compile :only [eval-in-project]])
(:require [leiningen.core :as core]))
(:use [clojure.java.io])
(:use [leiningen.core.eval :only [eval-in-project]]))

(defn cucumber
"Runs Cucumber features in test/features with glue in test/features/step_definitions"
[project]
(let [runtime (gensym "runtime")
feature-paths (:cucumber-feature-path project)]
feature-paths (into [] (get project :cucumber-feature-paths ["features"]))
glue-paths (into [] (map #(str (file % "step_definitions/")) feature-paths))
target-path (:target-path project)]
(eval-in-project
project
(-> project
(update-in [:dependencies] conj
['lein-cucumber "1.0.0.M1-SNAPSHOT"]
['info.cukes/cucumber-clojure "1.0.0.RC23-SNAPSHOT"])
(update-in [:source-paths] (partial apply conj) glue-paths))
`(do
(let [~runtime (leiningen.cucumber.util/run-cucumber! ~feature-paths)]
(println)
~(when-not core/*interactive?*
`(System/exit (.exitStatus ~runtime)))))
nil
nil
'(require 'leiningen.cucumber.util))))
(let [~runtime (leiningen.cucumber.util/run-cucumber! ~feature-paths ~glue-paths ~target-path)]
(leiningen.core.main/exit (.exitStatus ~runtime))))
'(require 'leiningen.cucumber.util 'leiningen.core.main))))
65 changes: 23 additions & 42 deletions src/leiningen/cucumber/util.clj
@@ -1,52 +1,33 @@
(ns leiningen.cucumber.util
(:use [clojure.java.io])
(:import [gherkin.formatter PrettyFormatter])
(:import [cucumber.formatter FormatterFactory MultiFormatter])
(:import [cucumber.formatter CucumberPrettyFormatter])
(:import [cucumber.io FileResourceLoader])
(:import [cucumber.cli DefaultRuntimeFactory])
(:import [cucumber.runtime.snippets SummaryPrinter])
(:import [java.io File])
(:import [java.util ArrayList]))
(:import [cucumber.runtime.model CucumberFeature])
(:import [cucumber.runtime RuntimeOptions]))

(defn- report-writer []
(let [report-directory (file "test-reports")
report-file (file report-directory "cucumber.out")]
(defn- report-writer [target-path]
(let [report-file (file target-path "test-reports" "cucumber.out")]
(make-parents report-file)
(writer report-file)))

(defn- create-multi-formatter []
(let [classloader (.getContextClassLoader (Thread/currentThread))
formatter-factory (FormatterFactory. classloader)
multi-formatter (MultiFormatter. classloader)
out (report-writer)
formatter (PrettyFormatter. out true true)]
(doto multi-formatter
(.add formatter)
(.add (.createFormatter formatter-factory "progress" System/out)))
multi-formatter))

(defn- create-runtime [stepdef-paths]
(let [runtime-factory (DefaultRuntimeFactory.)
classloader (.getContextClassLoader (Thread/currentThread))
glue-paths (ArrayList. stepdef-paths)
file-resource-loader (FileResourceLoader.)]
(.createRuntime runtime-factory file-resource-loader glue-paths classloader false)))
(defn- create-runtime-options [feature-paths glue-paths target-path]
(let [out (report-writer target-path)
formatter (CucumberPrettyFormatter. out)
runtime-options
(proxy [RuntimeOptions] [(into-array String [])]
(reporter [classloader] formatter)
(formatter [classloader] formatter)
(cucumberFeatures [resource-loader] (CucumberFeature/load resource-loader feature-paths [])))]
(set! (. runtime-options glue) (java.util.ArrayList. glue-paths))
runtime-options))

(defn- gen-stepdef-paths [feature-paths]
(map (fn [path]
(format "%s%sstep_definitions" path (if (= (last path) \/) "" "/")))
feature-paths))
(defn- create-runtime [runtime-options]
(let [classloader (.getContextClassLoader (Thread/currentThread))
resource-loader (FileResourceLoader.)]
(cucumber.runtime.Runtime. resource-loader classloader runtime-options)))

(defn run-cucumber! [feature-paths]
(let [multi-formatter (create-multi-formatter)
feature-paths (if (some identity feature-paths)
feature-paths ["features"])
runtime (create-runtime (gen-stepdef-paths feature-paths))
formatter (.formatterProxy multi-formatter)
reporter (.reporterProxy multi-formatter)
summary-printer (SummaryPrinter. System/out)]
(.run runtime (ArrayList. feature-paths) (ArrayList.) formatter reporter)
(.done formatter)
(.print summary-printer runtime)
(.close formatter)
(defn run-cucumber! [feature-paths glue-paths target-path]
(let [runtime-options (create-runtime-options feature-paths glue-paths target-path)
runtime (create-runtime runtime-options)]
(.run runtime)
runtime))

0 comments on commit e6dee91

Please sign in to comment.