2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "active_support/test_case"
|
|
|
|
require "active_support/testing/autorun"
|
|
|
|
require "rails/generators/rails/app/app_generator"
|
|
|
|
require "tempfile"
|
2013-10-30 18:03:34 -04:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Generators
|
2013-10-30 19:14:02 -04:00
|
|
|
class ARGVScrubberTest < ActiveSupport::TestCase # :nodoc:
|
|
|
|
# Future people who read this... These tests are just to surround the
|
|
|
|
# current behavior of the ARGVScrubber, they do not mean that the class
|
|
|
|
# *must* act this way, I just want to prevent regressions.
|
|
|
|
|
2013-10-30 18:03:34 -04:00
|
|
|
def test_version
|
2016-08-06 13:16:09 -04:00
|
|
|
["-v", "--version"].each do |str|
|
2013-10-30 18:03:34 -04:00
|
|
|
scrubber = ARGVScrubber.new [str]
|
|
|
|
output = nil
|
|
|
|
exit_code = nil
|
|
|
|
scrubber.extend(Module.new {
|
2014-05-07 05:02:27 -04:00
|
|
|
define_method(:puts) { |string| output = string }
|
2013-10-30 18:03:34 -04:00
|
|
|
define_method(:exit) { |code| exit_code = code }
|
|
|
|
})
|
2013-10-30 18:29:01 -04:00
|
|
|
scrubber.prepare!
|
2013-10-30 18:03:34 -04:00
|
|
|
assert_equal "Rails #{Rails::VERSION::STRING}", output
|
|
|
|
assert_equal 0, exit_code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-30 19:12:40 -04:00
|
|
|
def test_default_help
|
2016-08-06 13:16:09 -04:00
|
|
|
argv = ["zomg", "how", "are", "you"]
|
2013-10-30 19:12:40 -04:00
|
|
|
scrubber = ARGVScrubber.new argv
|
|
|
|
args = scrubber.prepare!
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal ["--help"] + argv.drop(1), args
|
2013-10-30 19:12:40 -04:00
|
|
|
end
|
|
|
|
|
2013-10-30 18:03:34 -04:00
|
|
|
def test_prepare_returns_args
|
2016-08-06 13:16:09 -04:00
|
|
|
scrubber = ARGVScrubber.new ["hi mom"]
|
2013-10-30 18:29:01 -04:00
|
|
|
args = scrubber.prepare!
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal "--help", args.first
|
2013-10-30 18:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_mutations
|
2016-08-06 13:16:09 -04:00
|
|
|
scrubber = ARGVScrubber.new ["hi mom"].freeze
|
2013-10-30 18:29:01 -04:00
|
|
|
args = scrubber.prepare!
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal "--help", args.first
|
2013-10-30 18:03:34 -04:00
|
|
|
end
|
2013-10-30 18:29:01 -04:00
|
|
|
|
|
|
|
def test_new_command_no_rc
|
|
|
|
scrubber = Class.new(ARGVScrubber) {
|
|
|
|
def self.default_rc_file
|
2016-08-06 13:16:09 -04:00
|
|
|
File.join(Dir.tmpdir, "whatever")
|
2013-10-30 18:29:01 -04:00
|
|
|
end
|
2016-08-06 13:16:09 -04:00
|
|
|
}.new ["new"]
|
2013-10-30 18:29:01 -04:00
|
|
|
args = scrubber.prepare!
|
|
|
|
assert_equal [], args
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_homedir_rc
|
2016-08-06 13:16:09 -04:00
|
|
|
file = Tempfile.new "myrcfile"
|
|
|
|
file.puts "--hello-world"
|
2013-10-30 18:29:01 -04:00
|
|
|
file.flush
|
|
|
|
|
|
|
|
message = nil
|
|
|
|
scrubber = Class.new(ARGVScrubber) {
|
|
|
|
define_singleton_method(:default_rc_file) do
|
|
|
|
file.path
|
|
|
|
end
|
|
|
|
define_method(:puts) { |msg| message = msg }
|
2016-08-06 13:16:09 -04:00
|
|
|
}.new ["new"]
|
2013-10-30 18:29:01 -04:00
|
|
|
args = scrubber.prepare!
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal ["--hello-world"], args
|
|
|
|
assert_match "hello-world", message
|
2013-10-30 18:29:01 -04:00
|
|
|
assert_match file.path, message
|
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
|
2013-10-30 19:53:08 -04:00
|
|
|
def test_rc_whitespace_separated
|
2016-08-06 13:16:09 -04:00
|
|
|
file = Tempfile.new "myrcfile"
|
|
|
|
file.puts "--hello --world"
|
2013-10-30 19:53:08 -04:00
|
|
|
file.flush
|
|
|
|
|
|
|
|
scrubber = Class.new(ARGVScrubber) {
|
2017-10-12 02:04:33 -04:00
|
|
|
define_method(:puts) { |msg| }
|
2016-08-06 13:16:09 -04:00
|
|
|
}.new ["new", "--rc=#{file.path}"]
|
2013-10-30 19:53:08 -04:00
|
|
|
args = scrubber.prepare!
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal ["--hello", "--world"], args
|
2013-10-30 19:53:08 -04:00
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
|
2013-10-30 18:33:46 -04:00
|
|
|
def test_new_rc_option
|
2016-08-06 13:16:09 -04:00
|
|
|
file = Tempfile.new "myrcfile"
|
|
|
|
file.puts "--hello-world"
|
2013-10-30 18:33:46 -04:00
|
|
|
file.flush
|
|
|
|
|
|
|
|
message = nil
|
|
|
|
scrubber = Class.new(ARGVScrubber) {
|
|
|
|
define_method(:puts) { |msg| message = msg }
|
2016-08-06 13:16:09 -04:00
|
|
|
}.new ["new", "--rc=#{file.path}"]
|
2013-10-30 18:33:46 -04:00
|
|
|
args = scrubber.prepare!
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal ["--hello-world"], args
|
|
|
|
assert_match "hello-world", message
|
2013-10-30 18:33:46 -04:00
|
|
|
assert_match file.path, message
|
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
|
2013-10-30 18:41:09 -04:00
|
|
|
def test_new_rc_option_and_custom_options
|
2016-08-06 13:16:09 -04:00
|
|
|
file = Tempfile.new "myrcfile"
|
|
|
|
file.puts "--hello"
|
|
|
|
file.puts "--world"
|
2013-10-30 18:41:09 -04:00
|
|
|
file.flush
|
|
|
|
|
|
|
|
scrubber = Class.new(ARGVScrubber) {
|
2013-10-30 18:49:47 -04:00
|
|
|
define_method(:puts) { |msg| }
|
2016-08-06 13:16:09 -04:00
|
|
|
}.new ["new", "tenderapp", "--love", "--rc=#{file.path}"]
|
2013-10-30 18:49:47 -04:00
|
|
|
|
2013-10-30 18:41:09 -04:00
|
|
|
args = scrubber.prepare!
|
2013-10-30 18:49:47 -04:00
|
|
|
assert_equal ["tenderapp", "--hello", "--world", "--love"], args
|
2013-10-30 18:41:09 -04:00
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
|
2013-10-30 18:29:01 -04:00
|
|
|
def test_no_rc
|
2016-08-06 13:16:09 -04:00
|
|
|
scrubber = ARGVScrubber.new ["new", "--no-rc"]
|
2013-10-30 18:29:01 -04:00
|
|
|
args = scrubber.prepare!
|
|
|
|
assert_equal [], args
|
|
|
|
end
|
2013-10-30 18:03:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|