$30 off During Our Annual Pro Sale. View Details »

Bang.sh - Bash Framework

Bang.sh - Bash Framework

Gustavo Dutra

May 18, 2013
Tweet

More Decks by Gustavo Dutra

Other Decks in Programming

Transcript

  1. #!/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 }
  2. #!/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 }
  3. #!/bin/bash source “bang.sh” function load_options () { # ... }

    function run () { # ... } # Run! run “$@”
  4. $ ./samples/parsing_argv.sh \ --text ‘Hello World’ 2> /dev/null Hello World

    $ ./samples/parsing_argv.sh --stderr\ --text ‘Hello World’ > /dev/null Hello World
  5. #!/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 }
  6. #!/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 }
  7. #!/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
  8. b.module.require foobar The program was aborted due to an error:

    Uncautch exception NoModuleFound: Module foobar not found
  9. function b.test.if_raises_error () { function foo () { b.raise InvalidArgumentsError

    } b.unittest.assert_raise \ foo InvalidArgumentsError }
  10. 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 }
  11. 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? }
  12. 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? }
  13. 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)
  14. FYI

  15. #!/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)" }