diff options
| author | 2026-02-21 13:38:02 +0800 | |
|---|---|---|
| committer | 2026-02-21 14:28:15 +0800 | |
| commit | 94482cb2c97aa215f83940643c5d4c0933727dcb (patch) | |
| tree | bee22fa113542abd1b863ee251fdcf0f9bd409b5 /internal | |
| parent | diff: Add package-level doc comment (diff) | |
| signature | No signature | |
*: Modernize and lint; add CI v0.1.17
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/cache/lru/lru.go | 3 | ||||
| -rw-r--r-- | internal/cache/lru/lru_test.go | 4 | ||||
| -rw-r--r-- | internal/intconv/intconv.go | 39 | ||||
| -rw-r--r-- | internal/internal.go | 1 | ||||
| -rw-r--r-- | internal/testgit/repo_commit_tree.go | 3 | ||||
| -rw-r--r-- | internal/testgit/repo_new.go | 6 | ||||
| -rw-r--r-- | internal/testgit/repo_run.go | 3 |
7 files changed, 51 insertions, 8 deletions
diff --git a/internal/cache/lru/lru.go b/internal/cache/lru/lru.go index fd080bbd..585aaa3f 100644 --- a/internal/cache/lru/lru.go +++ b/internal/cache/lru/lru.go @@ -90,6 +90,7 @@ func (cache *Cache[K, V]) Get(key K) (V, bool) { return zero, false } cache.lru.MoveToBack(elem) + //nolint:forcetypeassert return elem.Value.(*entry[K, V]).value, true } @@ -100,6 +101,7 @@ func (cache *Cache[K, V]) Peek(key K) (V, bool) { var zero V return zero, false } + //nolint:forcetypeassert return elem.Value.(*entry[K, V]).value, true } @@ -161,6 +163,7 @@ func (cache *Cache[K, V]) evictOverBudget() { } func (cache *Cache[K, V]) removeElem(elem *list.Element) *entry[K, V] { + //nolint:forcetypeassert ent := elem.Value.(*entry[K, V]) cache.lru.Remove(elem) delete(cache.items, ent.key) diff --git a/internal/cache/lru/lru_test.go b/internal/cache/lru/lru_test.go index 9ce113f0..fbd20f0b 100644 --- a/internal/cache/lru/lru_test.go +++ b/internal/cache/lru/lru_test.go @@ -169,6 +169,7 @@ func TestCachePanicsForInvalidConfiguration(t *testing.T) { t.Parallel() t.Run("negative max", func(t *testing.T) { + t.Parallel() defer func() { if recover() == nil { t.Fatalf("expected panic") @@ -178,6 +179,7 @@ func TestCachePanicsForInvalidConfiguration(t *testing.T) { }) t.Run("nil weight function", func(t *testing.T) { + t.Parallel() defer func() { if recover() == nil { t.Fatalf("expected panic") @@ -187,6 +189,7 @@ func TestCachePanicsForInvalidConfiguration(t *testing.T) { }) t.Run("negative entry weight", func(t *testing.T) { + t.Parallel() cache := lru.New[string, testValue](10, func(_ string, _ testValue) int64 { return -1 }, nil) @@ -199,6 +202,7 @@ func TestCachePanicsForInvalidConfiguration(t *testing.T) { }) t.Run("set negative max", func(t *testing.T) { + t.Parallel() cache := lru.New[string, testValue](10, weightFn, nil) defer func() { if recover() == nil { diff --git a/internal/intconv/intconv.go b/internal/intconv/intconv.go new file mode 100644 index 00000000..8bc77d8e --- /dev/null +++ b/internal/intconv/intconv.go @@ -0,0 +1,39 @@ +// Package intconv provides checked integer conversion helpers. +package intconv + +import ( + "fmt" + "math" +) + +// Uint64ToInt converts v to int, returning an error if it overflows. +func Uint64ToInt(v uint64) (int, error) { + if v > uint64(math.MaxInt) { + return 0, fmt.Errorf("intconv: uint64 %d overflows int", v) + } + return int(v), nil +} + +// UintptrToInt converts v to int, returning an error if it overflows. +func UintptrToInt(v uintptr) (int, error) { + if v > uintptr(math.MaxInt) { + return 0, fmt.Errorf("intconv: uintptr %d overflows int", v) + } + return int(v), nil +} + +// IntToUint64 converts v to uint64, returning an error if v is negative. +func IntToUint64(v int) (uint64, error) { + if v < 0 { + return 0, fmt.Errorf("intconv: int %d is negative", v) + } + return uint64(v), nil +} + +// Int64ToInt32 converts v to int32, returning an error if it overflows. +func Int64ToInt32(v int64) (int32, error) { + if v < math.MinInt32 || v > math.MaxInt32 { + return 0, fmt.Errorf("intconv: int64 %d overflows int32", v) + } + return int32(v), nil +} diff --git a/internal/internal.go b/internal/internal.go index 9c785745..0c4a6161 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -1,3 +1,2 @@ // Package internal provides private packages and helpers. package internal - diff --git a/internal/testgit/repo_commit_tree.go b/internal/testgit/repo_commit_tree.go index f8d78421..763474c2 100644 --- a/internal/testgit/repo_commit_tree.go +++ b/internal/testgit/repo_commit_tree.go @@ -9,7 +9,8 @@ import ( // CommitTree creates a commit from a tree and message, optionally with parents. func (testRepo *TestRepo) CommitTree(tb testing.TB, tree objectid.ObjectID, message string, parents ...objectid.ObjectID) objectid.ObjectID { tb.Helper() - args := []string{"commit-tree", tree.String()} + args := make([]string, 0, 2+2*len(parents)+2) + args = append(args, "commit-tree", tree.String()) for _, p := range parents { args = append(args, "-p", p.String()) } diff --git a/internal/testgit/repo_new.go b/internal/testgit/repo_new.go index 605eedd2..8120a9a2 100644 --- a/internal/testgit/repo_new.go +++ b/internal/testgit/repo_new.go @@ -26,11 +26,7 @@ func NewRepo(tb testing.TB, opts RepoOptions) *TestRepo { tb.Fatalf("invalid algorithm: %v", algo) } - dir, err := os.MkdirTemp("", "furgit-testgit-*") - if err != nil { - tb.Fatalf("create temp dir: %v", err) - } - tb.Cleanup(func() { _ = os.RemoveAll(dir) }) + dir := tb.TempDir() testRepo := &TestRepo{ dir: dir, diff --git a/internal/testgit/repo_run.go b/internal/testgit/repo_run.go index aafcc923..8022835e 100644 --- a/internal/testgit/repo_run.go +++ b/internal/testgit/repo_run.go @@ -35,7 +35,8 @@ func (testRepo *TestRepo) RunInputBytes(tb testing.TB, stdin []byte, args ...str func (testRepo *TestRepo) runBytes(tb testing.TB, stdin []byte, dir string, args ...string) []byte { tb.Helper() - cmd := exec.Command("git", args...) + //nolint:noctx + cmd := exec.Command("git", args...) //#nosec G204 cmd.Dir = dir cmd.Env = testRepo.env if stdin != nil { |
