package storage import ( "archive/zip" "bytes" "errors" "sort" "testing" ) func TestWalkSkipsInternalFiles(t *testing.T) { s := newStore(t) put(t, s, "d/a.txt", "a", PutOptions{}) put(t, s, "d/sous/b.txt", "bb", PutOptions{}) put(t, s, "d/a.txt", "a2", PutOptions{Conflict: "overwrite"}) // crée .history if err := s.EditTags([]string{"d/a.txt"}, []string{"x"}, nil); err != nil { t.Fatal(err) } var got []string if err := s.Walk("d", func(rel string, e Entry) error { if rel != e.Path { t.Errorf("rel %q != Path %q", rel, e.Path) } got = append(got, rel) return nil }); err != nil { t.Fatal(err) } sort.Strings(got) want := []string{"d", "d/a.txt", "d/sous", "d/sous/b.txt"} if len(got) != len(want) { t.Fatalf("Walk = %v, veut %v", got, want) } for i := range want { if got[i] != want[i] { t.Fatalf("Walk = %v, veut %v", got, want) } } var root []string _ = s.Walk("", func(rel string, e Entry) error { root = append(root, rel); return nil }) for _, r := range root { if r == ".history" || r == ".meta.json" { t.Errorf("Walk expose un fichier interne : %s", r) } } } func TestTransferBetweenRoots(t *testing.T) { src, dst := newStore(t), newStore(t) put(t, src, "projet/plan.txt", "plan", PutOptions{}) put(t, src, "projet/img/logo.svg", "", PutOptions{}) if _, err := src.Mkdir("projet/vide"); err != nil { t.Fatal(err) } e, err := transfer(src, "projet", dst, "copie/projet", "alice") if err != nil { t.Fatal(err) } if !e.IsDir || e.Path != "copie/projet" { t.Errorf("transfer = %+v", e) } if got := read(t, dst, "copie/projet/img/logo.svg"); got != "" { t.Errorf("contenu copié = %q", got) } if _, err := dst.Stat("copie/projet/vide"); err != nil { t.Errorf("dossier vide non copié : %v", err) } if _, err := transfer(src, "projet", dst, "copie/projet", "alice"); !errors.Is(err, ErrExists) { t.Errorf("destination existante : %v", err) } // Un fichier seul. if _, err := transfer(src, "projet/plan.txt", dst, "plan.txt", ""); err != nil { t.Fatal(err) } if got := read(t, dst, "plan.txt"); got != "plan" { t.Errorf("fichier copié = %q", got) } } func TestRemoveIsDefinitive(t *testing.T) { s := newStore(t) put(t, s, "d/a.txt", "a", PutOptions{}) if err := s.Remove("d"); err != nil { t.Fatal(err) } if _, err := s.Stat("d"); !errors.Is(err, ErrNotFound) { t.Errorf("Stat après Remove : %v", err) } if s.TrashCount() != 0 { t.Errorf("Remove ne doit pas passer par la corbeille") } if err := s.Remove(""); !errors.Is(err, ErrInvalidPath) { t.Errorf("Remove de la racine : %v", err) } } func TestZipThroughRoot(t *testing.T) { s := newStore(t) put(t, s, "d/a.txt", "a", PutOptions{}) put(t, s, "d/s/b.txt", "b", PutOptions{}) var buf bytes.Buffer if err := WriteZip(&buf, []ZipItem{{Root: s, Rel: "d", Name: "d"}, {Root: s, Rel: "d/a.txt", Name: "a.txt"}}); err != nil { t.Fatal(err) } zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) if err != nil { t.Fatal(err) } names := map[string]bool{} for _, f := range zr.File { names[f.Name] = true } for _, n := range []string{"d/", "d/a.txt", "d/s/", "d/s/b.txt", "a.txt"} { if !names[n] { t.Errorf("archive sans %s : %v", n, names) } } }