File: zeitwerk_checker_test.rb

package info (click to toggle)
rails 2%3A7.2.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,348 kB
  • sloc: ruby: 349,797; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (83 lines) | stat: -rw-r--r-- 2,169 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require "isolation/abstract_unit"
require "rails/zeitwerk_checker"

class ZeitwerkCheckerTest < ActiveSupport::TestCase
  include ActiveSupport::Testing::Isolation

  def setup
    build_app
  end

  def boot(env = "development")
    app(env)
  end

  test "returns an empty list for a default application" do
    boot

    assert_empty Rails::ZeitwerkChecker.check
  end

  test "raises if there is a missing constants in autoload_paths" do
    app_file "app/models/user.rb", ""

    boot

    e = assert_raises(Zeitwerk::NameError) do
      Rails::ZeitwerkChecker.check
    end
    assert_includes e.message, "expected file #{app_path}/app/models/user.rb to define constant User"
  end

  test "raises if there is a missing constant in autoload_once_paths" do
    app_dir "extras"
    app_file "extras/x.rb", ""

    add_to_config 'config.autoload_once_paths << "#{Rails.root}/extras"'
    add_to_config 'config.eager_load_paths << "#{Rails.root}/extras"'

    boot

    e = assert_raises(Zeitwerk::NameError) do
      Rails::ZeitwerkChecker.check
    end
    assert_includes e.message, "expected file #{Rails.root}/extras/x.rb to define constant X"
  end

  test "returns an empty list unchecked directories do not exist" do
    add_to_config 'config.autoload_paths << "#{Rails.root}/dir1"'
    add_to_config 'config.autoload_once_paths << "#{Rails.root}/dir2"'

    boot

    assert_empty Rails::ZeitwerkChecker.check
  end

  test "returns an empty list if unchecked directories are empty" do
    app_dir "dir1"
    add_to_config 'config.autoload_paths << "#{Rails.root}/dir1"'

    app_dir "dir2"
    add_to_config 'config.autoload_once_paths << "#{Rails.root}/dir2"'

    boot

    assert_empty Rails::ZeitwerkChecker.check
  end

  test "returns unchecked directories" do
    app_dir "dir1"
    app_file "dir1/x.rb", "X = 1"
    add_to_config 'config.autoload_paths << "#{Rails.root}/dir1"'

    app_dir "dir2"
    app_file "dir2/y.rb", "Y = 1"
    add_to_config 'config.autoload_once_paths << "#{Rails.root}/dir2"'

    boot

    assert_equal ["#{app_path}/dir1", "#{app_path}/dir2"], Rails::ZeitwerkChecker.check.sort
  end
end