forked from nspec/NSpec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher_dot_net.rb
802 lines (635 loc) · 20.6 KB
/
watcher_dot_net.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
class GrowlNotifier
def self.growl_path
@@growl_path
end
def self.growl_path= value
@@growl_path = value
end
def execute title, text, color
return unless GrowlNotifier.growl_path
text.gsub!('"', "'")
text = text + "\n\n---"
opts = ["\"#{GrowlNotifier.growl_path}\"", "\"#{text}\"", "/t:\"#{title}\""]
opts << "/i:\"#{File.expand_path("#{color}.png")}\""
`#{opts.join ' '}`
end
end
class MSBuilder
attr_accessor :failed
def initialize folder
@folder = folder
@sh = CommandShell.new
@failed = false
@@ms_build_path = "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe"
end
def execute
output = @sh.execute "#{build_cmd sln_file}"
@failed = output.match(/error/)
output
end
def sln_file
Find.find(@folder) { |f| return f if /\.sln$/.match(f) != nil && /\/.*\//.match(f) == nil }
end
def build_cmd file
"\"#{MSBuilder.ms_build_path}\" \"#{file}\" /verbosity:quiet /nologo"
end
def self.ms_build_path
@@ms_build_path
end
def self.ms_build_path= value
@@ms_build_path = value
end
end
class RakeBuilder
attr_accessor :failed
def initialize folder
@sh = CommandShell.new
@failed = false
@folder = folder
@@rake_command = "rake"
end
def self.rake_command
@@rake_command
end
def self.rake_command= command
@@rake_command = command
end
def execute
output = @sh.execute @@rake_command
@failed = output.match(/rake aborted/)
filtered_output = ""
output.split("\n").each do |line|
if(false ==
[/msbuild.exe/,
/rake aborted/,
/See full trace/,
/^\(in/,
/Command failed with/].any? { |pattern| line.match(pattern) })
filtered_output << line + "\n"
end
end
filtered_output
end
end
class TestRunner
attr_accessor :first_failed_test, :executed_context
def initialize folder
@folder = folder
@test_dlls_override = nil
@executed_context = Hash.new
end
def test_dlls= override
@test_dlls_override = override
end
def test_dlls
return @test_dlls_override if(@test_dlls_override)
dlls = Array.new
Find.find(@folder) do |f|
if(true == [/test.dll$/, /tests.dll$/, /spec.dll$/, /specs.dll$/].any? { |pattern| f.downcase.match(pattern) && f.downcase.match(/bin\/debug/) })
dlls << f if(!f.downcase.match(/\/nspec.dll/))
end
end
dlls
end
def get_test_dll_for(file_name)
rootDirectory = /\.\/.*\//.match(file_name)
test_dll = nil
test_dlls.each { |dll| test_dll = dll if /#{rootDirectory}/.match dll }
test_dll
end
def find file
return nil if [/\.sln$/, /\.csproj$/].any? { |pattern| file.match(pattern) }
return nil if !file.match(/\./)
just_file_name = File.basename(file, ".cs")
if(file.match(/describe_.*\//))
return file.match(/describe_.*\//).to_s.gsub "/", ""
elsif(just_file_name.match(/^describe_/))
return just_file_name
else
return "describe_" + just_file_name
end
end
def usage
"no usage defined"
end
end
class NSpecRunner < TestRunner
attr_accessor :failed
def initialize folder
super folder
@sh = CommandShell.new
@test_statuses = Hash.new
@@nspec_path_override = nil
end
def test_dlls
Dir['**/*NSpec.dll'].select {|f| !f.downcase.include?('packages')}.map {|f| f.gsub(/([^\/]*)\/bin\/Debug\/.*/, '\1/bin/Debug/\1.dll')}
end
def find file
return nil if super(file) == nil
just_file_name = File.basename(file, ".cs")
if(contained_in_test_project(file))
return file.gsub("./", "")
.gsub(root_folder(file), "")
.gsub(/^\//, "")
.gsub(".cs", "")
.gsub("/", "\\.")
else
return "describe_" + just_file_name
end
end
def root_folder file
return file.gsub("./", "").gsub(/\/.*/, "")
end
def contained_in_test_project file
test_dlls.each do |dll|
return root_folder(dll) == root_folder(file)
end
false
end
def self.nspec_path
return @@nspec_path_override if @@nspec_path_override != nil
exes = Array.new
Find.find(".") do |f|
exes << f if(/tools\/nspecrunner.exe$/.match(f.downcase))
end
exes.sort do |a, b|
version_a = a.gsub("./packages/nspec.", "").gsub("/tools/nspecrunner.exe", "")
version_b = b.gsub("./packages/nspec.", "").gsub("/tools/nspecrunner.exe", "")
tokens_a = version_a.split(/\./)
tokens_b = version_b.split(/\./)
if((tokens_a[0].to_i <=> tokens_b[0].to_i) != 0)
tokens_a[0].to_i <=> tokens_b[0].to_i
elsif((tokens_a[1].to_i <=> tokens_b[1].to_i) != 0)
tokens_a[1].to_i <=> tokens_b[1].to_i
else
tokens_a[2].to_i <=> tokens_b[2].to_i
end
end.last
end
def self.nspec_path= value
@@nspec_path_override = value
end
def execute test_name
@test_results = ""
@failed = false
@test_statuses.clear
@first_failed_test = nil
test_dlls.each do |dll|
output = @sh.execute(test_cmd(dll, test_name))
test_result = Hash.new
in_failure = false
first_new_line = false
output.each_line do |line|
if(in_failure == true)
@first_failed_test += line
if(line.strip == "" && first_new_line)
first_new_line = false
elsif(line.strip == "")
in_failure = false
end
end
if(/\*\*\*\* FAILURES \*\*\*\*/.match(line))
test_result[:failed] = true
if(@first_failed_test == nil)
@first_failed_test = "Failed Tests:"
in_failure = true
first_new_line = true
end
end
end
@test_statuses[dll] = test_result
@test_results += output
end
@test_statuses.each_value do |value|
@failed = @failed || value[:failed]
end
@test_results
end
def test_results
@test_results
end
def test_cmd dll, name
return "\"#{NSpecRunner.nspec_path}\" \"#{dll}\" \"#{name}\""
end
def inconclusive
false
end
def usage
puts "Discovered and using: #{@dll}"
end
end
class NUnitRunner < TestRunner
def initialize folder
super folder
@sh = CommandShell.new
@failed_tests = Array.new
@failed_tests = Array.new
@status_by_dll = Hash.new
@@nunit_path = "C:\\program files (x86)\\nunit 2.5.9\\bin\\net-2.0\\nunit-console-x86.exe"
end
def usage
<<-OUTPUT
NUnitRunner runner will use the following exe to run your tests:
#{NUnitRunner.nunit_path}
NUnitRunner for SpecWatchr uses category attributes for running unit tests. Let's say you have a class called Person (located in file Person.cs). You'll need to create a test class called describe_Person.cs (all tests associated with Person.cs should go under describe_Person.cs). Once the test class is created all tests defined should be decorated with the Category attribute. For example:
//here is the person class (located in Person.cs)
public class Person
{
public string FirstName { get; set; }
}
//here is the test class (located in describe_Person.cs)...notice the category attribute
namespace YourUnitTests
{
[TestFixture]
[Category("Person")]
public class when_initializing_person
{
[Test]
public void should_set_first_name_to_empty_string()
{
Person person = new Person();
Assert.AreEqual(string.Empty, person.FirstName);
}
}
[TestFixture]
[Category("Person")]
public class some_other_test_associated_with_person
{
[Test]
public void should_run_test_if_Person_class_changes()
{
Assert.Fail();
}
}
}
Whenever you save Person.cs, all tests with the category "Person" will get executed.
OUTPUT
end
def self.nunit_path
@@nunit_path
end
def self.nunit_path= value
@@nunit_path = value
end
def execute test_name
dll_test_results = Hash.new
@inconclusive = true
@failed = false
@test_results = ""
@tests = Hash.new
@first_failed_test = nil
test_dlls.each do |test_dll|
console_output = @sh.execute(test_cmd(test_dll, test_name))
puts console_output
test_result = Hash.new
test_result[:inconclusive] = false
test_result[:failed] = false
dll_test_results[test_dll] = test_result
console_output.each_line do |line|
if(/Tests run: 0/.match(line))
test_result[:inconclusive] = true
end
if(/Errors and Failures:/.match(line))
test_result[:failed] = true
end
end
in_failures = false
last_test = nil
failure_patterns =
[
/Test Failure : /,
/Test Error : /,
/TearDown Error : /,
/SetUp Error : /
]
console_output.each_line do |line|
if(/^\*\*\*\*\*/.match(line))
test = Hash.new
@tests[line.gsub("***** ", "").strip] = test
tokens = line.gsub("***** ", "").strip.split(".")
test[:name] = tokens[-1].gsub("_", " ")
test[:spec] = tokens[-2].gsub("_", " ")
test[:failed] = false
test[:error] = ""
test[:dll] = test_dll
elsif(failure_patterns.any? { |pattern| pattern.match(line) })
selected_pattern = failure_patterns.select { |pattern| pattern.match(line) != nil }.first
full = line.gsub!(selected_pattern, "").strip
full = full[3..-1].strip #the strip call needs to be put under test
@tests[full][:failed] = true
last_test = @tests[full]
in_failures = true
elsif(in_failures && line.strip != "")
last_test[:error] += " " + line.strip + "\n"
end
end
end
@test_results = ""
dll_test_results.each_value do |value|
@inconclusive = @inconclusive && value[:inconclusive]
@failed = @failed || value[:failed]
end
tests_to_display = Hash.new
test_dlls.each do |test_dll|
test_output = ""
failed = dll_test_results[test_dll][:failed]
inconclusive = dll_test_results[test_dll][:inconclusive]
if(failed)
test_output = "Failed Tests:\n"
tests_to_display = @tests.select { |k, v| v[:dll] == test_dll && v[:failed] == true }
elsif(!failed && !inconclusive)
if(!@failed)
test_output = "All Passed:\n"
tests_to_display = @tests.select { |k, v| v[:dll] == test_dll && v[:failed] == false }
else
test_output = "" #need a test for this line
tests_to_display = Hash.new #need a test for this line
end
else
if(@inconclusive)
test_output = "Test Inconclusive:\nNo tests found under #{ test_name }\n\n"
else
test_output = "" #need a test for this line
end
tests_to_display = Hash.new
end
current_spec = ""
tests_to_display.each do |k, v|
if(current_spec != v[:spec])
test_output += v[:spec] + "\n"
current_spec = v[:spec]
end
test_output += " " + v[:name] + "\n"
test_output += v[:error] if(v[:error])
test_output += "\n"
if(@failed && @first_failed_test == nil && v[:failed])
@first_failed_test = test_output
end
end
@test_results += test_output
end
if(!@inconclusive && !@failed)
@test_results += "#{@tests.count} tests ran and passed\n"
end
puts @test_results
end
def test_results
@test_results
end
def tests
@tests
end
def test_cmd test_dll, test_name
"\"#{@@nunit_path}\" \"#{test_dll}\" /nologo /labels /include=#{ test_name.gsub(/describe_/, "") }"
end
def inconclusive
@inconclusive
end
def failed
@failed
end
end
class MSTestRunner < TestRunner
attr_accessor :failed, :inconclusive, :test_results
def initialize folder
super folder
@sh = CommandShell.new
@failed_tests = Array.new
@status_by_dll = Hash.new
@@ms_test_path = "C:\\program files (x86)\\microsoft visual studio 10.0\\common7\\ide\\mstest.exe"
end
def self.ms_test_path
@@ms_test_path
end
def self.ms_test_path= value
@@ms_test_path = value
end
def usage
<<-OUTPUT
MSTestRunner will use the following exe to run your tests:
#{MSTestRunner.ms_test_path}
MSTestRunner for SpecWatchr uses a convension based approach for running unit tests. Let's say you have a class called Person (located in file Person.cs). You'll need to create a test class called describe_Person.cs (all tests associated with Person.cs should go under describe_Person.cs). Once the test class is created, change the namespace of the class to include PersonSpec. For example:
//here is the person class (located in Person.cs)
public class Person
{
public string FirstName { get; set; }
}
//here is the test class (located in describe_Person.cs)...notice the namespace
namespace YourUnitTests.describe_Person
{
[TestClass]
public class when_initializing_person
{
[TestMethod]
public void should_set_first_name_to_empty_string()
{
Person person = new Person();
Assert.AreEqual(string.Empty, person.FirstName);
}
}
[TestClass]
public class some_other_test_associated_with_person
{
[TestMethod]
public void should_run_test_if_Person_class_changes()
{
Assert.Fail();
}
}
}
Whenever you save Person.cs, all tests under the namespace describe_Person will get executed.
OUTPUT
end
def failed_tests
@failed_tests
end
def status_by_dll
@status_by_dll
end
def passed_tests
@passed_tests
end
def test_config
Find.find(@folder) { |f| return f.gsub("./", "") if /Local.testsettings$/.match(f) != nil || /LocalTestRun.testrunconfig$/.match(f) != nil }
end
def set_test_status test_dll, test_output
results = Hash.new
results[:failed] = false
results[:inconclusive] = false
test_output.each_line do |output|
results[:failed] = true if [/^Failed/, /errormessage/].any? { |pattern| output.match(pattern) }
results[:inconclusive] = true if output.match(/No tests to execute/)
end
@status_by_dll[test_dll] = results
end
def itemize_test_results test_dll, test_output, test_name
last_test_item = nil
in_error = false
test_output.split("\n").each do |line|
if(line.strip.match(/^Failed/))
in_error = false
tokens = line.split(".")
if(tokens.length > 1)
test_name = tokens[-1].strip
test_spec = tokens[-2].strip
last_test_item = { :spec => test_spec.gsub("_", " "), :name => test_name.gsub("_", " "), :dll => test_dll }
@failed_tests << last_test_item
end
elsif(line.strip.match(/errormessage/))
in_error = true
tokens = line.split(".")
if(last_test_item)
last_test_item[:errormessage] = line.gsub("[errormessage] ", "")
end
elsif(line.strip.match(/^Passed/))
in_error = false
tokens = line.split(".")
if(tokens.length > 1)
test_name = tokens[-1].strip
test_spec = tokens[-2].strip
last_test_item = { :spec => test_spec.gsub("_", " "), :name => test_name.gsub("_", " "), :dll => test_dll }
@passed_tests << last_test_item
end
elsif(in_error)
last_test_item[:errormessage] += "\n " + line
end
end
@failed_tests.sort! { |a, b| a[:spec] <=> b[:spec] }
@passed_tests.sort! { |a, b| a[:spec] <=> b[:spec] }
end
def set_test_result_output test_dll, test_name
tests = Array.new
failed = @status_by_dll[test_dll][:failed]
inconclusive = @status_by_dll[test_dll][:inconclusive]
if(failed)
test_output = "Failed Tests:\n"
tests = @failed_tests.select { |kvp| kvp[:dll] == test_dll }
elsif(!failed && !inconclusive)
if(!@failed)
test_output = "All Passed:\n"
tests = @passed_tests.select { |kvp| kvp[:dll] == test_dll }
else
test_output = ""
end
else
test_output = "Test Inconclusive:\nNo tests found under #{ test_name }\n\n"
end
current_spec = ""
tests.each do |line|
if(current_spec != line[:spec])
test_output += line[:spec] + "\n"
current_spec = line[:spec]
end
test_output += " " + line[:name] + "\n"
test_output += " " + line[:errormessage] + "\n" if(line[:errormessage])
test_output += "\n"
if(failed && @first_failed_test == nil && line[:errormessage])
@first_failed_test = test_output
end
end
@test_results += test_output
puts @test_results
end
def execute test_name
@test_results = ""
@failed_tests = Array.new
@passed_tests = Array.new
@status_by_dll.clear
@first_failed_test = nil
test_dlls.each do |test_dll|
test_output = @sh.execute "#{test_cmd(test_dll, test_name)}"
set_test_status test_dll, test_output
itemize_test_results test_dll, test_output, test_name
end
@inconclusive = true
@failed = false
@status_by_dll.each_value do |value|
@inconclusive = @inconclusive && value[:inconclusive]
@failed = @failed || value[:failed]
end
test_dlls.each do |test_dll|
set_test_result_output test_dll, test_name
end
if(!@inconclusive && !@failed)
@test_results += "#{@passed_tests.count} tests ran and passed\n"
end
@test_results
end
def test_cmd test_dll, test_name
if test_name
return "\"#{MSTestRunner.ms_test_path}\" /testcontainer:#{test_dll} /runconfig:#{test_config} /nologo /test:#{test_name} /detail:errormessage"
else
return "\"#{MSTestRunner.ms_test_path}\" /testcontainer:#{test_dll} /runconfig:#{test_config} /nologo /detail:errormessage"
end
end
end
class CommandShell
def execute cmd
puts cmd + "\n\n"
str=""
STDOUT.sync = true # That's all it takes...
IO.popen(cmd+" 2>&1") do |pipe| # Redirection is performed using operators
pipe.sync = true
while s = pipe.gets
str+=s # This is synchronous!
end
end
puts str + "\n\n"
str
end
end
class WatcherDotNet
attr_accessor :notifier, :test_runner, :builder, :sh, :first_run
require 'find'
EXCLUDES = [/\.dll$/, /debug/i, /TestResult.xml/, /testresults/i, /\.rb$/, /\.suo$/]
def initialize folder, config
@folder = folder
@sh = CommandShell.new
@notifier = GrowlNotifier.new
@builder = Kernel.const_get(config[:builder].to_s).new folder
@test_runner = Kernel.const_get(config[:test_runner].to_s).new folder
@first_run = true
end
def require_build file
false == EXCLUDES.any? { |pattern| file.match(pattern) }
end
def consider file
if(@first_run)
@notifier.execute "specwatchr", "builder: #{@builder.class}\ntest runner: #{@test_runner.class}\nconfig file: dotnet.watchr.rb", "green"
@first_run = false
end
puts "====================== changed: #{file} ===================="
puts "====================== excluded ============================" if false == require_build(file)
if false == require_build(file)
puts "===================== done consider ========================"
return
end
build_output = @builder.execute
puts build_output
@notifier.execute "build failed", build_output, 'red' if @builder.failed
if @builder.failed
puts "===================== done consider ========================"
return
end
if @test_runner.test_dlls.count == 0
@notifier.execute "discovery", "specwatchr didn't find any test dll's. specwatchr looks for a .csproj that ends in Test, Tests, Spec, or Specs. If you do have that, stop specwatchr, rebuild your solution and start specwatchr back up. If you want to explicitly specify the test dll's, you can do so via dotnet.watchr.rb.", "red"
puts "===================== done consider ========================"
return
end
test_results = ""
spec = @test_runner.find file
if(!spec)
puts "===================== done consider ========================"
return
end
puts "=========== running spec: #{spec} ====="
test_output = @test_runner.execute spec
if @test_runner.inconclusive
@notifier.execute "no spec found", "create spec #{spec}", 'red'
puts @test_runner.usage
end
@notifier.execute "tests failed", @test_runner.first_failed_test, 'red' if @test_runner.failed
@notifier.execute @test_runner.test_results.split("\n").last, '', 'green' if !@test_runner.failed and !@test_runner.inconclusive
puts "===================== done consider ========================"
end
end