Skip to content

Commit

Permalink
internal/tdtest: use sync.OnceValues
Browse files Browse the repository at this point in the history
The code gets a bit shorter and harder to misuse.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I19edf20f06369158e41f9a5fb6f7827422daf6bc
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201760
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mvdan committed Sep 25, 2024
1 parent ffafd61 commit 774e40e
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions internal/tdtest/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,18 @@ type callInfo struct {
fieldName string
}

var (
once sync.Once
pkgs []*packages.Package
pkgsErr error
)

func initPackages() ([]*packages.Package, error) {
once.Do(func() {
cfg := &packages.Config{
Mode: packages.NeedFiles |
packages.NeedDeps |
packages.NeedTypes |
packages.NeedTypesInfo |
packages.NeedSyntax,
Tests: true,
}
var loadPackages = sync.OnceValues(func() ([]*packages.Package, error) {
cfg := &packages.Config{
Mode: packages.NeedFiles |
packages.NeedDeps |
packages.NeedTypes |
packages.NeedTypesInfo |
packages.NeedSyntax,
Tests: true,
}

pkgs, pkgsErr = packages.Load(cfg, ".")
})
return pkgs, pkgsErr
}
return packages.Load(cfg, ".")
})

func (s *set[T]) getInfo(file string) *info {
if s.info != nil {
Expand All @@ -89,7 +80,7 @@ func (s *set[T]) getInfo(file string) *info {

t := s.t

pkgs, pkgsErr = initPackages()
pkgs, pkgsErr := loadPackages()
if pkgsErr != nil {
t.Fatalf("load: %v\n", pkgsErr)
}
Expand Down

0 comments on commit 774e40e

Please sign in to comment.