@@ -49,17 +49,6 @@ func runConstant(t *testing.T, inst *f1.F1, scenario string) {
4949 require .NoError (t , err )
5050}
5151
52- func TestEnvVarsUsedByDefault (t * testing.T ) {
53- ts , count := newPushGatewayServer (t )
54- t .Setenv ("PROMETHEUS_PUSH_GATEWAY" , ts .URL )
55-
56- inst := newF1WithScenario ("env_default" )
57- runConstant (t , inst , "env_default" )
58-
59- require .Positive (t , count .Load (),
60- "PROMETHEUS_PUSH_GATEWAY env var should trigger metrics push" )
61- }
62-
6352func TestWithSettingsReplacesPushGateway (t * testing.T ) {
6453 t .Parallel ()
6554
@@ -75,26 +64,17 @@ func TestWithSettingsReplacesPushGateway(t *testing.T) {
7564 "WithSettings should configure push gateway without env vars" )
7665}
7766
78- func TestWithSettingsEmptyIgnoresEnvVars (t * testing.T ) {
79- ts , count := newPushGatewayServer (t )
80- t .Setenv ("PROMETHEUS_PUSH_GATEWAY" , ts .URL )
81-
82- inst := newF1WithScenario ("no_env" , f1 .WithSettings (f1.Settings {}))
83- runConstant (t , inst , "no_env" )
84-
85- require .Equal (t , int32 (0 ), count .Load (),
86- "WithSettings(Settings{}) should ignore env vars" )
87- }
67+ func TestWithSettingsEmptyDisablesAllSettings (t * testing.T ) {
68+ t .Parallel ()
8869
89- func TestWithPrometheusPushGatewayOverridesEnv (t * testing.T ) {
9070 ts , count := newPushGatewayServer (t )
91- t . Setenv ( "PROMETHEUS_PUSH_GATEWAY" , "http://env-should-not-be-used.invalid" )
71+ _ = ts
9272
93- inst := newF1WithScenario ("override " , f1 .WithPrometheusPushGateway ( ts . URL ))
94- runConstant (t , inst , "override " )
73+ inst := newF1WithScenario ("empty_settings " , f1 .WithSettings (f1. Settings {} ))
74+ runConstant (t , inst , "empty_settings " )
9575
96- require .Positive ( t , count .Load (),
97- "WithPrometheusPushGateway should override env var " )
76+ require .Equal ( t , int32 ( 0 ) , count .Load (),
77+ "WithSettings(Settings{}) should start from zero values; no push gateway " )
9878}
9979
10080func TestFineGrainedOverridesAfterWithSettings (t * testing.T ) {
@@ -111,6 +91,21 @@ func TestFineGrainedOverridesAfterWithSettings(t *testing.T) {
11191 "fine-grained options should apply after WithSettings" )
11292}
11393
94+ func TestWithSettingsOverridesFinegrained (t * testing.T ) {
95+ t .Parallel ()
96+
97+ ts , count := newPushGatewayServer (t )
98+
99+ inst := newF1WithScenario ("settings_last" ,
100+ f1 .WithPrometheusPushGateway (ts .URL ),
101+ f1 .WithSettings (f1.Settings {}),
102+ )
103+ runConstant (t , inst , "settings_last" )
104+
105+ require .Equal (t , int32 (0 ), count .Load (),
106+ "WithSettings placed after fine-grained options should replace them" )
107+ }
108+
114109func TestWithLogLevelAndFormat (t * testing.T ) {
115110 t .Parallel ()
116111
@@ -141,35 +136,50 @@ func TestWithLoggerTakesPrecedenceOverLogOptions(t *testing.T) {
141136 "explicit logger format (text) should be used, not JSON from WithLogFormat" )
142137}
143138
144- func TestDefaultSettingsLoadsEnv (t * testing.T ) {
145- t .Setenv ("PROMETHEUS_PUSH_GATEWAY" , "http://test:9091" )
146- t .Setenv ("PROMETHEUS_NAMESPACE" , "test-ns" )
147- t .Setenv ("PROMETHEUS_LABEL_ID" , "test-id" )
148- t .Setenv ("LOG_FILE_PATH" , "/tmp/test.log" )
149- t .Setenv ("F1_LOG_LEVEL" , "debug" )
150- t .Setenv ("F1_LOG_FORMAT" , "json" )
151-
152- s := f1 .DefaultSettings ()
153- require .Equal (t , "http://test:9091" , s .Prometheus .PushGateway )
154- require .Equal (t , "test-ns" , s .Prometheus .Namespace )
155- require .Equal (t , "test-id" , s .Prometheus .LabelID )
156- require .Equal (t , "/tmp/test.log" , s .Logging .FilePath )
157- require .Equal (t , slog .LevelDebug , s .Logging .Level )
158- require .Equal (t , f1 .LogFormatJSON , s .Logging .Format )
159- }
160-
161- func TestDefaultSettingsReturnsDefaults (t * testing.T ) {
162- t .Setenv ("PROMETHEUS_PUSH_GATEWAY" , "" )
163- t .Setenv ("PROMETHEUS_NAMESPACE" , "" )
164- t .Setenv ("PROMETHEUS_LABEL_ID" , "" )
165- t .Setenv ("LOG_FILE_PATH" , "" )
166- t .Setenv ("F1_LOG_LEVEL" , "" )
167- t .Setenv ("F1_LOG_FORMAT" , "" )
168-
169- s := f1 .DefaultSettings ()
170- require .Empty (t , s .Prometheus .PushGateway )
171- require .Equal (t , slog .LevelInfo , s .Logging .Level )
172- require .Equal (t , f1 .LogFormatText , s .Logging .Format )
139+ func TestWithLoggerTakesPrecedenceOverWithSettings (t * testing.T ) {
140+ t .Parallel ()
141+
142+ var buf bytes.Buffer
143+ logger := log .NewTestLogger (& buf )
144+
145+ inst := newF1WithScenario ("logger_over_settings" ,
146+ f1 .WithSettings (f1.Settings {
147+ Logging : f1.LoggingSettings {
148+ Level : slog .LevelError ,
149+ Format : f1 .LogFormatJSON ,
150+ },
151+ }),
152+ f1 .WithLogger (logger ),
153+ )
154+ runConstant (t , inst , "logger_over_settings" )
155+
156+ output := buf .String ()
157+ require .NotEmpty (t , output , "WithLogger should capture output" )
158+ require .NotContains (t , output , `"level"` ,
159+ "WithLogger text format should override Settings JSON format" )
160+ }
161+
162+ func TestWithSettingsAllFields (t * testing.T ) {
163+ t .Parallel ()
164+
165+ ts , count := newPushGatewayServer (t )
166+ inst := newF1WithScenario ("all_fields" ,
167+ f1 .WithSettings (f1.Settings {
168+ Prometheus : f1.PrometheusSettings {
169+ PushGateway : ts .URL ,
170+ Namespace : "test-ns" ,
171+ LabelID : "test-id" ,
172+ },
173+ Logging : f1.LoggingSettings {
174+ Level : slog .LevelDebug ,
175+ Format : f1 .LogFormatJSON ,
176+ },
177+ }),
178+ )
179+ runConstant (t , inst , "all_fields" )
180+
181+ require .Positive (t , count .Load (),
182+ "WithSettings with PushGateway should trigger metrics push" )
173183}
174184
175185func TestParseLogLevel (t * testing.T ) {
0 commit comments