test-mysqld 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 miyucy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ = test-mysqld
2
+
3
+ see http://developer.cybozu.co.jp/kazuho/2009/08/perl-mysql-test.html
4
+
5
+ require 'pp'
6
+ require 'rubygems'
7
+ require 'test/mysqld'
8
+ mysqld = Test::Mysqld.new(:auto_start => true)
9
+ pp mysqld
10
+ pp mysqld.dsn
11
+ pp conn = Mysql2::Client.new(mysqld.dsn)
12
+ # >> #<Test::Mysqld:0x7fd4a9fd4000
13
+ # >> @base_dir="/tmp/d20101110-13587-1aemfmq",
14
+ # >> @mycnf=
15
+ # >> {"datadir"=>"/tmp/d20101110-13587-1aemfmq/var",
16
+ # >> "pid-file"=>"/tmp/d20101110-13587-1aemfmq/tmp/mysql.pid",
17
+ # >> "socket"=>"/tmp/d20101110-13587-1aemfmq/tmp/mysql.sock"},
18
+ # >> @mysql_install_db="/usr/bin/mysql_install_db",
19
+ # >> @mysqld="/usr/sbin/mysqld",
20
+ # >> @pid=13651>
21
+ # >> {:socket=>"/tmp/d20101110-13587-1aemfmq/tmp/mysql.sock",
22
+ # >> :username=>"root",
23
+ # >> :database=>"test"}
24
+ # >> #<Mysql2::Client:0x7fd4aa4f6830
25
+ # >> @query_options=
26
+ # >> {:database_timezone=>:local,
27
+ # >> :as=>:hash,
28
+ # >> :application_timezone=>nil,
29
+ # >> :cache_rows=>true,
30
+ # >> :async=>false,
31
+ # >> :cast_booleans=>false,
32
+ # >> :symbolize_keys=>false,
33
+ # >> :connect_flags=>2147525125}>
34
+
35
+ you need mysql2.gem or mysql.gem
36
+
37
+ == Note on Patches/Pull Requests
38
+
39
+ * Fork the project.
40
+ * Make your feature addition or bug fix.
41
+ * Add tests for it. This is important so I don't break it in a
42
+ future version unintentionally.
43
+ * Commit, do not mess with rakefile, version, or history.
44
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
45
+ * Send me a pull request. Bonus points for topic branches.
46
+
47
+ == Copyright
48
+
49
+ Copyright (c) 2010 miyucy. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "test-mysqld"
8
+ gem.summary = %Q{port of Test::mysqld}
9
+ gem.description = %Q{port of Test::mysqld (mysqld runner for tests)}
10
+ gem.email = "miyucy@gmail.com"
11
+ gem.homepage = "http://github.com/miyucy/test-mysqld"
12
+ gem.authors = ["miyucy"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "test-mysqld #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,177 @@
1
+ require "fileutils"
2
+ require "mkmf"
3
+ require "tmpdir"
4
+
5
+ module Test
6
+ class Mysqld
7
+ def initialize(options={})
8
+ parse_options options
9
+ raise "mysqld is alread running (#{mycnf["pid-file"]})" if FileTest.exist? mycnf["pid-file"]
10
+ if options[:auto_start]
11
+ setup
12
+ start
13
+ end
14
+ setup
15
+ end
16
+
17
+ def dsn(options={})
18
+ options.tap do |option|
19
+ options[:port] ||= mycnf["port"] if mycnf["port"]
20
+ if options[:port]
21
+ options[:host] ||= mycnf["bind-address"] || "127.0.0.1"
22
+ else
23
+ options[:socket] ||= mycnf["socket"]
24
+ end
25
+ options[:username] ||= "root"
26
+ options[:database] ||= "test"
27
+ end
28
+ end
29
+
30
+ def setup
31
+ File.open(base_dir + '/etc/my.cnf', 'wb') do |f|
32
+ f.puts "[mysqld]"
33
+ mycnf.each do |key, val|
34
+ if val.nil? or val.empty?
35
+ f.puts "#{val}"
36
+ else
37
+ f.puts "#{key}=#{val}"
38
+ end
39
+ end
40
+ end
41
+ return if FileTest.directory? base_dir + '/var/mysql'
42
+
43
+ mysql_base_dir = mysql_install_db.sub(%r'/[^/]+/mysql_install_db','')
44
+ output = nil
45
+ begin
46
+ IO.popen %[#{mysql_install_db} --basedir='#{mysql_base_dir}' --defaults-file='#{base_dir}/etc/my.cnf' 2>&1] do |pipe|
47
+ output = pipe.read
48
+ end
49
+ rescue
50
+ raise "mysql_install_db failed" + (output ||= "")
51
+ end
52
+ end
53
+
54
+ def start
55
+ return if pid
56
+
57
+ mysqld_log = File.open(base_dir + '/tmp/mysqld.log', 'a')
58
+ @pid = fork do
59
+ $stdout.reopen mysqld_log
60
+ $stderr.reopen mysqld_log
61
+ exec %[#{mysqld} --defaults-file='#{base_dir}/etc/my.cnf' --user=root]
62
+ end
63
+ exit unless @pid
64
+ mysqld_log.close
65
+
66
+ output = nil
67
+ begin
68
+ while !FileTest.exist?(mycnf["pid-file"])
69
+ if Process.waitpid pid, Process::WNOHANG
70
+ output = File.open(base_dir + '/tmp/mysqld.log'){ |f| f.read }
71
+ end
72
+ sleep 0.1
73
+ end
74
+ rescue
75
+ raise "mysqld failed" + (output ||= "")
76
+ end
77
+ create_database
78
+
79
+ at_exit { stop }
80
+ end
81
+
82
+ def stop(signal=nil)
83
+ return unless pid
84
+ if File.exist? mycnf["pid-file"]
85
+ realpid = File.open(mycnf["pid-file"], "rb"){ |f| f.read }.strip.to_i
86
+ Process.kill Signal.list[signal || "TERM"], realpid rescue nil
87
+ Process.waitpid(realpid) rescue nil
88
+ end
89
+ Process.kill Signal.list[signal || "TERM"], pid rescue nil
90
+ Process.waitpid(pid) rescue nil
91
+ FileUtils.rm_f mycnf["pid-file"] if File.exist? mycnf["pid-file"]
92
+ @pid = nil
93
+ end
94
+
95
+ attr_reader :base_dir, :mycnf, :mysqld, :mysql_install_db, :pid
96
+
97
+ private
98
+
99
+ def parse_options(options)
100
+ @base_dir = options[:base_dir] || default_base_dir
101
+
102
+ @mycnf = options[:mycnf] || {}
103
+ @mycnf["socket"] ||= base_dir + '/tmp/mysql.sock'
104
+ @mycnf["datadir"] ||= base_dir + '/var'
105
+ @mycnf["pid-file"] ||= base_dir + '/tmp/mysql.pid'
106
+
107
+ @mysqld = options[:mysqld] || find_mysqld
108
+ @mysql_install_db = options[:mysql_install_db] || find_mysql_install_db
109
+ end
110
+
111
+ def find_mysqld
112
+ suppress_logging
113
+ find_executable 'mysqld'
114
+ end
115
+
116
+ def find_mysql_install_db
117
+ suppress_logging
118
+ find_executable 'mysql_install_db'
119
+ end
120
+
121
+ def default_base_dir
122
+ Dir.mktmpdir.tap { |dir|
123
+ at_exit { FileUtils.remove_entry_secure dir if FileTest.directory? dir }
124
+
125
+ FileUtils.mkdir_p(dir + '/etc')
126
+ FileUtils.mkdir_p(dir + '/var')
127
+ FileUtils.mkdir_p(dir + '/tmp')
128
+ }
129
+ end
130
+
131
+ def suppress_logging
132
+ Logging.quiet = true
133
+ Logging.logfile @base_dir + '/tmp/mkmf.log'
134
+ end
135
+
136
+ def create_database
137
+ connection = mysql2_or_mysql_connection
138
+ connection.query("CREATE DATABASE IF NOT EXISTS test")
139
+ connection.close
140
+ end
141
+
142
+ def mysql2_or_mysql_connection
143
+ mysql2_connection || mysql_connection || raise("LoadError 'mysql2' or 'mysql'")
144
+ end
145
+
146
+ def mysql2_connection
147
+ unless defined? ::Mysql2
148
+ begin
149
+ require "rubygems"
150
+ require "mysql2"
151
+ rescue LoadError
152
+ end
153
+ end
154
+ if defined? ::Mysql2
155
+ Mysql2::Client.new(dsn :database => 'mysql')
156
+ else
157
+ nil
158
+ end
159
+ end
160
+
161
+ def mysql_connection
162
+ unless defined? ::Mysql
163
+ begin
164
+ require "rubygems"
165
+ require "mysql"
166
+ rescue LoadError
167
+ end
168
+ end
169
+ if defined? ::Mysql
170
+ opts = dsn :database => 'mysql'
171
+ Mysql.new(opts[:username], nil, opts[:host], opts[:port], opts[:database], opts[:socket], opts[:flags])
172
+ else
173
+ nil
174
+ end
175
+ end
176
+ end
177
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'test-mysqld'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "TestMysqld" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test-mysqld
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - miyucy
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-10 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: port of Test::mysqld (mysqld runner for tests)
38
+ email: miyucy@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/test/mysqld.rb
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ - spec/test-mysqld_spec.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/miyucy/test-mysqld
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: port of Test::mysqld
91
+ test_files:
92
+ - spec/test-mysqld_spec.rb
93
+ - spec/spec_helper.rb