tf 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,8 +29,13 @@ Example test file:
29
29
  The test can be negated by replacing `=` with `!=`
30
30
 
31
31
  - status=<number> - check if command returned given status (0 is success)
32
- - match=/<regexp>/ - regexp match command output
33
- - env[<var_name>]=/<regexp>/ - regexp match the given environment variable name
32
+ - match=/<regexp>/ - regexp match command output both stdout and stderr
33
+ - match[stdout|stderr]=/<regexp>/ - regexp match command either stdout or stderr
34
+ - env[<var_name>]=~/<regexp>/ - regexp match the given environment variable name
35
+ - env[<var_name>]?=[array|string|nil] - verify type of the given environment variable name
36
+ - env[<var_name>][]=<size> - verify size of the given environment variable name
37
+ - env[<var_name>][]=/<regexp>/ - regexp match all of elements of the given environment variable name
38
+ - env[<var_name>][<index>]=/<regexp>/ - regexp match given element of the given environment variable name
34
39
 
35
40
  ### Selecting shell / runner program
36
41
 
@@ -44,7 +49,7 @@ Still only Bash / ZSH like shells are allowed.
44
49
  ##### Processed commands 2 of 2, success tests 2 of 3, failure tests 1 of 3.
45
50
  $ false
46
51
  # failed: status = 0 # was 1
47
-
52
+
48
53
  $ bin/tf example_tests/comment/* --text
49
54
  ##### starting test failure.
50
55
  $ false
@@ -0,0 +1,65 @@
1
+ class TF::EnvArrTest
2
+ MATCHER = {
3
+ :all => /^env\[(.*)\]\[\]([!]?=)[~]?\/(.*)\/$/,
4
+ :one => /^env\[(.*)\]\[(.+)\]([!]?=)[~]?\/(.*)\/$/,
5
+ :size => /^env\[(.*)\]\[\]([!]?=)([[:digit:]]+)$/,
6
+ }
7
+
8
+ def matches? test
9
+ TF::EnvArrTest::MATCHER.find{|k,v| test =~ v }
10
+ end
11
+
12
+
13
+ def execute test, _stdout, _stderr, _stdboth, _status, env
14
+ type, matcher = TF::EnvArrTest::MATCHER.find{|k,v| test =~ v }
15
+ send "execute_#{type}".to_sym, matcher, test, _stdout, _stderr, _stdboth, _status, env
16
+ end
17
+
18
+ def execute_one test, matcher, _stdout, _stderr, _stdboth, _status, env
19
+ test =~ matcher
20
+ variable, index, sign, value = $1.strip, $2, $3, $4
21
+ var_val = env[ variable ]
22
+ if var_val.is_a? Hash
23
+ var_val = var_val[index]
24
+ else
25
+ return [ false, "failed: env #{variable}[#{index}] #{sign} /#{value}/ # not an array" ]
26
+ end
27
+ if ( sign == "=" ) ^ ( Regexp.new(value) =~ "#{var_val}" )
28
+ [ false, "failed: env #{variable}[#{index}] #{sign} /#{value}/ # was '#{var_val}'" ]
29
+ else
30
+ [ true, "passed: env #{variable}[#{index}] #{sign} /#{value}/" ]
31
+ end
32
+ end
33
+
34
+ def execute_all test, matcher, _stdout, _stderr, _stdboth, _status, env
35
+ test =~ matcher
36
+ variable, sign, value = $1.strip, $2, $3, $4
37
+ var_val = env[ variable ]
38
+ if var_val.is_a? Hash
39
+ var_val = var_val.sort_by{|k,v| k}.map{|k,v| v}.join(" ")
40
+ else
41
+ return [ false, "failed: env #{variable}[] #{sign} /#{value}/ # not an array" ]
42
+ end
43
+ if ( sign == "=" ) ^ ( Regexp.new(value) =~ "#{var_val}" )
44
+ [ false, "failed: env #{variable}[] #{sign} /#{value}/ # was '#{var_val}'" ]
45
+ else
46
+ [ true, "passed: env #{variable}[] #{sign} /#{value}/" ]
47
+ end
48
+ end
49
+
50
+ def execute_size test, matcher, _stdout, _stderr, _stdboth, _status, env
51
+ test =~ matcher
52
+ variable, sign, value = $1.strip, $2, $3, $4
53
+ var_val = env[ variable ]
54
+ if var_val.is_a? Hash
55
+ var_val = var_val.size
56
+ else
57
+ return [ false, "failed: env #{variable}[] #{sign} #{value} # not an array" ]
58
+ end
59
+ if ( sign == "=" ) ^ ( Regexp.new(value) =~ "#{var_val}" )
60
+ [ false, "failed: env #{variable}[] #{sign} #{value} # was #{var_val}" ]
61
+ else
62
+ [ true, "passed: env #{variable}[] #{sign} #{value}" ]
63
+ end
64
+ end
65
+ end
@@ -1,5 +1,5 @@
1
1
  class TF::EnvMatchTest
2
- MATCHER = /^env\[(.*)\]([!]?=)[~]?\/(.*)\//
2
+ MATCHER = /^env\[(.*)\]([!]?=)[~]?\/(.*)\/$/
3
3
 
4
4
  def matches? test
5
5
  test =~ TF::EnvMatchTest::MATCHER
@@ -9,6 +9,9 @@ class TF::EnvMatchTest
9
9
  test =~ TF::EnvMatchTest::MATCHER
10
10
  variable, sign, value = $1.strip, $2, $3
11
11
  var_val = env[ variable ]
12
+ unless var_val.is_a? String
13
+ return [ false, "failed: env #{variable} #{sign} /#{value}/ # not a string" ]
14
+ end
12
15
  if ( sign == "=" ) ^ ( Regexp.new(value) =~ "#{var_val}" )
13
16
  [ false, "failed: env #{variable} #{sign} /#{value}/ # was '#{var_val}'" ]
14
17
  else
@@ -0,0 +1,23 @@
1
+ class TF::EnvTypeTest
2
+ MATCHER = /^env\[(.*)\]\?([!]?=)(array|string|nil)/
3
+
4
+ def matches? test
5
+ test =~ TF::EnvTypeTest::MATCHER
6
+ end
7
+
8
+ def execute test, _stdout, _stderr, _stdboth, _status, env
9
+ test =~ TF::EnvTypeTest::MATCHER
10
+ variable, sign, type = $1.strip, $2, $3
11
+ var_type = variable_type( env[ variable ] )
12
+ if ( sign == "=" ) ^ ( var_type == type )
13
+ [ false, "failed: env? #{variable} #{sign} #{type} # was #{var_type}" ]
14
+ else
15
+ [ true, "passed: env? #{variable} #{sign} #{type}" ]
16
+ end
17
+ end
18
+
19
+ def variable_type value
20
+ value.nil? ? 'nil' :
21
+ value.is_a?(Hash) ? 'array' : 'string'
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  class TF::OutputMatchTest
2
- MATCHER = /^match([!]?=)[~]?\/(.*)\//
2
+ MATCHER = /^match(\[(stdout|stderr)\])?([!]?=)[~]?\/(.*)\/$/
3
3
 
4
4
  def matches? test
5
5
  test =~ TF::OutputMatchTest::MATCHER
@@ -7,11 +7,18 @@ class TF::OutputMatchTest
7
7
 
8
8
  def execute test, _stdout, _stderr, _stdboth, _status, env
9
9
  test =~ TF::OutputMatchTest::MATCHER
10
- sign, value = $1, $2
11
- if ( sign == "=" ) ^ ( Regexp.new(value) =~ "#{_stdboth}" )
12
- [ false, "failed: match #{sign} /#{value}/" ]
10
+ _, type, sign, value = $1, $2, $3, $4
11
+ test_val, match_type =
12
+ case type
13
+ when "stdout" then [ _stdout, "[#{type}]" ]
14
+ when "stderr" then [ _stderr, "[#{type}]" ]
15
+ when nil then [ _stdboth, "" ]
16
+ else return [ false, "failed: match[#{type}] #{sign} /#{value}/ # unknown output type" ]
17
+ end
18
+ if ( sign == "=" ) ^ ( Regexp.new(value) =~ "#{test_val}" )
19
+ [ false, "failed: match#{match_type} #{sign} /#{value}/" ]
13
20
  else
14
- [ true, "passed: match #{sign} /#{value}/" ]
21
+ [ true, "passed: match#{match_type} #{sign} /#{value}/" ]
15
22
  end
16
23
  end
17
24
  end
data/lib/tf.rb CHANGED
@@ -40,9 +40,11 @@ class TF
40
40
  end
41
41
 
42
42
  def env shell
43
- Hash[ shell.execute(
44
- @ruby + ' -e \'ENV.each{|k,v| printf("%s=%s\0","#{k}","#{v}") }\''
45
- )[0].split("\0").map{|var| var.split('=', 2) } ]
43
+ TF::Environment.parse_env(
44
+ shell.execute(
45
+ TF::Environment.show_env_command
46
+ )[0].split(/\n/)
47
+ )
46
48
  end
47
49
 
48
50
  def process_test test
@@ -0,0 +1,91 @@
1
+ ## https://github.com/mpapis/mpapis_test/blob/master/variables_list.sh
2
+
3
+ ## Examples BASH:
4
+ #~ USER=vagrant
5
+ #~ _=
6
+ #~ array2=([0]="four" [1]="five" [2]="six" [10]="ten")
7
+ #~ variable1='play'\''me'
8
+ #~ variable2=$'play\n with\n me\n now'
9
+
10
+ ## Examples ZSH:
11
+ #~ FIGNORE=''
12
+ #~ USER=vagrant
13
+ #~ array2=(four five six '' '' '' '' '' '' ten)
14
+ #~ variable1='play'\''me'
15
+ #~ variable2='play
16
+ #~ with
17
+ #~ me
18
+ #~ now'
19
+ require 'shellwords'
20
+
21
+ class TF::Environment
22
+ HANDLER=<<EOF.gsub(/\n/,'')
23
+ set | awk -F= '
24
+ BEGIN{v=0;}
25
+ /^[a-zA-Z_]+=/{v=1;}
26
+ v==1&&$2~/^['\\''\\$]/{v=2;}
27
+ v==1&&$2~/^\\(/{v=3;}
28
+ v==2&&/'\\''$/&&!/'\\'\\''$/{v=1;}
29
+ v==3&&/\\)$/{v=1;}
30
+ v{print;}
31
+ v==1{v=0;}
32
+ '
33
+ EOF
34
+ class << self
35
+ def show_env_command
36
+ TF::Environment::HANDLER
37
+ end
38
+ def parse_env output
39
+ env = []
40
+ holder=nil
41
+ terminator=nil
42
+ output.each do |line|
43
+ line.chomp!
44
+ if holder.nil? && line =~ /^[^=]+=([\('\$]?)/
45
+ holder = line
46
+ if $1 && !$1.empty?
47
+ terminator = $1.sub(/\$/,"'").sub(/\(/,")")
48
+ end
49
+ elsif holder.nil? && line =~ /^[^=]*=/
50
+ holder = line
51
+ terminator=nil
52
+ else
53
+ holder += line
54
+ end
55
+ if terminator && line.chars.to_a.last == terminator
56
+ terminator=nil
57
+ end
58
+ if holder && terminator.nil?
59
+ env << parse_var( holder.strip )
60
+ holder=nil
61
+ end
62
+ end
63
+ Hash[ env ]
64
+ end
65
+ def parse_var definition
66
+ definition =~ /\A([^=]*)=([$]?[\(']?)(.*?)([\)']?)\z/m
67
+ name = $1
68
+ type1 = $2
69
+ value = $3
70
+ type2 = $4
71
+ case type2
72
+ when ')'
73
+ parse_array( name, value.shellsplit.map{|v|v.gsub(/'\''/,'\'')} )
74
+ else
75
+ [ name, value.gsub(/'\''/,'\'') ]
76
+ end
77
+ end
78
+ def parse_array name, value
79
+ if value[0] && value[0].chars.to_a.first == '['
80
+ value = value.map do |string|
81
+ string =~ /\[([^\]]+)\]=(.*)/m
82
+ [ $1, $2 ]
83
+ end
84
+ else
85
+ value = value.to_enum.with_index.map{|v,i|[(i+1).to_s,v]}.to_a
86
+ # TODO: zsh -c 'typeset -A arr; arr[ala]=1; arr[kot]=2; set | grep -a ^arr=' => arr=(ala 1 kot 2 ) - space on the end
87
+ end
88
+ [ name, Hash[ value ] ]
89
+ end
90
+ end
91
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tf
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michal Papis
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-31 00:00:00 Z
18
+ date: 2012-08-09 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: session
@@ -42,14 +42,17 @@ extra_rdoc_files: []
42
42
 
43
43
  files:
44
44
  - lib/tf.rb
45
+ - lib/tf/environment.rb
45
46
  - lib/tf/active_patches.rb
46
47
  - lib/tf/plugins.rb
47
48
  - lib/plugins/tf/error_summary_output.rb
48
49
  - lib/plugins/tf/text_output.rb
49
50
  - lib/plugins/tf/output_match_test.rb
50
51
  - lib/plugins/tf/env_match_test.rb
52
+ - lib/plugins/tf/env_type_test.rb
51
53
  - lib/plugins/tf/stats_output.rb
52
54
  - lib/plugins/tf/comment_test_input.rb
55
+ - lib/plugins/tf/env_arr_test.rb
53
56
  - lib/plugins/tf/status_test.rb
54
57
  - bin/tf
55
58
  - LICENSE