mirror of
https://github.com/restic/restic.git
synced 2025-10-27 20:09:41 +00:00
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:
@@ -7,14 +7,13 @@ import (
|
||||
"github.com/restic/restic/internal/archiver"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
"github.com/restic/restic/internal/ui/termstatus"
|
||||
)
|
||||
|
||||
// JSONProgress reports progress for the `backup` command in JSON.
|
||||
type JSONProgress struct {
|
||||
*ui.Message
|
||||
|
||||
term *termstatus.Terminal
|
||||
term ui.Terminal
|
||||
v uint
|
||||
}
|
||||
|
||||
@@ -22,7 +21,7 @@ type JSONProgress struct {
|
||||
var _ ProgressPrinter = &JSONProgress{}
|
||||
|
||||
// NewJSONProgress returns a new backup progress reporter.
|
||||
func NewJSONProgress(term *termstatus.Terminal, verbosity uint) *JSONProgress {
|
||||
func NewJSONProgress(term ui.Terminal, verbosity uint) *JSONProgress {
|
||||
return &JSONProgress{
|
||||
Message: ui.NewMessage(term, verbosity),
|
||||
term: term,
|
||||
|
||||
27
internal/ui/backup/json_test.go
Normal file
27
internal/ui/backup/json_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/test"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
func createJSONProgress() (*ui.MockTerminal, ProgressPrinter) {
|
||||
term := &ui.MockTerminal{}
|
||||
printer := NewJSONProgress(term, 3)
|
||||
return term, printer
|
||||
}
|
||||
|
||||
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\":\"archival\",\"item\":\"/path\"}\n"}, term.Errors)
|
||||
}
|
||||
|
||||
func TestJSONScannerError(t *testing.T) {
|
||||
term, printer := createJSONProgress()
|
||||
test.Equals(t, printer.ScannerError("/path", errors.New("error \"message\"")), nil)
|
||||
test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":\"error \\\"message\\\"\",\"during\":\"scan\",\"item\":\"/path\"}\n"}, term.Errors)
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
type TextProgress struct {
|
||||
*ui.Message
|
||||
|
||||
term *termstatus.Terminal
|
||||
term ui.Terminal
|
||||
verbosity uint
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ type TextProgress struct {
|
||||
var _ ProgressPrinter = &TextProgress{}
|
||||
|
||||
// NewTextProgress returns a new backup progress reporter.
|
||||
func NewTextProgress(term *termstatus.Terminal, verbosity uint) *TextProgress {
|
||||
func NewTextProgress(term ui.Terminal, verbosity uint) *TextProgress {
|
||||
return &TextProgress{
|
||||
Message: ui.NewMessage(term, verbosity),
|
||||
term: term,
|
||||
|
||||
27
internal/ui/backup/text_test.go
Normal file
27
internal/ui/backup/text_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/test"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
func createTextProgress() (*ui.MockTerminal, ProgressPrinter) {
|
||||
term := &ui.MockTerminal{}
|
||||
printer := NewTextProgress(term, 3)
|
||||
return term, printer
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
term, printer := createTextProgress()
|
||||
test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil)
|
||||
test.Equals(t, []string{"error: error \"message\"\n"}, term.Errors)
|
||||
}
|
||||
|
||||
func TestScannerError(t *testing.T) {
|
||||
term, printer := createTextProgress()
|
||||
test.Equals(t, printer.ScannerError("/path", errors.New("error \"message\"")), nil)
|
||||
test.Equals(t, []string{"scan: error \"message\"\n"}, term.Errors)
|
||||
}
|
||||
Reference in New Issue
Block a user