Fix tests and add rake task

This commit is contained in:
Sami Samhuri 2022-01-21 20:26:05 -08:00
parent 58f1e48cd2
commit b4a1f28a11
No known key found for this signature in database
GPG key ID: 4B4195422742FC16
4 changed files with 26 additions and 13 deletions

View file

@ -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'

View file

@ -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)

7
ruby/Rakefile Normal file
View file

@ -0,0 +1,7 @@
require 'rake/testtask'
task default: 'test'
Rake::TestTask.new do |task|
task.pattern = 'test/*_test.rb'
end

26
ruby/test.rb → ruby/test/shell_test.rb Executable file → Normal file
View file

@ -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