Skip to content

Commit

Permalink
refactor: Add solver.logger field, and consume it
Browse files Browse the repository at this point in the history
Signed-off-by: Víctor Cuadrado Juan <[email protected]>
  • Loading branch information
viccuad committed Jul 9, 2021
1 parent 32eebf1 commit c8ef337
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions internal/solver/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func ExampleSolve() {
log.Current = logger
logger.Level = log.DebugLevel

s := New(InstallOne)
s := New(InstallOne, logger)

s.PkgDB.DebugPrintDB(logger)
s.BuildWorldMock(pkgs)

s.Solve(logger)
s.Solve()
s.PkgDB.DebugPrintDB(logger)

fmt.Println(s.FormatOutput(YAML))
Expand Down
16 changes: 9 additions & 7 deletions internal/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Solver struct {
PkgDB *PkgDB // DB containing packages
PkgResultSet PkgResultSet // outcome of sat solving
Strategy SolverStrategy
logger log.Logger
}

// PkgResultSet contains the status outcome of solving, and the different sets of
Expand All @@ -86,11 +87,12 @@ const (
)

// New creates a new Solver, initializing its database.
func New(strategy SolverStrategy) (s *Solver) {
func New(strategy SolverStrategy, logger log.Logger) (s *Solver) {
s = &Solver{
PkgDB: CreatePkgDBInstance(),
PkgResultSet: PkgResultSet{},
Strategy: strategy,
logger: logger,
}
s.PkgResultSet.Inconsistencies = []string{}
return s
Expand Down Expand Up @@ -131,20 +133,20 @@ func (s *Solver) BuildConstraints(p *pkg.Pkg) (constrs []maxsat.Constr) {
return constrs
}

func (s *Solver) Solve(logger log.Logger) {
func (s *Solver) Solve() {
// generate constraints for all packages
logger.Debug("Building constraints…")
s.logger.Debug("Building constraints…")
constrs := []maxsat.Constr{}
for _, p := range s.PkgDB.mapFingerprintToPkg {
constrs = append(constrs, s.BuildConstraints(p)...)
}

logger.Debug("Constraints:")
s.logger.Debug("Constraints:")
for _, c := range constrs {
logger.Debugf(" %v\n", c)
s.logger.Debugf(" %v\n", c)

}
logger.Debug("Solving…")
s.logger.Debug("Solving…")

// create problem with constraints, and solve
problem := maxsat.New(constrs...)
Expand All @@ -158,7 +160,7 @@ func (s *Solver) Solve(logger log.Logger) {
s.PkgResultSet.Status = "UNSAT"
}

logger.Debugf("Result %v\n", result)
s.logger.Debugf("Result %v\n", result)
}

func (s *Solver) IsSAT() bool {
Expand Down
8 changes: 4 additions & 4 deletions internal/solver/solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ func TestInstall(t *testing.T) {
logger.DebugOut = buf
log.Current = logger

s := New(InstallOne)
s := New(InstallOne, logger)
s.BuildWorldMock(tcase.pkgs)
s.Solve(logger)
s.Solve()
is := assert.New(t)
is.Equal(tcase.resultStatus, s.PkgResultSet.Status)

Expand Down Expand Up @@ -289,9 +289,9 @@ func TestFormatOutput(t *testing.T) {
logger.DebugOut = buf
log.Current = logger

s := New(InstallOne)
s := New(InstallOne, logger)
s.BuildWorldMock(tcase.pkgs)
s.Solve(logger)
s.Solve()
is := assert.New(t)
is.Equal(tcase.status, s.PkgResultSet.Status)

Expand Down
4 changes: 2 additions & 2 deletions pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (i *Install) Run(strategy solver.SolverStrategy, wantedChrt *helmChart.Char
return nil, err
}

s := solver.New(strategy)
s := solver.New(strategy, logger)

err = BuildWorld(s.PkgDB, rf.Repositories, rels, wantedPkg, wantedChrt, settings, logger)
if err != nil {
Expand Down Expand Up @@ -154,7 +154,7 @@ func (i *Install) Run(strategy solver.SolverStrategy, wantedChrt *helmChart.Char
}
}

s.Solve(logger)
s.Solve()

installedRels := make([]*release.Release, 0)
if s.IsSAT() {
Expand Down

0 comments on commit c8ef337

Please sign in to comment.