Class: RubyIndexer::ConfigurationTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/ruby_indexer/test/configuration_test.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



8
9
10
11
12
# File 'lib/ruby_indexer/test/configuration_test.rb', line 8

def setup
  @config = Configuration.new
  @workspace_path = File.expand_path(File.join("..", "..", ".."), __dir__)
  @config.workspace_path = @workspace_path
end

#test_configuration_raises_for_unknown_keysObject



112
113
114
115
116
# File 'lib/ruby_indexer/test/configuration_test.rb', line 112

def test_configuration_raises_for_unknown_keys
  assert_raises(ArgumentError) do
    @config.apply_config({ "unknown_config" => 123 })
  end
end

#test_does_not_fail_if_there_are_missing_specs_due_to_platform_constraintsObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/ruby_indexer/test/configuration_test.rb', line 222

def test_does_not_fail_if_there_are_missing_specs_due_to_platform_constraints
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      File.write(File.join(dir, "Gemfile"), "        source \"https://rubygems.org\"\n        gem \"ruby-lsp\", path: \"\#{Bundler.root}\"\n\n        platforms :windows do\n          gem \"tzinfo\"\n          gem \"tzinfo-data\"\n        end\n      RUBY\n\n      Bundler.with_unbundled_env do\n        capture_subprocess_io { system(\"bundle install\") }\n\n        _stdout, stderr = capture_subprocess_io do\n          script = [\n            \"require \\\"ruby_lsp/internal\\\"\",\n            \"RubyIndexer::Configuration.new.indexable_uris\",\n          ].join(\";\")\n\n          system(\"bundle exec ruby -e '\#{script}'\")\n        end\n\n        assert_empty(stderr)\n      end\n    end\n  end\nend\n")

#test_includes_top_level_filesObject



176
177
178
179
180
181
182
183
184
# File 'lib/ruby_indexer/test/configuration_test.rb', line 176

def test_includes_top_level_files
  Dir.mktmpdir do |dir|
    FileUtils.touch(File.join(dir, "find_me.rb"))
    @config.workspace_path = dir

    uris = @config.indexable_uris
    assert(uris.find { |u| File.basename(u.full_path) == "find_me.rb" })
  end
end

#test_indexable_uris_avoids_duplicates_if_bundle_path_is_inside_projectObject



74
75
76
77
78
79
80
# File 'lib/ruby_indexer/test/configuration_test.rb', line 74

def test_indexable_uris_avoids_duplicates_if_bundle_path_is_inside_project
  Bundler.settings.temporary(path: "vendor/bundle") do
    config = Configuration.new

    assert_includes(config.instance_variable_get(:@excluded_patterns), "vendor/bundle/**/*.rb")
  end
end

#test_indexable_uris_does_not_include_default_gem_path_when_in_bundleObject



52
53
54
55
# File 'lib/ruby_indexer/test/configuration_test.rb', line 52

def test_indexable_uris_does_not_include_default_gem_path_when_in_bundle
  uris = @config.indexable_uris
  assert(uris.none? { |uri| uri.full_path.start_with?("#{RbConfig::CONFIG["rubylibdir"]}/psych") })
end

#test_indexable_uris_does_not_include_gems_own_installed_filesObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_indexer/test/configuration_test.rb', line 82

def test_indexable_uris_does_not_include_gems_own_installed_files
  uris = @config.indexable_uris
  uris_inside_bundled_lsp = uris.select do |uri|
    uri.full_path.start_with?(Bundler.bundle_path.join("gems", "ruby-lsp").to_s)
  end

  assert_empty(
    uris_inside_bundled_lsp,
    "Indexable URIs should not include files from the gem currently being worked on. " \
      "Included: #{uris_inside_bundled_lsp.map(&:full_path)}",
  )
end

#test_indexable_uris_does_not_include_non_ruby_files_inside_rubylibdirObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_indexer/test/configuration_test.rb', line 95

def test_indexable_uris_does_not_include_non_ruby_files_inside_rubylibdir
  path = Pathname.new(RbConfig::CONFIG["rubylibdir"]).join("extra_file.txt").to_s
  FileUtils.touch(path)

  begin
    uris = @config.indexable_uris
    assert(uris.none? { |uri| uri.full_path == path })
  ensure
    FileUtils.rm(path)
  end
end

#test_indexable_uris_have_expanded_full_pathsObject



27
28
29
30
31
32
33
# File 'lib/ruby_indexer/test/configuration_test.rb', line 27

def test_indexable_uris_have_expanded_full_paths
  @config.apply_config({ "included_patterns" => ["**/*.rb"] })
  uris = @config.indexable_uris

  # All paths should be expanded
  assert(uris.all? { |uri| File.absolute_path?(uri.full_path) })
end

#test_indexable_uris_includes_default_gemsObject



57
58
59
60
61
62
# File 'lib/ruby_indexer/test/configuration_test.rb', line 57

def test_indexable_uris_includes_default_gems
  paths = @config.indexable_uris.map(&:full_path)

  assert_includes(paths, "#{RbConfig::CONFIG["rubylibdir"]}/pathname.rb")
  assert_includes(paths, "#{RbConfig::CONFIG["rubylibdir"]}/ipaddr.rb")
end

#test_indexable_uris_includes_project_filesObject



64
65
66
67
68
69
70
71
72
# File 'lib/ruby_indexer/test/configuration_test.rb', line 64

def test_indexable_uris_includes_project_files
  paths = @config.indexable_uris.map(&:full_path)

  Dir.glob("#{Dir.pwd}/lib/**/*.rb").each do |path|
    next if path.end_with?("_test.rb")

    assert_includes(paths, path)
  end
end

#test_indexable_uris_only_includes_gem_require_pathsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_indexer/test/configuration_test.rb', line 35

def test_indexable_uris_only_includes_gem_require_paths
  uris = @config.indexable_uris

  Bundler.locked_gems.specs.each do |lazy_spec|
    next if lazy_spec.name == "ruby-lsp"

    spec = Gem::Specification.find_by_name(lazy_spec.name)

    test_uris = uris.select do |uri|
      File.fnmatch?(File.join(spec.full_gem_path, "test/**/*"), uri.full_path, File::Constants::FNM_PATHNAME)
    end
    assert_empty(test_uris)
  rescue Gem::MissingSpecError
    # Transitive dependencies might be missing when running tests on Windows
  end
end

#test_indexable_uris_respect_given_workspace_pathObject



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
# File 'lib/ruby_indexer/test/configuration_test.rb', line 138

def test_indexable_uris_respect_given_workspace_path
  Dir.mktmpdir do |dir|
    FileUtils.mkdir(File.join(dir, "ignore"))
    FileUtils.touch(File.join(dir, "ignore", "file0.rb"))
    FileUtils.touch(File.join(dir, "file1.rb"))
    FileUtils.touch(File.join(dir, "file2.rb"))

    @config.apply_config({ "excluded_patterns" => ["ignore/**/*.rb"] })
    @config.workspace_path = dir

    uris = @config.indexable_uris
    assert(uris.none? { |uri| uri.full_path.start_with?(File.join(dir, "ignore")) })

    # The regular default gem path is ~/.rubies/3.4.1/lib/ruby/3.4.0
    # The alternative default gem path is ~/.rubies/3.4.1/lib/ruby/gems/3.4.0
    # Here part_1 contains ~/.rubies/3.4.1/lib/ruby/ and part_2 contains 3.4.0, so that we can turn it into the
    # alternative path
    part_1, part_2 = Pathname.new(RbConfig::CONFIG["rubylibdir"]).split
    other_default_gem_dir = part_1.join("gems").join(part_2).to_s

    # After switching the workspace path, all indexable URIs will be found in one of these places:
    # - The new workspace path
    # - The Ruby LSP's own code (because Bundler is requiring the dependency from source)
    # - Bundled gems
    # - Default gems
    # - Other default gem directory
    assert(
      uris.all? do |u|
        u.full_path.start_with?(dir) ||
        u.full_path.start_with?(File.join(Dir.pwd, "lib")) ||
        u.full_path.start_with?(Bundler.bundle_path.to_s) ||
        u.full_path.start_with?(RbConfig::CONFIG["rubylibdir"]) ||
        u.full_path.start_with?(other_default_gem_dir)
      end,
    )
  end
end

#test_indexables_include_non_test_files_in_test_directoriesObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/ruby_indexer/test/configuration_test.rb', line 253

def test_indexables_include_non_test_files_in_test_directories
  # In order to linearize test parent classes and accurately detect the framework being used, then intermediate
  # parent classes _must_ also be indexed. Otherwise, we have no way of linearizing the rest of the ancestors to
  # determine what the test class ultimately inherits from.
  #
  # Therefore, we need to ensure that test files are excluded, but non test files inside test directories have to be
  # indexed
  FileUtils.touch("test/test_case.rb")

  uris = @config.indexable_uris
  project_paths = uris.filter_map do |uri|
    path = uri.full_path
    next if path.start_with?(Bundler.bundle_path.to_s) || path.start_with?(RbConfig::CONFIG["rubylibdir"])

    Pathname.new(path).relative_path_from(Dir.pwd).to_s
  end

  begin
    assert_includes(project_paths, "test/requests/support/expectations_test_runner.rb")
    assert_includes(project_paths, "test/test_helper.rb")
    assert_includes(project_paths, "test/test_case.rb")
  ensure
    FileUtils.rm("test/test_case.rb")
  end
end

#test_load_configuration_executes_configure_blockObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_indexer/test/configuration_test.rb', line 14

def test_load_configuration_executes_configure_block
  @config.apply_config({ "excluded_patterns" => ["**/fixtures/**/*"] })
  uris = @config.indexable_uris

  bundle_path = Bundler.bundle_path.join("gems")

  assert(uris.none? { |uri| uri.full_path.include?("test/fixtures") })
  assert(uris.none? { |uri| uri.full_path.include?(bundle_path.join("minitest-reporters").to_s) })
  assert(uris.none? { |uri| uri.full_path.include?(bundle_path.join("ansi").to_s) })
  assert(uris.any? { |uri| uri.full_path.include?(bundle_path.join("prism").to_s) })
  assert(uris.none? { |uri| uri.full_path == __FILE__ })
end

#test_magic_comments_regexObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ruby_indexer/test/configuration_test.rb', line 118

def test_magic_comments_regex
  regex = @config.magic_comment_regex

  [
    "# frozen_string_literal:",
    "# typed:",
    "# compiled:",
    "# encoding:",
    "# shareable_constant_value:",
    "# warn_indent:",
    "# rubocop:",
    "# nodoc:",
    "# doc:",
    "# coding:",
    "# warn_past_scope:",
  ].each do |comment|
    assert_match(regex, comment)
  end
end

#test_paths_are_uniqueObject



107
108
109
110
# File 'lib/ruby_indexer/test/configuration_test.rb', line 107

def test_paths_are_unique
  uris = @config.indexable_uris
  assert_equal(uris.uniq.length, uris.length)
end

#test_transitive_dependencies_for_non_dev_gems_are_not_excludedObject



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
# File 'lib/ruby_indexer/test/configuration_test.rb', line 186

def test_transitive_dependencies_for_non_dev_gems_are_not_excluded
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      # Both IRB and debug depend on reline. Since IRB is in the default group, reline should not be excluded
      File.write(File.join(dir, "Gemfile"), "        source \"https://rubygems.org\"\n        gem \"irb\"\n        gem \"ruby-lsp\", path: \"\#{Bundler.root}\"\n\n        group :development do\n          gem \"debug\"\n        end\n      RUBY\n\n      Bundler.with_unbundled_env do\n        capture_subprocess_io do\n          system(\"bundle install\")\n        end\n\n        stdout, _stderr = capture_subprocess_io do\n          script = [\n            \"require \\\"ruby_lsp/internal\\\"\",\n            \"print RubyIndexer::Configuration.new.instance_variable_get(:@excluded_gems).join(\\\",\\\")\",\n          ].join(\";\")\n          system(\"bundle exec ruby -e '\#{script}'\")\n        end\n\n        excluded_gems = stdout.split(\",\")\n        assert_includes(excluded_gems, \"debug\")\n        refute_includes(excluded_gems, \"reline\")\n        refute_includes(excluded_gems, \"irb\")\n      end\n    end\n  end\nend\n")