Bang.sh - Bash Framework

Bang.sh - Bash Framework

4a849f1aa47e5c99f1aa156337f6c951?s=128

Gustavo Dutra

May 18, 2013
Tweet

Transcript

  1. 1.
  2. 3.
  3. 4.
  4. 5.
  5. 7.
  6. 8.
  7. 18.
  8. 25.
  9. 36.
  10. 37.

    #!/bin/bash source “bang.sh” function load_options () { b.opt.add_flag --stderr "Prints

    to stderr" b.opt.add_opt --text "Specify text to be printed" b.opt.add_alias --text -t b.opt.required_args --text }
  11. 38.

    #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { load_options b.opt.init "$@" if b.opt.check_required_args; then local text="$(b.opt.get_opt --text)" if b.opt.has_flag? --stderr; then echo "$text" 1>&2 else echo “$text” fi fi }
  12. 39.

    #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { # ... } # Run! run “$@”
  13. 40.

    $ ./samples/parsing_argv.sh \ --text ‘Hello World’ 2> /dev/null Hello World

    $ ./samples/parsing_argv.sh --stderr\ --text ‘Hello World’ > /dev/null Hello World
  14. 41.
  15. 53.
  16. 54.

    #!/bin/bash source “bang.sh” function load_options () { b.opt.add_flag --stderr "Prints

    to stderr" b.opt.add_opt --text "Specify text to be printed" b.opt.add_alias --text -t b.opt.required_args --text }
  17. 55.

    #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { load_options b.opt.init "$@" if b.opt.check_required_args; then local text="$(b.opt.get_opt --text)" if b.opt.has_flag? --stderr; then echo "$text" 1>&2 else echo “$text” fi fi }
  18. 56.

    #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { # ... } # Run! b.try.do run “$@” b.catch RequiredOptionNotSet b.opt.show_usage b.try.end
  19. 58.

    b.module.require foobar The program was aborted due to an error:

    Uncautch exception NoModuleFound: Module foobar not found
  20. 64.

    function b.test.if_raises_error () { function foo () { b.raise InvalidArgumentsError

    } b.unittest.assert_raise \ foo InvalidArgumentsError }
  21. 65.
  22. 66.

    function net_up? () { ping -c1 8.8.8.8 &> /dev/null }

    function print_net_stats () { if net_up?; then echo "UP" else echo "DOWN" fi }
  23. 67.

    function b.test.mocking_up () { function net_up () { return 0;

    } b.unittest.double.do net_up? net_up b.unittest.assert_equal \ "UP" "$(print_net_stats)" b.unittest.double.undo net_up? }
  24. 68.

    b.module.require unittest function b.test.mocking_up () { ... } function b.test.mocking_down

    () { function net_down { return 1; } b.unittest.double.do \ net_up? net_down b.unittest.assert_equal \ "DOWN" "$(print_net_stats)" b.unittest.double.undo net_up? }
  25. 72.

    Running test case ‘b.test.trim’ Running test case ‘b.test.if_creates_dir’ Running test

    case ‘b.test.if_fails’ Running test case ‘b.test.if_raises_error’ Running test case ‘b.test.doubling_calls’ 4 tests executed (Assertions: 5 passed / 0 failed)
  26. 73.

    FYI

  27. 75.

    #!/usr/bin/env bats create_version () { mkdir -p "${RBENV_ROOT}/versions/$1" } @test

    "set by RBENV_VERSION" { create_version "1.9.3" RBENV_VERSION=1.9.3 run rbenv-version assert_success "1.9.3 (set by RBENV_VERSION environment variable)" }
  28. 79.
  29. 83.