From b4a1f28a113a0b67601df133f4b63ca7056171b2 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Fri, 21 Jan 2022 20:26:05 -0800 Subject: [PATCH] Fix tests and add rake task --- ruby/Gemfile | 2 ++ ruby/Gemfile.lock | 4 ++++ ruby/Rakefile | 7 +++++++ ruby/{test.rb => test/shell_test.rb} | 26 +++++++++++++------------- 4 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 ruby/Rakefile rename ruby/{test.rb => test/shell_test.rb} (72%) mode change 100755 => 100644 diff --git a/ruby/Gemfile b/ruby/Gemfile index fff69db..c463042 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -1,5 +1,7 @@ source 'https://rubygems.org' ruby '3.1.0' +gem 'minitest', '~> 5.15' +gem 'rake', '~> 13.0' gem 'rubocop', '1.24.1' gem 'wordexp', '~> 0.1' diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock index a3e7c5f..47b95df 100644 --- a/ruby/Gemfile.lock +++ b/ruby/Gemfile.lock @@ -2,10 +2,12 @@ GEM remote: https://rubygems.org/ specs: ast (2.4.2) + minitest (5.15.0) parallel (1.21.0) parser (3.1.0.0) ast (~> 2.4.1) rainbow (3.1.1) + rake (13.0.6) regexp_parser (2.2.0) rexml (3.2.5) rubocop (1.24.1) @@ -27,6 +29,8 @@ PLATFORMS arm64-darwin-21 DEPENDENCIES + minitest (~> 5.15) + rake (~> 13.0) rubocop (= 1.24.1) wordexp (~> 0.1) diff --git a/ruby/Rakefile b/ruby/Rakefile new file mode 100644 index 0000000..3f8ff4a --- /dev/null +++ b/ruby/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +task default: 'test' + +Rake::TestTask.new do |task| + task.pattern = 'test/*_test.rb' +end diff --git a/ruby/test.rb b/ruby/test/shell_test.rb old mode 100755 new mode 100644 similarity index 72% rename from ruby/test.rb rename to ruby/test/shell_test.rb index a293d1b..782c5dc --- a/ruby/test.rb +++ b/ruby/test/shell_test.rb @@ -1,10 +1,10 @@ -#!/usr/bin/env ruby -w - require 'minitest/autorun' class ShellTest < Minitest::Test TRIVIAL_SHELL_SCRIPT = "#!/bin/sh\ntrue".freeze + A1_PATH = './a1'.freeze + def setup FileUtils.mkdir_p('test_bin') end @@ -18,12 +18,12 @@ class ShellTest < Minitest::Test end def test_expands_environment_variables - assert_equal ENV['HOME'], `./a1 -c 'echo $HOME'`.chomp - assert system("./a1 -c 'echo $HOME' >/dev/null") + assert_equal ENV['HOME'], `#{A1_PATH} -c 'echo $HOME'`.chomp + assert system("#{A1_PATH} -c 'echo $HOME' >/dev/null") end def test_fails_on_unknown_variables - assert_equal false, system("./a1 -c 'echo $DEFINITELY_DOES_NOT_EXIST' 2>/dev/null") + assert_equal false, system("#{A1_PATH} -c 'echo $DEFINITELY_DOES_NOT_EXIST' 2>/dev/null") end ################################# @@ -31,36 +31,36 @@ class ShellTest < Minitest::Test ################################# def test_background_job - output = `./a1 -c 'bg echo hello'` + output = `#{A1_PATH} -c 'bg echo hello'` assert output.match?(/\ABackground job 1 \(pid \d+\)\nhello\n\z/m), "'#{output}' is unexpected" end def test_resolves_executables_with_absolute_paths - assert_equal '/usr/bin/which', `./a1 -c '/usr/bin/which -a which'`.chomp + assert_equal '/usr/bin/which', `#{A1_PATH} -c '/usr/bin/which -a which'`.chomp end def test_resolves_executables_with_relative_paths File.write('test_bin/something', TRIVIAL_SHELL_SCRIPT) File.chmod(0o755, 'test_bin/something') - assert system('./a1 -c ./test_bin/something') + assert system("#{A1_PATH} -c ./test_bin/something") end def test_resolves_executables_in_absolute_paths - assert_equal '/usr/bin/which', `./a1 -c 'which -a which'`.chomp + assert_equal '/usr/bin/which', `#{A1_PATH} -c 'which -a which'`.chomp end def test_resolves_executables_in_relative_paths code = rand(1_000_000).to_s File.write('test_bin/definitely_executable', unique_shell_script(code)) File.chmod(0o755, 'test_bin/definitely_executable') - actual = `PATH="./test_bin:$PATH" ./a1 -c definitely_executable`.chomp + actual = `PATH="./test_bin:$PATH" #{A1_PATH} -c definitely_executable`.chomp assert_equal code, actual end def test_does_not_resolve_non_executable_files_in_path File.write('test_bin/definitely_not_executable', TRIVIAL_SHELL_SCRIPT) File.chmod(0o644, 'test_bin/definitely_not_executable') - actual = system('PATH="./test_bin:$PATH" ./a1 -c definitely_not_executable 2>/dev/null') + actual = system("PATH=\"./test_bin:$PATH\" #{A1_PATH} -c definitely_not_executable 2>/dev/null") assert_equal false, actual end @@ -89,9 +89,9 @@ class ShellTest < Minitest::Test end def test_builtin_pwd - assert_equal Dir.pwd, `./a1 -c pwd`.chomp + assert_equal Dir.pwd, `#{A1_PATH} -c pwd`.chomp - shell_path = File.expand_path('a1', __dir__) + shell_path = File.expand_path(A1_PATH, Dir.pwd) assert_equal '/usr/bin', `cd /usr/bin && '#{shell_path}' -c pwd`.chomp end end