Skip to content

Commit

Permalink
Refactor FirstNonEmptyBool to accept false values
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Jan 11, 2025
1 parent f9bf8d8 commit 604df3a
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 203 deletions.
241 changes: 129 additions & 112 deletions cmd/logfile/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,125 +19,142 @@ func TestLoadParams(t *testing.T) {

defer tmpFile.Close()

ctx := context.Background()

v := viper.New()
v.Set("log-file", tmpFile.Name())
v.Set("log-to-stdout", true)
v.Set("metrics", true)
v.Set("verbose", true)
v.Set("send-diagnostics-on-errors", true)

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.True(t, params.Verbose)
assert.True(t, params.Metrics)
assert.True(t, params.ToStdout)
assert.True(t, params.SendDiagsOnErrors)
assert.Equal(t, tmpFile.Name(), params.File)
}

func TestLoadParams_LogFile_FlagDeprecated(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)

defer tmpFile.Close()

ctx := context.Background()

v := viper.New()
v.Set("logfile", tmpFile.Name())

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.Equal(t, tmpFile.Name(), params.File)
}

func TestLoadParams_LogFile_FromConfig(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)

defer tmpFile.Close()

ctx := context.Background()

v := viper.New()
v.Set("settings.log_file", tmpFile.Name())

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.Equal(t, tmpFile.Name(), params.File)
}

func TestLoadParams_LogFile_FromEnvVar(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)

defer tmpFile.Close()

dir, _ := filepath.Split(tmpFile.Name())

logFile, err := os.Create(filepath.Join(dir, "wakatime.log"))
ctx := context.Background()

v := viper.New()
t.Setenv("WAKATIME_HOME", dir)

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

defer logFile.Close()
assert.Equal(t, filepath.Join(dir, "wakatime.log"), params.File)
}

home, err := os.UserHomeDir()
func TestLoadParams_LogFile_FlagTakesPrecedence(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)

defer tmpFile.Close()

ctx := context.Background()

tests := map[string]struct {
EnvVar string
ViperDebug bool
ViperDebugConfig bool
ViperLogFile string
ViperLogFileConfig string
ViperLogFileOld string
ViperMetrics bool
ViperMetricsConfig bool
ViperToStdout bool
Expected logfile.Params
}{
"verbose set": {
ViperDebug: true,
Expected: logfile.Params{
File: filepath.Join(home, ".wakatime", "wakatime.log"),
Verbose: true,
},
},
"verbose from config": {
ViperDebugConfig: true,
Expected: logfile.Params{
File: filepath.Join(home, ".wakatime", "wakatime.log"),
Verbose: true,
},
},
"log file flag takes precedence": {
ViperLogFile: tmpFile.Name(),
ViperLogFileConfig: "otherfolder/wakatime.config.log",
ViperLogFileOld: "otherfolder/wakatime.old.log",
Expected: logfile.Params{
File: tmpFile.Name(),
},
},
"log file deprecated flag takes precedence": {
ViperLogFileConfig: "otherfolder/wakatime.config.log",
ViperLogFileOld: tmpFile.Name(),
Expected: logfile.Params{
File: tmpFile.Name(),
},
},
"log file from config": {
ViperLogFileConfig: tmpFile.Name(),
Expected: logfile.Params{
File: tmpFile.Name(),
},
},
"log file from WAKATIME_HOME": {
EnvVar: dir,
Expected: logfile.Params{
File: filepath.Join(dir, "wakatime.log"),
},
},
"log file from home dir": {
Expected: logfile.Params{
File: filepath.Join(home, ".wakatime", "wakatime.log"),
},
},
"metrics set": {
ViperMetrics: true,
Expected: logfile.Params{
File: filepath.Join(home, ".wakatime", "wakatime.log"),
Metrics: true,
},
},
"metrics from config": {
ViperMetricsConfig: true,
Expected: logfile.Params{
File: filepath.Join(home, ".wakatime", "wakatime.log"),
Metrics: true,
},
},
"metrics flag takes precedence": {
ViperMetrics: true,
ViperMetricsConfig: false,
Expected: logfile.Params{
File: filepath.Join(home, ".wakatime", "wakatime.log"),
Metrics: true,
},
},
"log to stdout": {
ViperToStdout: true,
Expected: logfile.Params{
File: filepath.Join(home, ".wakatime", "wakatime.log"),
ToStdout: true,
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
v := viper.New()
v.Set("log-file", test.ViperLogFile)
v.Set("logfile", test.ViperLogFileOld)
v.Set("log-to-stdout", test.ViperToStdout)
v.Set("metrics", test.ViperMetrics)
v.Set("settings.metrics", test.ViperMetricsConfig)
v.Set("settings.log_file", test.ViperLogFileConfig)
v.Set("settings.debug", test.ViperDebug)
v.Set("verbose", test.ViperDebugConfig)

t.Setenv("WAKATIME_HOME", test.EnvVar)

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.Equal(t, test.Expected, params)
})
}
v := viper.New()
v.Set("log-file", tmpFile.Name())
v.Set("settings.log_file", "otherfolder/wakatime.config.log")

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.Equal(t, tmpFile.Name(), params.File)
}

func TestLoadParams_Metrics_FromConfig(t *testing.T) {
ctx := context.Background()

v := viper.New()
v.Set("settings.metrics", true)

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.True(t, params.Metrics)
}

func TestLoadParams_Metrics_FlagTakesPrecedence(t *testing.T) {
ctx := context.Background()

v := viper.New()
v.Set("metrics", false)
v.Set("settings.metrics", true)

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.False(t, params.Metrics)
}

func TestLoadParams_Verbose_FromConfig(t *testing.T) {
ctx := context.Background()

v := viper.New()
v.Set("settings.debug", true)

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.True(t, params.Verbose)
}

func TestLoadParams_Verbose_FlagTakesPrecedence(t *testing.T) {
ctx := context.Background()

v := viper.New()
v.Set("verbose", false)
v.Set("settings.debug", true)

params, err := logfile.LoadParams(ctx, v)
require.NoError(t, err)

assert.False(t, params.Verbose)
}
4 changes: 2 additions & 2 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func LoadHeartbeatParams(ctx context.Context, v *viper.Viper) (Heartbeat, error)
return Heartbeat{}, errors.New("failed to retrieve entity")
}

entityExpanded, err := homedir.Expand(entity)
entity, err := homedir.Expand(entity)
if err != nil {
return Heartbeat{}, fmt.Errorf("failed expanding entity: %s", err)
}
Expand Down Expand Up @@ -463,7 +463,7 @@ func LoadHeartbeatParams(ctx context.Context, v *viper.Viper) (Heartbeat, error)
return Heartbeat{
Category: category,
CursorPosition: cursorPosition,
Entity: entityExpanded,
Entity: entity,
ExtraHeartbeats: extraHeartbeats,
EntityType: entityType,
GuessLanguage: vipertools.FirstNonEmptyBool(v, "guess-language", "settings.guess_language"),
Expand Down
Loading

0 comments on commit 604df3a

Please sign in to comment.