Skip to content

Commit

Permalink
Write partial results on failure
Browse files Browse the repository at this point in the history
Previously if a command failed you lose the resulting doc, including everything up to that point which might be salvalgable. With this change a partial result is written to disk on failure.
  • Loading branch information
schneems committed Nov 20, 2024
1 parent f611aca commit 5e07870
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rundoc/cli.rb
Original file line number Diff line number Diff line change
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
4 changes: 4 additions & 0 deletions lib/rundoc/code_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def render
self.class.to_doc(result: result, env: env)
end

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

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

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 5e07870

Please sign in to comment.