I wanted to see how a jq track might work in Exercism. Here’s what I tried out this morning.
Exercism is a great resource for learning and practising languages. I’ve dabbled in a couple of tracks and it’s a fun and compelling way to iterate and meditate on constructs in the languages you’re interested in. One of the very appealing things to me is that as well as a very capable online editor environment, there’s a command line interface (CLI) for working locally.
Digging into jq
I’ve recently been digging into jq and wanting to build my knowledge out beyond the classic one-liners one might normally express in a JSON processing pipeline situation. jq
is a complete language, with a functional flavour and there’s support for modules, function definitions and more. The manual felt pretty terse at first, but after a while my brain got used to it.
I thought it might be an interesting exercise to see how a jq
track might work with Exercism; initially I just want to perhaps use some of the existing tests to code against, where I provide jq
scripts to compute the right answers.
Using the bash track for jq
As jq
is “just another Unix tool” that works well on the command line, it seemed logical to try and start with something similar, which I did – the bash
track. Here’s what I did to feel my way into this journey. It’s early days, and this blog post is more of a reminder to my future self what I did.
Downloading a bash track exercise as a base
Having set myself up for working locally I downloaded a simple exercise from the Bash track – Reverse String, and moved it to a new, local jq
track directory:
# /home/user
; cd work/Exercism/
# /home/user/work/Exercism
; ls
./ ../ bash/
# /home/user/work/Exercism
; mkdir jq
# /home/user/work/Exercism
; exercism download --exercise=reverse-string --track=bash
Downloaded to
/home/user/work/Exercism/bash/reverse-string
# /home/user/work/Exercism
; mv bash/reverse-string jq/
# /home/user/work/Exercism
; cd jq/reverse-string/
# /home/user/work/Exercism/jq/reverse-string/
; ls
./ ../ .exercism/ HELP.md README.md bats-extra.bash reverse_string.bats reverse_string.sh
# /home/user/work/Exercism/jq/reverse-string
;
Modifying the bats file
The bash
track uses the Bash Automated Testing System, known as bats
, for unit testing. The tests are in the reverse_string.bats
file and look like this (just the first two tests are shown here):
#!/usr/bin/env bats
load bats-extra@test "an empty string" {
run bash reverse_string.sh ""
assert_success
assert_output ""
}
@test "a wor