Skip to content
Browse files

replace testunit with rspec

  • Loading branch information...
1 parent 0b27350 commit abb22bf78fb4abc6a933632300e7fedbabcfcebc @thomasfl committed Mar 11, 2012
Showing with 189 additions and 44 deletions.
  1. +3 −0 .gitignore
  2. +12 −4 README.md
  3. +50 −0 Rakefile
  4. +21 −0 autotest.rb
  5. +2 −1 lib/open-uri-and-write.rb
  6. +18 −11 spec/integration/dav4rack_testserver.rb
  7. +37 −22 spec/integration/open-uri-and-write-spec.rb
  8. +46 −6 spec/spec_helper.rb
View
3 .gitignore
@@ -0,0 +1,3 @@
+*~
+*#
+.#*
View
16 README.md
@@ -45,23 +45,31 @@ The only difference between local files and directories and remote files and dir
To not interfer with the 'open-uri' standard library, the 'open-uri-and-write' gem is only active in file modes 'w','a','w+' and 'a+':
```ruby
- open("http://www.ruby-lang.org/open_uri_and_write.html","w").puts("<h1>HTML</h1>")
+ open("http://www.ruby-lang.org/open_uri_and_write.html","w").puts("<h1>HTML</h1>") # open-uri-and-write
```
If not any filemode is supplied, 'open-uri' is used:
```ruby
- puts open("http://www.ruby-lang.org").read
+ puts open("http://www.ruby-lang.org").read() # open-uri
```
-# Credentials
+# Authentication
-Credentials can be supplied with the DAVUSER and DAVPASS environment variables. If not set, ruby will prompt for username and password. On OS X the password will then be stored encrypted in the Keychain and reused later.
+As a default 'open-uri-and-write' will prompt for username and password. Credentials can be supplied with the DAVUSER and DAVPASS environment variables. If enviorment variables are not set, ruby will prompt for username and password. On OS X the password will then be stored encrypted in the Keychain and reused later.
# Install
This is work in progress, and not pushed as a gem yet.
+# Testing
+
+The tests will start a webserver at startup. To run all tests simply:
+
+```
+ $ ruby spec/integration/open-uri-and-write-spec.rb
+```
+
# Credits
* Tanaka Akira for the inspiration taken from 'open-uri' standard library.
View
50 Rakefile
@@ -0,0 +1,50 @@
+require 'rubygems'
+require 'rake'
+
+require 'spec/rake/spectask'
+exit
+
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+end
+
+task :spec => :check_dependencies
+
+task :default => :spec
+
+desc "release with no version change"
+task :dist => [:clean, :release]
+
+namespace :dist do
+ desc "release patch"
+ task :patch => [:clean, "version:bump:patch", :release]
+ desc "release with minor version bump"
+ task :minor => [:clean, "version:bump:minor", :release]
+end
+
+desc "build gem into pkg directory"
+task :gem => [:build]
+
+task :clean do
+ Dir.glob("**/*~").each do |file|
+ File.unlink file
+ end
+ puts "cleaned"
+end
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "net_dav #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
21 autotest.rb
@@ -0,0 +1,21 @@
+require 'rubygems'
+require 'filewatcher'
+require 'paint'
+
+def run_tests
+ puts Paint['Testing', :yellow]
+ output = %x[ruby test/test-open-uri-and-write.rb]
+ if(output[/Failure:/])
+ puts output
+ puts Paint['Failure', :red, :bright]
+ else
+ puts output
+ puts Paint['Ok', :green, :bright]
+ end
+end
+
+run_tests
+files = ["lib/open-uri-and-write.rb", "test/test-open-uri-and-write.rb"]
+FileWatcher.new(files).watch(0.5) do |filename|
+ run_tests
+end
View
3 lib/open-uri-and-write.rb
@@ -86,7 +86,8 @@ def close
end
-# Monkeypatching
+# Kernel extensions
+# Careful monkeypatching
module Kernel
private
alias open_uri_and_write_original open # :nodoc:
View
29 spec/integration/dav4rack_testserver.rb
@@ -60,9 +60,8 @@ def set_property(name, value)
# Starts a WebDAV server using dav4rack and unicorn
-def start_dav4rack(port, options)
-
- options = {:resource_class => MyResource, :root => Dir.pwd }
+def start_dav4rack(port, root)
+ options = {:resource_class => MyResource, :root => root }
app = Rack::Builder.new do
use Rack::ShowExceptions
@@ -73,17 +72,25 @@ def start_dav4rack(port, options)
end.to_app
runners = []
+
runners << lambda do |x|
- print 'Looking for unicorn... '
- require 'unicorn'
- puts 'OK'
- if(Unicorn.respond_to?(:run))
- Unicorn.run(x, :listeners => ["0.0.0.0:#{port}"])
- else
- Unicorn::HttpServer.new(x, :listeners => ["0.0.0.0:#{port}"]).start.join
- end
+ puts 'Loading WEBrick'
+ Rack::Handler::WEBrick.run(x, :Port => port)
end
+# If tests don't run fast enough. Comment in this code, and don't kill the server in "after(:all) do"
+#
+# runners << lambda do |x|
+# print 'Looking for unicorn... '
+# require 'unicorn'
+# puts 'OK'
+# if(Unicorn.respond_to?(:run))
+# Unicorn.run(x, :listeners => ["0.0.0.0:#{port}"])
+# else
+# Unicorn::HttpServer.new(x, :listeners => ["0.0.0.0:#{port}"]).start.join
+# end
+# end
+
begin
runner = runners.shift
runner.call(app)
View
59 spec/integration/open-uri-and-write-spec.rb
@@ -2,34 +2,18 @@
require File.expand_path(File.dirname(__FILE__) + '/dav4rack_testserver')
# To run:
-# $ ruby spec/integration/open-uri-and-write-spec.r
+# $ ruby spec/integration/open-uri-and-write-spec.rb
describe "OpenUriAndWrite" do
before(:all) do
- port = "3003"
- @base_uri = "http://localhost:#{port}/"
- if(not(server_up?(@base_uri)))
- options = {}
- # options[:username] = "davuser"
- # options[:password] = "davpass"
- options[:root] = Pathname.new(File.expand_path(File.dirname(__FILE__))).parent.to_s + '/fixtures'
-
- # Start webdav server in subprocess
- @pid = fork do
- start_dav4rack(port, options)
- end
- exit(0) if(@pid == nil)
- wait_for_server(@base_uri)
- else
- puts "Server is running."
- end
+ start_webdav_server(:port => 3003)
+ @base_uri = "http://localhost:3003/"
end
before(:each) do
end
-
it "should write to local file normally" do
timestamp = Time.now.to_s
filename = '/tmp/local_test_file.txt'
@@ -40,7 +24,6 @@
open(filename).read.strip.should == timestamp
end
-
it "should write files to webdav server" do
timestamp = Time.now.to_s
webdav_url = @base_uri + 'webdav_test_1.txt'
@@ -103,6 +86,39 @@
# TODO test authentication
+ # TODO Directories
+ it "should create and delete directory" do
+ # timestamp = Time.now.to_s
+ webdav_url = @base_uri + 'new_folder'
+ Dir.mkdir(webdav_url)
+ File.exists?(webdav_url).should == true
+
+ Dir.rmdir(webdav_url)
+ # File.exists?(webdav_url).should == false
+
+ # TODO let 'delete' and 'unlink' be aliases for 'rmdir'
+ # Support Dir.pwd, Dir.directory?
+ end
+
+ it "should not matter which order the rubygems 'open-uri' and 'open-uri-and-write' is loaded" do
+ # http://ruby-doc.org/core-1.9.3/Object.html
+ # http://stackoverflow.com/questions/335530/how-do-you-detect-that-monkey-patching-has-occurred-in-ruby
+ # http://blog.sidu.in/2007/12/rubys-methodadded.html
+
+ end
+
+
+ it "should to handle username and password supplied as parameter to open" do
+ # TODO: This is not documented!
+ webdav_url = @base_uri + 'yet_another_testfile.txt'
+ file = open(webdav_url, 'w', :webdav_username => 'username', :webdav_password => 'secret')
+ begin
+ file.read
+ should fail
+ rescue Exception => e
+ e.to_s[/401/].should != nil
+ end
+ end
@@ -116,8 +132,7 @@
after(:all) do
# File.delete(@base_uri + 'webdav_test.txt')
# Shut down webdav server:
- # The server takes a few seconds to stop, so we just keep it running.
- # Process.kill('SIGKILL', @pid) rescue nil
+ stop_webdav_server
end
end
View
52 spec/spec_helper.rb
@@ -16,20 +16,60 @@
end
+require 'pry'
+
+def stop_webdav_server
+ pidfile = File.expand_path(File.dirname(__FILE__) + '/webdavserver.pid')
+ pid = open(pidfile).read.strip.to_i
+ Process.kill('SIGKILL', pid) rescue nil
+end
+
+def start_webdav_server(*args)
+ port = args.first[:port].to_s
+ base_uri = "http://localhost:#{port}/"
+
+ server_root = Pathname.new(File.expand_path(File.dirname(__FILE__))).to_s + '/fixtures'
+ %x[rm -r #{server_root}]
+ %x[mkdir #{server_root}]
+
+ if(not(server_up?(@base_uri)))
+ options = {}
+ # options[:username] = "davuser"
+ # options[:password] = "davpass"
+
+
+ # Start webdav server in subprocess
+ pid = fork do
+ start_dav4rack(port, server_root)
+ end
+
+ pidfile = File.expand_path(File.dirname(__FILE__) + '/webdavserver.pid')
+ open(pidfile,'w') do |file|
+ file.puts pid
+ end
+
+ wait_for_server(base_uri)
+ else
+ puts "Server is running."
+ end
+
+end
+
def server_up?(address)
+ puts "Probing webserver #{address}:"
begin
- # puts "testing #{address}:" + Net::HTTP.get_response(URI(address)).code
- # print "."
- # return Net::HTTP.get_response(URI(address)).code.to_i > 0
return Net::HTTP.get_response(URI(address)).code == "200"
- rescue
+ rescue Exception => e
+ puts e.to_s
return false
end
end
def wait_for_server(address)
- sleep(0.1)
+ puts "waiting for #{address}"
+ sleep(0.5)
while(not(server_up?(address)))
- sleep(0.1)
+ puts "retrying"
+ sleep(0.5)
end
end

0 comments on commit abb22bf

Please sign in to comment.
Something went wrong with that request. Please try again.