|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/spf13/pflag" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/golangci/golangci-lint/v2/pkg/logutils" |
| 11 | +) |
| 12 | + |
| 13 | +func TestLoader_handleClearConfigOutputs(t *testing.T) { |
| 14 | + t.Run("flag not set", func(t *testing.T) { |
| 15 | + // Setup |
| 16 | + cfg := &Config{ |
| 17 | + Output: Output{ |
| 18 | + Formats: Formats{ |
| 19 | + JSON: SimpleFormat{Path: "/tmp/config.json"}, |
| 20 | + HTML: SimpleFormat{Path: "/tmp/config.html"}, |
| 21 | + }, |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 26 | + fs.Bool("clear-config-outputs", false, "test flag") |
| 27 | + |
| 28 | + loader := &Loader{ |
| 29 | + BaseLoader: &BaseLoader{ |
| 30 | + log: logutils.NewStderrLog(logutils.DebugKeyEmpty), |
| 31 | + }, |
| 32 | + fs: fs, |
| 33 | + cfg: cfg, |
| 34 | + } |
| 35 | + |
| 36 | + // Execute |
| 37 | + err := loader.handleClearConfigOutputs() |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + // Verify - config outputs should remain unchanged |
| 41 | + assert.Equal(t, "/tmp/config.json", cfg.Output.Formats.JSON.Path) |
| 42 | + assert.Equal(t, "/tmp/config.html", cfg.Output.Formats.HTML.Path) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("flag set with no CLI outputs", func(t *testing.T) { |
| 46 | + // Setup |
| 47 | + cfg := &Config{ |
| 48 | + Output: Output{ |
| 49 | + Formats: Formats{ |
| 50 | + JSON: SimpleFormat{Path: "/tmp/config.json"}, |
| 51 | + HTML: SimpleFormat{Path: "/tmp/config.html"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 57 | + fs.Bool("clear-config-outputs", false, "test flag") |
| 58 | + fs.String("output.json.path", "", "json output path") |
| 59 | + fs.String("output.html.path", "", "html output path") |
| 60 | + |
| 61 | + // Set the flag |
| 62 | + err := fs.Set("clear-config-outputs", "true") |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + loader := &Loader{ |
| 66 | + BaseLoader: &BaseLoader{ |
| 67 | + log: logutils.NewStderrLog(logutils.DebugKeyEmpty), |
| 68 | + }, |
| 69 | + fs: fs, |
| 70 | + cfg: cfg, |
| 71 | + } |
| 72 | + |
| 73 | + // Execute |
| 74 | + err = loader.handleClearConfigOutputs() |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + // Verify - all config outputs should be cleared |
| 78 | + assert.Empty(t, cfg.Output.Formats.JSON.Path) |
| 79 | + assert.Empty(t, cfg.Output.Formats.HTML.Path) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("flag set with CLI JSON output", func(t *testing.T) { |
| 83 | + // Setup |
| 84 | + cfg := &Config{ |
| 85 | + Output: Output{ |
| 86 | + Formats: Formats{ |
| 87 | + JSON: SimpleFormat{Path: "/tmp/config.json"}, |
| 88 | + HTML: SimpleFormat{Path: "/tmp/config.html"}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 94 | + fs.Bool("clear-config-outputs", false, "test flag") |
| 95 | + fs.String("output.json.path", "", "json output path") |
| 96 | + fs.String("output.html.path", "", "html output path") |
| 97 | + |
| 98 | + // Set the flag and CLI output |
| 99 | + err := fs.Set("clear-config-outputs", "true") |
| 100 | + require.NoError(t, err) |
| 101 | + err = fs.Set("output.json.path", "/tmp/cli.json") |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + loader := &Loader{ |
| 105 | + BaseLoader: &BaseLoader{ |
| 106 | + log: logutils.NewStderrLog(logutils.DebugKeyEmpty), |
| 107 | + }, |
| 108 | + fs: fs, |
| 109 | + cfg: cfg, |
| 110 | + } |
| 111 | + |
| 112 | + // Execute |
| 113 | + err = loader.handleClearConfigOutputs() |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + // Verify - only CLI output should remain |
| 117 | + assert.Equal(t, "/tmp/cli.json", cfg.Output.Formats.JSON.Path) |
| 118 | + assert.Empty(t, cfg.Output.Formats.HTML.Path) |
| 119 | + }) |
| 120 | + |
| 121 | + t.Run("flag set with multiple CLI outputs", func(t *testing.T) { |
| 122 | + // Setup |
| 123 | + cfg := &Config{ |
| 124 | + Output: Output{ |
| 125 | + Formats: Formats{ |
| 126 | + JSON: SimpleFormat{Path: "/tmp/config.json"}, |
| 127 | + HTML: SimpleFormat{Path: "/tmp/config.html"}, |
| 128 | + Text: Text{ |
| 129 | + SimpleFormat: SimpleFormat{Path: "/tmp/config.txt"}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 136 | + fs.Bool("clear-config-outputs", false, "test flag") |
| 137 | + fs.String("output.json.path", "", "json output path") |
| 138 | + fs.String("output.html.path", "", "html output path") |
| 139 | + fs.String("output.text.path", "", "text output path") |
| 140 | + |
| 141 | + // Set the flag and CLI outputs |
| 142 | + err := fs.Set("clear-config-outputs", "true") |
| 143 | + require.NoError(t, err) |
| 144 | + err = fs.Set("output.json.path", "/tmp/cli.json") |
| 145 | + require.NoError(t, err) |
| 146 | + err = fs.Set("output.html.path", "/tmp/cli.html") |
| 147 | + require.NoError(t, err) |
| 148 | + |
| 149 | + loader := &Loader{ |
| 150 | + BaseLoader: &BaseLoader{ |
| 151 | + log: logutils.NewStderrLog(logutils.DebugKeyEmpty), |
| 152 | + }, |
| 153 | + fs: fs, |
| 154 | + cfg: cfg, |
| 155 | + } |
| 156 | + |
| 157 | + // Execute |
| 158 | + err = loader.handleClearConfigOutputs() |
| 159 | + require.NoError(t, err) |
| 160 | + |
| 161 | + // Verify - only CLI outputs should remain |
| 162 | + assert.Equal(t, "/tmp/cli.json", cfg.Output.Formats.JSON.Path) |
| 163 | + assert.Equal(t, "/tmp/cli.html", cfg.Output.Formats.HTML.Path) |
| 164 | + assert.Empty(t, cfg.Output.Formats.Text.Path) |
| 165 | + }) |
| 166 | + |
| 167 | + t.Run("flag set with CLI format options", func(t *testing.T) { |
| 168 | + // Setup |
| 169 | + cfg := &Config{ |
| 170 | + Output: Output{ |
| 171 | + Formats: Formats{ |
| 172 | + Text: Text{ |
| 173 | + SimpleFormat: SimpleFormat{Path: "/tmp/config.txt"}, |
| 174 | + PrintLinterName: false, |
| 175 | + PrintIssuedLine: false, |
| 176 | + Colors: false, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + fs := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 183 | + fs.Bool("clear-config-outputs", false, "test flag") |
| 184 | + fs.String("output.text.path", "", "text output path") |
| 185 | + fs.Bool("output.text.print-linter-name", true, "print linter name") |
| 186 | + fs.Bool("output.text.colors", true, "use colors") |
| 187 | + |
| 188 | + // Set the flag and CLI outputs with options |
| 189 | + err := fs.Set("clear-config-outputs", "true") |
| 190 | + require.NoError(t, err) |
| 191 | + err = fs.Set("output.text.path", "/tmp/cli.txt") |
| 192 | + require.NoError(t, err) |
| 193 | + err = fs.Set("output.text.print-linter-name", "true") |
| 194 | + require.NoError(t, err) |
| 195 | + err = fs.Set("output.text.colors", "false") |
| 196 | + require.NoError(t, err) |
| 197 | + |
| 198 | + loader := &Loader{ |
| 199 | + BaseLoader: &BaseLoader{ |
| 200 | + log: logutils.NewStderrLog(logutils.DebugKeyEmpty), |
| 201 | + }, |
| 202 | + fs: fs, |
| 203 | + cfg: cfg, |
| 204 | + } |
| 205 | + |
| 206 | + // Execute |
| 207 | + err = loader.handleClearConfigOutputs() |
| 208 | + require.NoError(t, err) |
| 209 | + |
| 210 | + // Verify - CLI output with options should be preserved |
| 211 | + assert.Equal(t, "/tmp/cli.txt", cfg.Output.Formats.Text.Path) |
| 212 | + assert.True(t, cfg.Output.Formats.Text.PrintLinterName) |
| 213 | + assert.False(t, cfg.Output.Formats.Text.Colors) |
| 214 | + assert.False(t, cfg.Output.Formats.Text.PrintIssuedLine) // Not set via CLI, should be default false |
| 215 | + }) |
| 216 | +} |
0 commit comments