chore(text): use logger in new engine services (#1939)

* use logger in vcs

* use logger in query builder

* use logger in migrations
This commit is contained in:
Jo 2023-02-25 19:03:27 +00:00 committed by GitHub
parent 841395c318
commit fa2e726ca6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 69 additions and 24 deletions

View file

@ -96,12 +96,13 @@ func main() {
if config.SeparateSources {
config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
config.Runtime.AURClient, config.Runtime.AURCache,
config.SortBy,
config.Runtime.Logger.Child("querybuilder"), config.SortBy,
config.Runtime.Mode, config.SearchBy, config.BottomUp,
config.SingleLineResults, config.NewInstallEngine)
} else {
config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
config.Runtime.AURClient, config.Runtime.AURCache, config.SortBy,
config.Runtime.AURClient, config.Runtime.AURCache,
config.Runtime.Logger.Child("mixed.querybuilder"), config.SortBy,
config.Runtime.Mode, config.SearchBy,
config.BottomUp, config.SingleLineResults, config.NewInstallEngine)
}

View file

@ -345,7 +345,7 @@ func (g *Grapher) GraphFromAUR(ctx context.Context,
aurPkgs, errCache := g.aurClient.Get(ctx, &aurc.Query{By: aurc.Name, Needles: targets})
if errCache != nil {
text.Errorln(errCache)
g.logger.Errorln(errCache)
}
for i := range aurPkgs {
@ -424,7 +424,7 @@ func (g *Grapher) findDepsFromAUR(ctx context.Context,
By: aurc.Name, Needles: missingNeedles, Contains: false,
})
if errCache != nil {
text.Errorln(errCache)
g.logger.Errorln(errCache)
}
for i := range aurPkgs {

View file

@ -43,11 +43,13 @@ type MixedSourceQueryBuilder struct {
aurClient aur.QueryClient
rpcClient rpc.ClientInterface
logger *text.Logger
}
func NewMixedSourceQueryBuilder(
rpcClient rpc.ClientInterface,
aurClient aur.QueryClient,
logger *text.Logger,
sortBy string,
targetMode parser.TargetMode,
searchBy string,
@ -58,6 +60,7 @@ func NewMixedSourceQueryBuilder(
return &MixedSourceQueryBuilder{
rpcClient: rpcClient,
aurClient: aurClient,
logger: logger,
bottomUp: bottomUp,
sortBy: sortBy,
targetMode: targetMode,
@ -205,10 +208,10 @@ func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Exe
s.results = sortableResults.results
if aurErr != nil {
text.Errorln(ErrAURSearch{inner: aurErr})
s.logger.Errorln(ErrAURSearch{inner: aurErr})
if len(repoResults) != 0 {
text.Warnln(gotext.Get("Showing repo packages only"))
s.logger.Warnln(gotext.Get("Showing repo packages only"))
}
}
}

View file

@ -8,6 +8,7 @@ import (
"github.com/Jguer/aur/rpc"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -32,7 +33,11 @@ func TestMixedSourceQueryBuilder(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
queryBuilder := NewMixedSourceQueryBuilder(client, client, "votes", parser.ModeAny, "", tc.bottomUp, false, false)
w := &strings.Builder{}
queryBuilder := NewMixedSourceQueryBuilder(client, client,
text.NewLogger(w, strings.NewReader(""), false, "test"),
"votes", parser.ModeAny, "", tc.bottomUp, false, false)
search := []string{"linux"}
mockStore := &mockDB{}
@ -51,11 +56,10 @@ func TestMixedSourceQueryBuilder(t *testing.T) {
assert.Equal(t, "linux", queryBuilder.results[0].name)
}
w := &strings.Builder{}
queryBuilder.Results(w, mockStore, Detailed)
wString := w.String()
require.GreaterOrEqual(t, len(wString), 1)
require.GreaterOrEqual(t, len(wString), 1, wString)
assert.Equal(t, tc.want, wString)
})
}

View file

@ -43,11 +43,13 @@ type SourceQueryBuilder struct {
aurClient rpc.ClientInterface
aurCache aur.QueryClient
logger *text.Logger
}
func NewSourceQueryBuilder(
aurClient rpc.ClientInterface,
aurCache aur.QueryClient,
logger *text.Logger,
sortBy string,
targetMode parser.TargetMode,
searchBy string,
@ -58,6 +60,7 @@ func NewSourceQueryBuilder(
return &SourceQueryBuilder{
aurClient: aurClient,
aurCache: aurCache,
logger: logger,
repoQuery: []alpm.IPackage{},
aurQuery: []aur.Pkg{},
bottomUp: bottomUp,
@ -93,8 +96,8 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context,
}
if aurErr != nil && len(s.repoQuery) != 0 {
text.Errorln(ErrAURSearch{inner: aurErr})
text.Warnln(gotext.Get("Showing repo packages only"))
s.logger.Errorln(ErrAURSearch{inner: aurErr})
s.logger.Warnln(gotext.Get("Showing repo packages only"))
}
}

View file

@ -13,6 +13,7 @@ import (
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/db/mock"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/Jguer/go-alpm/v2"
"github.com/stretchr/testify/assert"
@ -109,7 +110,9 @@ func TestSourceQueryBuilder(t *testing.T) {
client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
require.NoError(t, err)
queryBuilder := NewSourceQueryBuilder(client, nil, "votes", parser.ModeAny, "", tc.bottomUp, false, false)
queryBuilder := NewSourceQueryBuilder(client, client,
text.NewLogger(io.Discard, bytes.NewBufferString(""), false, "test"),
"votes", parser.ModeAny, "", tc.bottomUp, false, false)
search := []string{"linux"}
mockStore := &mockDB{}

View file

@ -239,6 +239,9 @@ func DefaultConfig(version string) *Configuration {
Version: version,
Debug: false,
UseRPC: true,
Runtime: &Runtime{
Logger: text.GlobalLogger,
},
}
}
@ -331,7 +334,8 @@ func NewConfig(version string) (*Configuration, error) {
}
newConfig.Runtime.VCSStore = vcs.NewInfoStore(
filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder)
filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder,
newConfig.Runtime.Logger.Child("vcs"))
err := newConfig.Runtime.VCSStore.Load()

View file

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/leonelquinteros/gotext"
)
@ -51,7 +50,7 @@ func (c *Configuration) RunMigrations(migrations []configMigration, configPath s
for _, migration := range migrations {
if db.VerCmp(migration.TargetVersion(), c.Version) > 0 {
if migration.Do(c) {
text.Infoln("Config migration executed (",
c.Runtime.Logger.Infoln("Config migration executed (",
migration.TargetVersion(), "):", migration)
saveConfig = true

View file

@ -2,11 +2,15 @@ package settings
import (
"encoding/json"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Jguer/yay/v11/pkg/text"
)
func TestMigrationNothingToDo(t *testing.T) {
@ -21,7 +25,10 @@ func TestMigrationNothingToDo(t *testing.T) {
config := Configuration{
Version: "99.0.0",
// Create runtime with runtimeVersion
Runtime: &Runtime{Version: "20.0.0"},
Runtime: &Runtime{
Version: "20.0.0",
Logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
},
}
// Run Migration
@ -42,7 +49,12 @@ func TestMigrationNothingToDo(t *testing.T) {
func TestProvidesMigrationDo(t *testing.T) {
migration := &configProviderMigration{}
config := Configuration{Provides: true}
config := Configuration{
Provides: true,
Runtime: &Runtime{
Logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
},
}
assert.True(t, migration.Do(&config))
@ -120,7 +132,10 @@ func TestProvidesMigration(t *testing.T) {
Version: tc.testConfig.Version,
Provides: tc.testConfig.Provides,
// Create runtime with runtimeVersion
Runtime: &Runtime{Version: tc.testConfig.Runtime.Version},
Runtime: &Runtime{
Logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
Version: tc.testConfig.Runtime.Version,
},
}
// Run Migration

View file

@ -41,6 +41,7 @@ type InfoStore struct {
FilePath string
CmdBuilder exe.GitCmdBuilder
mux sync.Mutex
logger *text.Logger
}
// OriginInfoByURL stores the OriginInfo of each origin URL provided.
@ -62,12 +63,15 @@ type OriginInfo struct {
SHA string `json:"sha"`
}
func NewInfoStore(filePath string, cmdBuilder exe.GitCmdBuilder) *InfoStore {
func NewInfoStore(filePath string, cmdBuilder exe.GitCmdBuilder,
logger *text.Logger,
) *InfoStore {
infoStore := &InfoStore{
CmdBuilder: cmdBuilder,
FilePath: filePath,
OriginsByPackage: map[string]OriginInfoByURL{},
mux: sync.Mutex{},
logger: logger,
}
return infoStore
@ -87,11 +91,11 @@ func (v *InfoStore) getCommit(ctx context.Context, url, branch string, protocols
if err != nil {
exitError := &exec.ExitError{}
if ok := errors.As(err, &exitError); ok && exitError.ExitCode() == 128 {
text.Warnln(gotext.Get("devel check for package failed: '%s' encountered an error", cmd.String()))
v.logger.Warnln(gotext.Get("devel check for package failed: '%s' encountered an error", cmd.String()))
return ""
}
text.Warnln(err)
v.logger.Warnln(err)
return ""
}
@ -135,7 +139,7 @@ func (v *InfoStore) Update(ctx context.Context, pkgName string, sources []gosrc.
v.OriginsByPackage[pkgName] = info
text.Warnln(gotext.Get("Found git repo: %s", text.Cyan(url)))
v.logger.Warnln(gotext.Get("Found git repo: %s", text.Cyan(url)))
if err := v.Save(); err != nil {
fmt.Fprintln(os.Stderr, err)
@ -320,7 +324,7 @@ func (v *InfoStore) CleanOrphans(pkgs map[string]alpm.IPackage) {
for pkgName := range v.OriginsByPackage {
if _, ok := pkgs[pkgName]; !ok {
text.Debugln("removing orphaned vcs package:", pkgName)
v.logger.Debugln("removing orphaned vcs package:", pkgName)
missing = append(missing, pkgName)
}
}

View file

@ -5,8 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"
gosrc "github.com/Morganamilo/go-srcinfo"
@ -16,6 +18,7 @@ import (
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/text"
)
func TestParsing(t *testing.T) {
@ -76,7 +79,8 @@ func TestNewInfoStore(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := NewInfoStore(tt.args.filePath, tt.args.cmdBuilder)
got := NewInfoStore(tt.args.filePath, tt.args.cmdBuilder,
text.NewLogger(io.Discard, strings.NewReader(""), true, "test"))
assert.NotNil(t, got)
assert.Equal(t, []string{"--a", "--b"}, got.CmdBuilder.(*exe.CmdBuilder).GitFlags)
assert.Equal(t, tt.args.cmdBuilder, got.CmdBuilder)
@ -223,6 +227,7 @@ func TestInfoStoreToUpgrade(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
logger: text.GlobalLogger,
CmdBuilder: tt.fields.CmdBuilder,
OriginsByPackage: map[string]OriginInfoByURL{
"yay": tt.args.infos,
@ -355,6 +360,7 @@ func TestInfoStore_NeedsUpdate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
logger: text.GlobalLogger,
CmdBuilder: tt.fields.CmdBuilder,
}
got := v.needsUpdate(context.Background(), tt.args.infos)
@ -404,6 +410,7 @@ func TestInfoStore_Update(t *testing.T) {
t.Parallel()
v := &InfoStore{
OriginsByPackage: tt.fields.OriginsByPackage,
logger: text.GlobalLogger,
FilePath: filePath,
CmdBuilder: tt.fields.CmdBuilder,
}
@ -467,6 +474,7 @@ func TestInfoStore_Remove(t *testing.T) {
t.Parallel()
v := &InfoStore{
OriginsByPackage: tt.fields.OriginsByPackage,
logger: text.GlobalLogger,
FilePath: filePath,
}
v.RemovePackages(tt.args.pkgs)
@ -515,6 +523,7 @@ func TestInfoStore_CleanOrphans(t *testing.T) {
v := &InfoStore{
OriginsByPackage: tt.fields.OriginsByPackage,
FilePath: filePath,
logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
}
v.CleanOrphans(tt.args.pkgs)
assert.Len(t, tt.fields.OriginsByPackage, 3)