Skip to content

Commit

Permalink
Merge pull request #84 from zombocom/schneems/fix-require-relative
Browse files Browse the repository at this point in the history
Fix relative require
  • Loading branch information
schneems authored Nov 28, 2024
2 parents e1eab47 + fa49d11 commit 292f6b8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## HEAD

- Fix: Using `rundoc.require` inside of a document that was `rundoc.require`-d now sources files from the correct relative document path (https://github.com/zombocom/rundoc/pull/84)
- Fix: `rundoc <file> --with-contents <dir>` now expands the path passed in to the `--with-contents` so relative paths can safely be used (https://github.com/zombocom/rundoc/pull/83)

## 3.1.1
Expand Down
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 292f6b8

Please sign in to comment.