Skip to content

Commit 0236729

Browse files
fix specs
1 parent d18359c commit 0236729

11 files changed

Lines changed: 157 additions & 10 deletions

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ GEM
1717
minitest (6.0.5)
1818
drb (~> 2.0)
1919
prism (~> 1.5)
20+
minitest-mock (5.27.0)
2021
ostruct (0.6.1)
2122
path_expander (2.0.1)
2223
prism (1.9.0)
@@ -38,6 +39,7 @@ DEPENDENCIES
3839
flog
3940
markdown-run!
4041
minitest (= 6.0.5)
42+
minitest-mock (~> 5.27)
4143
rake
4244
simplecov (~> 0.22)
4345

exe/markdown-run

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
#!/usr/bin/env ruby
22

3+
require "optparse"
34
require_relative "../lib/markdown_run"
45

56
# Script to process markdown files, execute code blocks based on language,
67
# and insert their results back into the markdown.
78

9+
option_parser = OptionParser.new do |parser|
10+
parser.banner = "Usage: markdown-run <file.md>"
11+
parser.separator ""
12+
parser.separator "Processes a markdown file and executes code blocks, inserting their results."
13+
parser.separator ""
14+
parser.separator "Options:"
15+
16+
parser.on("-h", "--help", "Show this help message") do
17+
puts parser
18+
exit 0 unless $0 != __FILE__ # Don't exit when loaded as a library
19+
end
20+
end
21+
22+
option_parser.parse!
23+
824
if ARGV.empty?
9-
puts "Usage: markdown-run <file.md>"
10-
puts "Processes a markdown file and executes code blocks, inserting their results."
25+
puts option_parser
1126
exit 1 unless $0 != __FILE__ # Don't exit when loaded as a library
1227
else
1328
success = MarkdownRun.run_code_blocks(ARGV[0])
1429
exit success ? 0 : 1 unless $0 != __FILE__ # Don't exit when loaded as a library
1530
end
16-

lib/code_executor.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "json"
12
require "tempfile"
23
require "open3"
34
require_relative "language_configs"
@@ -36,16 +37,17 @@ def execute_with_config(code_content, lang_config, temp_dir, lang_key, input_fil
3637
temp_file_suffix = lang_config[:temp_file_suffix]
3738

3839
if temp_file_suffix
39-
execute_with_temp_file(code_content, cmd_lambda, temp_file_suffix, temp_dir, lang_key, input_file_path, explain, flamegraph)
40+
write_code_to_temp_file = lang_config.fetch(:write_code_to_temp_file, true)
41+
execute_with_temp_file(code_content, cmd_lambda, temp_file_suffix, temp_dir, lang_key, input_file_path, explain, flamegraph, write_code_to_temp_file)
4042
else
4143
execute_direct_command(code_content, cmd_lambda, input_file_path, explain, flamegraph)
4244
end
4345
end
4446

45-
def execute_with_temp_file(code_content, cmd_lambda, temp_file_suffix, temp_dir, lang_key, input_file_path = nil, explain = false, flamegraph = false)
47+
def execute_with_temp_file(code_content, cmd_lambda, temp_file_suffix, temp_dir, lang_key, input_file_path = nil, explain = false, flamegraph = false, write_code_to_temp_file = true)
4648
result = nil
4749
Tempfile.create([lang_key, temp_file_suffix], temp_dir) do |temp_file|
48-
temp_file.write(code_content)
50+
temp_file.write(code_content) if write_code_to_temp_file
4951
temp_file.close
5052
command_to_run, exec_options = cmd_lambda.call(**{
5153
code_content: code_content,

lib/language_configs.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
SQLITE_CONFIG = {
2121
command: proc { |code_content: nil, temp_file_path: nil, **| [ "sqlite3 #{temp_file_path}", { stdin_data: code_content } ] },
22-
temp_file_suffix: ".db" # Temp file is the database
22+
temp_file_suffix: ".db", # Temp file is the database
23+
write_code_to_temp_file: false
2324
}.freeze
2425

2526
SUPPORTED_LANGUAGES = {
@@ -46,7 +47,8 @@
4647
else
4748
[ "#{psql_cmd} -A -t -X", { stdin_data: code_content } ]
4849
end
49-
}
50+
},
51+
result_block_type: "plain"
5052
},
5153
"ruby" => {
5254
command: proc { |temp_file_path: nil, **|

lib/result_helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ def mermaid_style_result?(lang)
1616
lang_config && lang_config[:result_handling] == :mermaid_svg
1717
end
1818

19+
def plain_style_result?(lang)
20+
lang_config = SUPPORTED_LANGUAGES[lang]
21+
lang_config && lang_config[:result_block_type] == "plain"
22+
end
23+
1924
def result_block_header(lang)
20-
ruby_style_result?(lang) ? "```ruby\n" : "``` {result}\n"
25+
return "```ruby\n" if ruby_style_result?(lang)
26+
return "```RESULT\n" if plain_style_result?(lang)
27+
28+
"``` {result}\n"
2129
end
2230

2331
def result_block_regex(lang)
@@ -27,6 +35,8 @@ def result_block_regex(lang)
2735
elsif ruby_style_result?(lang)
2836
# For ruby, check for old-style ```ruby RESULT blocks (for backward compatibility during migration)
2937
/^```ruby\s+RESULT$/i
38+
elsif plain_style_result?(lang)
39+
/^```\s*RESULT$/i
3040
else
3141
/^```\s*\{result\}$/i
3242
end

lib/tasks/test_profiler.rake

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
desc "Run tests with detailed timing information"
2+
task :test_profile do
3+
require 'benchmark'
4+
5+
puts "Running tests with detailed profiling..."
6+
7+
# Run tests with verbose output and capture timing
8+
output = `bundle exec rake test TESTOPTS="-v" 2>&1`
9+
10+
# Extract test timings
11+
timings = []
12+
output.scan(/^(.+) = ([0-9]+\.[0-9]+) s = \.$/) do |test_name, time|
13+
timings << [test_name, time.to_f]
14+
end
15+
16+
# Sort by time (descending)
17+
timings.sort_by! { |_, time| -time }
18+
19+
puts "\n" + "="*80
20+
puts "TOP 15 SLOWEST TESTS"
21+
puts "="*80
22+
23+
timings.first(15).each_with_index do |(test_name, time), index|
24+
printf "%2d. %-60s %6.2f s\n", index + 1, test_name.truncate(60), time
25+
end
26+
27+
puts "\n" + "="*80
28+
puts "SUMMARY"
29+
puts "="*80
30+
total_time = timings.sum { |_, time| time }
31+
slow_tests = timings.select { |_, time| time > 0.1 }
32+
33+
puts "Total test time: #{total_time.round(2)} seconds"
34+
puts "Tests slower than 0.1s: #{slow_tests.count}"
35+
puts "Time spent in slow tests: #{slow_tests.sum { |_, time| time }.round(2)} seconds (#{((slow_tests.sum { |_, time| time } / total_time) * 100).round(1)}%)"
36+
37+
# Show original test output
38+
puts "\n" + "="*80
39+
puts "FULL TEST OUTPUT"
40+
puts "="*80
41+
puts output
42+
end
43+
44+
# String extension for truncation
45+
class String
46+
def truncate(max_length, omission = "...")
47+
if length > max_length
48+
self[0, max_length - omission.length] + omission
49+
else
50+
self
51+
end
52+
end
53+
end
Lines changed: 29 additions & 0 deletions
Loading

markdown-run.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
3939
spec.add_dependency 'ostruct', '0.6.1'
4040

4141
spec.add_development_dependency 'minitest', "6.0.5"
42+
spec.add_development_dependency 'minitest-mock', '~> 5.27'
4243
spec.add_development_dependency 'rake'
4344
spec.add_development_dependency 'flog'
4445
spec.add_development_dependency 'simplecov', '~> 0.22'

test/cli_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'minitest/autorun'
2+
require 'open3'
3+
require 'rbconfig'
4+
5+
class TestCli < Minitest::Test
6+
def test_help_matches_empty_invocation
7+
empty_stdout, empty_stderr, empty_status = run_cli
8+
help_stdout, help_stderr, help_status = run_cli("--help")
9+
10+
assert_equal help_stdout, empty_stdout
11+
assert_equal "", empty_stderr
12+
assert_equal "", help_stderr
13+
assert_equal 1, empty_status.exitstatus
14+
assert_equal 0, help_status.exitstatus
15+
assert_includes help_stdout, "Usage: markdown-run <file.md>"
16+
assert_includes help_stdout, "-h, --help"
17+
end
18+
19+
private
20+
21+
def run_cli(*args)
22+
bundler_env = ENV.each_key.each_with_object({ "RUBYOPT" => nil }) do |key, env|
23+
env[key] = nil if key.start_with?("BUNDLE")
24+
end
25+
26+
Open3.capture3(bundler_env, RbConfig.ruby, File.expand_path("../exe/markdown-run", __dir__), *args)
27+
end
28+
end

test/language_configs_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def test_psql_config_when_available
8484
end
8585
end
8686

87+
def test_psql_config_uses_plain_result_blocks
88+
assert_equal "plain", SUPPORTED_LANGUAGES["psql"][:result_block_type]
89+
end
90+
8791
def test_psql_config_when_unavailable
8892
# Stub PostgresHelper to indicate psql is not available
8993
PostgresHelper.stub :available?, false do
@@ -259,9 +263,10 @@ def test_all_supported_languages_have_command
259263

260264
def test_sqlite_config_properties
261265
assert_equal ".db", SQLITE_CONFIG[:temp_file_suffix]
266+
assert_equal false, SQLITE_CONFIG[:write_code_to_temp_file]
262267

263268
command, options = SQLITE_CONFIG[:command].call(**{ code_content: "SELECT 1;", temp_file_path: "/tmp/test.db" })
264269
assert_equal "sqlite3 /tmp/test.db", command
265270
assert_equal({ stdin_data: "SELECT 1;" }, options)
266271
end
267-
end
272+
end

0 commit comments

Comments
 (0)