Class: Dotcodegen::TestFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/dotcodegen/test_file_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (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

#matchersObject (readonly)

Returns the value of attribute matchers.



8
9
10
# File 'lib/dotcodegen/test_file_generator.rb', line 8

def matchers
  @matchers
end

#openai_keyObject (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_idObject (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_presenceObject



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

#matcherObject



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_editorObject



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_nameObject



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_pathObject



66
67
68
# File 'lib/dotcodegen/test_file_generator.rb', line 66

def root_path
  matcher['root_path'] || ''
end

#runObject



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_pathObject



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_suffixObject



70
71
72
# File 'lib/dotcodegen/test_file_generator.rb', line 70

def test_file_suffix
  matcher['test_file_suffix'] || ''
end

#test_root_pathObject



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_fileObject



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