Reduce config test setup duplication with option builders
This commit is contained in:
@@ -538,15 +538,9 @@ func TestCoalesceGapUsesValidOverride(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "merged.json")
|
output := filepath.Join(dir, "merged.json")
|
||||||
|
|
||||||
cfg, err := NewMergeConfig(MergeOptions{
|
opts := validMergeOptions(input, output)
|
||||||
InputFiles: []string{input},
|
opts.CoalesceGap = "1.5"
|
||||||
OutputFile: output,
|
cfg, err := NewMergeConfig(opts)
|
||||||
InputReader: DefaultInputReader,
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
PreprocessingModules: DefaultPreprocessingModules,
|
|
||||||
PostprocessingModules: DefaultPostprocessingModules,
|
|
||||||
CoalesceGap: "1.5",
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("config failed: %v", err)
|
t.Fatalf("config failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -560,15 +554,9 @@ func TestCoalesceGapAllowsZero(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "merged.json")
|
output := filepath.Join(dir, "merged.json")
|
||||||
|
|
||||||
cfg, err := NewMergeConfig(MergeOptions{
|
opts := validMergeOptions(input, output)
|
||||||
InputFiles: []string{input},
|
opts.CoalesceGap = "0"
|
||||||
OutputFile: output,
|
cfg, err := NewMergeConfig(opts)
|
||||||
InputReader: DefaultInputReader,
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
PreprocessingModules: DefaultPreprocessingModules,
|
|
||||||
PostprocessingModules: DefaultPostprocessingModules,
|
|
||||||
CoalesceGap: "0",
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("config failed: %v", err)
|
t.Fatalf("config failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -593,15 +581,9 @@ func TestCoalesceGapRejectsInvalidOverride(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "merged.json")
|
output := filepath.Join(dir, "merged.json")
|
||||||
|
|
||||||
_, err := NewMergeConfig(MergeOptions{
|
opts := validMergeOptions(input, output)
|
||||||
InputFiles: []string{input},
|
opts.CoalesceGap = test.value
|
||||||
OutputFile: output,
|
_, err := NewMergeConfig(opts)
|
||||||
InputReader: DefaultInputReader,
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
PreprocessingModules: DefaultPreprocessingModules,
|
|
||||||
PostprocessingModules: DefaultPostprocessingModules,
|
|
||||||
CoalesceGap: test.value,
|
|
||||||
})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error")
|
t.Fatal("expected error")
|
||||||
}
|
}
|
||||||
@@ -639,20 +621,16 @@ func TestNewTrimConfigRequiresExactlyOneSelectorFlag(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "trimmed.json")
|
output := filepath.Join(dir, "trimmed.json")
|
||||||
|
|
||||||
_, err := NewTrimConfig(TrimOptions{
|
opts := validTrimOptions(input, output)
|
||||||
InputFile: input,
|
opts.Keep = ""
|
||||||
OutputFile: output,
|
_, err := NewTrimConfig(opts)
|
||||||
})
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "exactly one of --keep or --remove is required") {
|
if err == nil || !strings.Contains(err.Error(), "exactly one of --keep or --remove is required") {
|
||||||
t.Fatalf("expected missing selector error, got %v", err)
|
t.Fatalf("expected missing selector error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = NewTrimConfig(TrimOptions{
|
opts = validTrimOptions(input, output)
|
||||||
InputFile: input,
|
opts.Remove = "2"
|
||||||
OutputFile: output,
|
_, err = NewTrimConfig(opts)
|
||||||
Keep: "1",
|
|
||||||
Remove: "2",
|
|
||||||
})
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "mutually exclusive") {
|
if err == nil || !strings.Contains(err.Error(), "mutually exclusive") {
|
||||||
t.Fatalf("expected mutually exclusive selector error, got %v", err)
|
t.Fatalf("expected mutually exclusive selector error, got %v", err)
|
||||||
}
|
}
|
||||||
@@ -664,14 +642,13 @@ func TestNewTrimConfigAcceptsOutputSchemaOverride(t *testing.T) {
|
|||||||
output := filepath.Join(dir, "trimmed.json")
|
output := filepath.Join(dir, "trimmed.json")
|
||||||
reportPath := filepath.Join(dir, "report.json")
|
reportPath := filepath.Join(dir, "report.json")
|
||||||
|
|
||||||
cfg, err := NewTrimConfig(TrimOptions{
|
opts := validTrimOptions(input, output)
|
||||||
InputFile: input,
|
opts.Keep = ""
|
||||||
OutputFile: output,
|
opts.Remove = "3-5"
|
||||||
ReportFile: reportPath,
|
opts.ReportFile = reportPath
|
||||||
Remove: "3-5",
|
opts.OutputSchema = OutputSchemaMinimal
|
||||||
OutputSchema: OutputSchemaMinimal,
|
opts.AllowEmpty = true
|
||||||
AllowEmpty: true,
|
cfg, err := NewTrimConfig(opts)
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("config failed: %v", err)
|
t.Fatalf("config failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -697,12 +674,9 @@ func TestNewTrimConfigTreatsWhitespaceReportFileAsOmitted(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "trimmed.json")
|
output := filepath.Join(dir, "trimmed.json")
|
||||||
|
|
||||||
cfg, err := NewTrimConfig(TrimOptions{
|
opts := validTrimOptions(input, output)
|
||||||
InputFile: input,
|
opts.ReportFile = " \t "
|
||||||
OutputFile: output,
|
cfg, err := NewTrimConfig(opts)
|
||||||
Keep: "1",
|
|
||||||
ReportFile: " \t ",
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("config failed: %v", err)
|
t.Fatalf("config failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -716,12 +690,9 @@ func TestNewTrimConfigRejectsInvalidOutputSchemaOverride(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "trimmed.json")
|
output := filepath.Join(dir, "trimmed.json")
|
||||||
|
|
||||||
_, err := NewTrimConfig(TrimOptions{
|
opts := validTrimOptions(input, output)
|
||||||
InputFile: input,
|
opts.OutputSchema = "compact"
|
||||||
OutputFile: output,
|
_, err := NewTrimConfig(opts)
|
||||||
Keep: "1",
|
|
||||||
OutputSchema: "compact",
|
|
||||||
})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected output schema validation error")
|
t.Fatal("expected output schema validation error")
|
||||||
}
|
}
|
||||||
@@ -750,10 +721,8 @@ func TestNewNormalizeConfigRequiresOutputFile(t *testing.T) {
|
|||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
|
|
||||||
_, err := NewNormalizeConfig(NormalizeOptions{
|
opts := validNormalizeOptions(input, "")
|
||||||
InputFile: input,
|
_, err := NewNormalizeConfig(opts)
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected output-file required error")
|
t.Fatal("expected output-file required error")
|
||||||
}
|
}
|
||||||
@@ -768,11 +737,8 @@ func TestNewNormalizeConfigResolvesOutputSchemaDefaultAndEnv(t *testing.T) {
|
|||||||
output := filepath.Join(dir, "normalized.json")
|
output := filepath.Join(dir, "normalized.json")
|
||||||
|
|
||||||
t.Setenv(OutputSchemaEnv, "")
|
t.Setenv(OutputSchemaEnv, "")
|
||||||
cfg, err := NewNormalizeConfig(NormalizeOptions{
|
opts := validNormalizeOptions(input, output)
|
||||||
InputFile: input,
|
cfg, err := NewNormalizeConfig(opts)
|
||||||
OutputFile: output,
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("config failed: %v", err)
|
t.Fatalf("config failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -781,11 +747,7 @@ func TestNewNormalizeConfigResolvesOutputSchemaDefaultAndEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Setenv(OutputSchemaEnv, OutputSchemaMinimal)
|
t.Setenv(OutputSchemaEnv, OutputSchemaMinimal)
|
||||||
cfg, err = NewNormalizeConfig(NormalizeOptions{
|
cfg, err = NewNormalizeConfig(opts)
|
||||||
InputFile: input,
|
|
||||||
OutputFile: output,
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("config failed: %v", err)
|
t.Fatalf("config failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -799,12 +761,9 @@ func TestNewNormalizeConfigRejectsInvalidOutputSchema(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "normalized.json")
|
output := filepath.Join(dir, "normalized.json")
|
||||||
|
|
||||||
_, err := NewNormalizeConfig(NormalizeOptions{
|
opts := validNormalizeOptions(input, output)
|
||||||
InputFile: input,
|
opts.OutputSchema = "compact"
|
||||||
OutputFile: output,
|
_, err := NewNormalizeConfig(opts)
|
||||||
OutputSchema: "compact",
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected output schema error")
|
t.Fatal("expected output schema error")
|
||||||
}
|
}
|
||||||
@@ -818,11 +777,9 @@ func TestNewNormalizeConfigRejectsUnknownOutputModule(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "normalized.json")
|
output := filepath.Join(dir, "normalized.json")
|
||||||
|
|
||||||
_, err := NewNormalizeConfig(NormalizeOptions{
|
opts := validNormalizeOptions(input, output)
|
||||||
InputFile: input,
|
opts.OutputModules = "json,yaml"
|
||||||
OutputFile: output,
|
_, err := NewNormalizeConfig(opts)
|
||||||
OutputModules: "json,yaml",
|
|
||||||
})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected output module error")
|
t.Fatal("expected output module error")
|
||||||
}
|
}
|
||||||
@@ -836,12 +793,9 @@ func TestNewNormalizeConfigTreatsWhitespaceReportFileAsOmitted(t *testing.T) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "normalized.json")
|
output := filepath.Join(dir, "normalized.json")
|
||||||
|
|
||||||
cfg, err := NewNormalizeConfig(NormalizeOptions{
|
opts := validNormalizeOptions(input, output)
|
||||||
InputFile: input,
|
opts.ReportFile = "\n\t "
|
||||||
OutputFile: output,
|
cfg, err := NewNormalizeConfig(opts)
|
||||||
ReportFile: "\n\t ",
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("config failed: %v", err)
|
t.Fatalf("config failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -870,14 +824,7 @@ func assertPositiveFloatEnvValidation(t *testing.T, envName string) {
|
|||||||
input := writeTempFile(t, dir, "input.json")
|
input := writeTempFile(t, dir, "input.json")
|
||||||
output := filepath.Join(dir, "merged.json")
|
output := filepath.Join(dir, "merged.json")
|
||||||
|
|
||||||
_, err := NewMergeConfig(MergeOptions{
|
_, err := NewMergeConfig(validMergeOptions(input, output))
|
||||||
InputFiles: []string{input},
|
|
||||||
OutputFile: output,
|
|
||||||
InputReader: DefaultInputReader,
|
|
||||||
OutputModules: DefaultOutputModules,
|
|
||||||
PreprocessingModules: DefaultPreprocessingModules,
|
|
||||||
PostprocessingModules: DefaultPostprocessingModules,
|
|
||||||
})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error")
|
t.Fatal("expected error")
|
||||||
}
|
}
|
||||||
@@ -888,6 +835,33 @@ func assertPositiveFloatEnvValidation(t *testing.T, envName string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validMergeOptions(inputFile string, outputFile string) MergeOptions {
|
||||||
|
return MergeOptions{
|
||||||
|
InputFiles: []string{inputFile},
|
||||||
|
OutputFile: outputFile,
|
||||||
|
InputReader: DefaultInputReader,
|
||||||
|
OutputModules: DefaultOutputModules,
|
||||||
|
PreprocessingModules: DefaultPreprocessingModules,
|
||||||
|
PostprocessingModules: DefaultPostprocessingModules,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validTrimOptions(inputFile string, outputFile string) TrimOptions {
|
||||||
|
return TrimOptions{
|
||||||
|
InputFile: inputFile,
|
||||||
|
OutputFile: outputFile,
|
||||||
|
Keep: "1",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validNormalizeOptions(inputFile string, outputFile string) NormalizeOptions {
|
||||||
|
return NormalizeOptions{
|
||||||
|
InputFile: inputFile,
|
||||||
|
OutputFile: outputFile,
|
||||||
|
OutputModules: DefaultOutputModules,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func writeTempFile(t *testing.T, dir string, name string) string {
|
func writeTempFile(t *testing.T, dir string, name string) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user