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