2023-05-01 09:19:09 +00:00
|
|
|
package restore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2023-05-01 09:19:09 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
2024-07-27 23:06:26 +00:00
|
|
|
"github.com/restic/restic/internal/ui"
|
2023-05-01 09:19:09 +00:00
|
|
|
)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
func createTextProgress() (*ui.MockTerminal, ProgressPrinter) {
|
|
|
|
term := &ui.MockTerminal{}
|
2024-05-31 18:38:51 +00:00
|
|
|
printer := NewTextProgress(term, 3)
|
|
|
|
return term, printer
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintUpdate(t *testing.T) {
|
|
|
|
term, printer := createTextProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Update(State{3, 11, 0, 29, 47, 0}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"[0:05] 61.70% 3 files/dirs 29 B, total 11 files/dirs 47 B"}, term.Output)
|
2023-05-01 09:19:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 12:12:06 +00:00
|
|
|
func TestPrintUpdateWithSkipped(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createTextProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Update(State{3, 11, 2, 29, 47, 59}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"[0:05] 61.70% 3 files/dirs 29 B, total 11 files/dirs 47 B, skipped 2 files/dirs 59 B"}, term.Output)
|
2024-05-31 12:12:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 09:19:09 +00:00
|
|
|
func TestPrintSummaryOnSuccess(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createTextProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Finish(State{11, 11, 0, 47, 47, 0}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"Summary: Restored 11 files/dirs (47 B) in 0:05"}, term.Output)
|
2023-05-01 09:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintSummaryOnErrors(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createTextProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Finish(State{3, 11, 0, 29, 47, 0}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"Summary: Restored 3 / 11 files/dirs (29 B / 47 B) in 0:05"}, term.Output)
|
2023-05-01 09:19:09 +00:00
|
|
|
}
|
2024-05-31 12:12:06 +00:00
|
|
|
|
|
|
|
func TestPrintSummaryOnSuccessWithSkipped(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createTextProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Finish(State{11, 11, 2, 47, 47, 59}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"Summary: Restored 11 files/dirs (47 B) in 0:05, skipped 2 files/dirs 59 B"}, term.Output)
|
2024-05-31 12:12:06 +00:00
|
|
|
}
|
2024-05-31 18:38:51 +00:00
|
|
|
|
|
|
|
func TestPrintCompleteItem(t *testing.T) {
|
|
|
|
for _, data := range []struct {
|
|
|
|
action ItemAction
|
|
|
|
size uint64
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{ActionDirRestored, 0, "restored test"},
|
|
|
|
{ActionFileRestored, 123, "restored test with size 123 B"},
|
2024-07-20 10:46:28 +00:00
|
|
|
{ActionOtherRestored, 0, "restored test"},
|
2024-05-31 18:38:51 +00:00
|
|
|
{ActionFileUpdated, 123, "updated test with size 123 B"},
|
|
|
|
{ActionFileUnchanged, 123, "unchanged test with size 123 B"},
|
2024-06-29 19:29:42 +00:00
|
|
|
{ActionDeleted, 0, "deleted test"},
|
2024-05-31 18:38:51 +00:00
|
|
|
} {
|
|
|
|
term, printer := createTextProgress()
|
|
|
|
printer.CompleteItem(data.action, "test", data.size)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{data.expected}, term.Output)
|
2024-05-31 18:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-27 23:06:26 +00:00
|
|
|
|
|
|
|
func TestError(t *testing.T) {
|
|
|
|
term, printer := createTextProgress()
|
|
|
|
test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil)
|
|
|
|
test.Equals(t, []string{"ignoring error for /path: error \"message\"\n"}, term.Errors)
|
|
|
|
}
|