<commit_before>(ns lein-xjc.t-integration-tests
  (:require [clojure.java.io :as io]
            [clojure.string :as s]
            [leiningen.install :as install]
            [leiningen.xjc :as xjc]
            [leiningen.core.project :as project]
            [leiningen.clean :as clean]
            [leiningen.core.user :as user]
            [midje.sweet :refer :all]))

(defn- read-test-project [name]
  "Read one of the test projects under test-projects.
  Copied (and slightly modified) from the leiningen source.
  See leiningen.test.helper."
  (with-redefs [user/profiles (constantly {})]
    (letfn [(read-prj []
              (let [prj (project/read (format
                                        "test-projects/%s/project.clj"
                                        name))]
                (project/init-project
                  (project/project-with-profiles-meta
                    prj (merge @project/default-profiles
                               (:profiles prj))))))
            (clean-prj [prj]
              (clean/clean prj) prj)]
      #(clean-prj (read-prj)))))

(defn- file-exists? [path]
  (let [file (io/file path)]
    (and (.exists file) (.isFile file))))

(defchecker java-sources-created [project class-names]
  (checker [_]
           (let [generated-java (format "%s/%s"
                                        (:target-path project)
                                        (get-in project [:xjc-plugin :generated-java]))
                 src-files (map #(format "%s/%s.java"
                                         generated-java
                                         (s/replace % #"\." "/"))
                                class-names)]
             (every? file-exists? src-files))))

(defchecker java-classes-created [project class-names]
  (checker [_]
           (let [classes-dir (format "%s/classes" (:target-path project))
                 class-files (map #(format "%s/%s.class"
                                           classes-dir
                                           (s/replace % #"\." "/"))
                                  class-names)]
             (every? file-exists? class-files))))

(fact-group
  :integration-test
  (let [project-reader (read-test-project  "single-xsd")]
    (fact "running 'lein xjc' generates the java sources for
          the simple.xsd schema."
          (let [project (project-reader)]
            (xjc/xjc project) => (java-sources-created
                                   project
                                   ["com.example.ObjectFactory"
                                    "com.example.Something"])))
    (fact "running 'lein install' compiles the generated java classes"
          (let [project (project-reader)]
            (install/install project) => (java-classes-created
                                       project
                                       ["com.example.ObjectFactory"
                                        "com.example.Something"])))))
<commit_msg>Restructure existing integration-tests.<commit_after>(ns lein-xjc.t-integration-tests
  (:require [clojure.java.io :as io]
            [clojure.string :as s]
            [leiningen.install :as install]
            [leiningen.xjc :as xjc]
            [leiningen.core.project :as project]
            [leiningen.clean :as clean]
            [leiningen.core.user :as user]
            [midje.sweet :refer :all]))

(defn- read-test-project [name]
  "Read one of the test projects under test-projects.
  Copied (and slightly modified) from the leiningen source.
  See leiningen.test.helper."
  (with-redefs [user/profiles (constantly {})]
    (letfn [(read-prj []
              (let [prj (project/read (format
                                        "test-projects/%s/project.clj"
                                        name))]
                (project/init-project
                  (project/project-with-profiles-meta
                    prj (merge @project/default-profiles
                               (:profiles prj))))))
            (clean-prj [prj]
              (clean/clean prj) prj)]
      #(clean-prj (read-prj)))))

(defn- file-exists? [path]
  (let [file (io/file path)]
    (and (.exists file) (.isFile file))))

(defchecker java-sources-created [project class-names]
  (checker [_]
           (let [generated-java (format "%s/%s"
                                        (:target-path project)
                                        (get-in project [:xjc-plugin :generated-java]))
                 src-files (map #(format "%s/%s.java"
                                         generated-java
                                         (s/replace % #"\." "/"))
                                class-names)]
             (every? file-exists? src-files))))

(defchecker java-classes-created [project class-names]
  (checker [_]
           (let [classes-dir (format "%s/classes" (:target-path project))
                 class-files (map #(format "%s/%s.class"
                                           classes-dir
                                           (s/replace % #"\." "/"))
                                  class-names)]
             (every? file-exists? class-files))))

(def read-single-xsd-project (read-test-project "single-xsd"))

(fact "running 'lein xjc' generates the java sources for
      the simple.xsd schema."
      :integration-test
      (let [project (read-single-xsd-project)]
        (xjc/xjc project) => (java-sources-created
                               project
                               ["com.example.ObjectFactory"
                                "com.example.Something"])))

(fact "running 'lein install' compiles the generated java classes"
      :integration-test
      (let [project (read-single-xsd-project)]
        (install/install project) => (java-classes-created
                                       project
                                       ["com.example.ObjectFactory"
                                        "com.example.Something"])))

{'fraction_non_alphanumeric': 0.12263677430148905, 'fraction_numerical': 0.0, 'mean_word_length': 1.3010007698229407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5434764', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1518, 'n_words': 354}