Files
zeekay fea2ca58e3 fix(test): reset ux.Logger in TestRunMigrations
The test was failing because ux.NewUserLog() only sets the global
Logger if nil, but Logger could be initialized from previous tests.
Reset Logger to nil before each test to ensure proper buffer capture.
2026-01-03 18:56:32 -08:00

145 lines
3.4 KiB
Go

// Copyright (C) 2022-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package migrations
import (
"bytes"
"errors"
"testing"
"github.com/luxfi/cli/pkg/application"
"github.com/luxfi/cli/pkg/config"
"github.com/luxfi/cli/pkg/prompts"
"github.com/luxfi/cli/pkg/ux"
luxlog "github.com/luxfi/log"
"github.com/stretchr/testify/require"
)
func TestRunMigrations(t *testing.T) {
buffer := make([]byte, 0, 100)
bufWriter := bytes.NewBuffer(buffer)
// Reset Logger so NewUserLog actually creates a new one with our buffer
ux.Logger = nil
ux.NewUserLog(luxlog.NewNoOpLogger(), bufWriter)
require := require.New(t)
testDir := t.TempDir()
app := application.New()
app.Setup(testDir, luxlog.NewNoOpLogger(), config.New(), prompts.NewPrompter(), application.NewDownloader())
type migTest struct {
migs map[int]migrationFunc
name string
shouldErr bool
expectedOutput string
}
expectedIfRan := runMessage + "\n" + endMessage + "\n"
expectedIfFailed := runMessage + "\n" + failedEndMessage + "\n"
tests := []migTest{
{
name: "no migrations",
shouldErr: false,
migs: map[int]migrationFunc{},
expectedOutput: "",
},
{
name: "migration fail",
shouldErr: true,
migs: map[int]migrationFunc{
0: func(_ *application.Lux, _ *migrationRunner) error {
return errors.New("bogus fail")
},
},
expectedOutput: "",
},
{
name: "1 mig, apply",
shouldErr: false,
migs: map[int]migrationFunc{
0: func(_ *application.Lux, r *migrationRunner) error {
r.printMigrationMessage()
return nil
},
},
expectedOutput: expectedIfRan,
},
{
name: "2 mig, apply both",
shouldErr: false,
migs: map[int]migrationFunc{
0: func(_ *application.Lux, r *migrationRunner) error {
r.printMigrationMessage()
return nil
},
1: func(_ *application.Lux, r *migrationRunner) error {
r.printMigrationMessage()
return nil
},
},
expectedOutput: expectedIfRan,
},
{
name: "2 mig, apply 1",
shouldErr: false,
migs: map[int]migrationFunc{
0: func(_ *application.Lux, _ *migrationRunner) error {
return nil
},
1: func(_ *application.Lux, r *migrationRunner) error {
r.printMigrationMessage()
return nil
},
},
expectedOutput: expectedIfRan,
},
{
name: "2 mig, first one fails",
shouldErr: true,
migs: map[int]migrationFunc{
0: func(_ *application.Lux, _ *migrationRunner) error {
return errors.New("bogus fail")
},
1: func(_ *application.Lux, r *migrationRunner) error {
r.printMigrationMessage()
return nil
},
},
expectedOutput: "",
},
{
name: "2 mig, apply 1, second one fails",
shouldErr: true,
migs: map[int]migrationFunc{
0: func(_ *application.Lux, r *migrationRunner) error {
r.printMigrationMessage()
return nil
},
1: func(_ *application.Lux, _ *migrationRunner) error {
return errors.New("bogus fail")
},
},
expectedOutput: expectedIfFailed,
},
}
for _, tt := range tests {
// reset the buffer on each run to match expected output
bufWriter.Reset()
runner := &migrationRunner{
showMsg: true,
running: false,
migrations: tt.migs,
}
err := runner.run(app)
if tt.shouldErr {
require.Error(err)
} else {
require.NoError(err)
}
require.Equal(tt.expectedOutput, bufWriter.String())
}
}