restore: print JSON versions of errors in --json mode
Previously, they were printed as freeform text. This also adds a ui.Terminal interface to make writing tests easier and also adds a few tests.
This commit is contained in:
parent
ad2585af67
commit
a376323331
17 changed files with 234 additions and 70 deletions
|
@ -4,11 +4,13 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/test"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
func createJSONProgress() (*mockTerm, ProgressPrinter) {
|
||||
term := &mockTerm{}
|
||||
func createJSONProgress() (*ui.MockTerminal, ProgressPrinter) {
|
||||
term := &ui.MockTerminal{}
|
||||
printer := NewJSONProgress(term, 3)
|
||||
return term, printer
|
||||
}
|
||||
|
@ -16,31 +18,31 @@ func createJSONProgress() (*mockTerm, ProgressPrinter) {
|
|||
func TestJSONPrintUpdate(t *testing.T) {
|
||||
term, printer := createJSONProgress()
|
||||
printer.Update(State{3, 11, 0, 29, 47, 0}, 5*time.Second)
|
||||
test.Equals(t, []string{"{\"message_type\":\"status\",\"seconds_elapsed\":5,\"percent_done\":0.6170212765957447,\"total_files\":11,\"files_restored\":3,\"total_bytes\":47,\"bytes_restored\":29}\n"}, term.output)
|
||||
test.Equals(t, []string{"{\"message_type\":\"status\",\"seconds_elapsed\":5,\"percent_done\":0.6170212765957447,\"total_files\":11,\"files_restored\":3,\"total_bytes\":47,\"bytes_restored\":29}\n"}, term.Output)
|
||||
}
|
||||
|
||||
func TestJSONPrintUpdateWithSkipped(t *testing.T) {
|
||||
term, printer := createJSONProgress()
|
||||
printer.Update(State{3, 11, 2, 29, 47, 59}, 5*time.Second)
|
||||
test.Equals(t, []string{"{\"message_type\":\"status\",\"seconds_elapsed\":5,\"percent_done\":0.6170212765957447,\"total_files\":11,\"files_restored\":3,\"files_skipped\":2,\"total_bytes\":47,\"bytes_restored\":29,\"bytes_skipped\":59}\n"}, term.output)
|
||||
test.Equals(t, []string{"{\"message_type\":\"status\",\"seconds_elapsed\":5,\"percent_done\":0.6170212765957447,\"total_files\":11,\"files_restored\":3,\"files_skipped\":2,\"total_bytes\":47,\"bytes_restored\":29,\"bytes_skipped\":59}\n"}, term.Output)
|
||||
}
|
||||
|
||||
func TestJSONPrintSummaryOnSuccess(t *testing.T) {
|
||||
term, printer := createJSONProgress()
|
||||
printer.Finish(State{11, 11, 0, 47, 47, 0}, 5*time.Second)
|
||||
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":11,\"total_bytes\":47,\"bytes_restored\":47}\n"}, term.output)
|
||||
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":11,\"total_bytes\":47,\"bytes_restored\":47}\n"}, term.Output)
|
||||
}
|
||||
|
||||
func TestJSONPrintSummaryOnErrors(t *testing.T) {
|
||||
term, printer := createJSONProgress()
|
||||
printer.Finish(State{3, 11, 0, 29, 47, 0}, 5*time.Second)
|
||||
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":3,\"total_bytes\":47,\"bytes_restored\":29}\n"}, term.output)
|
||||
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":3,\"total_bytes\":47,\"bytes_restored\":29}\n"}, term.Output)
|
||||
}
|
||||
|
||||
func TestJSONPrintSummaryOnSuccessWithSkipped(t *testing.T) {
|
||||
term, printer := createJSONProgress()
|
||||
printer.Finish(State{11, 11, 2, 47, 47, 59}, 5*time.Second)
|
||||
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":11,\"files_skipped\":2,\"total_bytes\":47,\"bytes_restored\":47,\"bytes_skipped\":59}\n"}, term.output)
|
||||
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":11,\"files_skipped\":2,\"total_bytes\":47,\"bytes_restored\":47,\"bytes_skipped\":59}\n"}, term.Output)
|
||||
}
|
||||
|
||||
func TestJSONPrintCompleteItem(t *testing.T) {
|
||||
|
@ -57,6 +59,12 @@ func TestJSONPrintCompleteItem(t *testing.T) {
|
|||
} {
|
||||
term, printer := createJSONProgress()
|
||||
printer.CompleteItem(data.action, "test", data.size)
|
||||
test.Equals(t, []string{data.expected}, term.output)
|
||||
test.Equals(t, []string{data.expected}, term.Output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONError(t *testing.T) {
|
||||
term, printer := createJSONProgress()
|
||||
test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil)
|
||||
test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":\"error \\\"message\\\"\",\"during\":\"restore\",\"item\":\"/path\"}\n"}, term.Errors)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue