Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative require #84

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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