Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Testing Dolt using BATS (Bash Automated Testing System) (dolthub.com)
47 points by timsehn on March 23, 2020 | hide | past | favorite | 15 comments


What I've learned from writing a Bash testing framework:

Bash is inherently untestable.

The only way to reliably return something besides a status code is via globals.

That means that most functions already rely on previously set global state. Which is one of the reasons that Bash scripts don't scale (the other is non-existent error handling).

Do yourself a favor and use a proper scripting language.


Pass-by-global is awful. Make smaller functions that return (echo) a single string.

Your bash is untestable, but that's just because of your chosen abstractions.

Bash is just a proper script language with a weird string and implicit IO system.

Disclaimer: I stick to POSIX sh but same difference.


I'm a BIG FAN of BATS (https://github.com/bats-core/bats-core) however sadly BATS is no longer being maintained ––good thing it's still working.


I used it for a big project about two years ago, and between it and Shellcheck I had pretty good time maintaining a relatively large Bash codebase (we're talking tens of thousands of lines of code across several distinct components).

Initially I was skeptical, and tried using Node and Mocha for writing tests instead, but calling shell from other languages is pretty cumbersome, and at some point that became very annoying. I gave Bats a try and was pleasantly surprised.

Once one gets used to it Shell is a really good language if the large chunk of your work is calling out external processes and passing the outputs between them. Sure, some language design choices of Shell are arcane and subpar, but with tools like Bats and Shellcheck it's possible to reach comparable levels of developer ergonomics that we all got used to with more popular languages.


Bless you <3

Lots of bash hate on HN today.

Shellcheck is the training wheels bash deserves, if you're learning it's indispensable just to harass you for quoting.

https://shellcheck.net/


>however sadly BATS is no longer being maintained

Are you sure?

https://github.com/bats-core/bats-core/issues/266


Looks like it was finally forked https://github.com/sstephenson/bats/issues/150



Used it many years ago and I must admit that while it's easy to start with you can end up with super weird bugs down the line where the only option can be to fork and fix it (after/if you find the issue) :) wouldn't use it again when there are maintained alternatives. Nowadays I just write these integration tests in Go since quite often it's handy to use api client libs to verify some data or prepare some fixtures.


Apologies if this is a dumb question. I glanced at the linked document and it is not obvious to me if this is for testing bash or for testing bash scripts.


Neither. It’s called that because it’s using a dialect/DSL of bash.

It’s a general unit test tool, but working in bash orients Bats towards testing of scripted/automated infrastructure. I’ve used it for tests of Chef/Puppet configuration and found Bats a good fit. Seems odd to find it used as the test framework for a data management tool but clearly it floats their boat.


BATS is a testing framework, written in bash.

https://github.com/bats-core/bats-core

It's really nice for when you want to run/test a bunch of things in bash and have nice output and test suites.


It's for writing tests that look like bash scripts. I wonder if `shellcheck` can still save the day on them.


Only very tangentially related: https://batstrafficsolutions.com/


BATS also happens to be the name of an Apple internal testing platform, interestingly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: