-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: override configuration from flags only if set (#865)
* fix: override configuration from flags only if set * use helper func and test it
- Loading branch information
Showing
2 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-shiori/shiori/internal/config" | ||
"github.com/spf13/pflag" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_setIfFlagChanged(t *testing.T) { | ||
type args struct { | ||
flagName string | ||
flags func() *pflag.FlagSet | ||
cfg *config.Config | ||
fn func(cfg *config.Config) | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
assertFn func(t *testing.T, cfg *config.Config) | ||
}{ | ||
{ | ||
name: "Flag didn't change", | ||
args: args{ | ||
flagName: "port", | ||
flags: func() *pflag.FlagSet { | ||
return &pflag.FlagSet{} | ||
}, | ||
cfg: &config.Config{ | ||
Http: &config.HttpConfig{ | ||
Port: 8080, | ||
}, | ||
}, | ||
fn: func(cfg *config.Config) { | ||
cfg.Http.Port = 9999 | ||
}, | ||
}, | ||
assertFn: func(t *testing.T, cfg *config.Config) { | ||
require.Equal(t, cfg.Http.Port, 8080) | ||
}, | ||
}, | ||
{ | ||
name: "Flag changed", | ||
args: args{ | ||
flagName: "port", | ||
flags: func() *pflag.FlagSet { | ||
pf := &pflag.FlagSet{} | ||
pf.IntP("port", "p", 8080, "Port used by the server") | ||
pf.Set("port", "9999") | ||
return pf | ||
}, | ||
cfg: &config.Config{ | ||
Http: &config.HttpConfig{ | ||
Port: 8080, | ||
}, | ||
}, | ||
fn: func(cfg *config.Config) { | ||
cfg.Http.Port = 9999 | ||
}, | ||
}, | ||
assertFn: func(t *testing.T, cfg *config.Config) { | ||
require.Equal(t, cfg.Http.Port, 9999) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
setIfFlagChanged(tt.args.flagName, tt.args.flags(), tt.args.cfg, tt.args.fn) | ||
}) | ||
} | ||
} |