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



109
110
111
112
113
# File 'lib/ruby_indexer/test/configuration_test.rb', line 109

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

#test_indexables_avoids_duplicates_if_bundle_path_is_inside_projectObject



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

def test_indexables_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_indexables_does_not_include_default_gem_path_when_in_bundleObject



46
47
48
49
50
51
52
# File 'lib/ruby_indexer/test/configuration_test.rb', line 46

def test_indexables_does_not_include_default_gem_path_when_in_bundle
  indexables = @config.indexables

  assert(
    indexables.none? { |indexable| indexable.full_path.start_with?("#{RbConfig::CONFIG["rubylibdir"]}/psych") },
  )
end

#test_indexables_does_not_include_gems_own_installed_filesObject



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

def test_indexables_does_not_include_gems_own_installed_files
  indexables = @config.indexables
  indexables_inside_bundled_lsp = indexables.select do |indexable|
    indexable.full_path.start_with?(Bundler.bundle_path.join("gems", "ruby-lsp").to_s)
  end

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

#test_indexables_does_not_include_non_ruby_files_inside_rubylibdirObject



93
94
95
96
97
98
99
100
101
# File 'lib/ruby_indexer/test/configuration_test.rb', line 93

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

  assert(indexables.none? { |indexable| indexable.full_path == path })
ensure
  FileUtils.rm(T.must(path))
end

#test_indexables_have_expanded_full_pathsObject



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

def test_indexables_have_expanded_full_paths
  @config.apply_config({ "included_patterns" => ["**/*.rb"] })
  indexables = @config.indexables

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

#test_indexables_includes_default_gemsObject



54
55
56
57
58
59
60
# File 'lib/ruby_indexer/test/configuration_test.rb', line 54

def test_indexables_includes_default_gems
  indexables = @config.indexables.map(&:full_path)

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

#test_indexables_includes_project_filesObject



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

def test_indexables_includes_project_files
  indexables = @config.indexables.map(&:full_path)

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

    assert_includes(indexables, path)
  end
end

#test_indexables_only_includes_gem_require_pathsObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_indexer/test/configuration_test.rb', line 33

def test_indexables_only_includes_gem_require_paths
  indexables = @config.indexables

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

    spec = Gem::Specification.find_by_name(lazy_spec.name)
    assert(indexables.none? { |indexable| indexable.full_path.start_with?("#{spec.full_gem_path}/test/") })
  rescue Gem::MissingSpecError
    # Transitive dependencies might be missing when running tests on Windows
  end
end

#test_indexables_respect_given_workspace_pathObject



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

def test_indexables_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
    indexables = @config.indexables

    assert(indexables.none? { |indexable| indexable.full_path.start_with?(File.join(dir, "ignore")) })

    # After switching the workspace path, all indexables 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
    assert(
      indexables.all? do |i|
        i.full_path.start_with?(dir) ||
        i.full_path.start_with?(File.join(Dir.pwd, "lib")) ||
        i.full_path.start_with?(Bundler.bundle_path.to_s) ||
        i.full_path.start_with?(RbConfig::CONFIG["rubylibdir"])
      end,
    )
  end
end

#test_load_configuration_executes_configure_blockObject



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

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

  assert(indexables.none? { |indexable| indexable.full_path.include?("test/fixtures") })
  assert(indexables.none? { |indexable| indexable.full_path.include?("minitest-reporters") })
  assert(indexables.none? { |indexable| indexable.full_path.include?("ansi") })
  assert(indexables.any? { |indexable| indexable.full_path.include?("sorbet-runtime") })
  assert(indexables.none? { |indexable| indexable.full_path == __FILE__ })
end

#test_magic_comments_regexObject



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

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



103
104
105
106
107
# File 'lib/ruby_indexer/test/configuration_test.rb', line 103

def test_paths_are_unique
  indexables = @config.indexables

  assert_equal(indexables.uniq.length, indexables.length)
end