File: configuration_options.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (238 lines) | stat: -rw-r--r-- 7,383 bytes parent folder | download
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require 'erb'
require 'shellwords'

module RSpec
  module Core
    # Responsible for utilizing externally provided configuration options,
    # whether via the command line, `.rspec`, `~/.rspec`,
    # `$XDG_CONFIG_HOME/rspec/options`, `.rspec-local` or a custom options
    # file.
    class ConfigurationOptions
      # @param args [Array<String>] command line arguments
      def initialize(args)
        @args = args.dup
        organize_options
      end

      # Updates the provided {Configuration} instance based on the provided
      # external configuration options.
      #
      # @param config [Configuration] the configuration instance to update
      def configure(config)
        process_options_into config
        configure_filter_manager config.filter_manager
        load_formatters_into config
      end

      # @api private
      # Updates the provided {FilterManager} based on the filter options.
      # @param filter_manager [FilterManager] instance to update
      def configure_filter_manager(filter_manager)
        @filter_manager_options.each do |command, value|
          filter_manager.__send__ command, value
        end
      end

      # @return [Hash] the final merged options, drawn from all external sources
      attr_reader :options

      # @return [Array<String>] the original command-line arguments
      attr_reader :args

    private

      def organize_options
        @filter_manager_options = []

        @options = (file_options << command_line_options << env_options).each do |opts|
          @filter_manager_options << [:include, opts.delete(:inclusion_filter)] if opts.key?(:inclusion_filter)
          @filter_manager_options << [:exclude, opts.delete(:exclusion_filter)] if opts.key?(:exclusion_filter)
        end

        @options = @options.inject(:libs => [], :requires => []) do |hash, opts|
          hash.merge(opts) do |key, oldval, newval|
            [:libs, :requires].include?(key) ? oldval + newval : newval
          end
        end
      end

      UNFORCED_OPTIONS = Set.new([
        :requires, :profile, :drb, :libs, :files_or_directories_to_run,
        :full_description, :full_backtrace, :tty
      ])

      UNPROCESSABLE_OPTIONS = Set.new([:formatters])

      def force?(key)
        !UNFORCED_OPTIONS.include?(key)
      end

      def order(keys)
        OPTIONS_ORDER.reverse_each do |key|
          keys.unshift(key) if keys.delete(key)
        end
        keys
      end

      OPTIONS_ORDER = [
        # It's important to set this before anything that might issue a
        # deprecation (or otherwise access the reporter).
        :deprecation_stream,

        # In order for `RSpec.configuration.dry_run?` to return `true` during
        # processing the `requires` option, it must be parsed before it.
        :dry_run,

        # load paths depend on nothing, but must be set before `requires`
        # to support load-path-relative requires.
        :libs,

        # `files_or_directories_to_run` uses `default_path` so it must be
        # set before it.
        :default_path, :only_failures,

        # These must be set before `requires` to support checking
        # `config.files_to_run` from within `spec_helper.rb` when a
        # `-rspec_helper` option is used.
        :files_or_directories_to_run, :pattern, :exclude_pattern,

        # Necessary so that the `--seed` option is applied before requires,
        # in case required files do something with the provided seed.
        # (such as seed global randomization with it).
        :order,

        # In general, we want to require the specified files as early as
        # possible. The `--require` option is specifically intended to allow
        # early requires. For later requires, they can just put the require in
        # their spec files, but `--require` provides a unique opportunity for
        # users to instruct RSpec to load an extension file early for maximum
        # flexibility.
        :requires
      ]

      def process_options_into(config)
        opts = options.reject { |k, _| UNPROCESSABLE_OPTIONS.include? k }

        order(opts.keys).each do |key|
          force?(key) ? config.force(key => opts[key]) : config.__send__("#{key}=", opts[key])
        end
      end

      def load_formatters_into(config)
        options[:formatters].each { |pair| config.add_formatter(*pair) } if options[:formatters]
      end

      def file_options
        if custom_options_file
          [custom_options]
        else
          [global_options, project_options, local_options]
        end
      end

      def env_options
        return {} unless ENV['SPEC_OPTS']

        parse_args_ignoring_files_or_dirs_to_run(
          Shellwords.split(ENV["SPEC_OPTS"]),
          "ENV['SPEC_OPTS']"
        )
      end

      def command_line_options
        @command_line_options ||= Parser.parse(@args)
      end

      def custom_options
        options_from(custom_options_file)
      end

      def local_options
        @local_options ||= options_from(local_options_file)
      end

      def project_options
        @project_options ||= options_from(project_options_file)
      end

      def global_options
        @global_options ||= options_from(global_options_file)
      end

      def options_from(path)
        args = args_from_options_file(path)
        parse_args_ignoring_files_or_dirs_to_run(args, path)
      end

      def parse_args_ignoring_files_or_dirs_to_run(args, source)
        options = Parser.parse(args, source)
        options.delete(:files_or_directories_to_run)
        options
      end

      def args_from_options_file(path)
        return [] unless path && File.exist?(path)
        config_string = options_file_as_erb_string(path)
        config_lines = config_string.split(/\n+/).reject { |s| s =~ /\A\s*#/ }
        FlatMap.flat_map(config_lines, &:shellsplit)
      end

      def options_file_as_erb_string(path)
        if RUBY_VERSION >= '2.6'
          ERB.new(File.read(path), :trim_mode => '-').result(binding)
        else
          ERB.new(File.read(path), nil, '-').result(binding)
        end
      end

      def custom_options_file
        command_line_options[:custom_options_file]
      end

      def project_options_file
        "./.rspec"
      end

      def local_options_file
        "./.rspec-local"
      end

      def global_options_file
        xdg_options_file_if_exists || home_options_file_path
      end

      def xdg_options_file_if_exists
        path = xdg_options_file_path
        if path && File.exist?(path)
          path
        end
      end

      def home_options_file_path
        File.join(File.expand_path("~"), ".rspec")
      rescue ArgumentError
        # :nocov:
        RSpec.warning "Unable to find ~/.rspec because the HOME environment variable is not set"
        nil
        # :nocov:
      end

      def xdg_options_file_path
        xdg_config_home = resolve_xdg_config_home
        if xdg_config_home
          File.join(xdg_config_home, "rspec", "options")
        end
      end

      def resolve_xdg_config_home
        File.expand_path(ENV.fetch("XDG_CONFIG_HOME", "~/.config"))
      rescue ArgumentError
        # :nocov:
        # On Ruby 2.4, `File.expand("~")` works even if `ENV['HOME']` is not set.
        # But on earlier versions, it fails.
        nil
        # :nocov:
      end
    end
  end
end