I have a function that saves some text to a file:
(defn save-keypair
"saves keypair to ~/.ssb-clj/secret"
[pair file-path]
(let [public-key-string (->> (:public pair) (.array) (byte-array) (b64/encode) (bs/to-string))
secret-key-string (->> (:secret pair) (.array) (byte-array) (b64/encode) (bs/to-string))]
(spit file-path (str "Public Key: " public-key-string))
(spit file-path (str "\nPrivate Key: " secret-key-string) :append true)))
It works fine (currently checking via just opening the file and looking at it myself). However, I'd like to write an actual test to check that everything is working correctly. Is there an idiomatic way of doing this in Clojure?
Use Java interop with
Filehttps://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html
In particular, see
File.createTempFile()and eitherfile.delete()orfile.deleteOnExit(). So you create a temp file, use that in your unit test, reading the file that you just wrote and verifying contents. Then either delete the file explicitly (ideally inside of try/finally) with auto-delete as a backup.Depending on how you set up the expected results in your tests, you may find the following useful:
These helper functions are especially useful for text file output, where the presence of a trailing
newlinechar can be OS dependent. They are also helpful to ignore differences due to the "newline" being CR, CR/LF, or LF.