Class: Tng::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/tng/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



13
14
15
# File 'lib/generators/tng/install_generator.rb', line 13

def self.source_root
  @source_root ||= File.expand_path("templates", __dir__)
end

Instance Method Details

#create_tng_configurationObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/tng/install_generator.rb', line 17

def create_tng_configuration
  @test_framework = detect_test_framework
  @authentication_library = detect_authentication_library
  @authz_library = detect_authorization_library
  @factory_library = detect_factory_library
  @test_examples = detect_test_examples

  initializer_path = "config/initializers/tng.rb"

  if File.exist?(initializer_path)
    say "Configuration file already exists at #{initializer_path}", :yellow
    if yes?("Do you want to overwrite it? (y/n)")
      remove_file initializer_path
      create_file initializer_path, ruby_configuration_template
      say "TNG configuration file updated!", :green
    else
      say "Skipping configuration file creation.", :blue
      nil
    end
  else
    create_file initializer_path, ruby_configuration_template
    say "tng.sh configuration file created at #{initializer_path}", :green
  end
end

#ruby_configuration_templateObject



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
# File 'lib/generators/tng/install_generator.rb', line 42

def ruby_configuration_template
  framework_config = generate_framework_config(@test_framework)

  if @authentication_library && @authentication_library != "none"
    auth_enabled = "config.authentication_enabled = true"
    auth_comment = " (detected: #{@authentication_library})"
    auth_lib = "config.authentication_library = \"#{@authentication_library}\""
  else
    auth_enabled = "config.authentication_enabled = true"
    auth_comment = ""
    auth_lib = "config.authentication_library = nil"
  end

  if @authz_library && @authz_library != "none"
    authz_lib = "config.authorization_library = \"#{@authz_library}\""
    authz_comment = " (detected: #{@authz_library})"
  else
    authz_lib = "config.authorization_library = nil"
    authz_comment = ""
  end

  factory_lib = "config.factory_library = \"#{@factory_library}\""

  framework_specific = generate_framework_specific_config(@test_framework, framework_config)

  test_examples_config = @test_examples.any? ? @test_examples.inspect : "[]"
  [
    "# frozen_string_literal: true",
    "",
    "return unless Rails.env.development?",
    "",
    "Tng.configure do |config|",
    "  config.api_key = nil",
    "  # You dont need to change this url, unless you will instructed by the CLI.",
    "  config.base_url = \"https://app.tng.sh/\"",
    "",
    "  # Testing Framework",
    "  config.testing_framework = \"#{@test_framework}\"      # Options: minitest, rspec",
    framework_specific,
    "  config.mock_library = \"#{framework_config["mock_library"]}\"      # Options: mocha, minitest/mock, rspec-mocks, nil",
    "  config.http_mock_library = \"#{framework_config["http_mock_library"]}\"       # Options: webmock, vcr, httparty, nil",
    "  #{factory_lib}        # Options: factory_bot, factory_girl, fabrication, fabricator, fixtures, active_record",
    "",
    "  # Test Examples",
    "  # Example test files for LLM to learn patterns and reduce hallucinations",
    "  # Format: [{{\"name\" => \"test_name\", \"path\" => \"spec/models/user_spec.rb\"}}]",
    "  # Leave empty [] to auto-detect from project",
    "  config.test_examples = #{test_examples_config}",
    "",
    "  # Source Code Reading Configuration",
    "  # When enabled (true), TNG will only read the file where the method is located",
    "  # and will not analyze other files in the project. This may increase the accuracy of the tests,",
    "  # but it may also increase the time it takes to generate the tests.",
    "  config.read_file_source_code = false      # Options: true, false",
    "",
    "  # Authentication#{auth_comment}",
    "  #{auth_enabled}      # Options: true, false",
    "  #{auth_lib}       # Options: devise, clearance, sorcery, nil",
    "",
    "",
    "  # ⚠️  IMPORTANT: AUTHENTICATION CONFIGURATION REQUIRED ⚠️",
    "  # You MUST configure your authentication methods below for TNG to work properly.",
    "  # Uncomment and modify the authentication_methods configuration:",
    "",
    "  # Authentication Methods (multiple methods supported)",
    "  # Supported authentication types: session, devise, jwt, token_auth, basic_auth, oauth, headers, custom, nil",
    "  # EXAMPLE: Uncomment and modify these examples to match your app's authentication:",
    "",
    "  # config.authentication_methods = [",
    "    # {",
    "    #   method: \"authenticate_user_via_session!\",",
    "    #   file_location: \"app/controllers/application_controller.rb\",",
    "    #   auth_type: \"session\"",
    "    # },",
    "    # {",
    "    #   method: \"authenticate_user_via_api_key!\",",
    "    #   file_location: \"app/controllers/application_controller.rb\",",
    "    #   auth_type: \"headers\"",
    "    # }",
    "  # ]",
    "  # ⚠️  Remember to configure your authentication methods above! ⚠️",
    "",
    "  # Authorization#{authz_comment}",
    "  #{authz_lib}      # Options: cancancan, pundit, rolify, nil",
    "end"
  ].join("\n") + "\n"
end