rakedotnet 1.1.51

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ def with(value)
2
+ yield(value)
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'registry_accessor'
2
+
3
+ module BradyW
4
+ module WindowsPaths
5
+ private
6
+ # Fetches the path for tools like bcp.exe and sqlcmd.exe from the registry
7
+ def sql_tool version
8
+ regvalue "SOFTWARE\\Microsoft\\Microsoft SQL Server\\#{version}\\Tools\\ClientSetup", 'Path'
9
+ end
10
+
11
+ # Fetches the path for Visual Studio tools like MSTest.exe from the registry
12
+ def visual_studio version
13
+ regvalue "SOFTWARE\\Microsoft\\VisualStudio\\#{version}", 'InstallDir'
14
+ end
15
+
16
+ # Fetches the .NET Framework path from the registry
17
+ def dotnet subpath
18
+ regvalue "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\#{subpath}", "InstallPath"
19
+ end
20
+
21
+ def regvalue(key, value)
22
+ keyAndVal = "#{key}\\#{value}"
23
+ log "Retrieving registry key #{keyAndVal}"
24
+ regacc = BradyW::RegistryAccessor.new
25
+ regacc.regvalue(key,value)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ $: << File.expand_path(File.dirname(__FILE__) +"/../lib")
2
+ require "rspec"
3
+ # Needed to mock out our config/props
4
+ require 'config'
5
+ require "singleton"
6
+
7
+ include FileUtils
8
+
9
+ RSpec.configure do |config|
10
+
11
+ # File dependent tests, so we always baseline ourselves in the test directory
12
+ config.before(:all) do
13
+ @current = pwd
14
+ cd File.expand_path(File.dirname(__FILE__))
15
+ end
16
+
17
+ config.after(:all) do
18
+ cd @current
19
+ end
20
+
21
+ config.before(:each) do
22
+ @config = BradyW::BaseConfig.new
23
+ class MockConfig
24
+ include Singleton
25
+ attr_accessor :values
26
+ end
27
+
28
+ # Force only our base class to be returned
29
+ BradyW::Config.stub!(:instance).and_return(MockConfig.instance)
30
+ MockConfig.instance.values = @config
31
+ end
32
+ end
@@ -0,0 +1,66 @@
1
+ require "base"
2
+ require "basetask"
3
+ require "basetaskmocking"
4
+
5
+ module BradyW
6
+ class BaseTask < Rake::TaskLib
7
+ attr_accessor :dependencies
8
+ end
9
+ end
10
+
11
+ describe BradyW::BaseTask do
12
+ before(:each) do
13
+ Rake::Task.clear
14
+ end
15
+
16
+ it "Task with no dependencies/default name" do
17
+ task = BradyW::BaseTask.new
18
+ task.should_receive(:log).with("Running task: task")
19
+ task.name.should == :task
20
+ task.dependencies.should == nil
21
+ task.unless.should == nil
22
+ task.should_receive(:exectask)
23
+ Rake::Task[:task].invoke
24
+ end
25
+
26
+ it "Task with no dependencies/custom name" do
27
+ task = BradyW::BaseTask.new "mytask"
28
+ task.should_receive(:log).with("Running task: mytask")
29
+ task.name.should == "mytask"
30
+ task.dependencies.should == nil
31
+ task.unless.should == nil
32
+ task.should_receive(:exectask)
33
+ Rake::Task[:mytask].invoke
34
+ end
35
+
36
+ it "Task with dependencies/custom name" do
37
+ task = BradyW::BaseTask.new "mytask" => :dependenttask
38
+ task.name.should == "mytask"
39
+ task.dependencies.should == :dependenttask
40
+ task.unless.should == nil
41
+
42
+ dtask = BradyW::BaseTask.new "dependenttask"
43
+ task.should_receive(:exectask)
44
+ dtask.should_receive(:exectask)
45
+
46
+ task.should_receive(:log).with("Running task: mytask")
47
+ dtask.should_receive(:log).with("Running task: dependenttask")
48
+ Rake::Task[:mytask].invoke
49
+ end
50
+
51
+ it "Task with dependencies/custom name + block" do
52
+ task = BradyW::BaseTask.new "mytask" => [:dependenttask, :test] do |t|
53
+ t.unless = "yes"
54
+ end
55
+ task.name.should == "mytask"
56
+ task.dependencies.should == [:dependenttask, :test]
57
+ task.unless.should == "yes"
58
+
59
+ dtask = BradyW::BaseTask.new "dependenttask"
60
+ task.should_not_receive(:exectask)
61
+ dtask.should_not_receive(:exectask)
62
+
63
+ task.should_receive(:log).with("Skipping task: mytask due to unless condition specified in rakefile")
64
+ Rake::Task[:mytask].invoke
65
+ end
66
+ end
@@ -0,0 +1,44 @@
1
+ require 'base'
2
+ require 'rake'
3
+ require 'rake/tasklib'
4
+
5
+ module BradyW
6
+ class BaseTask < Rake::TaskLib
7
+ def exectaskpublic
8
+ exectask
9
+ end
10
+
11
+ def shell(*cmd, &block)
12
+ command = cmd.first
13
+ puts command
14
+ # We aren't testing concurrent tasks here, so no thread safety worries
15
+ if !@sh
16
+ @sh = []
17
+ end
18
+
19
+ @sh << command
20
+
21
+ # Make it look like it went OK
22
+ yield true, nil if block_given?
23
+ end
24
+
25
+ def excecutedPop
26
+ return nil unless @sh
27
+ @sh.pop()
28
+ end
29
+ end
30
+ end
31
+
32
+ class SimulateProcessFailure
33
+ def exitstatus
34
+ return "BW Rake Task Problem"
35
+ end
36
+ end
37
+
38
+ def rm_safe directory
39
+ # Before we delete the files, copy them to a place where we can verify their correctness
40
+ if File.exist? directory
41
+ FileUtils::cp_r directory, "data/output"
42
+ end
43
+ FileUtils::rm_rf directory
44
+ end
@@ -0,0 +1,137 @@
1
+ require "base"
2
+ require "bcp"
3
+ require "basetaskmocking"
4
+
5
+ describe BradyW::BCP do
6
+ before(:each) do
7
+ def @config.db_name
8
+ "regulardb"
9
+ end
10
+
11
+ def @config.db_hostname
12
+ "myhostname"
13
+ end
14
+
15
+ def @config.db_general_authmode
16
+ :sqlauth
17
+ end
18
+
19
+ def @config.db_general_user
20
+ "theuser"
21
+ end
22
+
23
+ def @config.db_general_password
24
+ "thepassword"
25
+ end
26
+
27
+ def @config.project_prefix
28
+ "PRE"
29
+ end
30
+ end
31
+
32
+ after(:each) do
33
+ # Remove our generated test data
34
+ FileUtils::rm_rf "data/output/bcp"
35
+ end
36
+
37
+ it "Works OK with standard delimiters and SQL Auth" do
38
+ task = BradyW::BCP.new do |bcp|
39
+ bcp.files = FileList["data/bcp/01-firsttable.csv",
40
+ "data/bcp/02-nexttable.csv"]
41
+ end
42
+
43
+ # Don't want to depend on specific registry setting
44
+ task.should_receive(:sql_tool).any_number_of_times.with("100").and_return("z:\\")
45
+
46
+ task.exectaskpublic
47
+ task.excecutedPop.should == "\"z:\\bcp.exe\" \"nexttable\" in 02-nexttable.csv -U theuser -P thepassword /Smyhostname -t \"|d3l1m1t3r|\" /c -m 1 -F 2"
48
+ task.excecutedPop.should == "\"z:\\bcp.exe\" \"firsttable\" in 01-firsttable.csv -U theuser -P thepassword /Smyhostname -t \"|d3l1m1t3r|\" /c -m 1 -F 2"
49
+
50
+ expected = IO.readlines("data/bcp/01-firsttable-expectedout.csv")
51
+ actual = IO.readlines("data/output/bcp/01-firsttable.csv")
52
+
53
+ actual.should == expected
54
+
55
+ expected = IO.readlines("data/bcp/02-nexttable-expectedout.csv")
56
+ actual = IO.readlines("data/output/bcp/02-nexttable.csv")
57
+
58
+ actual.should == expected
59
+
60
+ end
61
+
62
+ it "Works OK with custom delimiters, Custom Version, and Windows Auth" do
63
+ def @config.db_general_authmode
64
+ :winauth
65
+ end
66
+
67
+ task = BradyW::BCP.new do |bcp|
68
+ bcp.files = FileList["data/bcp/01-firsttable.csv",
69
+ "data/bcp/02-nexttable.csv"]
70
+ bcp.delimiter = "foobar"
71
+ bcp.version = "852"
72
+ end
73
+
74
+ # Don't want to depend on specific registry setting
75
+ task.should_receive(:sql_tool).any_number_of_times.with("852").and_return("z:\\")
76
+
77
+ task.exectaskpublic
78
+ task.excecutedPop.should == "\"z:\\bcp.exe\" \"regulardb.dbo.nexttable\" in 02-nexttable.csv -T -S myhostname -t \"foobar\" /c -m 1 -F 2"
79
+ task.excecutedPop.should == "\"z:\\bcp.exe\" \"regulardb.dbo.firsttable\" in 01-firsttable.csv -T -S myhostname -t \"foobar\" /c -m 1 -F 2"
80
+
81
+ expected = IO.readlines("data/bcp/01-firsttable-expectedout2.csv")
82
+ actual = IO.readlines("data/output/bcp/01-firsttable.csv")
83
+
84
+ actual.should == expected
85
+
86
+ expected = IO.readlines("data/bcp/02-nexttable-expectedout2.csv")
87
+ actual = IO.readlines("data/output/bcp/02-nexttable.csv")
88
+
89
+ actual.should == expected
90
+ end
91
+
92
+ it "Handles delimiter interference Properly" do
93
+ def @config.db_general_authmode
94
+ :winauth
95
+ end
96
+
97
+ task = BradyW::BCP.new do |bcp|
98
+ bcp.files = FileList["data/bcp/delimInData.csv"]
99
+ end
100
+
101
+ lambda { task.exectaskpublic }.should raise_exception()
102
+ end
103
+
104
+ it "Handles failure gracefully" do
105
+ task = BradyW::BCP.new do |bcp|
106
+ bcp.files = FileList["data/bcp/01-firsttable.csv",
107
+ "data/bcp/02-nexttable.csv"]
108
+ end
109
+
110
+ # Don't want to depend on specific registry setting
111
+ task.should_receive(:sql_tool).any_number_of_times.with("100").and_return("z:\\")
112
+
113
+ task.stub!(:shell).and_yield(nil, SimulateProcessFailure.new)
114
+
115
+ lambda { task.exectaskpublic }.should raise_exception("Command failed with status (BW Rake Task Problem):")
116
+
117
+ # This means our temporary file was correctly cleaned up
118
+ File.exist?("#{ENV['tmp']}/bcp").should_not == true
119
+ # Our test code should have done this
120
+ File.exist?("data/output/bcp").should == true
121
+ end
122
+
123
+ it "Properly disables identity inserts when set" do
124
+ task = BradyW::BCP.new do |bcp|
125
+ bcp.identity_inserts = true
126
+ bcp.files = FileList["data/bcp/01-firsttable.csv",
127
+ "data/bcp/02-nexttable.csv"]
128
+ end
129
+ # Don't want to depend on specific registry setting
130
+ task.should_receive(:sql_tool).any_number_of_times.with("100").and_return("z:\\")
131
+
132
+ task.exectaskpublic
133
+ task.excecutedPop.should == "\"z:\\bcp.exe\" \"nexttable\" in 02-nexttable.csv -U theuser -P thepassword /Smyhostname -t \"|d3l1m1t3r|\" /c -E -m 1 -F 2"
134
+ task.excecutedPop.should == "\"z:\\bcp.exe\" \"firsttable\" in 01-firsttable.csv -U theuser -P thepassword /Smyhostname -t \"|d3l1m1t3r|\" /c -E -m 1 -F 2"
135
+
136
+ end
137
+ end
@@ -0,0 +1,43 @@
1
+ require "base"
2
+ require "config"
3
+
4
+ describe "Configuration Works Properly" do
5
+ before(:all) do
6
+ $: << File.expand_path(File.dirname(__FILE__))+"/config_testdata"
7
+ end
8
+
9
+ after(:each) do
10
+ File.delete "newuserfile.rb" if File.exist? "newuserfile.rb"
11
+ end
12
+
13
+ def fetchprops(defaultfile,userfile)
14
+ # get around the private method
15
+ BradyW::Config.send(:new,defaultfile,userfile).values
16
+ end
17
+
18
+ it "Should work fine with only default properties" do
19
+ props = fetchprops("onlydefault.rb",
20
+ "newuserfile.rb")
21
+
22
+ props.setting.should == "yep"
23
+ props.setting2.should == "nope"
24
+ props.setting3.should == "yep"
25
+ end
26
+
27
+ it "Should work OK with default + partially filled out user properties" do
28
+ props = fetchprops("defaultpartialuser_default.rb",
29
+ "defaultpartialuser_user.rb")
30
+
31
+ props.setting.should == "overrodethis"
32
+ props.setting2.should == "nope"
33
+ props.setting3.should == "yep"
34
+ end
35
+
36
+ it "Should work OK with default + completely filled out user properties" do
37
+ props = BradyW::Config.send(:new).values
38
+
39
+ props.setting.should == "yep2"
40
+ props.setting2.should == "nope2"
41
+ props.setting3.should == "yep2"
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ require "base_config"
2
+
3
+ module TestCase_2
4
+ class ADefaultConfig < BradyW::BaseConfig
5
+ def setting
6
+ "yep"
7
+ end
8
+
9
+ def setting2
10
+ "nope"
11
+ end
12
+
13
+ def setting3
14
+ "yep"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require "defaultpartialuser_default"
2
+
3
+ module TestCase_2
4
+ class UserConfig < TestCase_2::ADefaultConfig
5
+ def setting
6
+ "overrodethis"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module TestCase_3
2
+ class AUserConfig < ADefaultConfig
3
+ def setting
4
+ "yep2"
5
+ end
6
+ def setting2
7
+ "nope2"
8
+ end
9
+ def setting3
10
+ "yep2"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ require "base_config"
2
+
3
+ module TestCase_3
4
+ class ADefaultConfig < BradyW::BaseConfig
5
+ def setting
6
+ "yep"
7
+ end
8
+ def setting2
9
+ "nope"
10
+ end
11
+ def setting3
12
+ "yep"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require "config"
2
+
3
+ module TestCase_1
4
+ class Config < BradyW::BaseConfig
5
+ def setting
6
+ "yep"
7
+ end
8
+
9
+ def setting2
10
+ "nope"
11
+ end
12
+
13
+ def setting3
14
+ "yep"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,88 @@
1
+ require "base"
2
+ require "database"
3
+
4
+ describe BradyW::Database do
5
+ before(:each) do
6
+ @db = BradyW::Database.new
7
+ def @config.db_name
8
+ "regulardb"
9
+ end
10
+ def @config.project_prefix
11
+ "PRE"
12
+ end
13
+ end
14
+
15
+ it "DB Name Plain" do
16
+ @db.name.should == "regulardb"
17
+ end
18
+
19
+ it "DB Name with hostname/prefix" do
20
+ def @config.db_name
21
+ "@prefix@-@thismachinehostname@"
22
+ end
23
+ @db.name.should == "PRE-"+Socket.gethostname
24
+ end
25
+
26
+ it "User plain" do
27
+ def @config.db_general_user
28
+ "username2"
29
+ end
30
+
31
+ @db.user.should == "username2"
32
+ end
33
+
34
+ it "Password" do
35
+ def @config.db_general_password
36
+ "thepassword"
37
+ end
38
+
39
+ @db.password.should == "thepassword"
40
+ end
41
+
42
+ it "User hostname/prefix" do
43
+ def @config.db_general_user
44
+ "@prefix@-@thismachinehostname@"
45
+ end
46
+ @db.user.should == "PRE-"+Socket.gethostname
47
+ end
48
+
49
+ it "Connect String for .NET Code/SQL Auth" do
50
+ def @config.db_hostname
51
+ "myhostname"
52
+ end
53
+
54
+ def @config.db_general_authmode
55
+ :sqlauth
56
+ end
57
+
58
+ def @config.db_general_user
59
+ "theuser"
60
+ end
61
+
62
+ def @config.db_general_password
63
+ "thepassword"
64
+ end
65
+
66
+ def @config.db_connect_string_sqlauth
67
+ "user @user@ pass @password@ host @host@ db @initialcatalog@"
68
+ end
69
+
70
+ @db.connect_code.should == "user theuser pass thepassword host myhostname db regulardb"
71
+ end
72
+
73
+ it "Connect String for .NET Code/Windows Auth" do
74
+ def @config.db_hostname
75
+ "myhostname"
76
+ end
77
+
78
+ def @config.db_general_authmode
79
+ :winauth
80
+ end
81
+
82
+ def @config.db_connect_string_winauth
83
+ "host @host@ db @initialcatalog@"
84
+ end
85
+
86
+ @db.connect_code.should == "host myhostname db regulardb"
87
+ end
88
+ end