Save 37% off PRO during our Black Friday Sale! »

How to write good bash code using TDD?

How to write good bash code using TDD?

1. Understand the bash pitfalls
2. Use shellcheck
3. Emphasise on functional code
4. Discover the simplest concepts
5. Explore their optimal composition
6. Use containers for integration testing
7. Stop when the fun stops

https://github.com/gerhard/bash is meant to capture the mechanics and reasoning behind test-driven bash code. It also conveys the process behind evolving a good bash codebase.

6e1b896f6c4807aa4223acc428c57760?s=128

Gerhard Lazu

March 17, 2016
Tweet

Transcript

  1. TDD #!/bin/bash

  2. $ false $ echo $? 1 $ true $ echo

    $? 0 $ : $ echo $? 0
  3. test

  4. $ test -f ./missing $ echo $? 1 $ [

    -f ./missing ] $ echo $? 1
  5. [[ ]]

  6. $ [[ foobar =~ bar ]] $ echo $? 0

    $ [[ foobar =~ ^bar ]] $ echo $? 1
  7. ||

  8. $ cf push &>/dev/null $ echo $? 1 $ cf

    push >&- || echo "Call 222333" Call 222333 $ echo $? 0
  9. if

  10. if cf push >&- then true else echo "Call 222333"

    fi if ! cf push >&- then echo "Call 222333" fi
  11. The TDD Apostate1 1 Mark Seemann

  12. TDD != good code

  13. How to write good bash code?

  14. github.com/gerhard/bash

  15. The path to good bash code

  16. Understand the bash pitfalls

  17. Use shellcheck

  18. Emphasise on functional code

  19. Discover the simplest concepts

  20. Explore their optimal composition

  21. Stop when the fun stops

  22. Contain that bash

  23. Functional core, imperative shell2 2 Gary Bernhardt, Boundaries, SCNA &

    Ruby Conf 2012
  24. Containers for integration testing

  25. github.com/gerhard/bash