conjure_shield 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/conjure_shield/analyzer.rb +762 -0
- data/lib/conjure_shield/railtie.rb +35 -0
- data/lib/conjure_shield/tasks/conjure_shield_tasks.rake +150 -0
- data/lib/conjure_shield/test_generator.rb +2336 -0
- data/lib/conjure_shield/version.rb +3 -0
- data/lib/conjure_shield.rb +30 -0
- data/lib/generators/conjure_shield/install_generator.rb +194 -0
- metadata +125 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module ConjureShield
|
|
2
|
+
class Railtie < Rails::Railtie
|
|
3
|
+
rake_tasks do
|
|
4
|
+
# This glob finds all .rake files in your tasks directory
|
|
5
|
+
Dir[File.join(File.dirname(__FILE__), '/tasks/*.rake')].each { |f| load f }
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
initializer "conjureshield.post_install_message" do |app|
|
|
9
|
+
next unless Rails.env.development?
|
|
10
|
+
next if ConjureShield.install_shown
|
|
11
|
+
|
|
12
|
+
ConjureShield.install_shown = true
|
|
13
|
+
|
|
14
|
+
puts "\n" + "=" * 60
|
|
15
|
+
puts "š”ļø ConjureShield - Rails Test Generator Installed!"
|
|
16
|
+
puts "=" * 60
|
|
17
|
+
puts "\nš What it does:"
|
|
18
|
+
puts " ⢠Analyzes your Rails models, controllers, and callbacks"
|
|
19
|
+
puts " ⢠Identifies missing test coverage"
|
|
20
|
+
puts " ⢠Generates skeleton RSpec example files (all content commented out)"
|
|
21
|
+
puts "\nš Available Rake Tasks:"
|
|
22
|
+
puts " rake conjureshield:validate - Check Rails setup"
|
|
23
|
+
puts " rake conjureshield:analyze - Analyze codebase"
|
|
24
|
+
puts " rake conjureshield:generate - Generate example files"
|
|
25
|
+
puts " rake conjureshield:check_tests - Check coverage"
|
|
26
|
+
puts " rake conjureshield:full - Run all tasks"
|
|
27
|
+
puts "\nš” Quick Start:"
|
|
28
|
+
puts " 1. Run: rake conjureshield:full"
|
|
29
|
+
puts " 2. Review suggestions & open generated spec/ files"
|
|
30
|
+
puts " 3. Uncomment and adapt tests to match your app logic"
|
|
31
|
+
puts "\nš Learn more: https://github.com/plombix-pro/ConjureShield"
|
|
32
|
+
puts "=" * 60 + "\n"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
namespace :conjureshield do
|
|
3
|
+
desc "Validate Rails project setup for ConjureShield"
|
|
4
|
+
task :validate do
|
|
5
|
+
codebase = File.expand_path(ENV.fetch("CODEBASE_PATH") { Dir.pwd })
|
|
6
|
+
all_passed = true
|
|
7
|
+
|
|
8
|
+
# 1. Verify if it is a Rails app by checking for bin/rails
|
|
9
|
+
rails_bin = File.join(codebase, "bin", "rails")
|
|
10
|
+
if File.exist?(rails_bin) && File.executable?(rails_bin)
|
|
11
|
+
puts "ā
Rails project detected"
|
|
12
|
+
else
|
|
13
|
+
puts "ā Not a valid Rails project (bin/rails not found or not executable)"
|
|
14
|
+
all_passed = false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# 2. Check for database.yml (Optional but recommended)
|
|
18
|
+
db_config = File.join(codebase, "config", "database.yml")
|
|
19
|
+
if File.exist?(db_config)
|
|
20
|
+
puts "ā
config/database.yml"
|
|
21
|
+
else
|
|
22
|
+
puts "ā config/database.yml missing"
|
|
23
|
+
all_passed = false
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# 3. Handle 'spec/' directory with auto-creation
|
|
27
|
+
spec_path = File.join(codebase, "spec")
|
|
28
|
+
if Dir.exist?(spec_path)
|
|
29
|
+
puts "ā
spec/"
|
|
30
|
+
else
|
|
31
|
+
puts "ā ļø spec/ directory missing. Creating it for you..."
|
|
32
|
+
FileUtils.mkdir_p(spec_path)
|
|
33
|
+
puts "ā
spec/ (created successfully)"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# 4. Final status
|
|
37
|
+
if all_passed
|
|
38
|
+
puts "\nā
Rails project is ready for ConjureShield!"
|
|
39
|
+
else
|
|
40
|
+
puts "\nā ļø Some checks failed. Please fix the issues above."
|
|
41
|
+
exit 1
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
desc "Analyze Rails app and show missing tests"
|
|
46
|
+
task :analyze do
|
|
47
|
+
codebase = ENV.fetch("CODEBASE_PATH") { Dir.pwd }
|
|
48
|
+
codebase = File.expand_path(codebase)
|
|
49
|
+
|
|
50
|
+
puts "š ConjureShield - Rails Test Generator"
|
|
51
|
+
puts "=" * 50
|
|
52
|
+
puts "š Analyzing: #{codebase}"
|
|
53
|
+
puts "=" * 50
|
|
54
|
+
|
|
55
|
+
analyzer = ConjureShield.analyze(codebase)
|
|
56
|
+
|
|
57
|
+
puts "\nš Analysis Results:"
|
|
58
|
+
puts "-" * 50
|
|
59
|
+
puts "Files analyzed: #{analyzer.files.count}"
|
|
60
|
+
puts "Models found: #{analyzer.ast_nodes.count { |n| n[:type] == :model }}"
|
|
61
|
+
puts "Controllers found: #{analyzer.ast_nodes.count { |n| n[:type] == :controller }}"
|
|
62
|
+
puts "Missing tests: #{analyzer.missing_tests.count}"
|
|
63
|
+
|
|
64
|
+
if analyzer.missing_tests.any?
|
|
65
|
+
puts "\nš Suggested Tests:"
|
|
66
|
+
puts "-" * 50
|
|
67
|
+
analyzer.missing_tests.each_with_index do |test, i|
|
|
68
|
+
puts "\n#{i + 1}. #{test[:type].to_s.capitalize}"
|
|
69
|
+
test[:suggestions]&.each do |suggestion|
|
|
70
|
+
puts " ⢠#{suggestion[:name]}"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
desc "Generate test files based on analysis"
|
|
77
|
+
task :generate => :analyze do
|
|
78
|
+
codebase = ENV.fetch("CODEBASE_PATH") { Dir.pwd }
|
|
79
|
+
codebase = File.expand_path(codebase)
|
|
80
|
+
|
|
81
|
+
rails_helper = File.join(codebase, "spec", "rails_helper.rb")
|
|
82
|
+
spec_helper = File.join(codebase, "spec", "spec_helper.rb")
|
|
83
|
+
unless File.exist?(rails_helper) || File.exist?(spec_helper)
|
|
84
|
+
puts "\nā ļø spec/rails_helper.rb or spec/spec_helper.rb not found."
|
|
85
|
+
puts " Running rails generate conjure_shield:install first..."
|
|
86
|
+
Rails::Generators.invoke("conjure_shield:install", [], destination_root: codebase)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
puts "\nšÆ Generating example test files..."
|
|
90
|
+
puts "=" * 50
|
|
91
|
+
|
|
92
|
+
analyzer = ConjureShield.analyze(codebase)
|
|
93
|
+
|
|
94
|
+
if analyzer.missing_tests.any?
|
|
95
|
+
puts "š Generating #{analyzer.missing_tests.count} example file(s)..."
|
|
96
|
+
ConjureShield::TestGenerator.generate_for_all_frameworks(analyzer.files, analyzer.missing_tests, codebase)
|
|
97
|
+
|
|
98
|
+
generated_dirs = []
|
|
99
|
+
generated_dirs << "spec/" if Dir.exist?(File.join(codebase, "spec"))
|
|
100
|
+
generated_dirs << "test/" if Dir.exist?(File.join(codebase, "test"))
|
|
101
|
+
puts "\nā
Example files generated in #{generated_dirs.join(" and ")}. All content is commented out."
|
|
102
|
+
puts ""
|
|
103
|
+
puts "ā ļø These are skeleton examples only. To use them:"
|
|
104
|
+
puts "ā ļø 1. Open each generated file in #{generated_dirs.first} or #{generated_dirs.last}"
|
|
105
|
+
puts "ā ļø 2. Uncomment the relevant portions"
|
|
106
|
+
puts "ā ļø 3. Adapt assertions to match your actual application logic"
|
|
107
|
+
puts ""
|
|
108
|
+
else
|
|
109
|
+
puts "ā ļø No missing tests detected. All features appear to be covered."
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
desc "Check test coverage and setup"
|
|
114
|
+
task :check_tests do
|
|
115
|
+
codebase = ENV.fetch("CODEBASE_PATH") { Dir.pwd }
|
|
116
|
+
codebase = File.expand_path(codebase)
|
|
117
|
+
analyzer = ConjureShield.analyze(codebase)
|
|
118
|
+
|
|
119
|
+
puts "š ConjureShield - Test Setup Check"
|
|
120
|
+
puts "=" * 50
|
|
121
|
+
|
|
122
|
+
puts "\nš Test Infrastructure:"
|
|
123
|
+
puts "-" * 50
|
|
124
|
+
puts "RSpec tests: #{Dir.glob(File.join(codebase, "spec/**/*.rb")).count}"
|
|
125
|
+
puts "Minitest tests: #{Dir.glob(File.join(codebase, "test/**/*.rb")).count}"
|
|
126
|
+
|
|
127
|
+
puts "\nš Code Analysis:"
|
|
128
|
+
puts "-" * 50
|
|
129
|
+
puts "Models: #{analyzer.ast_nodes.count { |n| n[:type] == :model }}"
|
|
130
|
+
puts "Controllers: #{analyzer.ast_nodes.count { |n| n[:type] == :controller }}"
|
|
131
|
+
|
|
132
|
+
puts "\nš Coverage Analysis:"
|
|
133
|
+
puts "-" * 50
|
|
134
|
+
models = analyzer.ast_nodes.select { |n| n[:type] == :model }
|
|
135
|
+
model_prefixes = models.map { |m| "#{m[:model].underscore}_" }
|
|
136
|
+
|
|
137
|
+
models.each do |model|
|
|
138
|
+
model_prefix = "#{model[:model].underscore}_"
|
|
139
|
+
longer_prefixes = model_prefixes.select { |p| p != model_prefix && p.start_with?(model_prefix) }
|
|
140
|
+
|
|
141
|
+
all = Dir.glob(File.join(codebase, "spec", "#{model_prefix}*_spec.rb"))
|
|
142
|
+
all.reject! { |f| longer_prefixes.any? { |lp| File.basename(f).start_with?(lp) } }
|
|
143
|
+
|
|
144
|
+
puts " #{model[:model]}: #{all.count} test file(s) found"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
desc "Run all tasks: validate, analyze, generate, check"
|
|
149
|
+
task :full => [:validate, :analyze, :generate, :check_tests]
|
|
150
|
+
end
|