Skip to content

Commit

Permalink
Merge pull request #69 from zombocom/schneems/partial-render-too
Browse files Browse the repository at this point in the history
Preserve partial doc results on failure
  • Loading branch information
schneems authored Nov 20, 2024
2 parents 90c6464 + 1c483a6 commit b9493a6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

- Fix: Partial output of the document is now written to disk in a `RUNDOC_FAILED.md` file (https://github.com/zombocom/rundoc/pull/69)

## 3.0.1

- Fix: Save in-progress work in the "failure" directory when the rundoc command is interrupted via a signal such as `SIGTERM` (https://github.com/zombocom/rundoc/pull/67)
Expand Down
4 changes: 3 additions & 1 deletion lib/rundoc/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def call
output = begin
parser.to_md
rescue StandardError, SignalException => e
warn "Received exception: #{e.inspect}, cleaning up before re-raise"
io.puts "Received exception: #{e.inspect}, cleaning up before re-raise"
on_fail
raise e
end
Expand Down Expand Up @@ -190,6 +190,8 @@ def call
from: execution_context.output_dir,
to: on_failure_dir
)

on_failure_dir.join("RUNDOC_FAILED.md").write(Rundoc::CodeSection.partial_result_to_doc)
end

private def on_success(output)
Expand Down
16 changes: 16 additions & 0 deletions lib/rundoc/code_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def initialize(options = {})
AUTOGEN_WARNING = "\n<!-- STOP. This document is autogenerated. Do not manually modify. See the top of the doc for more details. -->"
attr_accessor :original, :fence, :lang, :code, :commands, :keyword

PARTIAL_RESULT = []
PARTIAL_ENV = {}

def initialize(match, keyword:, context:)
@original = match.to_s
@commands = []
Expand All @@ -40,6 +43,8 @@ def initialize(match, keyword:, context:)
@lang = match[:lang]
@code = match[:contents]
parse_code_command
PARTIAL_RESULT.clear
PARTIAL_ENV.clear
end

def render
Expand Down Expand Up @@ -69,10 +74,21 @@ def render
tmp_result << code_output if code_command.render_result?

result << tmp_result unless code_command.hidden?

PARTIAL_RESULT.replace(result)
PARTIAL_ENV.replace(env)
end

return "" if hidden?

self.class.to_doc(result: result, env: env)
end

def self.partial_result_to_doc
to_doc(result: PARTIAL_RESULT, env: PARTIAL_ENV)
end

def self.to_doc(result:, env:)
array = [env[:before]]

result.flatten!
Expand Down
46 changes: 46 additions & 0 deletions test/integration/failure_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "test_helper"

class IntegrationFailureTest < Minitest::Test
def test_writes_to_dir_on_failure
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
dir = Pathname(dir)

source_path = dir.join("RUNDOC.md")
source_path.write <<~EOF
```
:::>> $ mkdir lol
:::>> $ touch lol/rofl.txt
:::>> $ touch does/not/exist.txt
```
EOF

io = StringIO.new

error = nil
begin
Rundoc::CLI.new(
io: io,
source_path: source_path,
on_success_dir: dir.join(SUCCESS_DIRNAME)
).call
rescue => e
error = e
end

assert error
assert_includes error.message, "exited with non zero status"

refute dir.join(SUCCESS_DIRNAME).join("lol").exist?
refute dir.join(SUCCESS_DIRNAME).join("lol").join("rofl.txt").exist?

assert dir.join(FAILURE_DIRNAME).join("lol").exist?
assert dir.join(FAILURE_DIRNAME).join("lol").join("rofl.txt").exist?

doc = dir.join(FAILURE_DIRNAME).join("RUNDOC_FAILED.md").read
assert_includes doc, "$ mkdir lol"
assert_includes doc, "$ touch lol/rofl.txt"
end
end
end
end

0 comments on commit b9493a6

Please sign in to comment.