Skip to content

Commit

Permalink
convert to minitest
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Jan 6, 2016
1 parent bbd3267 commit 7c8f612
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 87 deletions.
3 changes: 2 additions & 1 deletion mini_portile2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "test-unit", "~> 3.0"
spec.add_development_dependency "minitest", "~> 5.8.0"
spec.add_development_dependency "minitest-hooks", "~> 1.4.0"
spec.add_development_dependency "minitar", "~> 0.5.4"

spec.required_ruby_version = ">= 1.9.2"
Expand Down
58 changes: 30 additions & 28 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'test/unit'
require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/hooks/test'
require 'webrick'
require 'fileutils'
require 'zlib'
Expand All @@ -7,43 +9,43 @@
require 'erb'
require 'mini_portile2'

class TestCase < Test::Unit::TestCase
class << self
HTTP_PORT = 23523
class TestCase < Minitest::Test
include Minitest::Hooks

attr_accessor :webrick
HTTP_PORT = 23523

def start_webrick(path)
@webrick = WEBrick::HTTPServer.new(:Port => HTTP_PORT, :DocumentRoot => path).tap do |w|
Thread.new do
w.start
end
until w.status==:Running
sleep 0.1
end
attr_accessor :webrick

def start_webrick(path)
@webrick = WEBrick::HTTPServer.new(:Port => HTTP_PORT, :DocumentRoot => path).tap do |w|
Thread.new do
w.start
end
until w.status==:Running
sleep 0.1
end
end
end

def stop_webrick
if w=@webrick
w.shutdown
until w.status==:Stop
sleep 0.1
end
def stop_webrick
if w=@webrick
w.shutdown
until w.status==:Stop
sleep 0.1
end
end
end

def create_tar(tar_path, assets_path)
FileUtils.mkdir_p(File.dirname(tar_path))
Zlib::GzipWriter.open(tar_path) do |fdtgz|
Dir.chdir(assets_path) do
Archive::Tar::Minitar.pack("test mini portile-1.0.0", fdtgz)
end
def create_tar(tar_path, assets_path)
FileUtils.mkdir_p(File.dirname(tar_path))
Zlib::GzipWriter.open(tar_path) do |fdtgz|
Dir.chdir(assets_path) do
Archive::Tar::Minitar.pack("test mini portile-1.0.0", fdtgz)
end
end
end

def work_dir(r=recipe)
"tmp/#{r.host}/ports/#{r.name}/#{r.version}/#{r.name}-#{r.version}"
end
def work_dir(r=recipe)
"tmp/#{r.host}/ports/#{r.name}/#{r.version}/#{r.name}-#{r.version}"
end
end
70 changes: 34 additions & 36 deletions test/test_cook.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
require File.expand_path('../helper', __FILE__)

class TestCook < TestCase
class << self
attr_accessor :assets_path
attr_accessor :tar_path
attr_accessor :recipe
attr_accessor :assets_path, :tar_path, :recipe

def with_custom_git_dir(dir)
old = ENV['GIT_DIR']
ENV['GIT_DIR'] = dir
yield
ensure
ENV['GIT_DIR'] = old
end
def with_custom_git_dir(dir)
old = ENV['GIT_DIR']
ENV['GIT_DIR'] = dir
yield
ensure
ENV['GIT_DIR'] = old
end

def startup
@assets_path = File.expand_path("../assets", __FILE__)
@tar_path = File.expand_path("../../tmp/test mini portile-1.0.0.tar.gz", __FILE__)
def before_all
super
@assets_path = File.expand_path("../assets", __FILE__)
@tar_path = File.expand_path("../../tmp/test mini portile-1.0.0.tar.gz", __FILE__)

# remove any previous test files
FileUtils.rm_rf("tmp")
# remove any previous test files
FileUtils.rm_rf("tmp")

create_tar(@tar_path, @assets_path)
start_webrick(File.dirname(@tar_path))
create_tar(@tar_path, @assets_path)
start_webrick(File.dirname(@tar_path))

@recipe = MiniPortile.new("test mini portile", "1.0.0").tap do |recipe|
recipe.files << "http://localhost:#{HTTP_PORT}/#{ERB::Util.url_encode(File.basename(@tar_path))}"
recipe.patch_files << File.join(@assets_path, "patch 1.diff")
recipe.configure_options << "--option=\"path with 'space'\""
git_dir = File.join(@assets_path, "git")
with_custom_git_dir(git_dir) do
recipe.cook
end
@recipe = MiniPortile.new("test mini portile", "1.0.0").tap do |recipe|
recipe.files << "http://localhost:#{HTTP_PORT}/#{ERB::Util.url_encode(File.basename(@tar_path))}"
recipe.patch_files << File.join(@assets_path, "patch 1.diff")
recipe.configure_options << "--option=\"path with 'space'\""
git_dir = File.join(@assets_path, "git")
with_custom_git_dir(git_dir) do
recipe.cook
end
end
end

def shutdown
stop_webrick
# leave test files for inspection
end
def after_all
super
stop_webrick
# leave test files for inspection
end

def test_download
Expand All @@ -47,32 +45,32 @@ def test_download
end

def test_untar
configure = File.join(self.class.work_dir, "configure")
configure = File.join(work_dir, "configure")
assert File.exist?(configure), configure
assert_match( /^#!\/bin\/sh/, IO.read(configure) )
end

def test_patch
patch1 = File.join(self.class.work_dir, "patch 1.txt")
patch1 = File.join(work_dir, "patch 1.txt")
assert File.exist?(patch1), patch1
assert_match( /^\tchange 1/, IO.read(patch1) )
end

def test_configure
txt = File.join(self.class.work_dir, "configure.txt")
txt = File.join(work_dir, "configure.txt")
assert File.exist?(txt), txt
opts = self.class.recipe.configure_options + ["--prefix=#{self.class.recipe.path}"]
opts = recipe.configure_options + ["--prefix=#{recipe.path}"]
assert_equal( opts.inspect, IO.read(txt).chomp )
end

def test_compile
txt = File.join(self.class.work_dir, "compile.txt")
txt = File.join(work_dir, "compile.txt")
assert File.exist?(txt), txt
assert_equal( ["all"].inspect, IO.read(txt).chomp )
end

def test_install
txt = File.join(self.class.work_dir, "install.txt")
txt = File.join(work_dir, "install.txt")
assert File.exist?(txt), txt
assert_equal( ["install"].inspect, IO.read(txt).chomp )
end
Expand Down
41 changes: 19 additions & 22 deletions test/test_digest.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
require File.expand_path('../helper', __FILE__)

class TestDigest < TestCase
class << self
attr_accessor :assets_path
attr_accessor :tar_path
attr_accessor :recipe
attr :assets_path, :tar_path, :recipe

def startup
@assets_path = File.expand_path("../assets", __FILE__)
@tar_path = File.expand_path("../../tmp/test-digest-1.0.0.tar.gz", __FILE__)
def before_all
super
@assets_path = File.expand_path("../assets", __FILE__)
@tar_path = File.expand_path("../../tmp/test-digest-1.0.0.tar.gz", __FILE__)

# remove any previous test files
FileUtils.rm_rf("tmp")
# remove any previous test files
FileUtils.rm_rf("tmp")

create_tar(@tar_path, @assets_path)
start_webrick(File.dirname(@tar_path))
end
create_tar(@tar_path, @assets_path)
start_webrick(File.dirname(@tar_path))
end

def shutdown
stop_webrick
# leave test files for inspection
end
def after_all
stop_webrick
# leave test files for inspection
end

def setup
# remove any download files
super
FileUtils.rm_rf("ports/archives")

@recipe = MiniPortile.new("test-digest", "1.0.0")
end

def download_with_digest(key, klass)
@recipe.files << {
:url => "http://localhost:#{self.class.webrick.config[:Port]}/#{ERB::Util.url_encode(File.basename(self.class.tar_path))}",
key => klass.file(self.class.tar_path).hexdigest,
:url => "http://localhost:#{webrick.config[:Port]}/#{ERB::Util.url_encode(File.basename(tar_path))}",
key => klass.file(tar_path).hexdigest,
}
@recipe.download
end

def download_with_wrong_digest(key)
@recipe.files << {
:url => "http://localhost:#{self.class.webrick.config[:Port]}/#{ERB::Util.url_encode(File.basename(self.class.tar_path))}",
:url => "http://localhost:#{webrick.config[:Port]}/#{ERB::Util.url_encode(File.basename(tar_path))}",
key => "0011223344556677",
}
assert_raise(RuntimeError){ @recipe.download }
assert_raises(RuntimeError){ @recipe.download }
end

def test_sha256
Expand All @@ -70,3 +66,4 @@ def test_wrong_md5
download_with_wrong_digest(:md5)
end
end

1 comment on commit 7c8f612

@larskanis
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flavorjones I often missed before_all/after_all in minitest. Good to know there is minitest-hooks.

Please sign in to comment.