From b4e2dc36ff3dd99eafd1ac8ea5e642a1885aa129 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 5 Sep 2026 23:13:07 +0200 Subject: [PATCH] test(web): fail when two packages claim the same world error code --- pkg/web/error_codes_test.go | 84 +++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 pkg/web/error_codes_test.go diff --git a/pkg/web/error_codes_test.go b/pkg/web/error_codes_test.go new file mode 100644 index 000000000..1bfeb7b6d --- /dev/null +++ b/pkg/web/error_codes_test.go @@ -0,0 +1,84 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package web + +import ( + "io/fs" + "os" + "path/filepath" + "regexp" + "runtime" + "sort" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var errCodeConstRegex = regexp.MustCompile(`\bErrCode(\w+)\s*=\s*(\d+)\b`) + +// TestErrorCodesAreUnique guards the world-error code space, which is flat across all packages: +// clients (and the frontend translations) map a code to a message without knowing which package +// raised it, so two packages picking the same number silently mislabels one of them. +func TestErrorCodesAreUnique(t *testing.T) { + _, thisFile, _, ok := runtime.Caller(0) + require.True(t, ok) + pkgDir := filepath.Dir(filepath.Dir(thisFile)) + + codes := make(map[string][]string) + err := filepath.WalkDir(pkgDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() || !strings.HasSuffix(path, ".go") { + return nil + } + + content, err := os.ReadFile(path) + if err != nil { + return err + } + + for _, match := range errCodeConstRegex.FindAllStringSubmatch(string(content), -1) { + codes[match[2]] = append(codes[match[2]], "ErrCode"+match[1]) + } + return nil + }) + require.NoError(t, err) + require.NotEmpty(t, codes, "no error code constants found, the test is not looking at the right place") + + duplicates := []string{} + for code, names := range codes { + unique := map[string]bool{} + for _, name := range names { + unique[name] = true + } + if len(unique) < 2 { + continue + } + sorted := make([]string, 0, len(unique)) + for name := range unique { + sorted = append(sorted, name) + } + sort.Strings(sorted) + duplicates = append(duplicates, code+": "+strings.Join(sorted, ", ")) + } + sort.Strings(duplicates) + + assert.Emptyf(t, duplicates, "error codes must be unique across all packages, got duplicates:\n%s", strings.Join(duplicates, "\n")) +}