package backup import ( "os" "path/filepath" "reflect" "runtime" "testing" ) func TestMySQLCLIArgsTCP(t *testing.T) { args, db, err := MySQLCLIArgs("user:pass@tcp(127.0.0.1:3307)/gitocean_test?parseTime=true") if err != nil { t.Fatal(err) } want := []string{"-uuser", "-ppass", "-h127.0.0.1", "-P3307"} if !reflect.DeepEqual(args, want) || db != "gitocean_test" { t.Fatalf("args=%#v db=%q, want %#v %q", args, db, want, "gitocean_test") } } func TestMySQLCLIArgsUnix(t *testing.T) { args, db, err := MySQLCLIArgs("user@unix(/tmp/mysql.sock)/gitocean") if err != nil { t.Fatal(err) } want := []string{"-uuser", "--socket", "/tmp/mysql.sock"} if !reflect.DeepEqual(args, want) || db != "gitocean" { t.Fatalf("args=%#v db=%q, want %#v %q", args, db, want, "gitocean") } } func TestMySQLCLIArgsInvalidDSN(t *testing.T) { if _, _, err := MySQLCLIArgs("bad dsn"); err == nil { t.Fatal("expected invalid DSN to fail") } } func TestCopyDir(t *testing.T) { src := t.TempDir() dst := filepath.Join(t.TempDir(), "dst") if err := os.MkdirAll(filepath.Join(src, "nested"), 0750); err != nil { t.Fatal(err) } file := filepath.Join(src, "nested", "file.txt") if err := os.WriteFile(file, []byte("hello"), 0640); err != nil { t.Fatal(err) } if err := CopyDir(src, dst); err != nil { t.Fatal(err) } got, err := os.ReadFile(filepath.Join(dst, "nested", "file.txt")) if err != nil { t.Fatal(err) } if string(got) != "hello" { t.Fatalf("unexpected copied content: %q", got) } if runtime.GOOS != "windows" { info, err := os.Stat(filepath.Join(dst, "nested", "file.txt")) if err != nil { t.Fatal(err) } if info.Mode().Perm() != 0640 { t.Fatalf("mode = %v, want 0640", info.Mode().Perm()) } } } func TestCopyDirMissingSource(t *testing.T) { if err := CopyDir(filepath.Join(t.TempDir(), "missing"), filepath.Join(t.TempDir(), "dst")); err == nil { t.Fatal("expected missing source to fail") } }