slacker 1.0.9 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +15 -0
  2. data/Gemfile +4 -4
  3. data/README.markdown +22 -22
  4. data/Rakefile +11 -11
  5. data/bin/slacker +28 -29
  6. data/bin/slacker_new +34 -34
  7. data/lib/slacker.rb +175 -175
  8. data/lib/slacker/application.rb +211 -206
  9. data/lib/slacker/command_line_formatter.rb +59 -59
  10. data/lib/slacker/command_line_formatter2.rb +61 -0
  11. data/lib/slacker/configuration.rb +59 -59
  12. data/lib/slacker/formatter.rb +14 -19
  13. data/lib/slacker/query_result_matcher.rb +178 -178
  14. data/lib/slacker/rspec_ext.rb +49 -49
  15. data/lib/slacker/rspec_monkey.rb +7 -7
  16. data/lib/slacker/sql.rb +39 -39
  17. data/lib/slacker/sql_preprocessor.rb +23 -23
  18. data/lib/slacker/string_helper.rb +16 -16
  19. data/lib/slacker/version.rb +3 -3
  20. data/lib/slacker_new/project/data/sample_1/my_table_expected_power_results.csv +11 -11
  21. data/lib/slacker_new/project/data/sample_1/my_table_initial_data.csv +11 -11
  22. data/lib/slacker_new/project/data/sample_1/numbers_expected_output.csv +3 -3
  23. data/lib/slacker_new/project/database.yml +9 -9
  24. data/lib/slacker_new/project/lib/helpers/my_helper.rb +1 -1
  25. data/lib/slacker_new/project/spec/sample_1.rb +66 -66
  26. data/lib/slacker_new/project/sql/sample_1/my_table_on_power.sql.erb +1 -1
  27. data/lib/slacker_new/project/sql/sample_1/play_with_numbers.sql.erb +16 -16
  28. data/lib/slacker_new/project/sql/sample_1/sysobjects_with_params.sql.erb +1 -1
  29. data/slacker.gemspec +27 -26
  30. data/spec/application_spec.rb +13 -13
  31. data/spec/query_result_matcher_spec.rb +268 -268
  32. data/spec/rspec_ext_spec.rb +87 -87
  33. data/spec/slacker_spec.rb +59 -59
  34. data/spec/spec_helper.rb +9 -9
  35. data/spec/test_files/matcher/test_1.csv +3 -3
  36. data/spec/test_files/test_slacker_project/data/test_1.csv +3 -3
  37. data/spec/test_files/test_slacker_project/sql/nest/nested_1.sql.erb +1 -1
  38. data/spec/test_files/test_slacker_project/sql/no_params.sql.erb +2 -2
  39. data/spec/test_files/test_slacker_project/sql/params.sql.erb +1 -1
  40. metadata +19 -20
@@ -1,207 +1,212 @@
1
- require 'logger'
2
- require 'rspec/core'
3
- require 'slacker/rspec_monkey'
4
- require 'slacker/rspec_ext'
5
- require 'slacker/string_helper'
6
- require 'odbc'
7
-
8
- module Slacker
9
- class Application
10
- attr_reader :target_folder_structure
11
-
12
- SQL_OPTIONS = <<EOF
13
- set textsize 2147483647;
14
- set language us_english;
15
- set dateformat mdy;
16
- set datefirst 7;
17
- set lock_timeout -1;
18
- set quoted_identifier on;
19
- set arithabort on;
20
- set ansi_null_dflt_on on;
21
- set ansi_warnings on;
22
- set ansi_padding on;
23
- set ansi_nulls on;
24
- set concat_null_yields_null on;
25
- EOF
26
-
27
- def initialize(configuration)
28
- @configuration = configuration
29
- @target_folder_structure = ['data', 'debug', 'debug/passed_examples', 'debug/failed_examples', 'sql', 'spec', 'lib', 'lib/helpers']
30
- @error_message = ''
31
- @database = ODBC::Database.new
32
- end
33
-
34
- def print_connection_message
35
- puts "#{@configuration.db_name} (#{@configuration.db_server})" if @configuration.console_enabled
36
- end
37
-
38
- # Customize RSpec and run it
39
- def run
40
- begin
41
- error = catch :error_exit do
42
- print_connection_message
43
- test_folder_structure
44
- cleanup_folders
45
- configure
46
- run_rspec
47
- false #Return false to error
48
- end
49
- ensure
50
- cleanup_after_run
51
- end
52
-
53
- if @configuration.console_enabled
54
- puts @error_message if error
55
- else
56
- raise @error_message if error
57
- end
58
- end
59
-
60
- def run_rspec
61
- RSpec::Core::Runner.disable_autorun!
62
-
63
- RSpec::Core::Runner.run(@configuration.rspec_args,
64
- @configuration.error_stream,
65
- @configuration.output_stream)
66
- end
67
-
68
- # Configure Slacker
69
- def configure
70
- configure_db
71
- configure_rspec
72
- configure_misc
73
- end
74
-
75
- def cleanup_after_run
76
- @database.disconnect if (@database && @database.connected?)
77
- end
78
-
79
- def cleanup_folders
80
- cleanup_folder('debug/passed_examples')
81
- cleanup_folder('debug/failed_examples')
82
- end
83
-
84
- def cleanup_folder(folder)
85
- folder_path = get_path(folder)
86
- Dir.new(folder_path).each{|file_name| File.delete("#{folder_path}/#{file_name}") if File.file?("#{folder_path}/#{file_name}")}
87
- end
88
-
89
- # Get a path relative to the current path
90
- def get_path(path)
91
- @configuration.expand_path(path)
92
- end
93
-
94
- def configure_misc
95
- # Add the lib folder to the load path
96
- $:.push get_path('lib')
97
- # Mixin the helper modules
98
- mixin_helpers
99
- end
100
-
101
- # Mix in the helper modules
102
- def mixin_helpers
103
- helpers_dir = get_path('lib/helpers')
104
- $:.push helpers_dir
105
- Dir.new(helpers_dir).each do |file_name|
106
- if file_name =~ /\.rb$/
107
- require file_name
108
- module_class = Slacker::StringHelper.constantize(Slacker::StringHelper.camelize(file_name.gsub(/\.rb$/,'')))
109
- RSpec.configure do |config|
110
- config.include(module_class)
111
- end
112
- Slacker.mixin_module(module_class)
113
- end
114
- end
115
- end
116
-
117
- # Configure database connection
118
- def configure_db
119
- drv = ODBC::Driver.new
120
- drv.name = 'Driver1'
121
- drv.attrs.tap do |a|
122
- a['Driver'] = '{SQL Server}'
123
- a['Server']= @configuration.db_server
124
- a['Database']= @configuration.db_name
125
- a['Uid'] = @configuration.db_user
126
- a['Pwd'] = @configuration.db_password
127
- a['TDS_Version'] = '7.0' #Used by the linux driver
128
- end
129
-
130
- begin
131
- @database.drvconnect(drv)
132
- rescue ODBC::Error => e
133
- throw_error("#{e.class}: #{e.message}")
134
- end
135
- end
136
-
137
- # Run a script against the currently configured database
138
- def query_script(sql)
139
- results = []
140
- begin
141
- st = @database.run(sql)
142
- begin
143
- if st.ncols > 0
144
- rows = []
145
- st.each_hash(false, true){|row| rows << row}
146
- results << rows
147
- end
148
- end while(st.more_results)
149
- ensure
150
- st.drop unless st.nil?
151
- end
152
- results.count > 1 ? results : results.first
153
- end
154
-
155
- # Customize RSpec
156
- def configure_rspec
157
- before_proc = lambda do |example|
158
- # Initialize the example's SQL
159
- example.metadata[:sql] = ''
160
- Slacker.query_script(example, 'begin transaction;', 'Initiate the example script')
161
- Slacker.query_script(example, SQL_OPTIONS, 'Set default options')
162
- end
163
-
164
- after_proc = lambda do |example|
165
- Slacker.query_script(example, 'rollback transaction;', 'Rollback the changes made by the example script')
166
- end
167
-
168
- # Reset RSpec through a monkey-patched method
169
- RSpec.slacker_reset
170
-
171
- RSpec.configure do |config|
172
- # Global "before" hooks to begin a transaction
173
- config.before(:each) do
174
- before_proc.call(example)
175
- end
176
-
177
- # Global "after" hooks to rollback a transaction
178
- config.after(:each) do
179
- after_proc.call(example)
180
- end
181
-
182
- # Slacker's RSpec extension module
183
- config.include(Slacker::RSpecExt)
184
- config.extend(Slacker::RSpecExt)
185
-
186
- config.output_stream = @configuration.output_stream
187
- config.error_stream = @configuration.error_stream
188
-
189
- config.formatters << @configuration.formatter unless @configuration.formatter.nil?
190
- end
191
- end
192
-
193
- # Tests the current folder's structure
194
- def test_folder_structure()
195
- target_folder_structure.each do |dir|
196
- if !File.directory?(get_path(dir))
197
- throw_error("Cannot find directory \"#{get_path(dir)}\"")
198
- end
199
- end
200
- end
201
-
202
- def throw_error(msg)
203
- @error_message = msg
204
- throw :error_exit, true
205
- end
206
- end
1
+ require 'logger'
2
+ require 'rspec/core'
3
+ require 'slacker/rspec_monkey'
4
+ require 'slacker/rspec_ext'
5
+ require 'slacker/string_helper'
6
+ require 'odbc'
7
+
8
+ module Slacker
9
+ class Application
10
+ attr_reader :target_folder_structure
11
+
12
+ SQL_OPTIONS = <<EOF
13
+ set textsize 2147483647;
14
+ set language us_english;
15
+ set dateformat mdy;
16
+ set datefirst 7;
17
+ set lock_timeout -1;
18
+ set quoted_identifier on;
19
+ set arithabort on;
20
+ set ansi_null_dflt_on on;
21
+ set ansi_warnings on;
22
+ set ansi_padding on;
23
+ set ansi_nulls on;
24
+ set concat_null_yields_null on;
25
+ EOF
26
+
27
+ def initialize(configuration)
28
+ @configuration = configuration
29
+ @target_folder_structure = ['data', 'debug', 'debug/passed_examples', 'debug/failed_examples', 'sql', 'spec', 'lib', 'lib/helpers']
30
+ @error_message = ''
31
+ @database = ODBC::Database.new
32
+ end
33
+
34
+ def print_connection_message
35
+ puts "#{@configuration.db_name} (#{@configuration.db_server})" if @configuration.console_enabled
36
+ end
37
+
38
+ # Customize RSpec and run it
39
+ def run
40
+ begin
41
+ error = catch :error_exit do
42
+ print_connection_message
43
+ test_folder_structure
44
+ cleanup_folders
45
+ configure
46
+ run_rspec
47
+ false #Return false to error
48
+ end
49
+ ensure
50
+ cleanup_after_run
51
+ end
52
+
53
+ if @configuration.console_enabled
54
+ puts @error_message if error
55
+ else
56
+ raise @error_message if error
57
+ end
58
+ end
59
+
60
+ def run_rspec
61
+ RSpec::Core::Runner.disable_autorun!
62
+
63
+ RSpec::Core::Runner.run(@configuration.rspec_args,
64
+ @configuration.error_stream,
65
+ @configuration.output_stream)
66
+ end
67
+
68
+ # Configure Slacker
69
+ def configure
70
+ configure_db
71
+ configure_rspec
72
+ configure_misc
73
+ end
74
+
75
+ def cleanup_after_run
76
+ @database.disconnect if (@database && @database.connected?)
77
+ end
78
+
79
+ def cleanup_folders
80
+ cleanup_folder('debug/passed_examples')
81
+ cleanup_folder('debug/failed_examples')
82
+ end
83
+
84
+ def cleanup_folder(folder)
85
+ folder_path = get_path(folder)
86
+ Dir.new(folder_path).each{|file_name| File.delete("#{folder_path}/#{file_name}") if File.file?("#{folder_path}/#{file_name}")}
87
+ end
88
+
89
+ # Get a path relative to the current path
90
+ def get_path(path)
91
+ @configuration.expand_path(path)
92
+ end
93
+
94
+ def configure_misc
95
+ # Add the lib folder to the load path
96
+ $:.push get_path('lib')
97
+ # Mixin the helper modules
98
+ mixin_helpers
99
+ end
100
+
101
+ # Mix in the helper modules
102
+ def mixin_helpers
103
+ helpers_dir = get_path('lib/helpers')
104
+ $:.push helpers_dir
105
+ Dir.new(helpers_dir).each do |file_name|
106
+ if file_name =~ /\.rb$/
107
+ require file_name
108
+ module_class = Slacker::StringHelper.constantize(Slacker::StringHelper.camelize(file_name.gsub(/\.rb$/,'')))
109
+ RSpec.configure do |config|
110
+ config.include(module_class)
111
+ end
112
+ Slacker.mixin_module(module_class)
113
+ end
114
+ end
115
+ end
116
+
117
+ # Configure database connection
118
+ def configure_db
119
+ drv = ODBC::Driver.new
120
+ drv.name = 'Driver1'
121
+ drv.attrs.tap do |a|
122
+ a['Driver'] = '{SQL Server}'
123
+ a['Server']= @configuration.db_server
124
+ a['Database']= @configuration.db_name
125
+ a['Uid'] = @configuration.db_user
126
+ a['Pwd'] = @configuration.db_password
127
+ a['TDS_Version'] = '7.0' #Used by the linux driver
128
+ end
129
+
130
+ begin
131
+ @database.drvconnect(drv)
132
+ rescue ODBC::Error => e
133
+ throw_error("#{e.class}: #{e.message}")
134
+ end
135
+ end
136
+
137
+ # Run a script against the currently configured database
138
+ def query_script(sql)
139
+ results = []
140
+ begin
141
+ st = @database.run(sql)
142
+ begin
143
+ if st.ncols > 0
144
+ rows = []
145
+ st.each_hash(false, true){|row| rows << row}
146
+ results << rows
147
+ end
148
+ end while(st.more_results)
149
+ ensure
150
+ st.drop unless st.nil?
151
+ end
152
+ results.count > 1 ? results : results.first
153
+ end
154
+
155
+ # Customize RSpec
156
+ def configure_rspec
157
+ before_proc = lambda do |example|
158
+ # Initialize the example's SQL
159
+ example.metadata[:sql] = ''
160
+ Slacker.query_script(example, 'begin transaction;', 'Initiate the example script')
161
+ Slacker.query_script(example, SQL_OPTIONS, 'Set default options')
162
+ end
163
+
164
+ after_proc = lambda do |example|
165
+ Slacker.query_script(example, 'rollback transaction;', 'Rollback the changes made by the example script')
166
+ end
167
+
168
+ # Reset RSpec through a monkey-patched method
169
+ RSpec.slacker_reset
170
+
171
+ RSpec.configure do |config|
172
+
173
+ # Expose the current example to the ExampleGroup extension
174
+ # This is necessary in order to have this work with RSpec 3
175
+ config.expose_current_running_example_as :example
176
+
177
+ # Global "before" hooks to begin a transaction
178
+ config.before(:each) do
179
+ before_proc.call(example)
180
+ end
181
+
182
+ # Global "after" hooks to rollback a transaction
183
+ config.after(:each) do
184
+ after_proc.call(example)
185
+ end
186
+
187
+ # Slacker's RSpec extension module
188
+ config.include(Slacker::RSpecExt)
189
+ config.extend(Slacker::RSpecExt)
190
+
191
+ config.output_stream = @configuration.output_stream
192
+ config.error_stream = @configuration.error_stream
193
+
194
+ config.add_formatter(Slacker::CommandLineFormatter2)
195
+ end
196
+ end
197
+
198
+ # Tests the current folder's structure
199
+ def test_folder_structure()
200
+ target_folder_structure.each do |dir|
201
+ if !File.directory?(get_path(dir))
202
+ throw_error("Cannot find directory \"#{get_path(dir)}\"")
203
+ end
204
+ end
205
+ end
206
+
207
+ def throw_error(msg)
208
+ @error_message = msg
209
+ throw :error_exit, true
210
+ end
211
+ end
207
212
  end
@@ -1,59 +1,59 @@
1
- require 'slacker/formatter'
2
- require 'rspec/core/formatters/progress_formatter'
3
-
4
- module Slacker
5
- class CommandLineFormatter < RSpec::Core::Formatters::ProgressFormatter
6
- include Slacker::Formatter
7
-
8
- def initialize(output)
9
- super(output)
10
- @failed_examples_count = 0
11
- @passed_examples_count = 0
12
- end
13
-
14
- def example_passed(example)
15
- process_example_debug_output(example, false)
16
- super(example)
17
- end
18
-
19
- def example_failed(example)
20
- process_example_debug_output(example, true)
21
- super(example)
22
- end
23
-
24
- private
25
-
26
- def process_example_debug_output(example, example_failed)
27
- if example_failed
28
- @failed_examples_count += 1
29
- debug_output(example, Slacker.configuration.expand_path('debug/failed_examples'), @failed_examples_count, example_failed)
30
- else
31
- @passed_examples_count += 1
32
- debug_output(example, Slacker.configuration.expand_path('debug/passed_examples'), @passed_examples_count, example_failed)
33
- end
34
- end
35
-
36
- def debug_output(example, out_folder, file_number, example_failed)
37
- # Write out the SQL
38
- File.open("#{out_folder}/example_#{'%03d' % file_number}.sql", 'w') do |out_file|
39
- out_file.write(get_formatted_example_sql(example, example_failed))
40
- end
41
- end
42
-
43
- def get_formatted_example_sql(example, example_failed)
44
- sql = <<EOF
45
- -- Example "#{example.metadata[:full_description]}"
46
- -- #{example.metadata[:location]}
47
- -- Executed at #{example.metadata[:execution_result][:started_at]}
48
-
49
- #{example.metadata[:sql]}
50
-
51
- -- SLACKER RESULTS
52
- -- *******************************************
53
- #{example_failed ? example_failure_text(example).split("\n").collect{|line| '-- ' + line}.join("\n") : '-- Example Passed OK'}
54
- -- *******************************************
55
- EOF
56
- sql.strip
57
- end
58
- end
59
- end
1
+ require 'slacker/formatter'
2
+ require 'rspec/core/formatters/progress_formatter'
3
+
4
+ module Slacker
5
+ class CommandLineFormatter < RSpec::Core::Formatters::ProgressFormatter
6
+ include Slacker::Formatter
7
+
8
+ def initialize(output)
9
+ super(output)
10
+ @failed_examples_count = 0
11
+ @passed_examples_count = 0
12
+ end
13
+
14
+ def example_passed(example)
15
+ process_example_debug_output(example, false)
16
+ super(example)
17
+ end
18
+
19
+ def example_failed(example)
20
+ process_example_debug_output(example, true)
21
+ super(example)
22
+ end
23
+
24
+ private
25
+
26
+ def process_example_debug_output(example, example_failed)
27
+ if example_failed
28
+ @failed_examples_count += 1
29
+ debug_output(example, Slacker.configuration.expand_path('debug/failed_examples'), @failed_examples_count, example_failed)
30
+ else
31
+ @passed_examples_count += 1
32
+ debug_output(example, Slacker.configuration.expand_path('debug/passed_examples'), @passed_examples_count, example_failed)
33
+ end
34
+ end
35
+
36
+ def debug_output(example, out_folder, file_number, example_failed)
37
+ # Write out the SQL
38
+ File.open("#{out_folder}/example_#{'%03d' % file_number}.sql", 'w') do |out_file|
39
+ out_file.write(get_formatted_example_sql(example, example_failed))
40
+ end
41
+ end
42
+
43
+ def get_formatted_example_sql(example, example_failed)
44
+ sql = <<EOF
45
+ -- Example "#{example.metadata[:full_description]}"
46
+ -- #{example.metadata[:location]}
47
+ -- Executed at #{example.metadata[:execution_result][:started_at]}
48
+
49
+ #{example.metadata[:sql]}
50
+
51
+ -- SLACKER RESULTS
52
+ -- *******************************************
53
+ #{example_failed ? example_failure_text(example).split("\n").collect{|line| '-- ' + line}.join("\n") : '-- Example Passed OK'}
54
+ -- *******************************************
55
+ EOF
56
+ sql.strip
57
+ end
58
+ end
59
+ end