From 8ee5e2520f0054e4998329200515375b22c7a85e Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Fri, 21 Aug 2020 17:23:43 -0500 Subject: [PATCH 1/2] For funsies: Crystal implementation Mostly identical and trimmed down version of `lib/cfme-versions.rb` (trimmed down since it doesn't need the library features). For simplicity, the tests just confirm that the outputs match with the ruby version for the same flags. --- .gitignore | 1 + Rakefile | 15 ++++- lib/cfme-versions.cr | 92 +++++++++++++++++++++++++++++ test/crystal_implementation_test.rb | 22 +++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 lib/cfme-versions.cr create mode 100644 test/crystal_implementation_test.rb diff --git a/.gitignore b/.gitignore index 6853244..fd73f41 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ /pkg/ /spec/reports/ /tmp/ +/crystal_build/ Gemfile.lock diff --git a/Rakefile b/Rakefile index d433a1e..762c991 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,8 @@ require "bundler/gem_tasks" require "rake/testtask" +require "rake/clean" + +CLEAN << "crystal_build" Rake::TestTask.new(:test) do |t| t.libs << "test" @@ -7,4 +10,14 @@ Rake::TestTask.new(:test) do |t| t.test_files = FileList["test/**/*_test.rb"] end -task :default => :test +namespace :crystal do + directory "crystal_build" + file "crystal_build/cfme-versions" => "crystal_build" do + sh "crystal build --release lib/cfme-versions.cr -o crystal_build/cfme-versions" + end + + desc "Build the crystal " + task :build => "crystal_build/cfme-versions" +end + +task :default => ["crystal:build", :test] diff --git a/lib/cfme-versions.cr b/lib/cfme-versions.cr new file mode 100644 index 0000000..750135e --- /dev/null +++ b/lib/cfme-versions.cr @@ -0,0 +1,92 @@ +module CFME + class Versions + FIELDS = [ + "ManageIQ", "", "CFME", "CloudForms", "CP4MCM", "Ruby", "Rails", "PostgreSQL" + ] + VERSIONS = [ + %w[ N/A N/A 5.1.z 2.0 N/A N/A N/A N/A ], + %w[ N/A N/A 5.2.z 3.0 N/A N/A N/A N/A ], + %w[ Anand 1.y.z 5.3.z 3.1 N/A N/A N/A N/A ], + %w[ Botvinnik 2.y.z 5.4.z 3.2 N/A N/A N/A N/A ], + %w[ Capablanca 3.y.z 5.5.z 4.0 N/A 2.2.z 4.2.z 9.4.z ], + %w[ Darga 4.y.z 5.6.z 4.1 N/A 2.2.z 5.0.z 9.4.z ], + %w[ Euwe 5.y.z 5.7.z 4.2 N/A 2.3.z 5.0.z 9.5.z ], + %w[ Fine 6.y.z 5.8.z 4.5 N/A 2.3.z 5.0.z 9.5.z ], + %w[ Gaprindashvili 7.y.z 5.9.z 4.6 N/A 2.3.z 5.0.z 9.5.z ], + %w[ Hammer 8.y.z 5.10.z 4.7 N/A 2.4.z 5.0.z 9.5.z ], + %w[ Ivanchuk 9.y.z 5.11.z 5.0 1.2,1.3 2.5.z 5.1.z 10.y ], + %w[ Jansa 10.y.z N/A N/A 2.0 2.5.z 5.2.z 10.y ], + %w[ Kasparov 11.y.z N/A N/A N/A 2.6.z 5.2.z 10.y ] + ] + + def self.run(argv = ARGV) + until argv.empty? + arg = argv.shift + + # The `return` statements are there for specs + case arg + when "--version" then return print_version + when "--help" then return print_help + end + end + + CFME::Versions.print_table + end + + def self.version + VERSIONS.last[1].split(".").first + ".0.0" + end + + def self.print_help + puts <<-HELP + "\n\n" + usage: cfme-versions [OPTION]... + + Options: + + --version Prints the version number and exits + --help This help + HELP + exit + end + + def self.print_table + # Print Header + puts printable_row(FIELDS) + puts printable_row(spacings.map { |size| "-" * size }) + + # Print version data + VERSIONS.each do |version| + version_data = version.map { |column| column == "N/A" ? "" : column } # remove N/A values + puts printable_row(version_data) + end + end + + def self.print_version + puts CFME::Versions.version + exit + end + + private def self.printable_row(data) + "| #{data.map_with_index { |header, index| header.ljust(spacings[index]) }.join(" | ")} |" + end + + @@spacings = [] of Int32 + + # Column width based on Miq::Versions.raw_data + private def self.spacings + spacings = @@spacings + return spacings unless spacings.empty? + + spacings = FIELDS.map(&.size) + VERSIONS.each do |version| + version.each.with_index do |col, index| + spacings[index] = [spacings[index].to_i, col.size].max + end + end + + @@spacings = spacings + end + end +end + +CFME::Versions.run diff --git a/test/crystal_implementation_test.rb b/test/crystal_implementation_test.rb new file mode 100644 index 0000000..5fee1ba --- /dev/null +++ b/test/crystal_implementation_test.rb @@ -0,0 +1,22 @@ +require "test_helper" + +class CrystalImplementation < Minitest::Test + def cfme_versions_cr *args + `crystal_build/cfme-versions #{args.join " "}` + end + + def test_output_is_correct + expected, _ = capture_io { CFME::Versions.print_help } + assert_equal expected, cfme_versions_cr('--help') + end + + def test_version_flag_is_correct + expected, _ = capture_io { CFME::Versions.print_version } + assert_equal expected, cfme_versions_cr('--version') + end + + def test_help_text_is_correct + expected, _ = capture_io { CFME::Versions.print_table } + assert_equal expected, cfme_versions_cr + end +end From f63c59c5ffd9954fae48cfbada06e75a83fc31cf Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Fri, 21 Aug 2020 18:33:58 -0500 Subject: [PATCH 2/2] [.travis.yml] Test crystal on Travis --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index dbcb768..c8aa9ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,11 @@ --- language: ruby cache: bundler +# ref: https://github.com/travis-ci/travis-build/blob/543bb7e3/lib/travis/build/script/crystal.rb#L104-L108 +before_script: + - travis_apt_get_update + - sudo apt-get install -y gcc pkg-config git tzdata libpcre3-dev libevent-dev libyaml-dev libgmp-dev libssl-dev libxml2-dev 2>&1 > /dev/null + - sudo snap install crystal --classic --channel=latest/stable rvm: - 2.5 - 2.6