8 ms·
I recently wrote a program of slightly over 200 lines in portable AWK: https://gitlab.com/dbohdan/humsize https://gitlab.com/dbohdan/humsize. I wrote it for a s
by networked 4y ago
I recently wrote a program of slightly over 200 lines in portable AWK: https://gitlab.com/dbohdan/humsize https://gitlab.com/dbohdan/humsize. I wrote it for a specific operating system (NetBSD), but I have ended up using it everywhere; the portability helped with it. I can recommend AWK for small utilities that transform text in a line- and column-oriented manner and don't need libraries.
The main difficulties were making the command-line interface and testing. You can't have flags that begin with a dash in portable AWK without a shell wrapper, and I didn't want one. I settled on manually parsing key=value options, which I don't think are bad, just nonstandard. They look like this:
humsize format=%6.1f%1s 'zero= empty'
There is no standard way to test AWK code. For testing I wrote a shell script that checks the program's outputs with grep: https://gitlab.com/dbohdan/humsize/-/blob/122aaed8d65dc8c2859d269979ffa7b4b93ab365/test https://gitlab.com/dbohdan/humsize/-/blob/122aaed8d65dc8c285.... Don't do this; your tests should give the user (you) better feedback. You may think your program doesn't need anything but a couple of trivial tests that won't ever change; it is a pain when you inevitably are proven wrong. I should have instead had a directory with reference outputs and diffed against them to see what went wrong (my own example: https://github.com/dbohdan/initool/blob/72f65d3fde245ff8660cf360316666656810829b/test.sh https://github.com/dbohdan/initool/blob/72f65d3fde245ff8660c...).
To ensure I didn't introduce portability issues, I set up testing against different awks in GitLab CI.
image: debian:bullseye-slim
before_script:
- apt update
- apt install -y busybox gawk mawk original-awk
- ln -s "$(which busybox)" awk
- busybox wget -O goawk.tar.gz https://github.com/benhoyt/goawk/releases/download/v1.21.0/goawk_v1.21.0_linux_amd64.tar.gz
- tar xzvf goawk.tar.gz
test:
script:
- AWK=false ./test || true
- AWK=./awk ./test
- AWK=gawk ./test
- AWK=./goawk ./test
- AWK=mawk ./test
- AWK=original-awk ./test
Edit: Rephrased and added a nicer shell test example.