2023-03-29 12:58:09 +00:00
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
"github.com/databricks/cli/libs/flags"
|
2023-07-26 11:17:09 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-03-29 12:58:09 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-07-26 11:17:09 +00:00
|
|
|
type progressLoggerTest struct {
|
|
|
|
*cobra.Command
|
|
|
|
*logFlags
|
|
|
|
*progressLoggerFlag
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeProgressLoggerTest(t *testing.T) (
|
|
|
|
*progressLoggerTest,
|
|
|
|
*flags.LogLevelFlag,
|
|
|
|
*flags.LogFileFlag,
|
|
|
|
*flags.ProgressLogFormat,
|
|
|
|
) {
|
|
|
|
plt := &progressLoggerTest{
|
|
|
|
Command: &cobra.Command{},
|
|
|
|
}
|
|
|
|
plt.logFlags = initLogFlags(plt.Command)
|
|
|
|
plt.progressLoggerFlag = initProgressLoggerFlag(plt.Command, plt.logFlags)
|
|
|
|
return plt, &plt.logFlags.level, &plt.logFlags.file, &plt.progressLoggerFlag.ProgressLogFormat
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:58:09 +00:00
|
|
|
func TestInitializeErrorOnIncompatibleConfig(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
plt, logLevel, logFile, progressFormat := initializeProgressLoggerTest(t)
|
2023-03-29 12:58:09 +00:00
|
|
|
logLevel.Set("info")
|
|
|
|
logFile.Set("stderr")
|
|
|
|
progressFormat.Set("inplace")
|
2023-07-26 11:17:09 +00:00
|
|
|
_, err := plt.progressLoggerFlag.initializeContext(context.Background())
|
2023-03-29 12:58:09 +00:00
|
|
|
assert.ErrorContains(t, err, "inplace progress logging cannot be used when log-file is stderr")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoErrorOnDisabledLogLevel(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
plt, logLevel, logFile, progressFormat := initializeProgressLoggerTest(t)
|
2023-03-29 12:58:09 +00:00
|
|
|
logLevel.Set("disabled")
|
|
|
|
logFile.Set("stderr")
|
|
|
|
progressFormat.Set("inplace")
|
2023-07-26 11:17:09 +00:00
|
|
|
_, err := plt.progressLoggerFlag.initializeContext(context.Background())
|
2023-03-29 12:58:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoErrorOnNonStderrLogFile(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
plt, logLevel, logFile, progressFormat := initializeProgressLoggerTest(t)
|
2023-03-29 12:58:09 +00:00
|
|
|
logLevel.Set("info")
|
|
|
|
logFile.Set("stdout")
|
|
|
|
progressFormat.Set("inplace")
|
2023-07-26 11:17:09 +00:00
|
|
|
_, err := plt.progressLoggerFlag.initializeContext(context.Background())
|
2023-03-29 12:58:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultLoggerModeResolution(t *testing.T) {
|
2023-07-26 11:17:09 +00:00
|
|
|
plt, _, _, progressFormat := initializeProgressLoggerTest(t)
|
|
|
|
require.Equal(t, *progressFormat, flags.ModeDefault)
|
|
|
|
ctx, err := plt.progressLoggerFlag.initializeContext(context.Background())
|
2023-03-29 12:58:09 +00:00
|
|
|
require.NoError(t, err)
|
2023-04-06 10:54:58 +00:00
|
|
|
logger, ok := cmdio.FromContext(ctx)
|
2023-03-29 12:58:09 +00:00
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, logger.Mode, flags.ModeAppend)
|
|
|
|
}
|