Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Gerhard Lazu

March 17, 2016
Tweet

More Decks by Gerhard Lazu

Other Decks in Technology

Transcript

  1. TDD #!/bin/bash

    View Slide

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

    View Slide

  3. test

    View Slide

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

    View Slide

  5. [[ ]]

    View Slide

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

    View Slide

  7. ||

    View Slide

  8. $ cf push &>/dev/null
    $ echo $?
    1
    $ cf push >&- || echo "Call 222333"
    Call 222333
    $ echo $?
    0

    View Slide

  9. if

    View Slide

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

    View Slide

  11. The TDD Apostate1
    1 Mark Seemann

    View Slide

  12. TDD != good code

    View Slide

  13. How to write
    good bash code?

    View Slide

  14. github.com/gerhard/bash

    View Slide

  15. The path to
    good bash code

    View Slide

  16. Understand the bash pitfalls

    View Slide

  17. Use shellcheck

    View Slide

  18. Emphasise on functional code

    View Slide

  19. Discover the simplest concepts

    View Slide

  20. Explore their optimal composition

    View Slide

  21. Stop when the fun stops

    View Slide

  22. Contain that bash

    View Slide

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

    View Slide

  24. Containers for integration testing

    View Slide

  25. github.com/gerhard/bash

    View Slide