Class: Dotcodegen::TestFileGenerator
- Inherits:
-
Object
- Object
- Dotcodegen::TestFileGenerator
- Defined in:
- lib/dotcodegen/test_file_generator.rb
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#matchers ⇒ Object
readonly
Returns the value of attribute matchers.
-
#openai_key ⇒ Object
readonly
Returns the value of attribute openai_key.
-
#openai_org_id ⇒ Object
readonly
Returns the value of attribute openai_org_id.
Instance Method Summary collapse
- #ensure_test_file_presence ⇒ Object
-
#initialize(file_path:, matchers:, openai_key:, openai_org_id: nil) ⇒ TestFileGenerator
constructor
A new instance of TestFileGenerator.
- #matcher ⇒ Object
- #open_test_file_in_editor ⇒ Object
- #relative_file_name ⇒ Object
- #root_path ⇒ Object
- #run ⇒ Object
- #test_file_path ⇒ Object
- #test_file_suffix ⇒ Object
- #test_root_path ⇒ Object
- #write_generated_code_to_test_file ⇒ Object
Constructor Details
#initialize(file_path:, matchers:, openai_key:, openai_org_id: nil) ⇒ TestFileGenerator
Returns a new instance of TestFileGenerator.
10 11 12 13 14 15 |
# File 'lib/dotcodegen/test_file_generator.rb', line 10 def initialize(file_path:, matchers:, openai_key:, openai_org_id: nil) @file_path = file_path @matchers = matchers @openai_key = openai_key @openai_org_id = openai_org_id end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
8 9 10 |
# File 'lib/dotcodegen/test_file_generator.rb', line 8 def file_path @file_path end |
#matchers ⇒ Object (readonly)
Returns the value of attribute matchers.
8 9 10 |
# File 'lib/dotcodegen/test_file_generator.rb', line 8 def matchers @matchers end |
#openai_key ⇒ Object (readonly)
Returns the value of attribute openai_key.
8 9 10 |
# File 'lib/dotcodegen/test_file_generator.rb', line 8 def openai_key @openai_key end |
#openai_org_id ⇒ Object (readonly)
Returns the value of attribute openai_org_id.
8 9 10 |
# File 'lib/dotcodegen/test_file_generator.rb', line 8 def openai_org_id @openai_org_id end |
Instance Method Details
#ensure_test_file_presence ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/dotcodegen/test_file_generator.rb', line 30 def ensure_test_file_presence puts "Creating test file if it doesn't exist..." return if File.exist?(test_file_path) FileUtils.mkdir_p(File.dirname(test_file_path)) File.write(test_file_path, '') end |
#matcher ⇒ Object
50 51 52 |
# File 'lib/dotcodegen/test_file_generator.rb', line 50 def matcher @matcher ||= matchers.find { |m| file_path.match?(m['regex']) } end |
#open_test_file_in_editor ⇒ Object
46 47 48 |
# File 'lib/dotcodegen/test_file_generator.rb', line 46 def open_test_file_in_editor system("code #{test_file_path}") end |
#relative_file_name ⇒ Object
58 59 60 |
# File 'lib/dotcodegen/test_file_generator.rb', line 58 def relative_file_name file_path.sub(root_path, '').sub(/\.\w+$/, '') end |
#root_path ⇒ Object
66 67 68 |
# File 'lib/dotcodegen/test_file_generator.rb', line 66 def root_path matcher['root_path'] || '' end |
#run ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dotcodegen/test_file_generator.rb', line 17 def run puts "Finding matcher for #{file_path}..." return puts "No matcher found for #{file_path}" unless matcher puts "Test file path: #{test_file_path}" ensure_test_file_presence write_generated_code_to_test_file open_test_file_in_editor unless $running_tests puts 'Running codegen...' end |
#test_file_path ⇒ Object
54 55 56 |
# File 'lib/dotcodegen/test_file_generator.rb', line 54 def test_file_path @test_file_path ||= "#{test_root_path}#{relative_file_name}#{test_file_suffix}" end |
#test_file_suffix ⇒ Object
70 71 72 |
# File 'lib/dotcodegen/test_file_generator.rb', line 70 def test_file_suffix matcher['test_file_suffix'] || '' end |
#test_root_path ⇒ Object
62 63 64 |
# File 'lib/dotcodegen/test_file_generator.rb', line 62 def test_root_path matcher['test_root_path'] || '' end |
#write_generated_code_to_test_file ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/dotcodegen/test_file_generator.rb', line 38 def write_generated_code_to_test_file generated_code = Dotcodegen::TestCodeGenerator.new(config: matcher, file_to_test_path: file_path, openai_key:, openai_org_id:).generate_test_code File.write(test_file_path, generated_code) end |