diff --git a/exe/matrixeval b/exe/matrixeval deleted file mode 100755 index f9fe4da..0000000 --- a/exe/matrixeval +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env ruby - -require 'matrixeval/ruby' - -Matrixeval.start(ARGV) \ No newline at end of file diff --git a/exe/meval b/exe/meval deleted file mode 100755 index f9fe4da..0000000 --- a/exe/meval +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env ruby - -require 'matrixeval/ruby' - -Matrixeval.start(ARGV) \ No newline at end of file diff --git a/lib/matrixeval/ruby.rb b/lib/matrixeval/ruby.rb index bb960eb..e6d09f6 100644 --- a/lib/matrixeval/ruby.rb +++ b/lib/matrixeval/ruby.rb @@ -1,30 +1,14 @@ -# frozen_string_literal: true - -require_relative "ruby/version" -require 'rainbow' -require 'matrixeval/ruby/docker_compose' -require 'matrixeval/ruby/context' -require 'matrixeval/ruby/gemfile_locks' -require 'matrixeval/ruby/extra_mount_files' -require 'matrixeval/ruby/runner' -require 'matrixeval/ruby/gitignore' +require 'matrixeval' +require_relative 'ruby/version' +require_relative 'ruby/target' module Matrixeval module Ruby - class Error < StandardError; end - module_function def root Pathname.new("#{__dir__}/../..") end end - - module_function - def start(argv) - Ruby::Runner.start(argv) - end - - def working_dir - Pathname.new(Dir.getwd) - end end + +Matrixeval.register_target(:ruby, Matrixeval::Ruby::Target) diff --git a/lib/matrixeval/ruby/command_line.rb b/lib/matrixeval/ruby/command_line.rb deleted file mode 100644 index 097b9d5..0000000 --- a/lib/matrixeval/ruby/command_line.rb +++ /dev/null @@ -1,57 +0,0 @@ -require_relative "./command_line/parse_context_arguments" - -module Matrixeval - module Ruby - COMMANDS = [ - 'ruby', 'rake', 'rails', 'rspec', 'bundle', - 'bin/rake', 'bin/rails', 'bin/rspec', 'bin/test', - 'bash', 'dash', 'sh', 'zsh' - ] - - class CommandLine - - attr_reader :argv - - def initialize(argv) - @argv = argv - end - - def valid? - init? || - !context_options.empty? || - !seperator_index.nil? - end - - def init? - @argv[0] == 'init' - end - - def all? - context_options[:all] - end - - def context_options - @context_options ||= ParseContextArguments.call(context_arguments) - end - - def context_arguments - arguments = @argv[0...seperator_index] - arguments << "-h" if @argv.empty? - arguments - end - - def rest_arguments - @argv[seperator_index..-1] - end - - private - - def seperator_index - @argv.index do |argument| - Config.commands.include?(argument) - end - end - - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/command_line/parse_context_arguments.rb b/lib/matrixeval/ruby/command_line/parse_context_arguments.rb deleted file mode 100644 index a1c2a09..0000000 --- a/lib/matrixeval/ruby/command_line/parse_context_arguments.rb +++ /dev/null @@ -1,85 +0,0 @@ -module Matrixeval - module Ruby - class CommandLine - class ParseContextArguments - class << self - def call(context_arguments) - new(context_arguments).call - end - end - - attr_reader :context_arguments, :options - - def initialize(context_arguments) - @context_arguments = context_arguments - @options = {} - end - - def call - parse! - options - end - - private - - def parse! - OptionParser.new do |opts| - opts.version = Matrixeval::Ruby::VERSION - opts.program_name = "" - opts.banner = <<~USAGE - Usage: - matrixeval(meval) [OPTIONS] COMMAND - USAGE - - opts.separator "" - opts.separator "Options:" - - opts.on "-a", "--all", "# Run the COMMAND against all matrix combinations" - - Config.vectors.each do |vector| - # short = "-#{vector.short_key}" - long = "--#{vector.key} [VERSION]" - desc = [ - "# Run the COMMAND against a specific #{vector.key} version", - "# Options: #{vector.variants.map(&:key).join("/")}", - "# Default: #{vector.default_variant.key}", - "# Customizable" - ] - opts.separator "" - opts.on(long, *desc) - end - - opts.separator "" - opts.separator "Commands: #{Config.commands.join("/")} (Customizable)" - - opts.separator "" - opts.separator "MatrixEval Options:" - - opts.on("-h", "--help", "# Show help") do - puts opts.help - exit - end - - opts.on("-v", "--version", "# Show version") do - puts opts.version - exit - end - - opts.separator "" - opts.separator "Customizations:" - opts.separator " You can customize all options in matrixeval.yml" - - opts.separator "" - opts.separator "Example:" - opts.separator " matrixeval --all bundle install" - opts.separator " matrixeval --ruby 3.0 rspec a_spec.rb" - opts.separator " matrixeval --ruby 3.1 --active_model 7.0 rake test" - opts.separator " matrixeval bash" - - end.parse!(context_arguments, into: options) - end - - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/config.rb b/lib/matrixeval/ruby/config.rb deleted file mode 100644 index 5178fd2..0000000 --- a/lib/matrixeval/ruby/config.rb +++ /dev/null @@ -1,98 +0,0 @@ -require 'yaml' -require_relative "./vector" -require_relative "./config/yaml" -require_relative "./docker_compose/extend_raw" -require_relative "./docker_compose/extend" - -module Matrixeval - module Ruby - class Config - class << self - - def version - YAML["version"] - end - - def target - YAML["target"] - end - - def project_name - name = YAML["project_name"] - - if name.nil? || name.strip.empty? - raise Error.new('missing project_name') - end - - name - end - - def vectors - @vectors = YAML["matrix"].map do |key, vector_config| - Vector.new(key, vector_config) - end - end - - def main_vector - vectors.find(&:main?) - end - - def rest_vectors - vectors.reject(&:main?) - end - - def variant_combinations - main_vector_variants.product(*rest_vector_variants_matrix) - end - - def main_vector_variants - main_vector.variants - end - - def rest_vector_variants_matrix - rest_vectors.map(&:variants) - end - - def exclusions - YAML["exclude"] || [] - end - - def parallel_workers - YAML["parallel_workers"] || "number_of_processors" - end - - def commands - cmds = YAML["commands"] || [] - COMMANDS + cmds - end - - def docker_compose_extend_raw - DockerCompose::ExtendRaw.new( - YAML["docker-compose-extend"] || {} - ) - end - - def env - YAML["env"] || {} - end - - def mounts - YAML["mounts"] || [] - end - - def all_mounts - mounts + all_variant_mounts - end - - private - - def all_variant_mounts - Config.vectors - .map(&:variants).flatten - .map(&:mounts).flatten - end - - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/config/yaml.rb b/lib/matrixeval/ruby/config/yaml.rb deleted file mode 100644 index b7d5fae..0000000 --- a/lib/matrixeval/ruby/config/yaml.rb +++ /dev/null @@ -1,40 +0,0 @@ -module Matrixeval - module Ruby - class Config - class YAML - - class MissingError < StandardError; end - - class << self - - def create - return if File.exist?(path) - - FileUtils.cp(template_path, path) - end - - def template_path - Matrixeval::Ruby.root.join( - "lib/matrixeval/ruby/templates/matrixeval.yml" - ) - end - - def path - Matrixeval.working_dir.join("matrixeval.yml") - end - - def [](key) - yaml[key] - end - - def yaml - raise MissingError unless File.exist?(path) - - ::YAML.load File.read(path) - end - - end - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/container.rb b/lib/matrixeval/ruby/container.rb deleted file mode 100644 index 83ffee4..0000000 --- a/lib/matrixeval/ruby/container.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Matrixeval - module Ruby - class Container - - attr_reader :image, :env - - def initialize(options) - options ||= {} - @image = options["image"] - @env = options["env"] || {} - end - - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/context.rb b/lib/matrixeval/ruby/context.rb deleted file mode 100644 index a5e0416..0000000 --- a/lib/matrixeval/ruby/context.rb +++ /dev/null @@ -1,86 +0,0 @@ -require_relative "./context/find_by_command_options" -require_relative "./context/build_docker_compose_extend" - -module Matrixeval - module Ruby - class Context - - class << self - - def find_by_command_options!(options) - FindByCommandOptions.call(options) - end - - def all - Config.variant_combinations.map do |variants| - Context.new( - main_variant: variants.find { |v| v.vector.main? }, - rest_variants: variants.reject { |v| v.vector.main? } - ) - end.select do |context| - Config.exclusions.none? do |exclusion| - context.match_exclusion?(exclusion) - end - end - end - - end - - attr_reader :main_variant, :rest_variants - - def initialize(main_variant:, rest_variants:) - @main_variant = main_variant - @rest_variants = (rest_variants || []).sort do |v1, v2| - v1.id <=> v2.id - end - end - - def name - variants.map(&:name).join(", ") - end - - def id - [[main_variant.id] + rest_variants.map(&:id)].join("_") - end - - def env - rest_variants.map(&:env).reduce({}, &:merge) - .merge(main_variant.env) - end - - def docker_compose_service_name - main_variant.id - end - - def gemfile_lock_path - Matrixeval.working_dir.join(".matrixeval/gemfile_locks/#{id}") - end - - def docker_compose_file_path - Matrixeval.working_dir.join(".matrixeval/docker-compose/#{id}.yml") - end - - def variants - [main_variant] + rest_variants - end - - def match_exclusion?(exclusion) - return false if exclusion.empty? - - variants.all? do |variant| - vector_key = variant.vector.key - if exclusion.key?(vector_key) - exclusion[vector_key].to_s == variant.key - else - true - end - end - end - - def docker_compose_extend - BuildDockerComposeExtend.call(self) - end - - end - end -end diff --git a/lib/matrixeval/ruby/context/build_docker_compose_extend.rb b/lib/matrixeval/ruby/context/build_docker_compose_extend.rb deleted file mode 100644 index d1e3ca5..0000000 --- a/lib/matrixeval/ruby/context/build_docker_compose_extend.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'erb' -require 'json' - -module Matrixeval - module Ruby - class Context - class BuildDockerComposeExtend - class << self - def call(context) - new(context).call - end - end - - attr_reader :context - - def initialize(context) - @context = context - end - - def matrix_combination_id - context.id - end - - def call - DockerCompose::Extend.new(docker_compose_extend) - end - - private - - def docker_compose_extend - JSON.parse(render_erb) - end - - def render_erb - ERB.new( - Config.docker_compose_extend_raw.content - ).result(binding) - end - - end - end - end -end diff --git a/lib/matrixeval/ruby/context/find_by_command_options.rb b/lib/matrixeval/ruby/context/find_by_command_options.rb deleted file mode 100644 index 7f40e52..0000000 --- a/lib/matrixeval/ruby/context/find_by_command_options.rb +++ /dev/null @@ -1,67 +0,0 @@ -module Matrixeval - module Ruby - class Context - class FindByCommandOptions - class << self - def call(options) - new(options).call - end - end - - attr_reader :options - - def initialize(options) - @options = options - end - - def call - context = Context.all.find do |context| - context.main_variant == main_variant && - context.rest_variants == rest_variants - end - - raise Error.new("Can't find a corresponding matrix") if context.nil? - - context - end - - private - - def main_variant - dig_variant Config.main_vector - end - - def rest_variants - Config.rest_vectors.map do |vector| - dig_variant vector - end.sort do |v1, v2| - v1.id <=> v2.id - end - end - - def dig_variant(vector) - if option_key?(vector.key) - find_variant(vector) - else - vector.default_variant - end - end - - def find_variant(vector) - vector.variants.find do |variant| - option(vector.key) == variant.key - end - end - - def option(key) - options[key.to_sym] || options[key.to_s] - end - - def option_key?(key) - options.key?(key.to_sym) || options.key?(key.to_s) - end - - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/docker_compose.rb b/lib/matrixeval/ruby/docker_compose.rb deleted file mode 100644 index dc9ca66..0000000 --- a/lib/matrixeval/ruby/docker_compose.rb +++ /dev/null @@ -1,68 +0,0 @@ - -require_relative "./docker_compose/file" - -module Matrixeval - module Ruby - class DockerCompose - - attr_reader :context - - def initialize(context) - @context = context - end - - def run(arguments) - forward_arguments = arguments.map do |arg| - arg.match(/\s/) ? "\"#{arg}\"" : arg - end.join(" ") - - no_tty = %w[bash sh zsh dash].include?(arguments[0]) ? '' : '--no-TTY' - - system( - <<~DOCKER_COMPOSE_COMMAND - #{docker_compose} \ - run --rm \ - #{no_tty} \ - #{context.docker_compose_service_name} \ - #{forward_arguments} - DOCKER_COMPOSE_COMMAND - ) - ensure - stop_containers - clean_containers_and_anonymous_volumes - turn_on_stty_opost - end - - private - - def stop_containers - system("#{docker_compose} stop >> /dev/null 2>&1") - end - - def clean_containers_and_anonymous_volumes - system("#{docker_compose} rm -v -f >> /dev/null 2>&1") - end - - def docker_compose - <<~DOCKER_COMPOSE_COMMAND.strip - docker --log-level error compose \ - -f #{yaml_file} \ - -p matrixeval-#{project_name}-#{context.id} - DOCKER_COMPOSE_COMMAND - end - - def yaml_file - ".matrixeval/docker-compose/#{context.id}.yml" - end - - def turn_on_stty_opost - system("stty opost") - end - - def project_name - Config.project_name.gsub(/[^A-Za-z0-9-]/,'_').downcase - end - - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/docker_compose/extend.rb b/lib/matrixeval/ruby/docker_compose/extend.rb deleted file mode 100644 index 4910e42..0000000 --- a/lib/matrixeval/ruby/docker_compose/extend.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Matrixeval - module Ruby - class DockerCompose - class Extend - - def initialize(config) - @config = config || {} - end - - def volumes - @config["volumes"] || {} - end - - def services - @config["services"] || {} - end - - end - end - end -end diff --git a/lib/matrixeval/ruby/docker_compose/extend_raw.rb b/lib/matrixeval/ruby/docker_compose/extend_raw.rb deleted file mode 100644 index 15d8e24..0000000 --- a/lib/matrixeval/ruby/docker_compose/extend_raw.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'json' - -module Matrixeval - module Ruby - class DockerCompose - class ExtendRaw - - def initialize(config) - @config = config || {} - end - - def content - @config.to_json - end - - end - end - end -end diff --git a/lib/matrixeval/ruby/docker_compose/file.rb b/lib/matrixeval/ruby/docker_compose/file.rb deleted file mode 100644 index 4d03840..0000000 --- a/lib/matrixeval/ruby/docker_compose/file.rb +++ /dev/null @@ -1,135 +0,0 @@ -require "erb" - -module Matrixeval - module Ruby - class DockerCompose - class File - class << self - - def create_all - FileUtils.mkdir_p folder - - Context.all.each do |context| - new(context).create - end - end - - private - - def folder - Matrixeval.working_dir.join(".matrixeval/docker-compose") - end - end - - attr_reader :context - - def initialize(context) - @context = context - end - - def create - ::File.open(docker_compose_file_path, 'w+') do |file| - file.puts build_content - end - end - - private - - def docker_compose_file_path - context.docker_compose_file_path - end - - def build_content - { - "version" => "3", - "services" => services_json, - "volumes" => volumes_json - }.to_yaml.sub(/---\n/, "") - end - - def services_json - services = {} - - services[main_variant.docker_compose_service_name] = { - "image" => main_variant.container.image, - "volumes" => mounts(main_variant), - "environment" => { - "BUNDLE_PATH" => "/bundle", - "GEM_HOME" => "/bundle", - "BUNDLE_APP_CONFIG" => "/bundle", - "BUNDLE_BIN" => "/bundle/bin", - "PATH" => "/app/bin:/bundle/bin:$PATH" - }.merge(extra_env), - "working_dir" => "/app" - }.merge(depends_on) - - services.merge(docker_compose_extend.services) - end - - def volumes_json - { - bundle_volume => { - "name" => bundle_volume - } - }.merge(docker_compose_extend.volumes) - end - - def depends_on - if docker_compose_extend.services.keys.empty? - {} - else - { "depends_on" => docker_compose_extend.services.keys } - end - end - - def extra_env - Config.env.merge(context.env) - .merge(main_variant.container.env) - end - - def main_variant - context.main_variant - end - - def bundle_volume - main_variant.bundle_volume_name - end - - def mounts(variant) - [ - "../..:/app:cached", - "#{variant.bundle_volume_name}:/bundle", - "../gemfile_locks/#{context.id}:/app/Gemfile.lock" - ] + extra_mounts - end - - def extra_mounts - mounts = Config.mounts + context.variants.map(&:mounts).flatten - mounts.map do |mount| - local_path, in_docker_path = mount.split(':') - next mount if Pathname.new(local_path).absolute? - - local_path = Matrixeval.working_dir.join(local_path) - docker_compose_folder_path = Matrixeval.working_dir.join(".matrixeval/docker-compose") - local_path = local_path.relative_path_from docker_compose_folder_path - - "#{local_path}:#{in_docker_path}" - end - end - - def docker_compose_extend - @docker_compose_extend ||= context.docker_compose_extend - end - - def working_dir_name - Matrixeval.working_dir.basename - end - - def project_name - Config.project_name.gsub(/[^A-Za-z0-9-]/,'_').downcase - end - - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/extra_mount_files.rb b/lib/matrixeval/ruby/extra_mount_files.rb deleted file mode 100644 index 3d4781c..0000000 --- a/lib/matrixeval/ruby/extra_mount_files.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Matrixeval - module Ruby - class ExtraMountFiles - class << self - - def create - Config.all_mounts.each do |mount| - local_path, _ = mount.split(':') - next mount if Pathname.new(local_path).absolute? - - local_path = Matrixeval.working_dir.join(local_path) - next if local_path.extname.empty? - next if local_path.ascend.none? { |path| path == Matrixeval.working_dir } - - FileUtils.mkdir_p local_path.dirname - FileUtils.touch local_path - end - end - - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/gemfile_locks.rb b/lib/matrixeval/ruby/gemfile_locks.rb deleted file mode 100644 index 89b61d8..0000000 --- a/lib/matrixeval/ruby/gemfile_locks.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Matrixeval - module Ruby - class GemfileLocks - class << self - - def create - FileUtils.mkdir_p gemfile_lock_folder - - Context.all.each do |context| - FileUtils.touch context.gemfile_lock_path - end - end - - private - - def gemfile_lock_folder - Matrixeval.working_dir.join(".matrixeval/gemfile_locks") - end - - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/gitignore.rb b/lib/matrixeval/ruby/gitignore.rb deleted file mode 100644 index 000e669..0000000 --- a/lib/matrixeval/ruby/gitignore.rb +++ /dev/null @@ -1,54 +0,0 @@ -module Matrixeval - module Ruby - class Gitignore - class << self - - def update - add_docker_compose - add_gemfile_locks - end - - private - - def add_docker_compose - return if docker_compose_included? - - File.open(gitignore_path, 'a+') do |file| - file.puts docker_compose - end - end - - def add_gemfile_locks - return if gemfile_locks_included? - - File.open(gitignore_path, 'a+') do |file| - file.puts gemfile_locks - end - end - - def docker_compose_included? - File.exist?(gitignore_path) && - File.read(gitignore_path).include?(docker_compose) - end - - def gemfile_locks_included? - File.exist?(gitignore_path) && - File.read(gitignore_path).include?(gemfile_locks) - end - - def docker_compose - ".matrixeval/docker-compose" - end - - def gemfile_locks - ".matrixeval/gemfile_locks" - end - - def gitignore_path - Matrixeval.working_dir.join(".gitignore") - end - - end - end - end -end \ No newline at end of file diff --git a/lib/matrixeval/ruby/runner.rb b/lib/matrixeval/ruby/runner.rb deleted file mode 100644 index 6725a5a..0000000 --- a/lib/matrixeval/ruby/runner.rb +++ /dev/null @@ -1,205 +0,0 @@ -require 'optparse' -require 'pathname' -require 'fileutils' -require 'matrixeval/ruby/config' -require 'matrixeval/ruby/command_line' -require "concurrent/utility/processor_counter" -require 'terminal-table' - -module Matrixeval - module Ruby - class Runner - class << self - def start(argv) - new(argv).start - end - end - - attr_reader :argv, :command - - def initialize(argv) - @argv = argv - @command = CommandLine.new(argv) - @threads ||= [] - @matrixeval_results ||= [] - end - - def start - validates - - if command.init? - init - elsif command.all? - run_all_contexts - else - run_a_specific_context - end - rescue OptionParser::InvalidOption => e - puts <<~ERROR - #{e.message} - See 'matrixeval --help' - ERROR - exit - rescue Config::YAML::MissingError - puts "Please run 'matrixeval init' first to generate matrixeval.yml" - exit - ensure - turn_on_stty_opost - end - - private - - def validates - return if command.valid? - - puts <<~ERROR - matrixeval: '#{argv.join(' ')}' is not a MatrixEval command. - See 'matrixeval --help' - ERROR - exit - end - - def init - Config::YAML.create - Gitignore.update - end - - def run_all_contexts - Config::YAML.create - DockerCompose::File.create_all - GemfileLocks.create - Gitignore.update - ExtraMountFiles.create - - pull_all_images - - if workers_count == 1 - run_all_contexts_sequentially - else - run_all_contexts_in_parallel - end - end - - def run_all_contexts_sequentially - Context.all.each do |context| - puts Rainbow("[ MatrixEval ] ").blue.bright + Rainbow(" #{context.name} ").white.bright.bg(:blue) - puts Rainbow("[ MatrixEval ] Run \"#{command.rest_arguments.join(" ")}\"").blue.bright - - docker_compose = DockerCompose.new(context) - success = docker_compose.run(command.rest_arguments) - - @matrixeval_results << [context, !!success] - end - - report - end - - def run_all_contexts_in_parallel - parallel(contexts) do |sub_contexts| - Thread.current[:matrixeval_results] = [] - - sub_contexts.each do |context| - docker_compose = DockerCompose.new(context) - success = docker_compose.run(command.rest_arguments) - - Thread.current[:matrixeval_results] << [context, !!success] - end - end - - report - end - - def run_a_specific_context - Config::YAML.create - DockerCompose::File.create_all - GemfileLocks.create - Gitignore.update - ExtraMountFiles.create - - context = Context.find_by_command_options!(command.context_options) - - puts Rainbow("[ MatrixEval ] ").blue.bright + Rainbow(" #{context.name} ").white.bright.bg(:blue) - puts Rainbow("[ MatrixEval ] Run \"#{command.rest_arguments.join(" ")}\"").blue.bright - - docker_compose = DockerCompose.new(context) - docker_compose.run(command.rest_arguments) - end - - def pull_all_images - parallel(Config.main_vector_variants) do |sub_variants| - sub_variants.each do |variant| - puts "Docker image check/pull #{variant.container.image}" - image_exists = system %Q{[ -n "$(docker images -q #{variant.container.image})" ]} - next if image_exists - - system "docker pull #{variant.container.image}" - end - end - end - - def report - turn_on_stty_opost - - table = Terminal::Table.new(title: Rainbow("MatrixEval").blue.bright + " Summary", alignment: :center) do |table| - - headers = Config.vectors.map(&:key) + ['result'] - table.add_row headers.map { |value| { value: value, alignment: :center } } - table.add_separator - - @matrixeval_results.each do |context, success| - success_cell = [success ? Rainbow('Success').green : Rainbow('Failed').red] - row = (context.variants.map(&:key) + success_cell).map do |value| - { value: value, alignment: :center } - end - - table.add_row row - end - - end - - puts table - end - - def parallel(collection) - @threads = [] unless @threads.empty? - @matrixeval_results = [] unless @matrixeval_results.empty? - - collection.each_slice(per_worker_contexts_count) do |sub_collection| - @threads << Thread.new do - yield sub_collection - end - end - - @threads.each(&:join) - - @threads.each do |thread| - @matrixeval_results += (thread[:matrixeval_results] || []) - end - end - - - def per_worker_contexts_count - [(contexts.count / workers_count), 1].max - end - - def contexts - @contexts ||= Context.all - end - - def workers_count - count = if Config.parallel_workers == "number_of_processors" - Concurrent.physical_processor_count - else - Integer(Config.parallel_workers) - end - - [count, 1].max - end - - def turn_on_stty_opost - system("stty opost") - end - - end - end -end diff --git a/lib/matrixeval/ruby/target.rb b/lib/matrixeval/ruby/target.rb new file mode 100644 index 0000000..b16a5a1 --- /dev/null +++ b/lib/matrixeval/ruby/target.rb @@ -0,0 +1,87 @@ +module Matrixeval + module Ruby + class Target < Matrixeval::Target + + def version + Matrixeval::Ruby::VERSION + end + + def matrixeval_yml_template_path + Matrixeval::Ruby.root.join("lib/matrixeval/ruby/templates/matrixeval.yml") + end + + def vector_key + "ruby" + end + + def env(context) + { + "BUNDLE_PATH" => "/bundle", + "GEM_HOME" => "/bundle", + "BUNDLE_APP_CONFIG" => "/bundle", + "BUNDLE_BIN" => "/bundle/bin", + "PATH" => "/app/bin:/bundle/bin:$PATH" + } + end + + def mounts(context) + bundle_volume = bundle_volume(context) + + [ + "#{bundle_volume}:/bundle", + "../gemfile_locks/#{context.id}:/app/Gemfile.lock" + ] + end + + def volumes(context) + bundle_volume = bundle_volume(context) + + { + bundle_volume => { + "name" => bundle_volume + } + } + end + + def gitignore_paths + [ + ".matrixeval/gemfile_locks" + ] + end + + def support_commands + [ + 'ruby', 'rake', 'rails', 'rspec', 'bundle', + 'bin/rake', 'bin/rails', 'bin/rspec', 'bin/test' + ] + end + + def cli_example_lines + [ + "", + "Example:", + " matrixeval --all bundle install", + " matrixeval --ruby 3.0 rspec a_spec.rb", + " matrixeval --ruby 3.1 --active_model 7.0 rake test", + " matrixeval bash" + ] + end + + def create_files + gemfile_lock_folder = Matrixeval.working_dir.join(".matrixeval/gemfile_locks") + FileUtils.mkdir_p gemfile_lock_folder + + Context.all.each do |context| + FileUtils.touch gemfile_lock_folder.join(context.id) + end + end + + private + + def bundle_volume(context) + docker_image = context.main_variant.container.image + "bundle_#{docker_image.gsub(/[^A-Za-z0-9]/,'_')}" + end + end + end +end diff --git a/lib/matrixeval/ruby/templates/matrixeval.yml b/lib/matrixeval/ruby/templates/matrixeval.yml index 168b830..7ef337c 100644 --- a/lib/matrixeval/ruby/templates/matrixeval.yml +++ b/lib/matrixeval/ruby/templates/matrixeval.yml @@ -1,6 +1,6 @@ -version: 0.3 -project_name: REPLACE_ME +version: 0.4 target: ruby +project_name: REPLACE_ME parallel_workers: number_of_processors # commands: # - ps diff --git a/lib/matrixeval/ruby/variant.rb b/lib/matrixeval/ruby/variant.rb deleted file mode 100644 index 1e3ea59..0000000 --- a/lib/matrixeval/ruby/variant.rb +++ /dev/null @@ -1,59 +0,0 @@ -require_relative "./container" - -module Matrixeval - module Ruby - class Variant - class << self - def default(key, vector) - self.new({"key" => key}, vector) - end - end - - attr_reader :key, :env, :vector, :default, :container, :mounts - - def initialize(config = {}, vector) - raise Error.new("Variant#key is missing") if config["key"].nil? - - @vector = vector - @key = config["key"].to_s - @container = Container.new(config["container"]) - @env = config["env"] || {} - @default = config["default"] || false - @mounts = config["mounts"] || [] - end - - def name - "#{vector.key}: #{key}" - end - - def bundle_volume_name - "bundle_#{container.image.gsub(/[^A-Za-z0-9]/,'_')}" - end - - def id - "#{vector.id}_#{key.to_s.gsub(/[^A-Za-z0-9]/,'_')}" - end - - def docker_compose_service_name - id - end - - def pathname - id - end - - def default? - default - end - - def match_command_options?(options) - options[vector.key] == key.to_s - end - - def ==(variant) - vector.key == variant.vector.key && - key == variant.key - end - end - end -end diff --git a/lib/matrixeval/ruby/vector.rb b/lib/matrixeval/ruby/vector.rb deleted file mode 100644 index 2e365f2..0000000 --- a/lib/matrixeval/ruby/vector.rb +++ /dev/null @@ -1,39 +0,0 @@ -require_relative "./variant" - -module Matrixeval - module Ruby - class Vector - attr_reader :key, :variants - - def initialize(key, config) - @key = key.to_s - @variants = (config["variants"] || []).map do |variant_config| - config = if variant_config.is_a?(Hash) - variant_config - else - { "key" => variant_config.to_s } - end - - Variant.new(config, self) - end - end - - def main? - key == "ruby" - end - - def id - "#{key.to_s.gsub(/[^A-Za-z0-9]/,'_')}" - end - - def default_variant - variant = variants.find(&:default?) - if variant.nil? - raise Error.new("Please set a default variant for matrix #{key}") - end - - variant - end - end - end -end diff --git a/lib/matrixeval/ruby/version.rb b/lib/matrixeval/ruby/version.rb index 49e3e8b..c0c7b36 100644 --- a/lib/matrixeval/ruby/version.rb +++ b/lib/matrixeval/ruby/version.rb @@ -1,7 +1,5 @@ -# frozen_string_literal: true - module Matrixeval module Ruby - VERSION = "0.3.1" + VERSION = '0.4.0' end end diff --git a/matrixeval-ruby.gemspec b/matrixeval-ruby.gemspec index e6fe931..128f7fc 100644 --- a/matrixeval-ruby.gemspec +++ b/matrixeval-ruby.gemspec @@ -1,5 +1,4 @@ # frozen_string_literal: true - require_relative "lib/matrixeval/ruby/version" Gem::Specification.new do |spec| @@ -25,14 +24,11 @@ Gem::Specification.new do |spec| (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)}) end end - spec.bindir = "exe" - spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + # spec.bindir = "exe" + # spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - # Uncomment to register a new dependency of your gem - spec.add_dependency "rainbow", "~> 3.1" - spec.add_dependency "concurrent-ruby" - spec.add_dependency "terminal-table" + spec.add_dependency "matrixeval", "~> 0.4.2" # For more information and examples about making a new gem, checkout our # guide at: https://bundler.io/guides/creating_gem.html diff --git a/test/matrixeval/ruby/command_line/parse_context_arguments_test.rb b/test/matrixeval/ruby/command_line/parse_context_arguments_test.rb deleted file mode 100644 index f8d35eb..0000000 --- a/test/matrixeval/ruby/command_line/parse_context_arguments_test.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::CommandLine::ParseContextArgumentsTest < MatrixevalTest - - def setup - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({ - "version" => "0.3", - "target" => "ruby", - "matrix" => { - "ruby" => { - "variants" => [ - { "key" => "3.0", "default" => true }, - { "key" => "3.1" } - ] - }, - "active_model" => { - "variants" => [ - { "key" => "6.1", "default" => true }, - { "key" => "7.0" } - ] - } - } - }) - end - - def test_call - context_arguments = ["--ruby", "3.0"] - options = Matrixeval::Ruby::CommandLine::ParseContextArguments.call(context_arguments) - assert_equal "3.0", options[:ruby] - assert_nil options[:active_model] - end - -end diff --git a/test/matrixeval/ruby/command_line_test.rb b/test/matrixeval/ruby/command_line_test.rb deleted file mode 100644 index 2c41162..0000000 --- a/test/matrixeval/ruby/command_line_test.rb +++ /dev/null @@ -1,67 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::CommandLineTest < MatrixevalTest - - def setup - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({"matrix" => {}}) - end - - def test_commands - commands = [ - 'ruby', 'rake', 'rails', 'rspec', 'bundle', - 'bin/rake', 'bin/rails', 'bin/rspec', 'bin/test', - 'bash', 'dash', 'sh', 'zsh' - ] - assert_equal commands, Matrixeval::Ruby::COMMANDS - end - - def test_init? - command = Matrixeval::Ruby::CommandLine.new(["init"]) - assert command.init? - - command = Matrixeval::Ruby::CommandLine.new(["rake", "test"]) - refute command.init? - end - - def test_all? - command = Matrixeval::Ruby::CommandLine.new(["--all", "rake", "test"]) - assert command.all? - - command = Matrixeval::Ruby::CommandLine.new(["-a", "rake", "test"]) - assert command.all? - - command = Matrixeval::Ruby::CommandLine.new(["rake", "test"]) - refute command.all? - end - - def test_context_arguments - command = Matrixeval::Ruby::CommandLine.new([ - "--ruby", "3.0", - "--active_model", "7.0.0", - "rake", "test" - ]) - assert_equal ["--ruby", "3.0", "--active_model", "7.0.0"], command.context_arguments - end - - def test_rest_arguments - command = Matrixeval::Ruby::CommandLine.new([ - "--ruby", "3.0", - "--active_model", "7.0.0", - "rake", "test" - ]) - assert_equal ["rake", "test"], command.rest_arguments - end - - def test_context_options - Matrixeval::Ruby::CommandLine::ParseContextArguments.expects(:call).with(["--ruby", "3.0"]).returns({ruby: "3.0"}) - - command = Matrixeval::Ruby::CommandLine.new([ - "--ruby", "3.0", - "rake", "test" - ]) - assert_equal({ruby: "3.0"}, command.context_options) - end - -end diff --git a/test/matrixeval/ruby/config/yaml_test.rb b/test/matrixeval/ruby/config/yaml_test.rb deleted file mode 100644 index 1ac4e66..0000000 --- a/test/matrixeval/ruby/config/yaml_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::Config::YAMLTest < MatrixevalTest - - def setup - FileUtils.rm(dummy_gem_matrixeval_file_path) rescue nil - Matrixeval.stubs(:working_dir).returns(dummy_gem_working_dir) - end - - def test_creating_dot_matrixeval_yaml - refute File.exist?(dummy_gem_matrixeval_file_path) - - Matrixeval::Ruby::Config::YAML.create - - assert File.exist?(dummy_gem_matrixeval_file_path) - end - - def test_not_delete_exising_dot_matrixeval_yaml - File.open(dummy_gem_matrixeval_file_path, "w+") do |file| - file.puts "Customizations" - end - - Matrixeval::Ruby::Config::YAML.create - - assert_equal "Customizations\n", File.read(dummy_gem_matrixeval_file_path) - end - - def test_square_brackets - yaml_content = { - "version" => "0.3", - "target" => "ruby", - "matrix" => { - "ruby" => { - "variants" => [ - { "key" => "3.0" }, - { "key" => "3.1" } - ] - }, - "active_model" => { - "variants" => [ - { "key" => "6.1" }, - { "key" => "7.0" } - ] - } - } - } - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns(yaml_content) - assert_equal "0.3", Matrixeval::Ruby::Config::YAML["version"] - assert_equal "ruby", Matrixeval::Ruby::Config::YAML["target"] - assert_equal "3.0", Matrixeval::Ruby::Config::YAML["matrix"]["ruby"]["variants"][0]["key"] - assert_equal "3.1", Matrixeval::Ruby::Config::YAML["matrix"]["ruby"]["variants"][1]["key"] - assert_equal "6.1", Matrixeval::Ruby::Config::YAML["matrix"]["active_model"]["variants"][0]["key"] - assert_equal "7.0", Matrixeval::Ruby::Config::YAML["matrix"]["active_model"]["variants"][1]["key"] - end - -end diff --git a/test/matrixeval/ruby/config_test.rb b/test/matrixeval/ruby/config_test.rb deleted file mode 100644 index 3933abf..0000000 --- a/test/matrixeval/ruby/config_test.rb +++ /dev/null @@ -1,148 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::ConfigTest < MatrixevalTest - - def setup - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({ - "version" => "0.3", - "target" => "ruby", - "project_name" => "sample app", - "mounts" => ["/a:/b"], - "matrix" => { - "ruby" => { - "variants" => [ - { "key" => "3.0" }, - { "key" => "3.1" } - ] - }, - "active_model" => { - "variants" => [ - { "key" => "6.1", "mounts" => [".matrixeval/schema/rails_6_1.rb:/app/test/dummy/db/schema.rb"] }, - { "key" => "7.0", "mounts" => [".matrixeval/schema/rails_7_0.rb:/app/test/dummy/db/schema.rb"] } - ] - } - }, - "exclude" => [ - { "ruby" => "3.0", "active_model" => "7.0" }, - { "ruby" => "3.1", "active_model" => "6.1" } - ], - "docker-compose-extend" => { - "volumes" => { - "postgres12-<%= matrix_combination_id %>" => nil - } - } - }) - end - - def test_version - assert_equal "0.3", Matrixeval::Ruby::Config.version - end - - def test_target - assert_equal "ruby", Matrixeval::Ruby::Config.target - end - - def test_project_name - assert_equal "sample app", Matrixeval::Ruby::Config.project_name - end - - def test_env - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({"env" => { 'A' => 'a' }}) - assert_equal({'A' => 'a'}, Matrixeval::Ruby::Config.env) - end - - def test_env_default - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({}) - assert_equal({}, Matrixeval::Ruby::Config.env) - end - - def test_mounts - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({"mounts" => ["/a:/b"]}) - assert_equal ["/a:/b"], Matrixeval::Ruby::Config.mounts - end - - def test_mounts_default - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({}) - assert_equal [], Matrixeval::Ruby::Config.mounts - end - - def test_all_mounts - assert_equal([ - "/a:/b", - ".matrixeval/schema/rails_6_1.rb:/app/test/dummy/db/schema.rb", - ".matrixeval/schema/rails_7_0.rb:/app/test/dummy/db/schema.rb" - ], Matrixeval::Ruby::Config.all_mounts) - end - - def test_vectors - vectors = Matrixeval::Ruby::Config.vectors - assert_equal 2, vectors.count - assert_equal "ruby", vectors[0].key - assert_equal "active_model", vectors[1].key - end - - def test_main_vector - assert_equal "ruby", Matrixeval::Ruby::Config.main_vector.key - end - - def test_rest_vectors - rest_vectors = Matrixeval::Ruby::Config.rest_vectors - assert_equal 1, rest_vectors.count - assert_equal "active_model", rest_vectors[0].key - end - - def test_variant_combinations - variant_combinations = Matrixeval::Ruby::Config.variant_combinations - assert_equal 4, variant_combinations.count - end - - def test_main_vector_variants - variants = Matrixeval::Ruby::Config.main_vector_variants - assert_equal 2, variants.count - assert_equal "3.0", variants[0].key - assert_equal "3.1", variants[1].key - end - - def test_rest_vector_variants_matrix - variants_matrix = Matrixeval::Ruby::Config.rest_vector_variants_matrix - assert_equal 1, variants_matrix.count - assert_equal 2, variants_matrix[0].count - assert_equal "6.1", variants_matrix[0][0].key - assert_equal "7.0", variants_matrix[0][1].key - end - - def test_exclusions - exclusions = [ - { "ruby" => "3.0", "active_model" => "7.0" }, - { "ruby" => "3.1", "active_model" => "6.1" } - ] - assert_equal exclusions, Matrixeval::Ruby::Config.exclusions - end - - def test_commands - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({}) - assert_equal([ - 'ruby', 'rake', 'rails', 'rspec', 'bundle', - 'bin/rake', 'bin/rails', 'bin/rspec', 'bin/test', - 'bash', 'dash', 'sh', 'zsh' - ], Matrixeval::Ruby::Config.commands) - - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({'commands' => ['ls']}) - assert_equal([ - 'ruby', 'rake', 'rails', 'rspec', 'bundle', - 'bin/rake', 'bin/rails', 'bin/rspec', 'bin/test', - 'bash', 'dash', 'sh', 'zsh', - 'ls' - ], Matrixeval::Ruby::Config.commands) - end - - def test_docker_compose_extend_raw - extend_raw = Matrixeval::Ruby::Config.docker_compose_extend_raw - assert extend_raw.is_a?(Matrixeval::Ruby::DockerCompose::ExtendRaw) - - assert_equal "{\"volumes\":{\"postgres12-<%= matrix_combination_id %>\":null}}", extend_raw.content - end - -end diff --git a/test/matrixeval/ruby/context/build_docker_compose_extend_test.rb b/test/matrixeval/ruby/context/build_docker_compose_extend_test.rb deleted file mode 100644 index eceb9a3..0000000 --- a/test/matrixeval/ruby/context/build_docker_compose_extend_test.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: true - -require 'test_helper' - -class Matrixeval::Ruby::Context::BuildDockerComposeExtendTest < MatrixevalTest - - def setup - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({ - "docker-compose-extend" => { - "volumes" => { - "postgres12-<%= matrix_combination_id %>" => nil - } - } - }) - - @ruby_vector = Matrixeval::Ruby::Vector.new("ruby", {"key" => "ruby"}) - @sidekiq_vector = Matrixeval::Ruby::Vector.new("sidekiq", {"key" => "sidekiq"}) - @rails_vector = Matrixeval::Ruby::Vector.new("rails", {"key" => "rails"}) - - @ruby_3_variant = Matrixeval::Ruby::Variant.new({"key" => "3.0"}, @ruby_vector) - @sidekiq_5_variant = Matrixeval::Ruby::Variant.new( - { - "key" => "5.0", - "env" => {"SIDEKIQ_VERSION" => "5.0.0"} - }, @sidekiq_vector) - - @rails_6_variant = Matrixeval::Ruby::Variant.new( - { - "key" => "6.1", - "env" => {"RAILS_VERSION" => "6.1.0"} - }, @rails_vector) - - @context = Matrixeval::Ruby::Context.new( - main_variant: @ruby_3_variant, - rest_variants: [@sidekiq_5_variant, @rails_6_variant] - ) - end - - def test_call - compose_extend = @context.docker_compose_extend - assert compose_extend.is_a?(Matrixeval::Ruby::DockerCompose::Extend) - assert_equal({ "postgres12-ruby_3_0_rails_6_1_sidekiq_5_0" => nil }, compose_extend.volumes) - end - -end diff --git a/test/matrixeval/ruby/context/find_by_command_options_test.rb b/test/matrixeval/ruby/context/find_by_command_options_test.rb deleted file mode 100644 index 820c1ab..0000000 --- a/test/matrixeval/ruby/context/find_by_command_options_test.rb +++ /dev/null @@ -1,52 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::Context::FindByCommandOptionsTest < MatrixevalTest - - def setup - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({ - "version" => "0.3", - "target" => "ruby", - "matrix" => { - "ruby" => { - "variants" => [ - { "key" => "3.0", "default" => true }, - { "key" => "3.1" } - ] - }, - "rails" => { - "variants" => [ - { "key" => "6.1" }, - { "key" => "7.0", "default" => true } - ] - }, - "sidekiq" => { - "variants" => [ - { "key" => "5.0" }, - { "key" => "6.0", "default" => true } - ] - } - } - }) - end - - def test_find_by_command_options - context = Matrixeval::Ruby::Context.find_by_command_options!({ruby: "3.1"}) - - assert context.is_a?(Matrixeval::Ruby::Context) - assert_equal "ruby", context.main_variant.vector.key - assert_equal "3.1", context.main_variant.key - assert_equal "rails", context.rest_variants[0].vector.key - assert_equal "7.0", context.rest_variants[0].key - assert_equal "sidekiq", context.rest_variants[1].vector.key - assert_equal "6.0", context.rest_variants[1].key - end - - def test_find_by_command_options_fail - assert_raises "Can't find a corresponding matrix" do - Matrixeval::Ruby::Context.find_by_command_options!({ruby: "wrong"}) - end - end - -end diff --git a/test/matrixeval/ruby/context_test.rb b/test/matrixeval/ruby/context_test.rb deleted file mode 100644 index 87a82ac..0000000 --- a/test/matrixeval/ruby/context_test.rb +++ /dev/null @@ -1,125 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::ContextTest < MatrixevalTest - - def setup - @ruby_vector = Matrixeval::Ruby::Vector.new("ruby", {"key" => "ruby"}) - @sidekiq_vector = Matrixeval::Ruby::Vector.new("sidekiq", {"key" => "sidekiq"}) - @rails_vector = Matrixeval::Ruby::Vector.new("rails", {"key" => "rails"}) - - @ruby_3_variant = Matrixeval::Ruby::Variant.new({"key" => "3.0"}, @ruby_vector) - @sidekiq_5_variant = Matrixeval::Ruby::Variant.new( - { - "key" => "5.0", - "env" => {"SIDEKIQ_VERSION" => "5.0.0"} - }, @sidekiq_vector) - - @rails_6_variant = Matrixeval::Ruby::Variant.new( - { - "key" => "6.1", - "env" => {"RAILS_VERSION" => "6.1.0"} - }, @rails_vector) - - @context = Matrixeval::Ruby::Context.new( - main_variant: @ruby_3_variant, - rest_variants: [@sidekiq_5_variant, @rails_6_variant] - ) - end - - def test_main_variant - assert @context.main_variant.is_a?(Matrixeval::Ruby::Variant) - assert_equal "ruby", @context.main_variant.vector.key - assert_equal "3.0", @context.main_variant.key - end - - def test_rest_variants - assert_equal 2, @context.rest_variants.count - assert_equal "rails", @context.rest_variants[0].vector.key - assert_equal "6.1", @context.rest_variants[0].key - assert_equal "sidekiq", @context.rest_variants[1].vector.key - assert_equal "5.0", @context.rest_variants[1].key - end - - def test_id - assert_equal "ruby_3_0_rails_6_1_sidekiq_5_0", @context.id - end - - def test_env - expected_env = { - "RAILS_VERSION" => "6.1.0", - "SIDEKIQ_VERSION" => "5.0.0" - } - assert_equal expected_env, @context.env - end - - def test_docker_compose_service_name - assert_equal "ruby_3_0", @context.docker_compose_service_name - end - - def test_gemfile_lock_path - Matrixeval.stubs(:working_dir).returns(Pathname.new("working_dir")) - - assert_equal "working_dir/.matrixeval/gemfile_locks/ruby_3_0_rails_6_1_sidekiq_5_0", @context.gemfile_lock_path.to_s - end - - def test_docker_compose_file_path - Matrixeval.stubs(:working_dir).returns(Pathname.new("working_dir")) - - assert_equal "working_dir/.matrixeval/docker-compose/ruby_3_0_rails_6_1_sidekiq_5_0.yml", @context.docker_compose_file_path.to_s - end - - def test_variants - assert_equal 3, @context.variants.count - assert_equal "ruby", @context.variants[0].vector.key - assert_equal "3.0", @context.variants[0].key - assert_equal "rails", @context.variants[1].vector.key - assert_equal "6.1", @context.variants[1].key - assert_equal "sidekiq", @context.variants[2].vector.key - assert_equal "5.0", @context.variants[2].key - end - - def test_match_exclusion - assert @context.match_exclusion?({ "ruby" => 3.0, "rails" => "6.1" }) - assert @context.match_exclusion?({ "ruby" => "3.0", "rails" => 6.1, "sidekiq" => "5.0" }) - assert @context.match_exclusion?({ "rails" => "6.1", "sidekiq" => "5.0" }) - refute @context.match_exclusion?({ "ruby" => 2.7, "rails" => "6.1" }) - refute @context.match_exclusion?({ "sidekiq" => "6.0"}) - refute @context.match_exclusion?({}) - end - - def test_all - Matrixeval::Ruby::Config.stubs(:variant_combinations).returns([ - [ - @ruby_3_variant, - @sidekiq_5_variant, - @rails_6_variant - ] - ]) - Matrixeval::Ruby::Config.stubs(:exclusions).returns([]) - - contexts = Matrixeval::Ruby::Context.all - - assert_equal 1, contexts.count - assert contexts[0].is_a?(Matrixeval::Ruby::Context) - assert_equal @ruby_3_variant, contexts[0].main_variant - assert_equal [@rails_6_variant, @sidekiq_5_variant], contexts[0].rest_variants - end - - def test_all_with_exclusions - Matrixeval::Ruby::Config.stubs(:variant_combinations).returns([ - [ - @ruby_3_variant, - @sidekiq_5_variant, - @rails_6_variant - ] - ]) - Matrixeval::Ruby::Config.stubs(:exclusions).returns([{ "ruby" => "3.0", "rails" => "6.1" }]) - - contexts = Matrixeval::Ruby::Context.all - - assert_equal 0, contexts.count - end - -end diff --git a/test/matrixeval/ruby/docker_compose/extend_raw_test.rb b/test/matrixeval/ruby/docker_compose/extend_raw_test.rb deleted file mode 100644 index 64a8bb7..0000000 --- a/test/matrixeval/ruby/docker_compose/extend_raw_test.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require 'test_helper' - -class Matrixeval::Ruby::DockerCompose::ExtendRawTest < MatrixevalTest - - def setup - @extend_raw = Matrixeval::Ruby::DockerCompose::ExtendRaw.new({ - "volumes" => { - "postgres12-<%= matrix_combination_id %>" => nil - } - }) - # @docker_compose_extend = Matrixeval::Ruby::DockerCompose::ExtendRaw.new({ - # 'services' => [ - # { - # "postgres" => { - # "image" => "postgres:12.8", - # "volumes" => [ - # "postgres12-<%= matrix_combination_id %>:/var/lib/postgresql/data" - # ], - # "environment" => [ - # "POSTGRES_HOST_AUTH_METHOD" => 'trust' - # ] - # } - # }, - # { - # "redis" => { - # "image" => "redis:6.2-alpine" - # } - # } - # ], - # "volumes" => { - # "postgres12-<%= matrix_combination_id %>" => nil - # } - # }) - end - - def test_content - assert_equal "{\"volumes\":{\"postgres12-<%= matrix_combination_id %>\":null}}", @extend_raw.content - end - -end diff --git a/test/matrixeval/ruby/docker_compose/extend_test.rb b/test/matrixeval/ruby/docker_compose/extend_test.rb deleted file mode 100644 index ed28197..0000000 --- a/test/matrixeval/ruby/docker_compose/extend_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require 'test_helper' - -class Matrixeval::Ruby::DockerCompose::ExtendTest < MatrixevalTest - - def setup - @compose_extend = Matrixeval::Ruby::DockerCompose::Extend.new( - { - "volumes" => { - "postgres12-ruby_3_1_rails_6_0" => nil - } - } - ) - end - - def test_volumes - assert_equal({ "postgres12-ruby_3_1_rails_6_0" => nil }, @compose_extend.volumes) - end - - def test_services - assert_equal({}, @compose_extend.services) - end - -end diff --git a/test/matrixeval/ruby/docker_compose/file_test.rb b/test/matrixeval/ruby/docker_compose/file_test.rb deleted file mode 100644 index f964bb5..0000000 --- a/test/matrixeval/ruby/docker_compose/file_test.rb +++ /dev/null @@ -1,216 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::DockerCompose::FileTest < MatrixevalTest - - def setup - FileUtils.rm_rf(dummy_gem_docker_compose_folder_path) rescue nil - Matrixeval.stubs(:working_dir).returns(dummy_gem_working_dir) - end - - def test_create_all - refute File.exist?(dummy_gem_docker_compose_folder_path) - - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({ - "version" => "0.3", - "target" => "ruby", - "project_name" => "Dummy_gem", - "mounts" => ["/a/b:/app/c/d"], - "env" => { - "DATABASE_HOST" => "postgres" - }, - "matrix" => { - "ruby" => { - "variants" => [ - { "key" => "3.0", "container" => { "image" => "ruby:3.0.0" }, "default" => true }, - { "key" => "3.1", "container" => { "image" => "ruby:3.1.0" } } - ] - }, - "active_model" => { - "variants" => [ - { - "key" => "6.1", - "env" => { "ACTIVE_MODEL_VERSION" => "6.1.4" }, - "default" => true, - "mounts" => [ - ".matrixeval/schema/rails_6_1.rb:/app/test/dummy/db/schema.rb" - ] - }, - { - "key" => "7.0", - "env" => { "ACTIVE_MODEL_VERSION" => "7.0.0" }, - "mounts" => [ - ".matrixeval/schema/rails_7_0.rb:/app/test/dummy/db/schema.rb" - ] - } - ] - } - }, - "docker-compose-extend" => { - "services" => { - "postgres" => { - "image" => "postgres:12.8", - "volumes" => [ - "postgres12:/var/lib/postgresql/data" - ], - "environment" => { - "POSTGRES_HOST_AUTH_METHOD" => "trust" - } - } - }, - "volumes" => { - "postgres12" => nil - } - } - }) - Matrixeval::Ruby::DockerCompose::File.create_all - - file_content = File.read(dummy_gem_docker_compose_folder_path.join("ruby_3_0_active_model_6_1.yml")) - - expect_file_content = <<~DOCKER_COMPOSE - version: '3' - services: - ruby_3_0: - image: ruby:3.0.0 - volumes: - - "../..:/app:cached" - - bundle_ruby_3_0_0:/bundle - - "../gemfile_locks/ruby_3_0_active_model_6_1:/app/Gemfile.lock" - - "/a/b:/app/c/d" - - "../schema/rails_6_1.rb:/app/test/dummy/db/schema.rb" - environment: - BUNDLE_PATH: "/bundle" - GEM_HOME: "/bundle" - BUNDLE_APP_CONFIG: "/bundle" - BUNDLE_BIN: "/bundle/bin" - PATH: "/app/bin:/bundle/bin:$PATH" - DATABASE_HOST: postgres - ACTIVE_MODEL_VERSION: 6.1.4 - working_dir: "/app" - depends_on: - - postgres - postgres: - image: postgres:12.8 - volumes: - - postgres12:/var/lib/postgresql/data - environment: - POSTGRES_HOST_AUTH_METHOD: trust - volumes: - bundle_ruby_3_0_0: - name: bundle_ruby_3_0_0 - postgres12: - DOCKER_COMPOSE - assert_equal expect_file_content.strip, file_content.strip - - file_content = File.read(dummy_gem_docker_compose_folder_path.join("ruby_3_0_active_model_7_0.yml")) - expect_file_content = <<~DOCKER_COMPOSE - version: '3' - services: - ruby_3_0: - image: ruby:3.0.0 - volumes: - - "../..:/app:cached" - - bundle_ruby_3_0_0:/bundle - - "../gemfile_locks/ruby_3_0_active_model_7_0:/app/Gemfile.lock" - - "/a/b:/app/c/d" - - "../schema/rails_7_0.rb:/app/test/dummy/db/schema.rb" - environment: - BUNDLE_PATH: "/bundle" - GEM_HOME: "/bundle" - BUNDLE_APP_CONFIG: "/bundle" - BUNDLE_BIN: "/bundle/bin" - PATH: "/app/bin:/bundle/bin:$PATH" - DATABASE_HOST: postgres - ACTIVE_MODEL_VERSION: 7.0.0 - working_dir: "/app" - depends_on: - - postgres - postgres: - image: postgres:12.8 - volumes: - - postgres12:/var/lib/postgresql/data - environment: - POSTGRES_HOST_AUTH_METHOD: trust - volumes: - bundle_ruby_3_0_0: - name: bundle_ruby_3_0_0 - postgres12: - DOCKER_COMPOSE - assert_equal expect_file_content.strip, file_content.strip - - file_content = File.read(dummy_gem_docker_compose_folder_path.join("ruby_3_1_active_model_6_1.yml")) - expect_file_content = <<~DOCKER_COMPOSE - version: '3' - services: - ruby_3_1: - image: ruby:3.1.0 - volumes: - - "../..:/app:cached" - - bundle_ruby_3_1_0:/bundle - - "../gemfile_locks/ruby_3_1_active_model_6_1:/app/Gemfile.lock" - - "/a/b:/app/c/d" - - "../schema/rails_6_1.rb:/app/test/dummy/db/schema.rb" - environment: - BUNDLE_PATH: "/bundle" - GEM_HOME: "/bundle" - BUNDLE_APP_CONFIG: "/bundle" - BUNDLE_BIN: "/bundle/bin" - PATH: "/app/bin:/bundle/bin:$PATH" - DATABASE_HOST: postgres - ACTIVE_MODEL_VERSION: 6.1.4 - working_dir: "/app" - depends_on: - - postgres - postgres: - image: postgres:12.8 - volumes: - - postgres12:/var/lib/postgresql/data - environment: - POSTGRES_HOST_AUTH_METHOD: trust - volumes: - bundle_ruby_3_1_0: - name: bundle_ruby_3_1_0 - postgres12: - DOCKER_COMPOSE - assert_equal expect_file_content.strip, file_content.strip - - file_content = File.read(dummy_gem_docker_compose_folder_path.join("ruby_3_1_active_model_7_0.yml")) - expect_file_content = <<~DOCKER_COMPOSE - version: '3' - services: - ruby_3_1: - image: ruby:3.1.0 - volumes: - - "../..:/app:cached" - - bundle_ruby_3_1_0:/bundle - - "../gemfile_locks/ruby_3_1_active_model_7_0:/app/Gemfile.lock" - - "/a/b:/app/c/d" - - "../schema/rails_7_0.rb:/app/test/dummy/db/schema.rb" - environment: - BUNDLE_PATH: "/bundle" - GEM_HOME: "/bundle" - BUNDLE_APP_CONFIG: "/bundle" - BUNDLE_BIN: "/bundle/bin" - PATH: "/app/bin:/bundle/bin:$PATH" - DATABASE_HOST: postgres - ACTIVE_MODEL_VERSION: 7.0.0 - working_dir: "/app" - depends_on: - - postgres - postgres: - image: postgres:12.8 - volumes: - - postgres12:/var/lib/postgresql/data - environment: - POSTGRES_HOST_AUTH_METHOD: trust - volumes: - bundle_ruby_3_1_0: - name: bundle_ruby_3_1_0 - postgres12: - DOCKER_COMPOSE - assert_equal expect_file_content.strip, file_content.strip - end - - -end diff --git a/test/matrixeval/ruby/docker_compose_test.rb b/test/matrixeval/ruby/docker_compose_test.rb deleted file mode 100644 index 4434825..0000000 --- a/test/matrixeval/ruby/docker_compose_test.rb +++ /dev/null @@ -1,77 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::DockerComposeTest < MatrixevalTest - - def test_context - context = stub - docker_compose = Matrixeval::Ruby::DockerCompose.new(context) - assert_equal context, docker_compose.context - end - - def test_run - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({"project_name" => "dummy app"}) - - context = Matrixeval::Ruby::Context.new( - main_variant: Matrixeval::Ruby::Variant.new( - { - "key" => "3.0", - "image" => "ruby:3.0.0" - }, - Matrixeval::Ruby::Vector.new("ruby", {"key" => "ruby"}) - ), - rest_variants: [ - Matrixeval::Ruby::Variant.new( - { - "key" => "6.0", - "env" => { - "RAILS_VERSION" => "6.0.0" - } - }, - Matrixeval::Ruby::Vector.new("rails", {"key" => "rails"}) - ), - Matrixeval::Ruby::Variant.new( - { - "key" => "5.0", - "env" => { - "SIDEKIQ_VERSION" => "5.0.0" - } - }, - Matrixeval::Ruby::Vector.new("sidekiq", {"key" => "sidekiq"}) - ), - ] - ) - docker_compose = Matrixeval::Ruby::DockerCompose.new(context) - - docker_compose.expects(:system).with(<<~COMMAND - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0_rails_6_0_sidekiq_5_0.yml \ - -p matrixeval-dummy_app-ruby_3_0_rails_6_0_sidekiq_5_0 \ - run --rm --no-TTY \ - ruby_3_0 rake test - COMMAND - ) - - docker_compose.expects(:system).with(<<~COMMAND.strip - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0_rails_6_0_sidekiq_5_0.yml \ - -p matrixeval-dummy_app-ruby_3_0_rails_6_0_sidekiq_5_0 \ - stop >> /dev/null 2>&1 - COMMAND - ) - - docker_compose.expects(:system).with(<<~COMMAND.strip - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0_rails_6_0_sidekiq_5_0.yml \ - -p matrixeval-dummy_app-ruby_3_0_rails_6_0_sidekiq_5_0 \ - rm -v -f >> /dev/null 2>&1 - COMMAND - ) - - docker_compose.expects(:system).with("stty opost") - - docker_compose.run(["rake", "test"]) - end - -end diff --git a/test/matrixeval/ruby/extra_mount_files_test.rb b/test/matrixeval/ruby/extra_mount_files_test.rb deleted file mode 100644 index ba6bb9c..0000000 --- a/test/matrixeval/ruby/extra_mount_files_test.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::ExtraMountFilesTest < MatrixevalTest - - def setup - FileUtils.rm_rf dummy_gem_working_dir.join(".test_mount") rescue nil - FileUtils.rm dummy_gem_working_dir.join("mount.txt") rescue nil - FileUtils.rm_rf dummy_gem_working_dir.join(".matrixeval/schema") rescue nil - Matrixeval.stubs(:working_dir).returns(dummy_gem_working_dir) - end - - def test_create - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({ - "target" => "ruby", - "mounts" => [ - "/matrixeval-a/b:/c/d", - ".test_mount:/test_mount", - "mount.txt:/app/mount.txt" - ], - "matrix" => { - "ruby" => { - "variants" => [ - { "key" => "3.0", "default" => true }, - { "key" => "3.1" } - ] - }, - "rails" => { - "variants" => [ - { - "key" => "6.1", - "mounts" => [ - ".matrixeval/schema/rails_6_1.rb:/app/test/dummy/db/schema.rb" - ] - }, - { - "key" => "7.0", - "default" => true, - "mounts" => [ - ".matrixeval/schema/rails_7_0.rb:/app/test/dummy/db/schema.rb" - ] - } - ] - } - } - }) - - refute File.exist? dummy_gem_working_dir.join(".matrixeval/schema/rails_6_1.rb") - refute File.exist? dummy_gem_working_dir.join(".matrixeval/schema/rails_7_0.rb") - refute File.exist? dummy_gem_working_dir.join("mount.txt") - refute File.exist? "/matrixeval-a" - refute File.exist? dummy_gem_working_dir.join(".test_mount") - - Matrixeval::Ruby::ExtraMountFiles.create - - assert File.exist? dummy_gem_working_dir.join(".matrixeval/schema/rails_6_1.rb") - assert File.exist? dummy_gem_working_dir.join(".matrixeval/schema/rails_7_0.rb") - assert File.exist? dummy_gem_working_dir.join("mount.txt") - refute File.exist? "/matrixeval-a" - refute File.exist? dummy_gem_working_dir.join(".test_mount") - end - -end diff --git a/test/matrixeval/ruby/gemfile_locks_test.rb b/test/matrixeval/ruby/gemfile_locks_test.rb deleted file mode 100644 index 2470a06..0000000 --- a/test/matrixeval/ruby/gemfile_locks_test.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::GemfileLocksTest < MatrixevalTest - - def setup - FileUtils.rm Dir.glob(dummy_gem_working_dir.join(".matrixeval/gemfile_locks/*")) - Matrixeval.stubs(:working_dir).returns(dummy_gem_working_dir) - end - - def test_create - Matrixeval::Ruby::Config::YAML.stubs(:yaml).returns({ - "version" => "0.3", - "target" => "ruby", - "matrix" => { - "ruby" => { - "variants" => [ - { "key" => "3.0", "default" => true }, - { "key" => "3.1" } - ] - }, - "rails" => { - "variants" => [ - { "key" => "6.1" }, - { "key" => "7.0", "default" => true } - ] - } - } - }) - - refute File.exist? gemfile_lock("ruby_3_0_rails_6_1") - refute File.exist? gemfile_lock("ruby_3_0_rails_7_0") - refute File.exist? gemfile_lock("ruby_3_1_rails_6_1") - refute File.exist? gemfile_lock("ruby_3_1_rails_6_1") - - Matrixeval::Ruby::GemfileLocks.create - - assert File.exist? gemfile_lock("ruby_3_0_rails_6_1") - assert File.exist? gemfile_lock("ruby_3_0_rails_7_0") - assert File.exist? gemfile_lock("ruby_3_1_rails_6_1") - assert File.exist? gemfile_lock("ruby_3_1_rails_6_1") - end - - def gemfile_lock(filename) - dummy_gem_working_dir.join(".matrixeval/gemfile_locks/#{filename}") - end - -end diff --git a/test/matrixeval/ruby/gitignore_test.rb b/test/matrixeval/ruby/gitignore_test.rb deleted file mode 100644 index 9c2c127..0000000 --- a/test/matrixeval/ruby/gitignore_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::GitignoreTest < MatrixevalTest - - def setup - FileUtils.rm(dummy_gem_working_dir.join(".gitignore")) rescue nil - Matrixeval.stubs(:working_dir).returns(dummy_gem_working_dir) - end - - def test_create - refute File.exist?(dummy_gem_working_dir.join(".gitignore")) - - Matrixeval::Ruby::Gitignore.update - - expected_gitignore_content = <<~GITIGNORE - .matrixeval/docker-compose - .matrixeval/gemfile_locks - GITIGNORE - gitignore_content = File.read dummy_gem_working_dir.join(".gitignore") - assert_equal expected_gitignore_content, gitignore_content - end - - def test_update - File.open(dummy_gem_working_dir.join(".gitignore"), 'w+') do |file| - file.puts ".env" - end - - Matrixeval::Ruby::Gitignore.update - - expected_gitignore_content = <<~GITIGNORE - .env - .matrixeval/docker-compose - .matrixeval/gemfile_locks - GITIGNORE - gitignore_content = File.read dummy_gem_working_dir.join(".gitignore") - assert_equal expected_gitignore_content, gitignore_content - end - - def test_update_duplicate_check - File.open(dummy_gem_working_dir.join(".gitignore"), 'w+') do |file| - file.puts ".env" - file.puts ".matrixeval/gemfile_locks" - end - - Matrixeval::Ruby::Gitignore.update - - expected_gitignore_content = <<~GITIGNORE - .env - .matrixeval/gemfile_locks - .matrixeval/docker-compose - GITIGNORE - gitignore_content = File.read dummy_gem_working_dir.join(".gitignore") - assert_equal expected_gitignore_content, gitignore_content - end - -end diff --git a/test/matrixeval/ruby/runner_test.rb b/test/matrixeval/ruby/runner_test.rb deleted file mode 100644 index 7e43f5f..0000000 --- a/test/matrixeval/ruby/runner_test.rb +++ /dev/null @@ -1,129 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::RunnerTest < MatrixevalTest - - def setup - Matrixeval.stubs(:working_dir).returns(dummy_gem_working_dir) - - FileUtils.rm(dummy_gem_docker_compose_file_path) rescue nil - FileUtils.rm(dummy_gem_matrixeval_file_path) rescue nil - FileUtils.rm(dummy_gem_working_dir.join(".gitignore")) rescue nil - FileUtils.rm Dir.glob(dummy_gem_working_dir.join(".matrixeval/gemfile_locks/*")) - end - - def test_start_with_init - refute File.exist?(dummy_gem_matrixeval_file_path) - - Matrixeval::Ruby::Runner.start(["init"]) - - assert File.exist?(dummy_gem_matrixeval_file_path) - end - - def test_start_first_time - FileUtils.rm(dummy_gem_matrixeval_file_path) rescue nil - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with(<<~COMMAND - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0.yml \ - -p matrixeval-replace_me-ruby_3_0 \ - run --rm --no-TTY \ - ruby_3_0 \ - rake test - COMMAND - ) - - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with(<<~COMMAND.strip - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0.yml \ - -p matrixeval-replace_me-ruby_3_0 \ - stop >> /dev/null 2>&1 - COMMAND - ) - - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with(<<~COMMAND.strip - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0.yml \ - -p matrixeval-replace_me-ruby_3_0 \ - rm -v -f >> /dev/null 2>&1 - COMMAND - ) - - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with("stty opost") - - Matrixeval::Ruby::Runner.start(["init"]) - Matrixeval::Ruby::Runner.start(["--ruby", "3.0", "rake", "test"]) - end - - def test_start_with_existing_config_file - File.open(dummy_gem_matrixeval_file_path, 'w+') do |file| - file.puts(<<~MATRIXEVAL_YAML - version: 0.1 - project_name: sample - target: ruby - parallel_workers: 1 - matrix: - ruby: - variants: - - key: 3.0 - container: - image: ruby:3.0.0 - default: true - - key: 3.1 - container: - image: ruby:3.1.0 - rails: - variants: - - key: 6.0 - default: true - env: - RAILS_VERSION: 6.0.0 - - key: 6.1 - env: - RAILS_VERSION: 6.1.0 - sidekiq: - variants: - - key: 5.0 - default: true - env: - SIDEKIQ_VERSION: 5.0.0 - - key: 6.0 - env: - SIDEKIQ_VERSION: 6.0.0 - MATRIXEVAL_YAML - ) - end - - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with(<<~COMMAND - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0_rails_6_0_sidekiq_5_0.yml \ - -p matrixeval-sample-ruby_3_0_rails_6_0_sidekiq_5_0 \ - run --rm --no-TTY \ - ruby_3_0 \ - rake test - COMMAND - ) - - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with(<<~COMMAND.strip - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0_rails_6_0_sidekiq_5_0.yml \ - -p matrixeval-sample-ruby_3_0_rails_6_0_sidekiq_5_0 \ - stop >> /dev/null 2>&1 - COMMAND - ) - - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with(<<~COMMAND.strip - docker --log-level error compose \ - -f .matrixeval/docker-compose/ruby_3_0_rails_6_0_sidekiq_5_0.yml \ - -p matrixeval-sample-ruby_3_0_rails_6_0_sidekiq_5_0 \ - rm -v -f >> /dev/null 2>&1 - COMMAND - ) - - Matrixeval::Ruby::DockerCompose.any_instance.expects(:system).with("stty opost") - - - Matrixeval::Ruby::Runner.start(["--rails", "6.0", "--sidekiq", "5.0", "rake", "test"]) - end - -end diff --git a/test/matrixeval/ruby/target_test.rb b/test/matrixeval/ruby/target_test.rb new file mode 100644 index 0000000..3a0168b --- /dev/null +++ b/test/matrixeval/ruby/target_test.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +require "test_helper" + +class Matrixeval::TargetTest < MatrixevalTest + + def setup + @target = Matrixeval::Ruby::Target.new + + Matrixeval::Config::YAML.stubs(:yaml).returns({}) + ruby_vector = Matrixeval::Vector.new("ruby", {"key" => "ruby", "main" => true}) + sidekiq_vector = Matrixeval::Vector.new("sidekiq", {"key" => "sidekiq"}) + rails_vector = Matrixeval::Vector.new("rails", {"key" => "rails"}) + ruby_3_variant = Matrixeval::Variant.new({"key" => "3.0", "container" => { "image" => "ruby:3.0" }}, ruby_vector) + sidekiq_5_variant = Matrixeval::Variant.new( + { + "key" => "5.0", + "env" => {"SIDEKIQ_VERSION" => "5.0.0"} + }, sidekiq_vector) + + rails_6_variant = Matrixeval::Variant.new( + { + "key" => "6.1", + "env" => {"RAILS_VERSION" => "6.1.0"} + }, rails_vector) + + @context = Matrixeval::Context.new( + main_variant: ruby_3_variant, + rest_variants: [sidekiq_5_variant, rails_6_variant] + ) + end + + def test_version + assert_equal '0.4.0', @target.version + end + + def test_matrixeval_yml_template_path + assert_match %r(lib/matrixeval/ruby/templates/matrixeval.yml), @target.matrixeval_yml_template_path.to_s + end + + def test_vector_key + assert_equal "ruby", @target.vector_key + end + + def test_env + expected_env = { + "BUNDLE_PATH" => "/bundle", + "GEM_HOME" => "/bundle", + "BUNDLE_APP_CONFIG" => "/bundle", + "BUNDLE_BIN" => "/bundle/bin", + "PATH" => "/app/bin:/bundle/bin:$PATH" + } + assert_equal expected_env, @target.env(@context) + end + + def test_mounts + expected_mounts = [ + "bundle_ruby_3_0:/bundle", + "../gemfile_locks/ruby_3_0_rails_6_1_sidekiq_5_0:/app/Gemfile.lock" + ] + assert_equal expected_mounts, @target.mounts(@context) + end + + def test_volumes + expected_volumes = { + "bundle_ruby_3_0" => { + "name" => "bundle_ruby_3_0" + } + } + assert_equal expected_volumes, @target.volumes(@context) + end + + def test_gitignore_paths + assert_equal [".matrixeval/gemfile_locks"], @target.gitignore_paths + end + + def test_support_commands + expected_commands = [ + 'ruby', 'rake', 'rails', 'rspec', 'bundle', + 'bin/rake', 'bin/rails', 'bin/rspec', 'bin/test' + ] + assert_equal expected_commands, @target.support_commands + end + + def test_cli_example_lines + expected_example_lines = [ + "", + "Example:", + " matrixeval --all bundle install", + " matrixeval --ruby 3.0 rspec a_spec.rb", + " matrixeval --ruby 3.1 --active_model 7.0 rake test", + " matrixeval bash" + ] + assert_equal expected_example_lines, @target.cli_example_lines + end + + def test_create_files + Matrixeval.stubs(:working_dir).returns(dummy_gem_working_dir) + FileUtils.rm_rf(dummy_gem_working_dir.join(".matrixeval/gemfile_locks")) + Matrixeval::Context.stubs(:all).returns([@context]) + + @target.create_files + + assert File.exist?(dummy_gem_working_dir.join(".matrixeval/gemfile_locks/ruby_3_0_rails_6_1_sidekiq_5_0")) + end + +end diff --git a/test/matrixeval/ruby/variant_test.rb b/test/matrixeval/ruby/variant_test.rb deleted file mode 100644 index f36fecf..0000000 --- a/test/matrixeval/ruby/variant_test.rb +++ /dev/null @@ -1,119 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::VariantTest < MatrixevalTest - - def setup - @vector = stub(id: 'ruby', key: 'ruby') - end - - def test_key - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1}, @vector) - assert_equal "3.1", variant.key - end - - def test_key_missing - assert_raises "Variant#key is missing" do - Matrixeval::Ruby::Variant.new({}, @vector) - end - end - - def test_container - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "container" => { "image" => "ruby:3.1.0"} }, @vector) - assert variant.container.is_a?(Matrixeval::Ruby::Container) - assert_equal "ruby:3.1.0", variant.container.image - end - - def test_env - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "env" => { 'A' => 'a' }}, @vector) - assert_equal({'A' => 'a'}, variant.env) - end - - def test_env_default - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1}, @vector) - assert_equal({}, variant.env) - end - - def test_mounts - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "mounts" => [".matrixeval/schema/rails_6.1.rb:/app/db/schema.rb"]}, @vector) - assert_equal([".matrixeval/schema/rails_6.1.rb:/app/db/schema.rb"], variant.mounts) - end - - def test_mounts_default - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1}, @vector) - assert_equal([], variant.mounts) - end - - def test_vector - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1}, @vector) - assert_equal @vector, variant.vector - end - - def test_bundle_volume_name - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "container" => { "image" => "ruby:3.1.0"} }, @vector) - assert_equal "bundle_ruby_3_1_0", variant.bundle_volume_name - end - - def test_id - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "container" => { "image" => "ruby:3.1.0"} }, @vector) - assert_equal "ruby_3_1", variant.id - end - - def test_docker_compose_service_name - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "container" => { "image" => "ruby:3.1.0"} }, @vector) - assert_equal "ruby_3_1", variant.docker_compose_service_name - end - - def test_pathname - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "container" => { "image" => "ruby:3.1.0"} }, @vector) - assert_equal "ruby_3_1", variant.pathname - end - - def test_default - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1}, @vector) - assert_equal false, variant.default? - - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "default" => true}, @vector) - assert_equal true, variant.default? - end - - def test_match_command_options - variant = Matrixeval::Ruby::Variant.new({"key" => 3.1, "default" => true}, @vector) - assert variant.match_command_options?({"ruby" => '3.1'}) - refute variant.match_command_options?({"ruby" => '3.0'}) - end - - def test_equal - v1 = Matrixeval::Ruby::Variant.new( - {"key" => 3.1}, - Matrixeval::Ruby::Vector.new('ruby', {}) - ) - v2 = Matrixeval::Ruby::Variant.new( - {"key" => 3.1}, - Matrixeval::Ruby::Vector.new('ruby', {}) - ) - assert v1 == v2 - end - - def test_equal_with_array - v1 = Matrixeval::Ruby::Variant.new( - {"key" => 3.1}, - Matrixeval::Ruby::Vector.new('ruby', {}) - ) - v2 = Matrixeval::Ruby::Variant.new( - {"key" => 3.1}, - Matrixeval::Ruby::Vector.new('ruby', {}) - ) - v3 = Matrixeval::Ruby::Variant.new( - {"key" => 7.0}, - Matrixeval::Ruby::Vector.new('rails', {}) - ) - v4 = Matrixeval::Ruby::Variant.new( - {"key" => 7.0}, - Matrixeval::Ruby::Vector.new('rails', {}) - ) - assert [v1, v3] == [v2, v4] - end - -end diff --git a/test/matrixeval/ruby/vector_test.rb b/test/matrixeval/ruby/vector_test.rb deleted file mode 100644 index abae358..0000000 --- a/test/matrixeval/ruby/vector_test.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Matrixeval::Ruby::VectorTest < MatrixevalTest - - def test_key - vector = Matrixeval::Ruby::Vector.new('ruby', {}) - assert_equal "ruby", vector.key - end - - def test_variants_with_hash - vector = Matrixeval::Ruby::Vector.new('ruby', {"variants" => [{"key" => "3.1"}]}) - variants = vector.variants - assert_equal 1, variants.count - assert_equal "3.1", variants[0].key - end - - def test_main - vector = Matrixeval::Ruby::Vector.new('ruby', {}) - assert vector.main? - - vector = Matrixeval::Ruby::Vector.new('active_model', {}) - refute vector.main? - end - - def test_id - vector = Matrixeval::Ruby::Vector.new('ruby-3.0', {}) - assert_equal 'ruby_3_0', vector.id - end - - def test_default_variant - vector = Matrixeval::Ruby::Vector.new('ruby', {"variants" => [{"key" => "3.0", "default" => true}, { "key" => "3.1" }]}) - variant = vector.default_variant - assert_equal "ruby", variant.vector.key - assert_equal "3.0", variant.key - end - - def test_default_variant_on_missing_default - assert_raises "Please set a default variant for matrix ruby" do - config = {"variants" => [{"key" => "3.0"}, { "key" => "3.1" }]} - Matrixeval::Ruby::Vector.new('ruby', config).default_variant - end - end - -end