Module: Linecook::Test::ShellTest
- Included in:
- Linecook::Test
- Defined in:
- lib/linecook/test/shell_test.rb
Instance Method Summary collapse
- #_assert_alike(a, b, msg = nil) ⇒ Object
- #_assert_output_equal(a, b, msg = nil) ⇒ Object
- #_assert_script(script, options = {}) ⇒ Object
- #_assert_script_match(script, options = {}) ⇒ Object
-
#assert_alike(a, b, msg = nil) ⇒ Object
Asserts whether or not b is like a (which should be a Regexp), and provides a more readable output in the case of a failure as compared with assert_match.
-
#assert_output_equal(a, b, msg = nil) ⇒ Object
Asserts whether or not the a and b strings are equal, with a more readable output than assert_equal for large strings (especially large strings with significant whitespace).
- #assert_script(script, options = {}) ⇒ Object
- #assert_script_match(script, options = {}) ⇒ Object
-
#outdent(str) ⇒ Object
helper for stripping indentation off a string.
-
#set_env(env = {}, replace = false) ⇒ Object
Sets the specified ENV variables and returns the current env.
- #setup ⇒ Object
- #sh(cmd) ⇒ Object
-
#verbose? ⇒ Boolean
Returns true if the ENV variable ‘VERBOSE’ is true.
-
#whitespace_escape(str) ⇒ Object
helper for formatting escaping whitespace into readable text.
-
#with_env(env = {}, replace = false) ⇒ Object
Sets the specified ENV variables for the duration of the block.
Instance Method Details
#_assert_alike(a, b, msg = nil) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/linecook/test/shell_test.rb', line 139 def _assert_alike(a, b, msg=nil) if a.kind_of?(String) a = RegexpEscape.new(a) end if b =~ a assert true else flunk %Q{ #{msg} ================= expected output like ================== #{whitespace_escape(a)} ======================== but was ======================== #{whitespace_escape(b)} ========================================================= } end end |
#_assert_output_equal(a, b, msg = nil) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/linecook/test/shell_test.rb', line 114 def _assert_output_equal(a, b, msg=nil) if a == b assert true else flunk %Q{ #{msg} ==================== expected output ==================== #{whitespace_escape(a)} ======================== but was ======================== #{whitespace_escape(b)} ========================================================= } end end |
#_assert_script(script, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/linecook/test/shell_test.rb', line 83 def _assert_script(script, ={}) commands = CommandParser.new().parse(script) commands.each do |cmd, output, status| result = sh(cmd) _assert_output_equal(output, result, cmd) if output assert_equal(status, $?.exitstatus, cmd) if status end end |
#_assert_script_match(script, options = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/linecook/test/shell_test.rb', line 97 def _assert_script_match(script, ={}) commands = CommandParser.new().parse(script) commands.each do |cmd, output, status| result = sh(cmd) _assert_alike(output, result, cmd) if output assert_equal(status, $?.exitstatus, cmd) if status end end |
#assert_alike(a, b, msg = nil) ⇒ Object
Asserts whether or not b is like a (which should be a Regexp), and provides a more readable output in the case of a failure as compared with assert_match.
If a is a string it is turned into a RegexpEscape.
134 135 136 137 |
# File 'lib/linecook/test/shell_test.rb', line 134 def assert_alike(a, b, msg=nil) a = outdent(a) if a.kind_of?(String) _assert_alike a, b, msg end |
#assert_output_equal(a, b, msg = nil) ⇒ Object
Asserts whether or not the a and b strings are equal, with a more readable output than assert_equal for large strings (especially large strings with significant whitespace).
110 111 112 |
# File 'lib/linecook/test/shell_test.rb', line 110 def assert_output_equal(a, b, msg=nil) _assert_output_equal outdent(a), b, msg end |
#assert_script(script, options = {}) ⇒ Object
79 80 81 |
# File 'lib/linecook/test/shell_test.rb', line 79 def assert_script(script, ={}) _assert_script outdent(script), end |
#assert_script_match(script, options = {}) ⇒ Object
93 94 95 |
# File 'lib/linecook/test/shell_test.rb', line 93 def assert_script_match(script, ={}) _assert_script_match outdent(script), end |
#outdent(str) ⇒ Object
helper for stripping indentation off a string
159 160 161 |
# File 'lib/linecook/test/shell_test.rb', line 159 def outdent(str) str =~ /\A(?:\s*?\n)( *)(.*)\z/m ? $2.gsub!(/^ {0,#{$1.length}}/, '') : str end |
#set_env(env = {}, replace = false) ⇒ Object
Sets the specified ENV variables and returns the current env. If replace is true, current ENV variables are replaced; otherwise the new env variables are simply added to the existing set.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/linecook/test/shell_test.rb', line 24 def set_env(env={}, replace=false) current_env = {} ENV.each_pair do |key, value| current_env[key] = value end ENV.clear if replace env.each_pair do |key, value| if value.nil? ENV.delete(key) else ENV[key] = value end end if env current_env end |
#setup ⇒ Object
9 10 11 12 |
# File 'lib/linecook/test/shell_test.rb', line 9 def setup super @notify_method_name = true end |
#sh(cmd) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/linecook/test/shell_test.rb', line 60 def sh(cmd) if @notify_method_name && verbose? @notify_method_name = false puts puts method_name end start = Time.now result = `#{cmd}` finish = Time.now if verbose? elapsed = "%.3f" % [finish-start] puts " (#{elapsed}s) #{cmd}" end result end |
#verbose? ⇒ Boolean
Returns true if the ENV variable ‘VERBOSE’ is true. When verbose, ShellTest prints the expanded commands of sh_test to $stdout.
16 17 18 19 |
# File 'lib/linecook/test/shell_test.rb', line 16 def verbose? verbose = ENV['VERBOSE'] verbose && verbose =~ /^true$/i ? true : false end |
#whitespace_escape(str) ⇒ Object
helper for formatting escaping whitespace into readable text
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/linecook/test/shell_test.rb', line 164 def whitespace_escape(str) str.to_s.gsub(/\s/) do |match| case match when "\n" then "\\n\n" when "\t" then "\\t" when "\r" then "\\r" when "\f" then "\\f" else match end end end |
#with_env(env = {}, replace = false) ⇒ Object
Sets the specified ENV variables for the duration of the block. If replace is true, current ENV variables are replaced; otherwise the new env variables are simply added to the existing set.
Returns the block return.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/linecook/test/shell_test.rb', line 48 def with_env(env={}, replace=false) current_env = nil begin current_env = set_env(env, replace) yield ensure if current_env set_env(current_env, true) end end end |