From d13dbc34d6816de80d9db535d5b189e23a8592ea Mon Sep 17 00:00:00 2001 From: Schneems Date: Wed, 27 Nov 2024 21:44:57 -0600 Subject: [PATCH] Fix relative require 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 --- lib/rundoc/code_command/rundoc/require.rb | 11 ++++++-- test/integration/require_test.rb | 34 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/rundoc/code_command/rundoc/require.rb b/lib/rundoc/code_command/rundoc/require.rb index 8cf6655..6943dbd 100644 --- a/lib/rundoc/code_command/rundoc/require.rb +++ b/lib/rundoc/code_command/rundoc/require.rb @@ -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? diff --git a/test/integration/require_test.rb b/test/integration/require_test.rb index b709ae2..4a56a2a 100644 --- a/test/integration/require_test.rb +++ b/test/integration/require_test.rb @@ -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