Skip to content

Commit da1ff41

Browse files
committed
[DOCS] Updates example docs generator to eval the code
This update adds a test module which instantiates an Elasticsearch client and runs `eval` on the generated example code. It catches exceptions for both Elasticsearch responses and client responses, logging them to different files: - logs/docs-generation-elasticsearch.log - logs/docs-generation-client.log This way we can generate the code, run it and check if there are errors in the documentation, the client or the code generation. It generates all the available examples and mutes all outputs in the console, replacing it with a progress status.
1 parent 5ba9a5c commit da1ff41

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

rake_tasks/doc_generator.rake

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
require 'json'
1919
require 'fileutils'
20+
require 'logger'
2021

2122
namespace :docs do
2223
SRC_FILE = "#{__dir__}/docs/parsed_alternative_report.json".freeze
@@ -29,13 +30,11 @@ namespace :docs do
2930
FileUtils.remove_dir(TARGET_DIR)
3031
Dir.mkdir(TARGET_DIR)
3132

32-
# Only select the files in the EXAMPLES_TO_PARSE array and that are not
33-
# console result examples
34-
entries = json_data.select do |d|
35-
EXAMPLES_TO_PARSE.include? d['source_location']['file']
36-
end.select { |d| d['lang'] == 'console' }
37-
38-
entries.each do |entry|
33+
entries = json_data.select { |d| d['lang'] == 'console' }
34+
entries.each_with_index do |entry, index|
35+
percentage = index * 100 / entries.length
36+
print "\r" + ("\e[A\e[K") if index > 0
37+
puts "Generating file #{index + 1} of #{entries.length} - #{percentage}% complete"
3938
generate_docs(entry)
4039
end
4140
end
@@ -45,11 +44,12 @@ namespace :docs do
4544
end
4645

4746
def generate_docs(entry)
48-
file_name = "#{entry['digest']}.asciidoc"
47+
filename = "#{entry['digest']}.asciidoc"
4948
unless entry['parsed_source'].empty?
5049
api = entry['parsed_source'].first['api']
5150
code = build_client_query(api, entry)
52-
write_file(code, file_name)
51+
TestDocs::perform(code, filename)
52+
write_file(code, filename)
5353
end
5454
end
5555

@@ -63,7 +63,8 @@ namespace :docs do
6363
request_body << show_parameters(params) if params
6464
body = entry&.[]('body')
6565
request_body << show_body(body) if body
66-
request_body = request_body.compact.join(",\n")
66+
request_body = request_body.compact.join(",\n").gsub('null', 'nil')
67+
6768
code = "response = client.#{api}(\n#{request_body}\n)\nputs response"
6869
client_query << format_code(code)
6970
end
@@ -76,7 +77,7 @@ namespace :docs do
7677
f.puts code
7778
end
7879
# Format code:
79-
system("rubocop --config #{__dir__}/docs_rubocop_config.yml --format autogenconf -a ./temp.rb")
80+
system("rubocop --config #{__dir__}/docs_rubocop_config.yml -o /dev/null -a ./temp.rb")
8081
# Read it back
8182
template = File.read('./temp.rb')
8283
File.delete('./temp.rb')
@@ -122,3 +123,28 @@ namespace :docs do
122123
end
123124
end
124125
end
126+
127+
#
128+
# Test module to run the generated code
129+
#
130+
module TestDocs
131+
require 'elasticsearch'
132+
@formatter = -> (_, d, _, msg) { "#{d}: #{msg}" }
133+
134+
def self.perform(code, filename)
135+
# Eval the example code, but remove printing out the response
136+
eval(code.gsub('puts response', ''))
137+
rescue Elastic::Transport::Transport::Error => e
138+
logger = Logger.new('log/docs-generation-elasticsearch.log')
139+
logger.formatter = @formatter
140+
logger.info("Located in #{filename}: #{e.message}\n")
141+
rescue ArgumentError => e
142+
logger = Logger.new('log/docs-generation-client.log')
143+
logger.formatter = @formatter
144+
logger.info("Located in #{filename}: #{e.message}\n")
145+
end
146+
147+
def self.client
148+
@client ||= Elasticsearch::Client.new(trace: false, log: false)
149+
end
150+
end

0 commit comments

Comments
 (0)