Repository: etcd-io/etcd
File: server/etcdmain/config_test.go (Go)

Code before:
			expectedWatchProgressNotifyInterval: 2 * time.Minute,
		},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			cmdLineArgs := []string{}
			yc := struct {
				ExperimentalWatchProgressNotifyInterval time.Duration `json:"experimental-watch-progress-notify-interval,omitempty"`
				WatchProgressNotifyInterval             time.Duration `json:"watch-progress-notify-interval,omitempty"`
			}{}

			if tc.watchProgressNotifyInterval != "" {
				cmdLineArgs = append(cmdLineArgs, fmt.Sprintf("--watch-progress-notify-interval=%s", tc.watchProgressNotifyInterval))
				watchProgressNotifyInterval, err := time.ParseDuration(tc.watchProgressNotifyInterval)
				if err != nil {
					t.Fatal(err)
				}
				yc.WatchProgressNotifyInterval = watchProgressNotifyInterval
			}

			if tc.experimentalWatchProgressNotifyInterval != "" {
				cmdLineArgs = append(cmdLineArgs, fmt.Sprintf("--experimental-watch-progress-notify-interval=%s", tc.experimentalWatchProgressNotifyInterval))
				experimentalWatchProgressNotifyInterval, err := time.ParseDuration(tc.experimentalWatchProgressNotifyInterval)
				if err != nil {
					t.Fatal(err)
				}
				yc.ExperimentalWatchProgressNotifyInterval = experimentalWatchProgressNotifyInterval
			}

			cfgFromCmdLine, errFromCmdLine, cfgFromFile, errFromFile := generateCfgsFromFileAndCmdLine(t, yc, cmdLineArgs)

			if tc.expectErr {
				if errFromCmdLine == nil || errFromFile == nil {
					t.Fatal("expect parse error")
				}
				return
			}
			if errFromCmdLine != nil || errFromFile != nil {
				t.Fatal("error parsing config")
			}

			if cfgFromCmdLine.ec.WatchProgressNotifyInterval != tc.expectedWatchProgressNotifyInterval {
				t.Errorf("expected WatchProgressNotifyInterval=%v, got %v", tc.expectedWatchProgressNotifyInterval, cfgFromCmdLine.ec.WatchProgressNotifyInterval)
			}
			if cfgFromFile.ec.WatchProgressNotifyInterval != tc.expectedWatchProgressNotifyInterval {
				t.Errorf("expected WatchProgressNotifyInterval=%v, got %v", tc.expectedWatchProgressNotifyInterval, cfgFromFile.ec.WatchProgressNotifyInterval)
			}
		})
	}
}


Review comment (suggestion):
```suggestion
				if err != nil {
					t.Fatal(err)
				}
				require.NoError(t, err)
```

Code after:
				cmdLineArgs = append(cmdLineArgs, fmt.Sprintf("--experimental-watch-progress-notify-interval=%s", tc.experimentalWatchProgressNotifyInterval))
				experimentalWatchProgressNotifyInterval, err := time.ParseDuration(tc.experimentalWatchProgressNotifyInterval)
				require.NoError(t, err)
				yc.ExperimentalWatchProgressNotifyInterval = experimentalWatchProgressNotifyInterval
			}

			cfgFromCmdLine, errFromCmdLine, cfgFromFile, errFromFile := generateCfgsFromFileAndCmdLine(t, yc, cmdLineArgs)

			if tc.expectErr {
				if errFromCmdLine == nil || errFromFile == nil {
					t.Fatal("expect parse error")
				}
				return
			}
			if errFromCmdLine != nil || errFromFile != nil {
				t.Fatal("error parsing config")
			}

			require.Equal(t, tc.expectedWatchProgressNotifyInterval, cfgFromCmdLine.ec.WatchProgressNotifyInterval)
			require.Equal(t, tc.expectedWatchProgressNotifyInterval, cfgFromFile.ec.WatchProgressNotifyInterval)
		})
	}
}

// TODO delete in v3.7
func generateCfgsFromFileAndCmdLine(t *testing.T, yc any, cmdLineArgs []string) (*config, error, *config, error) {
	b, err := yaml.Marshal(&yc)
	require.NoError(t, err)

	tmpfile := mustCreateCfgFile(t, b)
	defer os.Remove(tmpfile.Name())

	cfgFromCmdLine := newConfig()
	errFromCmdLine := cfgFromCmdLine.parse(cmdLineArgs)

	cfgFromFile := newConfig()
	errFromFile := cfgFromFile.parse([]string{fmt.Sprintf("--config-file=%s", tmpfile.Name())})
	return cfgFromCmdLine, errFromCmdLine, cfgFromFile, errFromFile
}

func mustCreateCfgFile(t *testing.T, b []byte) *os.File {
	tmpfile, err := os.CreateTemp("", "servercfg")
	if err != nil {
		t.Fatal(err)
	}

	_, err = tmpfile.Write(b)
	if err != nil {
		t.Fatal(err)
	}
	err = tmpfile.Close()