Archive

Posts Tagged ‘shell’

Testing shell scripts with Testing Framework

August 9th, 2012 2 comments

TF – Testing Framework

For some time I have been testing shell scripts using my own creation TF – Testing Framework.

I’m proud to present release 0.4.0 of it today. It includes:

  • 2-4x speed improvement depending on used Ruby interpreter,
  • add support for different shells via shebang
  • improved validation of environment variables, including array variables testing and testing environment variable type,
  • extended output matching allowing to separately match stdout and stderr,

Installation

Install using (ruby required):


gem install tf

Supported tests

The test can be negated by replacing = with !=:

  • #status=<number> – check if command returned given status (0 is success)
  • #match=/<regexp>/ – regexp match command output both stdout and stderr
  • #match[stdout|stderr]=/<regexp>/ – regexp match command either stdout or stderr
  • #env[<var_name>]=~/<regexp>/ – regexp match the given environment variable name
  • #env[<var_name>]?=[array|string|nil] – verify type of the given environment variable name
  • #env[<var_name><var_name>][]=<size> – verify size of the given environment variable name
  • #env[<var_name>][]=/<regexp>/ – regexp match all of elements of the given environment variable name
  • #env[<var_name>][<index>]=/<regexp>/ – regexp match given element of the given environment variable name

And here is very simple test:


true # status=0

false # status!=0

echo "Super" # match=~/^Super$/

rvm install 1.9.3 # match[stderr]!=/Error/; status=0

rvm alias create default 1.9.3 # status=0

TF is used for testing RVM, SM Framework and rubygems-bundler. Let me know if any other awesome projects use TF!

TF was earlier known as DTF – Deryl’s Testing Framework, but as original author Deryl wanted to go in a bit different direction (more validation related), I had to move code to this new repository and name.

extended less usage

June 28th, 2011 Comments off

Ever used grep and wanted to check that file with less? Now it is easy with wrapping functions, they parse grep outputted lines.

So lets say we search for info in rvm packages:

grep -rn '__rvm_use(' ~/.rvm/scripts
/home/mpapis/.rvm/scripts/selector:391:__rvm_use()

So now you want to see what is inside of ‘.rvm/scripts/selector’ at line 391

less -N +j391 /home/mpapis/.rvm/scripts/selector

But wouldn’t be nice to just write less, double click the found line and middle click:

less /home/mpapis/.rvm/scripts/selector:391:__rvm_use

This will work when using following function:

less()
{
  local params=() param parsed
  for param in "$@"
  do
    case $param in
    (*:*)
      parsed=( ${param//:/ } )
      params+=( "-N" "+j${parsed[1]}" "${parsed[0]}" )
      ;;
    (*)
      params+=( "$param" )
      ;;
    esac
  done
  command less "${params[@]}"
}

Put the above code into ~/.functions and source it in ~/.bashrc

test -s ~/.functions && . ~/.functions || true

Now restart your shell and enjoy you extended less function.

Categories: Development, Linux Tags: , , ,