markdown-run 0.1.8 → 0.1.10
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 +4 -4
- data/.tool-versions +1 -0
- data/CHANGELOG.md +11 -0
- data/README.md +51 -2
- data/Rakefile +22 -0
- data/exe/markdown-run +6 -433
- data/lib/code_block_parser.rb +77 -0
- data/lib/code_executor.rb +212 -0
- data/lib/enum_helper.rb +17 -0
- data/lib/execution_decider.rb +225 -0
- data/lib/frontmatter_parser.rb +72 -0
- data/lib/language_configs.rb +53 -8
- data/lib/markdown/run/version.rb +1 -1
- data/lib/markdown_file_writer.rb +25 -0
- data/lib/markdown_processor.rb +342 -0
- data/lib/markdown_run.rb +20 -0
- metadata +49 -11
- data/test_markdown_exec.rb +0 -297
data/test_markdown_exec.rb
DELETED
@@ -1,297 +0,0 @@
|
|
1
|
-
require "bundler/inline"
|
2
|
-
gemfile(true) do
|
3
|
-
source "https://rubygems.org"
|
4
|
-
gem "minitest", "5.25.5" # Specify the required version
|
5
|
-
gem "rcodetools"
|
6
|
-
end
|
7
|
-
|
8
|
-
require "minitest/test"
|
9
|
-
require "minitest/autorun"
|
10
|
-
require "fileutils"
|
11
|
-
require "tmpdir"
|
12
|
-
|
13
|
-
# Load the main script to access process_markdown_file_main
|
14
|
-
load File.expand_path("./exe/markdown-run", __dir__)
|
15
|
-
|
16
|
-
# --- Minitest Test Class Definition ---
|
17
|
-
class TestMarkdownExec < Minitest::Test
|
18
|
-
def setup
|
19
|
-
@temp_dir = Dir.mktmpdir("markdown_exec_tests")
|
20
|
-
@test_md_file_path = File.join(@temp_dir, "test.md")
|
21
|
-
end
|
22
|
-
|
23
|
-
def teardown
|
24
|
-
FileUtils.remove_entry @temp_dir if @temp_dir && Dir.exist?(@temp_dir)
|
25
|
-
end
|
26
|
-
|
27
|
-
def create_md_file(content)
|
28
|
-
File.write(@test_md_file_path, content)
|
29
|
-
@test_md_file_path
|
30
|
-
end
|
31
|
-
|
32
|
-
def read_md_file
|
33
|
-
File.read(@test_md_file_path)
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_script_runs_without_error_on_empty_file
|
37
|
-
create_md_file("")
|
38
|
-
assert process_markdown_file_main(@test_md_file_path), "Processing empty file should succeed"
|
39
|
-
assert_equal "", read_md_file.strip, "Empty file should remain empty after processing"
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_psql_block_execution
|
43
|
-
skip "Skipping test_psql_block_execution on GitHub CI" if ENV['CI']
|
44
|
-
|
45
|
-
md_content = <<~MARKDOWN
|
46
|
-
```psql
|
47
|
-
SELECT 'hello psql test';
|
48
|
-
```
|
49
|
-
MARKDOWN
|
50
|
-
create_md_file(md_content)
|
51
|
-
process_markdown_file_main(@test_md_file_path)
|
52
|
-
|
53
|
-
expected_output = <<~MARKDOWN.strip
|
54
|
-
```psql
|
55
|
-
SELECT 'hello psql test';
|
56
|
-
```
|
57
|
-
|
58
|
-
```RESULT
|
59
|
-
hello psql test
|
60
|
-
```
|
61
|
-
MARKDOWN
|
62
|
-
assert_equal expected_output, read_md_file.strip
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_ruby_block_execution_and_result_generation
|
66
|
-
md_content = <<~MARKDOWN
|
67
|
-
```ruby
|
68
|
-
puts "Hello from Ruby"
|
69
|
-
p 1 + 2
|
70
|
-
```
|
71
|
-
MARKDOWN
|
72
|
-
create_md_file(md_content)
|
73
|
-
process_markdown_file_main(@test_md_file_path)
|
74
|
-
|
75
|
-
file_content = read_md_file
|
76
|
-
assert file_content.include?("```ruby\nputs \"Hello from Ruby\""), "Original Ruby code should be present"
|
77
|
-
assert file_content.include?("```ruby RESULT\n"), "Ruby RESULT block should be created"
|
78
|
-
assert file_content.include?("3"), "Output from p 1 + 2 should be in the result"
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_skip_execution_if_result_block_exists
|
82
|
-
original_content = <<~MARKDOWN
|
83
|
-
```psql
|
84
|
-
SELECT 'this should not run';
|
85
|
-
```
|
86
|
-
|
87
|
-
```RESULT
|
88
|
-
pre-existing result
|
89
|
-
```
|
90
|
-
MARKDOWN
|
91
|
-
create_md_file(original_content)
|
92
|
-
process_markdown_file_main(@test_md_file_path)
|
93
|
-
|
94
|
-
assert_equal original_content.strip, read_md_file.strip, "Should not execute if RESULT block exists"
|
95
|
-
end
|
96
|
-
|
97
|
-
def test_skip_execution_if_ruby_result_block_exists
|
98
|
-
original_content = <<~MARKDOWN
|
99
|
-
```ruby
|
100
|
-
puts "this should not run either"
|
101
|
-
```
|
102
|
-
|
103
|
-
```ruby RESULT
|
104
|
-
this is a pre-existing ruby result
|
105
|
-
```
|
106
|
-
MARKDOWN
|
107
|
-
create_md_file(original_content)
|
108
|
-
process_markdown_file_main(@test_md_file_path)
|
109
|
-
|
110
|
-
assert_equal original_content.strip, read_md_file.strip, "Should not execute if ```ruby RESULT block exists"
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_frontmatter_alias_functionality
|
114
|
-
skip "Skipping test_frontmatter_alias_functionality on GitHub CI" if ENV['CI']
|
115
|
-
|
116
|
-
md_content = <<~MARKDOWN
|
117
|
-
---
|
118
|
-
markdown-run:
|
119
|
-
alias:
|
120
|
-
- sql: psql
|
121
|
-
---
|
122
|
-
|
123
|
-
# Test Document
|
124
|
-
|
125
|
-
```sql
|
126
|
-
SELECT 'aliased to psql' as test;
|
127
|
-
```
|
128
|
-
MARKDOWN
|
129
|
-
create_md_file(md_content)
|
130
|
-
process_markdown_file_main(@test_md_file_path)
|
131
|
-
|
132
|
-
file_content = read_md_file
|
133
|
-
assert file_content.include?("```sql\nSELECT 'aliased to psql' as test;"), "Original SQL code should be present"
|
134
|
-
assert file_content.include?("```RESULT\n"), "RESULT block should be created for aliased language"
|
135
|
-
assert file_content.include?("aliased to psql"), "Output should contain the expected result"
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_rerun_functionality
|
139
|
-
# Test 1: Default behavior (no rerun option) should skip existing result
|
140
|
-
md_content_with_result = <<~MARKDOWN
|
141
|
-
```ruby
|
142
|
-
puts "Should not change: \#{Time.now.to_i}"
|
143
|
-
```
|
144
|
-
|
145
|
-
```ruby RESULT
|
146
|
-
Should not change: 999999999
|
147
|
-
```
|
148
|
-
MARKDOWN
|
149
|
-
create_md_file(md_content_with_result)
|
150
|
-
process_markdown_file_main(@test_md_file_path)
|
151
|
-
|
152
|
-
file_content = read_md_file
|
153
|
-
assert file_content.include?("Should not change: 999999999"), "Default behavior should preserve existing result"
|
154
|
-
refute file_content.match?(/Should not change: (?!999999999)\d+/), "Default behavior should not generate new timestamp"
|
155
|
-
|
156
|
-
# Test 2: rerun=false should skip existing result
|
157
|
-
md_content_rerun_false = <<~MARKDOWN
|
158
|
-
```ruby rerun=false
|
159
|
-
puts "Should not change either: \#{Time.now.to_i}"
|
160
|
-
```
|
161
|
-
|
162
|
-
```ruby RESULT
|
163
|
-
Should not change either: 888888888
|
164
|
-
```
|
165
|
-
MARKDOWN
|
166
|
-
create_md_file(md_content_rerun_false)
|
167
|
-
process_markdown_file_main(@test_md_file_path)
|
168
|
-
|
169
|
-
file_content = read_md_file
|
170
|
-
assert file_content.include?("Should not change either: 888888888"), "rerun=false should preserve existing result"
|
171
|
-
refute file_content.match?(/Should not change either: (?!888888888)\d+/), "rerun=false should not generate new timestamp"
|
172
|
-
|
173
|
-
# Test 3: rerun=true should replace existing result
|
174
|
-
md_content_rerun_true = <<~MARKDOWN
|
175
|
-
```ruby rerun=true
|
176
|
-
puts "Should change: \#{Time.now.to_i}"
|
177
|
-
```
|
178
|
-
|
179
|
-
```ruby RESULT
|
180
|
-
Should change: 777777777
|
181
|
-
```
|
182
|
-
MARKDOWN
|
183
|
-
create_md_file(md_content_rerun_true)
|
184
|
-
process_markdown_file_main(@test_md_file_path)
|
185
|
-
|
186
|
-
file_content = read_md_file
|
187
|
-
refute file_content.include?("Should change: 777777777"), "rerun=true should replace existing result"
|
188
|
-
assert file_content.match?(/Should change: \d+/), "rerun=true should generate new result with actual timestamp"
|
189
|
-
|
190
|
-
# Test 4: rerun=true with blank line before result block
|
191
|
-
md_content_rerun_true_blank = <<~MARKDOWN
|
192
|
-
```ruby rerun=true
|
193
|
-
puts "Should also change: \#{Time.now.to_i}"
|
194
|
-
```
|
195
|
-
|
196
|
-
```ruby RESULT
|
197
|
-
Should also change: 666666666
|
198
|
-
```
|
199
|
-
MARKDOWN
|
200
|
-
create_md_file(md_content_rerun_true_blank)
|
201
|
-
process_markdown_file_main(@test_md_file_path)
|
202
|
-
|
203
|
-
file_content = read_md_file
|
204
|
-
refute file_content.include?("Should also change: 666666666"), "rerun=true with blank line should replace existing result"
|
205
|
-
assert file_content.match?(/Should also change: \d+/), "rerun=true with blank line should generate new result"
|
206
|
-
end
|
207
|
-
|
208
|
-
def test_run_functionality
|
209
|
-
# Test 1: Default behavior (run=true implicit) should execute new code block
|
210
|
-
md_content_default = <<~MARKDOWN
|
211
|
-
```ruby
|
212
|
-
puts "Should execute by default"
|
213
|
-
```
|
214
|
-
MARKDOWN
|
215
|
-
create_md_file(md_content_default)
|
216
|
-
process_markdown_file_main(@test_md_file_path)
|
217
|
-
|
218
|
-
file_content = read_md_file
|
219
|
-
assert file_content.include?("```ruby RESULT"), "Default behavior should create result block"
|
220
|
-
assert file_content.include?("Should execute by default"), "Default behavior should execute and show output"
|
221
|
-
|
222
|
-
# Test 2: run=true explicit should execute new code block
|
223
|
-
md_content_run_true = <<~MARKDOWN
|
224
|
-
```ruby run=true
|
225
|
-
puts "Should execute with run=true"
|
226
|
-
```
|
227
|
-
MARKDOWN
|
228
|
-
create_md_file(md_content_run_true)
|
229
|
-
process_markdown_file_main(@test_md_file_path)
|
230
|
-
|
231
|
-
file_content = read_md_file
|
232
|
-
assert file_content.include?("```ruby RESULT"), "run=true should create result block"
|
233
|
-
assert file_content.include?("Should execute with run=true"), "run=true should execute and show output"
|
234
|
-
|
235
|
-
# Test 3: run=false should not execute at all (no result block created)
|
236
|
-
md_content_run_false = <<~MARKDOWN
|
237
|
-
```ruby run=false
|
238
|
-
puts "Should not execute"
|
239
|
-
puts "No result block should be created"
|
240
|
-
```
|
241
|
-
MARKDOWN
|
242
|
-
create_md_file(md_content_run_false)
|
243
|
-
process_markdown_file_main(@test_md_file_path)
|
244
|
-
|
245
|
-
file_content = read_md_file
|
246
|
-
refute file_content.include?("```ruby RESULT"), "run=false should not create result block"
|
247
|
-
refute file_content.match?(/puts "Should not execute"\n# >>/), "run=false should not execute code (no # >> output)"
|
248
|
-
assert file_content.include?("puts \"Should not execute\""), "run=false should preserve original code block"
|
249
|
-
|
250
|
-
# Test 4: run=false with existing result block should skip execution but preserve result
|
251
|
-
md_content_run_false_with_result = <<~MARKDOWN
|
252
|
-
```ruby run=false
|
253
|
-
puts "Should not execute"
|
254
|
-
```
|
255
|
-
|
256
|
-
```ruby RESULT
|
257
|
-
Old result that should be preserved
|
258
|
-
```
|
259
|
-
MARKDOWN
|
260
|
-
create_md_file(md_content_run_false_with_result)
|
261
|
-
process_markdown_file_main(@test_md_file_path)
|
262
|
-
|
263
|
-
file_content = read_md_file
|
264
|
-
assert file_content.include?("Old result that should be preserved"), "run=false should preserve existing result"
|
265
|
-
refute file_content.match?(/puts "Should not execute"\n# >>/), "run=false should not create new execution output"
|
266
|
-
|
267
|
-
# Test 5: Combined options - run=false with rerun=true should still not execute
|
268
|
-
md_content_combined = <<~MARKDOWN
|
269
|
-
```ruby run=false rerun=true
|
270
|
-
puts "Should not execute despite rerun=true"
|
271
|
-
```
|
272
|
-
|
273
|
-
```ruby RESULT
|
274
|
-
Existing result
|
275
|
-
```
|
276
|
-
MARKDOWN
|
277
|
-
create_md_file(md_content_combined)
|
278
|
-
process_markdown_file_main(@test_md_file_path)
|
279
|
-
|
280
|
-
file_content = read_md_file
|
281
|
-
assert file_content.include?("Existing result"), "run=false should override rerun=true"
|
282
|
-
refute file_content.match?(/puts "Should not execute despite rerun=true"\n# >>/), "run=false should prevent execution even with rerun=true"
|
283
|
-
|
284
|
-
# Test 6: Combined options - run=true with rerun=false should execute if no result exists
|
285
|
-
md_content_run_true_rerun_false = <<~MARKDOWN
|
286
|
-
```ruby run=true rerun=false
|
287
|
-
puts "Should execute because no result exists"
|
288
|
-
```
|
289
|
-
MARKDOWN
|
290
|
-
create_md_file(md_content_run_true_rerun_false)
|
291
|
-
process_markdown_file_main(@test_md_file_path)
|
292
|
-
|
293
|
-
file_content = read_md_file
|
294
|
-
assert file_content.include?("```ruby RESULT"), "run=true rerun=false should execute when no result exists"
|
295
|
-
assert file_content.include?("Should execute because no result exists"), "run=true rerun=false should show output when no result exists"
|
296
|
-
end
|
297
|
-
end
|