<commit_before>(ns puppetlabs.puppetdb.testutils.queue
  (:require [clojure.string :as str]
            [clojure.java.shell :as shell]
            [stockpile :as stock]
            [puppetlabs.puppetdb.nio :refer [get-path]]
            [puppetlabs.puppetdb.testutils.nio :refer [create-temp-dir]]
            [puppetlabs.puppetdb.queue :as q]
            [puppetlabs.puppetdb.cheshire :as json]))

(defn rm-r [pathstr]
  (let [rm (shell/sh "rm" "-r" pathstr)]
    (when-not (zero? (:exit rm))
      (throw (-> "'rm -r %s' failed: %s"
                 (format (pr-str pathstr) (pr-str rm))
                 Exception.)))))

(defmacro with-stockpile [queue-sym & body]
  `(let [ns-str#  (str (ns-name ~*ns*))
         queue-dir# (-> (get-path "target" ns-str#)
                        (create-temp-dir "stk")
                        (.resolve "q")
                        str)
         ~queue-sym (stock/create queue-dir#)]
     (try
       ~@body
       (finally (rm-r queue-dir#)))))

(defprotocol CoerceToStream
  (coerce-to-stream [x]
    "Converts the given input to the input stream that stockpile requires"))

(extend-protocol CoerceToStream
  java.io.InputStream
  (coerce-to-stream [x] x)
  String
  (coerce-to-stream [x]
    (java.io.ByteArrayInputStream. (.getBytes x "UTF-8")))
  clojure.lang.IEditableCollection
  (coerce-to-stream [x]
    (let [baos (java.io.ByteArrayOutputStream.)]
      (with-open [osw (java.io.OutputStreamWriter. baos java.nio.charset.StandardCharsets/UTF_8)]
        (json/generate-stream x osw))
      (.close baos)
      (-> baos
          .toByteArray
          java.io.ByteArrayInputStream.))))

(defn store-command [q command-type version certname payload]
  (q/store-command q command-type version certname (coerce-to-stream payload)))
<commit_msg>(PDB-2905) Replace rm-r with fs/delete-dir<commit_after>(ns puppetlabs.puppetdb.testutils.queue
  (:require [clojure.string :as str]
            [clojure.java.shell :as shell]
            [me.raynes.fs :refer [delete-dir]]
            [stockpile :as stock]
            [puppetlabs.puppetdb.nio :refer [get-path]]
            [puppetlabs.puppetdb.testutils.nio :refer [create-temp-dir]]
            [puppetlabs.puppetdb.queue :as q]
            [puppetlabs.puppetdb.cheshire :as json]))

(defmacro with-stockpile [queue-sym & body]
  `(let [ns-str#  (str (ns-name ~*ns*))
         queue-dir# (-> (get-path "target" ns-str#)
                        (create-temp-dir "stk")
                        (.resolve "q")
                        str)
         ~queue-sym (stock/create queue-dir#)]
     (try
       ~@body
       (finally
         (delete-dir queue-dir#)))))

(defprotocol CoerceToStream
  (coerce-to-stream [x]
    "Converts the given input to the input stream that stockpile requires"))

(extend-protocol CoerceToStream
  java.io.InputStream
  (coerce-to-stream [x] x)
  String
  (coerce-to-stream [x]
    (java.io.ByteArrayInputStream. (.getBytes x "UTF-8")))
  clojure.lang.IEditableCollection
  (coerce-to-stream [x]
    (let [baos (java.io.ByteArrayOutputStream.)]
      (with-open [osw (java.io.OutputStreamWriter. baos java.nio.charset.StandardCharsets/UTF_8)]
        (json/generate-stream x osw))
      (.close baos)
      (-> baos
          .toByteArray
          java.io.ByteArrayInputStream.))))

(defn store-command [q command-type version certname payload]
  (q/store-command q command-type version certname (coerce-to-stream payload)))

{'fraction_non_alphanumeric': 0.1424840394660476, 'fraction_numerical': 0.0023215322112594312, 'mean_word_length': 2.346601941747573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2357773', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1183, 'n_words': 254}