Skip to content

Commit

Permalink
Fix relative require
Browse files Browse the repository at this point in the history
Requiring from one file to another works, requiring one file that requires another doesn't. This change fixes that.

The problem was the execution context was being threaded through unchanged, instead it should have been modifying the source path to reflect that a different document was being executed.

Close #80
  • Loading branch information
schneems committed Nov 28, 2024
1 parent e1eab47 commit d13dbc3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/rundoc/code_command/rundoc/require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ def to_md(env = {})
end

def call(env = {})
document_path = @path.expand_path(env[:context].source_dir)
execution_context = env[:context]
document_path = @path.expand_path(execution_context.source_dir)

puts "rundoc.require: Start executing #{@path.to_s.inspect}"
output = Rundoc::Parser.new(
document_path.read,
context: env[:context]
context: Rundoc::Context::Execution.new(
source_path: document_path,
output_dir: execution_context.output_dir,
screenshots_dirname: execution_context.screenshots_dir,
with_contents_dir: execution_context.with_contents_dir
)
).to_md

if render_result?
Expand Down
34 changes: 34 additions & 0 deletions test/integration/require_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,38 @@ def test_require_runs_code_but_embeds_nothing_if_hidden
end
end
end

def test_require_is_relative_to_current_file_not_source_file
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
dir = Pathname(dir)

source_path = dir.join("RUNDOC.md")
source_path.write <<~EOF
```
:::>> rundoc.require "./src/a.md"
```
EOF

dir.join("src").tap(&:mkpath).join("a.md").write <<~EOF
```
:::-> rundoc.require "./b.md"
```
EOF

dir.join("src").join("b.md").write <<~EOF
```
:::-> print.text Hello World!
```
EOF

parsed = parse_contents(
source_path.read,
source_path: source_path
)
actual = parsed.to_md.gsub(Rundoc::CodeSection::AUTOGEN_WARNING, "")
assert_equal "Hello World!", actual.strip
end
end
end
end

0 comments on commit d13dbc3

Please sign in to comment.