Fork me on GitHub
#rewrite-clj
<
2021-11-01
>
pithyless07:11:55

@nbtheduke that reminds me of this post from CircleCI - before rewrite-clj was released :) https://circleci.com/blog/rewriting-your-test-suite-in-clojure-in-24-hours/

🎉 1
lread12:11:30

Interesting @pithyless! > There was a long delay between actually rewriting the test suite, and the publishing of this blog post. In the meantime, https://github.com/xsc/rewrite-clj has been released. I haven’t used it, but it looks like exactly what we were missing.

Noah Bogart13:11:04

that’s a great blog post! i’d forgotten about it

Noah Bogart15:11:36

i’m using the node API cuz it’s more my style, and I’m struggling a bit when a node has metadata:

(<token: deftest> <whitespace: " "> <meta:
  ^{:card-title "15-minutes"}
    fifteen-minutes
>)

Noah Bogart15:11:20

compared to a test that doesn’t have metadata: (<token: deftest> <whitespace: " "> <token: above-the-law>)

Noah Bogart15:11:23

do i just have to check n/inner? on the deftest name to see if it has metadata and then (somehow) extract the symbol?

Noah Bogart15:11:51

and then when i run n/children, i get back [<map: {:card-title "15-minutes"}> <newline: "\n"> <whitespace: " "> <token: fifteen-minutes>]

Noah Bogart15:11:07

if this means i have to write a helper function to extract the symbol, then okay i guess

borkdude15:11:52

@nbtheduke This is one minor thing that I don't find that intuitive about rewrite-clj. What I've done in clj-kondo is make a function which turns this upside down: a meta node is a child of the other node. I can get away with this because clj-kondo doesn't write code to text. Yes, a helper function is the answer I think.

borkdude15:11:15

This tripped up clj-kondo badly in the beginning because metadata can be pretty much anywhere so you have to do these checks a lot.

Noah Bogart16:11:16

i’ve subscribed so i can track it!

Noah Bogart16:11:57

with the zipper api, the location is a point within the whole parsed text so at any point you can move forward and backwards. that doesn’t seem possible with the node API. have i missed something?

borkdude16:11:53

Good old times with @sogaiu :)

❤️ 1
borkdude16:11:45

@nbtheduke yes, a zipper location and moving that location (cursor if you will ) around is only something in the zipper API

Noah Bogart20:11:37

okay, switched to zipper api and while it’s more awkward, it’s a lot more powerful

Noah Bogart20:11:06

i have filtered all of the testing branches from inside my tests so they’re in a list:

(defn get-testing-branches [zloc]
  (->> zloc
       (iterate z/right)
       (take 100)
       (filter #(testing? (z/down %)))
       doall))
which i remove and set aside with (let [testing-branches (get-testing-branches zloc) zloc (remove-testing-branches zloc)] …)

Noah Bogart20:11:02

now i want to insert these into the zipper after the deftests. (z/up zloc) gets me to the top-level, and then (z/insert-right ???) to insert a given testing block into the top level file?

Noah Bogart21:11:50

if I just use (first testing-branches), i get something like:

(deftest ^{:card-title "15-minutes"}
    fifteen-minutes
    ;; 15 Minutes - check if it works correctly from both sides
  ) ^{:zip/branch? #function[rewrite-clj.node.protocols/eval50757/fn--50758/G--50742--50763], :zip/children #function[clojure.core/comp/fn--5529], :zip/make-node #function[rewrite-clj.node.protocols/eval50757/fn--50791/G--50746--50798], :rewrite-clj.zip/opts {:track-position? false, :auto-resolve #function[rewrite-clj.node.protocols/default-auto-resolve]}} [(testing "in corp score area"
...

Noah Bogart21:11:42

idk how to convert a seq of zlocs to nodes i can insert into the document. everything in the node namespace seems to be about manually creating them

Noah Bogart21:11:52

my apologies for the flurry of messages/spam lol

lread22:11:36

It rarely snows here @nbtheduke, so your flurry is most welcome!

1
lread22:11:32

So, if I understand here… you’ve saved a bunch of zipper locations and now you want to insert them as nodes to specific spots. You can always get the node at zipper location with rewrite-clj.zip/node. Does that help?

lread23:11:57

I just re-read your use case @nbtheduke : > i’m considering using rewrite-clj to rewrite/rearrange my test suite. it’s currently written in the philosophy of “few `deftest`s, many `testing` branches”, which makes running specific branches hard. i’m looking to pluck each `testing` branch out of the deftests and then place them in a new `deftest` with some name. would this be possible with rewrite-clj? I’ve been meaning to beef up the examples included in the user guide. This sounds like a good one to eventually write up (so, thanks for your question!)

lread23:11:51

I guess it might get a little bit trickier if you have nested testings…

Noah Bogart23:11:13

I’ll check out zip.node, I must have missed it. Thankfully, I don’t want to recurse to find nested testing blocks, we’ve been careful about keeping the top-level testing blocks as complete units. Maybe once I’m done and have this working, I’ll show my code and we can write up a longer guide to demonstrate!

Noah Bogart23:11:08

Something I noticed is that insert-right and the related functions use item as the argument name does the object to be placed. Without examples or further description, I didn’t/don’t know what kind of object item is supposed to be, and my small experiments weren’t fruitful.

Noah Bogart23:11:55

(I purposefully didn’t dive into the source cuz that can easily dovetail into reading the whole code base lol)

lread23:11:30

Thanks for the great feedback on the vagueness of item! I’ll make an issue to update this and similarly vague docstrings.

lread23:11:33

Oh I didn’t answer your question about item… If it is not already a rewrite-clj node, rewrite-clj will attempt to coerce it to one.

lread23:11:07

Except it won’t attempt to coerce a zloc to a node, as you’ve noticed!

👍 1