X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=main_test.go;h=2b48bcc7c9d37315910f8efdac80633ed269a6af;hb=3393730a802af2c7d0849287c12f55f35ed6a09b;hp=47865651f8e5e8e0dc17f867091e52800dcc6daa;hpb=2c0aa8d6355d54bea3824db280dd4e0f10e595a7;p=nsscash%2Fnsscash.git diff --git a/main_test.go b/main_test.go index 4786565..2b48bcc 100644 --- a/main_test.go +++ b/main_test.go @@ -207,6 +207,12 @@ func TestMainFetch(t *testing.T) { fetchSecondFetchFails, } + for _, f := range tests { + runMainTest(t, f) + } +} + +func runMainTest(t *testing.T, f func(args)) { cleanup := []string{ configPath, statePath, @@ -215,40 +221,37 @@ func TestMainFetch(t *testing.T) { groupPath, } - for _, f := range tests { - // NOTE: This is not guaranteed to work according to reflect's - // documentation but seems to work reliable for normal - // functions. - fn := runtime.FuncForPC(reflect.ValueOf(f).Pointer()) - name := fn.Name() - name = name[strings.LastIndex(name, ".")+1:] - - t.Run(name, func(t *testing.T) { - // Preparation & cleanup - for _, p := range cleanup { - err := os.Remove(p) - if err != nil && !os.IsNotExist(err) { - t.Fatal(err) - } - // Remove the file at the end of this test - // run, if it was created - defer os.Remove(p) + // NOTE: This is not guaranteed to work according to reflect's + // documentation but seems to work reliable for normal functions. + fn := runtime.FuncForPC(reflect.ValueOf(f).Pointer()) + name := fn.Name() + name = name[strings.LastIndex(name, ".")+1:] + + t.Run(name, func(t *testing.T) { + // Preparation & cleanup + for _, p := range cleanup { + err := os.Remove(p) + if err != nil && !os.IsNotExist(err) { + t.Fatal(err) } + // Remove the file at the end of this test run, if it + // was created + defer os.Remove(p) + } - var handler func(http.ResponseWriter, *http.Request) - ts := httptest.NewServer(http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - handler(w, r) - })) - defer ts.Close() - - f(args{ - t: t, - url: ts.URL, - handler: &handler, - }) + var handler func(http.ResponseWriter, *http.Request) + ts := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + handler(w, r) + })) + defer ts.Close() + + f(args{ + t: t, + url: ts.URL, + handler: &handler, }) - } + }) } func fetchPasswdCacheFileDoesNotExist(a args) {