aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/argv_scrubber_test.rb
blob: 61f28c073152f29832ca691e8fe120bd225aac49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
require "active_support/test_case"
require "active_support/testing/autorun"
require "rails/generators/rails/app/app_generator"
require "tempfile"

module Rails
  module Generators
    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.

      def test_version
        ["-v", "--version"].each do |str|
          scrubber = ARGVScrubber.new [str]
          output    = nil
          exit_code = nil
          scrubber.extend(Module.new {
            define_method(:puts) { |string| output = string }
            define_method(:exit) { |code| exit_code = code }
          })
          scrubber.prepare!
          assert_equal "Rails #{Rails::VERSION::STRING}", output
          assert_equal 0, exit_code
        end
      end

      def test_default_help
        argv = ["zomg", "how", "are", "you"]
        scrubber = ARGVScrubber.new argv
        args = scrubber.prepare!
        assert_equal ["--help"] + argv.drop(1), args
      end

      def test_prepare_returns_args
        scrubber = ARGVScrubber.new ["hi mom"]
        args = scrubber.prepare!
        assert_equal "--help", args.first
      end

      def test_no_mutations
        scrubber = ARGVScrubber.new ["hi mom"].freeze
        args = scrubber.prepare!
        assert_equal "--help", args.first
      end

      def test_new_command_no_rc
        scrubber = Class.new(ARGVScrubber) {
          def self.default_rc_file
            File.join(Dir.tmpdir, "whatever")
          end
        }.new ["new"]
        args = scrubber.prepare!
        assert_equal [], args
      end

      def test_new_homedir_rc
        file = Tempfile.new "myrcfile"
        file.puts "--hello-world"
        file.flush

        message = nil
        scrubber = Class.new(ARGVScrubber) {
          define_singleton_method(:default_rc_file) do
            file.path
          end
          define_method(:puts) { |msg| message = msg }
        }.new ["new"]
        args = scrubber.prepare!
        assert_equal ["--hello-world"], args
        assert_match "hello-world", message
        assert_match file.path, message
      ensure
        file.close
        file.unlink
      end

      def test_rc_whitespace_separated
        file = Tempfile.new "myrcfile"
        file.puts "--hello --world"
        file.flush

        message = nil
        scrubber = Class.new(ARGVScrubber) {
          define_method(:puts) { |msg| message = msg }
        }.new ["new", "--rc=#{file.path}"]
        args = scrubber.prepare!
        assert_equal ["--hello", "--world"], args
      ensure
        file.close
        file.unlink
      end

      def test_new_rc_option
        file = Tempfile.new "myrcfile"
        file.puts "--hello-world"
        file.flush

        message = nil
        scrubber = Class.new(ARGVScrubber) {
          define_method(:puts) { |msg| message = msg }
        }.new ["new", "--rc=#{file.path}"]
        args = scrubber.prepare!
        assert_equal ["--hello-world"], args
        assert_match "hello-world", message
        assert_match file.path, message
      ensure
        file.close
        file.unlink
      end

      def test_new_rc_option_and_custom_options
        file = Tempfile.new "myrcfile"
        file.puts "--hello"
        file.puts "--world"
        file.flush

        scrubber = Class.new(ARGVScrubber) {
          define_method(:puts) { |msg| }
        }.new ["new", "tenderapp", "--love", "--rc=#{file.path}"]

        args = scrubber.prepare!
        assert_equal ["tenderapp", "--hello", "--world", "--love"], args
      ensure
        file.close
        file.unlink
      end

      def test_no_rc
        scrubber = ARGVScrubber.new ["new", "--no-rc"]
        args = scrubber.prepare!
        assert_equal [], args
      end
    end
  end
end