Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed Jun 3, 2024
1 parent 4232cc6 commit 15fa05e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ jobs:
name: Test
steps:
- uses: actions/checkout@v4
- run: docker run $(docker build -q .) bash -c 'bin/rake test X=/image_magick/' # test im+vips
- run: docker run $(docker build -q .) bash -c 'apt-get remove -y --purge libvips libvips-tools && bin/rake test X=/vips/' # test im
#- run: docker run $(docker build -q .) bash -c 'apt-get remove -y --purge imagemagick* inkscape libvips libvips-tools && bin/rake test X=/image_magick/' # test none
- run: bin/rake test:e2e
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM ruby:3.3

ARG IMAGE_MAGICK_VERSION=7.1.1-33

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

Expand All @@ -10,7 +12,7 @@ RUN apt-get update && apt-get install -y \
rm -rf /var/lib/apt/lists/*
RUN t=$(mktemp) && \
wget 'https://dist.1-2.dev/imei.sh' -qO "$t" && \
bash "$t" --checkinstall && \
bash "$t" --checkinstall --imagemagick-version=$IMAGE_MAGICK_VERSION && \
rm "$t"

RUN apt-get update && apt-get install -y \
Expand Down
27 changes: 27 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@
require "bundler/gem_tasks"
require "minitest/test_task"

require "open3"

Minitest::TestTask.create

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[test rubocop]

namespace :test do
task :e2e do
statuses = [
['bin/rake test X=/image_magick\|e2e__without_deps/'],
['apt-get remove -y --purge libvips libvips-tools && bin/rake test X=/vips\|e2e__without_deps/'],
['apt-get remove -y --purge *imagemagick* inkscape libvips libvips-tools && bin/rake test X=/image_magick\|vips\|e2e__with_deps/'],
[
'apt-get remove -y --purge libvips libvips-tools && bin/rake test X=/vips\|e2e__without_deps/',
"--build-arg IMAGE_MAGICK_VERSION=6.9.13-11"
]
].map do |command, build_args|
command = "docker run $(docker build -q #{build_args} .) bash -c '#{command}'"
stdout, stderr, status = Open3.capture3(command)
puts "=" * command.size
puts command
puts "=" * command.size
puts stderr
puts stdout
status
end

exit statuses.map(&:exitstatus).max
end
end
20 changes: 15 additions & 5 deletions lib/favicon_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ def initialize(argv, stderr)
def call
params, status = parse(@argv)
return status if status >= 0
BaseAdapter.find.new(params, stderr).create_icons

adapter = BaseAdapter.find
if adapter.nil?
stderr.puts "Error: Neither vips or imagemagick found, install one"
return 1
end
adapter.new(params, stderr).create_icons
0
end

Expand Down Expand Up @@ -111,7 +117,11 @@ def exit_message(status, message)
class BaseAdapter
class << self
def find
TTY::Which.which("vips") ? VipsAdapter : ImageMagickAdapter
if TTY::Which.which("vips") || TTY::Which.which("libvips")
VipsAdapter
elsif TTY::Which.which("magick") || TTY::Which.which("convert")
ImageMagickAdapter
end
end
end

Expand Down Expand Up @@ -203,7 +213,7 @@ def touch!(path, params)

def square(size, hex)
pixel = (Vips::Image.black(1, 1) + hex2rgb(hex)).cast(:uchar)
pixel.embed 0, 0, 180, 180, extend: :copy
pixel.embed 0, 0, size, size, extend: :copy
end

def hex2rgb(hex)
Expand All @@ -220,8 +230,8 @@ class ImageMagickAdapter < BaseAdapter

def initialize(*)
super
stderr.puts "imagemagick v7 not found, please install for best results" unless MiniMagick.imagemagick7?
stderr.puts "inkscape not found, install inkscape for best results" unless TTY::Which.which("inkscape")
stderr.puts "Warn: Install imagemagick v7 for best results, using v6" unless MiniMagick.imagemagick7?
stderr.puts "Warn: Inkscape not found, install it for best results" unless TTY::Which.which("inkscape")
end

def ico!(path, params)
Expand Down
12 changes: 11 additions & 1 deletion test/test_e2e.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
require "open3"

class TestE2e < Minitest::Test
def test_e2e__with_existing_svg__it_succeeds
def test_e2e__without_deps__with_existing_svg__it_succeeds
with_svg do |_dir, path|
_, stderr, status = Open3.capture3("bundle exec exe/favicon_factory #{path}")

assert_equal 1, status.exitstatus
assert_match Regexp.new("Error: Neither vips or imagemagick found, install one"), stderr
end
end

def test_e2e__with_deps__with_existing_svg__it_succeeds
with_svg do |dir, path|
_, stderr, status = Open3.capture3("bundle exec exe/favicon_factory #{path}")

assert_equal 0, status.exitstatus
assert_match Regexp.new("Info: Add the following to the `<head>`"), stderr
TARGETS.each do |name|
path = File.join(dir, name)

assert_match Regexp.new("Info: Generating #{path}"), stderr
assert_path_exists path
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_image_magick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test__image_magick__it_uses_the_background_option
called = 0

FaviconFactory::ImageMagickAdapter.class_eval do
alias :touch_! :touch!
alias_method :touch_!, :touch!

define_method(:touch!) do |*|
called += 1
Expand Down Expand Up @@ -63,7 +63,7 @@ def test__image_magick__it_uses_the_background_option
ensure
FaviconFactory::ImageMagickAdapter.class_eval do
remove_method(:touch!)
alias :touch! :touch_!
alias_method :touch!, :touch_!
end
end
end
4 changes: 2 additions & 2 deletions test/test_vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test__vips__it_uses_the_background_option
called = 0

FaviconFactory::VipsAdapter.class_eval do
alias :touch_! :touch!
alias_method :touch_!, :touch!

define_method(:touch!) do |*|
called += 1
Expand Down Expand Up @@ -56,7 +56,7 @@ def test__vips__it_uses_the_background_option
ensure
FaviconFactory::VipsAdapter.class_eval do
remove_method(:touch!)
alias :touch! :touch_!
alias_method :touch!, :touch_!
end
end
end

0 comments on commit 15fa05e

Please sign in to comment.