Files
stalwart-migrator/internal/backup/vandelay_test.go
T
jcoffey-dev 4b0bec8956 Add SPDX headers to every Go file
GPLv3's "How to Apply These Terms" asks for a notice in each source file;
this is the modern two-line SPDX form of it rather than the full paragraph.
82 files, including tests.

The blank line after the header is load-bearing. In Go a comment block
immediately preceding `package X` becomes the package doc comment, so
without the separator the SPDX lines would be absorbed into the doc for the
eleven packages whose doc.go (or main.go) opens with one, and `go doc` would
print them. Verified it doesn't.
2026-08-23 18:03:15 -07:00

60 lines
2.3 KiB
Go

// SPDX-FileCopyrightText: 2026 LINUXexpert-org
// SPDX-License-Identifier: GPL-3.0-or-later
package backup
import (
"context"
"path/filepath"
"strings"
"testing"
)
func TestBuildVandelayImportArgs(t *testing.T) {
args := BuildVandelayImportArgs(VandelayOptions{URL: "https://mail.example.com", AuthBasic: "alice:app-pass"}, "[email protected]", "/backups/alice.sqlite")
joined := strings.Join(args, " ")
for _, want := range []string{"import", "jmap", "--url https://mail.example.com", "--auth-basic alice:app-pass", "--account-name [email protected]", "/backups/alice.sqlite"} {
if !strings.Contains(joined, want) {
t.Errorf("BuildVandelayImportArgs = %q, missing %q", joined, want)
}
}
}
func TestExportAccountsRunsEachAccount(t *testing.T) {
dir := t.TempDir()
log := argsFile(t, dir)
withFakeExecutable(t, "vandelay", fakeScriptLoggingArgs(log, "exit 0"))
outDir := filepath.Join(dir, "out")
files, err := ExportAccounts(context.Background(), VandelayOptions{URL: "https://mail.example.com", AuthBasic: "a:b", OutDir: outDir}, []string{"[email protected]", "[email protected]"})
if err != nil {
t.Fatalf("ExportAccounts: %v", err)
}
if len(files) != 2 {
t.Fatalf("got %d files, want 2: %v", len(files), files)
}
got := readArgsFile(t, log)
if !strings.Contains(got, "[email protected]") || !strings.Contains(got, "[email protected]") {
t.Errorf("vandelay invocations = %q, want both accounts", got)
}
}
func TestExportAccountsStopsAtFirstFailure(t *testing.T) {
dir := t.TempDir()
// Fails on the second invocation (bob), succeeds on the first (alice).
script := "#!/bin/sh\ncase \"$*\" in\n *bob*) echo 'account not found' >&2; exit 1 ;;\n *) exit 0 ;;\nesac\n"
withFakeExecutable(t, "vandelay", script)
outDir := filepath.Join(dir, "out")
files, err := ExportAccounts(context.Background(), VandelayOptions{URL: "https://mail.example.com", AuthBasic: "a:b", OutDir: outDir}, []string{"[email protected]", "[email protected]", "[email protected]"})
if err == nil {
t.Fatal("ExportAccounts should have failed on [email protected]")
}
if len(files) != 1 {
t.Errorf("got %d successful exports before failure, want 1 (alice only)", len(files))
}
if !strings.Contains(err.Error(), "1/3") {
t.Errorf("error = %v, want it to report 1/3 accounts exported before failing", err)
}
}