Artifact 0b14c50eb2b4e082f449db45dc14b4c9bdc50813495aa137653f1902a56b556d:
- File
internal/repo/hash_policy_test.go
— part of check-in
[feea6a1614]
at
2026-10-13 10:59:56
on branch main
— feat(repo): set the hash policy by name (#408)
artifact-format-01 7.1 (AF-7.1-2) matches policy value strings case-sensitively and exactly and makes an unrecognized value fatal. No surface took a value string, so the row stayed partial.
Repo.SetHashPolicy takes sha1, auto, sha3, sha3-only or shun-sha1, as fossil's hash-policy command does with fossil_strcmp, stores the ordinal, and refuses any other string without touching the stored policy. Auto in a repository that already holds a SHA3 artifact stores sha3 (7.3).
New conformance cases drive every name, wrong-case names, unknown names and the empty name through it, and the AF-7.1-2 scope note is dropped. (user: [email protected], size: 2059) [annotate] [blame] [check-ins using]
package repo_test import ( "path/filepath" "testing" "github.com/danmestas/go-libfossil/internal/blob" "github.com/danmestas/go-libfossil/internal/repo" _ "github.com/danmestas/go-libfossil/internal/testdriver" "github.com/danmestas/go-libfossil/simio" ) func newRepo(t *testing.T) *repo.Repo { t.Helper() r, err := repo.Create(filepath.Join(t.TempDir(), "r.fossil"), "tester", simio.CryptoRand{}, "") if err != nil { t.Fatalf("repo.Create: %v", err) } t.Cleanup(func() { r.Close() }) return r } func storedPolicy(t *testing.T, r *repo.Repo) string { t.Helper() got, err := r.Config("hash-policy") if err != nil { t.Fatalf("Config: %v", err) } return got } // artifact-format-01 7.3: requesting auto once a SHA3 artifact exists stores // sha3, as fossil's hash-policy command does; without one, auto is stored. func TestSetHashPolicyCoercesAuto(t *testing.T) { for _, tt := range []struct { name string before string // policy content is stored under first; "" stores none want string }{ {"empty repo", "", "1"}, {"sha1 artifact", "sha1", "1"}, {"sha3 artifact", "sha3", "2"}, } { t.Run(tt.name, func(t *testing.T) { r := newRepo(t) if tt.before != "" { if err := r.SetHashPolicy(tt.before); err != nil { t.Fatalf("SetHashPolicy(%q): %v", tt.before, err) } if _, _, err := blob.Store(r.DB(), []byte("content")); err != nil { t.Fatalf("blob.Store: %v", err) } } if err := r.SetHashPolicy("auto"); err != nil { t.Fatalf("SetHashPolicy(auto): %v", err) } if got := storedPolicy(t, r); got != tt.want { t.Errorf("stored hash-policy = %q, want %q", got, tt.want) } }) } } // 7.3 has no demotion guard: sha1 may be set after sha3. func TestSetHashPolicyNoDemotionGuard(t *testing.T) { r := newRepo(t) for _, name := range []string{"shun-sha1", "sha1"} { if err := r.SetHashPolicy(name); err != nil { t.Fatalf("SetHashPolicy(%q): %v", name, err) } } if got := storedPolicy(t, r); got != "0" { t.Errorf("stored hash-policy = %q, want 0", got) } }