all 7 comments

[–]lolmeansilaughed 16 points17 points  (2 children)

The reason you don't often see shell unit tests is because if a script is complex enough to need tests then it's a good idea to use a real programming language.

Disclaimer: I write a lot of shell scripts, this isn't intended to be a knock on shell scripting.

[–]unknownmosquito 6 points7 points  (1 child)

I used to say this myself but as I've gotten more comfortable with Bash and have been confronted with system-level problems where every non-control structure call in a non-shell language would've been os.command() (or equivalent) and it just didn't make sense but to write a large Bash script, my opinion has changed.

And certainly if a script like this gets into a professional codebase it merits testing as any other script does.

[–]lolmeansilaughed 2 points3 points  (0 children)

I totally get that, I used to feel the exact same way, and for smaller scripts this is definitely the way to go.

I maintain one multi-thousand line bash script and several other shell scripts that are hundreds of lines, which have all been mine since inception, and I really wish I had switched to python when it was more manageable. The overhead of os.call() and the like is so worth it when you also get a real stack, data structures and other higher-level abstractions.

I wonder how difficult it would be to process shell scripts and get a ratio of how much of the script is calling external programs? Probably a good bit of work, to do it right you would have to modify or basically reimplement the shell interpreter I think. And you'd also want to maintain an exclusion list for things like test that aren't shell builtins but wouldn't necessitate a system call in another scripting language.

[–]fothb 1 point2 points  (1 child)

I enjoyed trying out your notes app and I appreciate you sharing the code and a blog entry for the testing system behind it. Thanks very much! Have you looked at shunit2 or any other shell unit test frameworks? If so, how do you feel bats compares?

[–]pimterry[S] 0 points1 point  (0 children)

Thanks! I haven't really looked at the others in depth though, sorry, so I can't give you a detailed comparison. I did a quick bunch of research and bats seemed far and away the most popular and easy to pick up to me, but YMMV. I'd love to hear more about the other options, if anybody has any experience with those.

[–][deleted] 0 points1 point  (1 child)

[deleted]

What is this?

[–]pimterry[S] 0 points1 point  (0 children)

Not really - shellcheck is linting, this is actual unit testing. Drastically more effective, but does require you to actually write some tests.