(t *testing.T)
| 16 | ) |
| 17 | |
| 18 | func TestCreateBackup(t *testing.T) { |
| 19 | app, _ := tests.NewTestApp() |
| 20 | defer app.Cleanup() |
| 21 | |
| 22 | // set some long app name with spaces and special characters |
| 23 | app.Settings().Meta.AppName = "test @! " + strings.Repeat("a", 100) |
| 24 | |
| 25 | expectedAppNamePrefix := "test_" + strings.Repeat("a", 45) |
| 26 | |
| 27 | // test pending error |
| 28 | app.Store().Set(core.StoreKeyActiveBackup, "") |
| 29 | if err := app.CreateBackup(context.Background(), "test.zip"); err == nil { |
| 30 | t.Fatal("Expected pending error, got nil") |
| 31 | } |
| 32 | app.Store().Remove(core.StoreKeyActiveBackup) |
| 33 | |
| 34 | // create with auto generated name |
| 35 | if err := app.CreateBackup(context.Background(), ""); err != nil { |
| 36 | t.Fatal("Failed to create a backup with autogenerated name") |
| 37 | } |
| 38 | |
| 39 | // create with custom name |
| 40 | if err := app.CreateBackup(context.Background(), "custom"); err != nil { |
| 41 | t.Fatal("Failed to create a backup with custom name") |
| 42 | } |
| 43 | |
| 44 | // create new with the same name (aka. replace) |
| 45 | if err := app.CreateBackup(context.Background(), "custom"); err != nil { |
| 46 | t.Fatal("Failed to create and replace a backup with the same name") |
| 47 | } |
| 48 | |
| 49 | backupsDir := filepath.Join(app.DataDir(), core.LocalBackupsDirName) |
| 50 | |
| 51 | entries, err := os.ReadDir(backupsDir) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | |
| 56 | expectedFiles := []string{ |
| 57 | `^pb_backup_` + expectedAppNamePrefix + `_\w+\.zip$`, |
| 58 | `^pb_backup_` + expectedAppNamePrefix + `_\w+\.zip.attrs$`, |
| 59 | "custom", |
| 60 | "custom.attrs", |
| 61 | } |
| 62 | |
| 63 | if len(entries) != len(expectedFiles) { |
| 64 | names := getEntryNames(entries) |
| 65 | t.Fatalf("Expected %d backup files, got %d: \n%v", len(expectedFiles), len(entries), names) |
| 66 | } |
| 67 | |
| 68 | for i, entry := range entries { |
| 69 | if !list.ExistInSliceWithRegex(entry.Name(), expectedFiles) { |
| 70 | t.Fatalf("[%d] Missing backup file %q", i, entry.Name()) |
| 71 | } |
| 72 | |
| 73 | if strings.HasSuffix(entry.Name(), ".attrs") { |
| 74 | continue |
| 75 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…