github.com/release-engineering/exodus-rsync@v1.11.2/internal/cmd/helpers_test.go (about) 1 package cmd 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "github.com/golang/mock/gomock" 9 "github.com/release-engineering/exodus-rsync/internal/args" 10 "github.com/release-engineering/exodus-rsync/internal/log" 11 ) 12 13 func MockController(t *testing.T) *gomock.Controller { 14 oldExt := ext 15 t.Cleanup(func() { ext = oldExt }) 16 17 return gomock.NewController(t) 18 } 19 20 func RestoreWd(t *testing.T) { 21 oldDir, err := os.Getwd() 22 if err != nil { 23 t.Fatal("getwd:", err) 24 } 25 26 t.Cleanup(func() { 27 if err := os.Chdir(oldDir); err != nil { 28 t.Fatal("chdir (cleanup):", err) 29 } 30 }) 31 } 32 33 // Ensure exodus-rsync.conf contains given text for duration of current test. 34 // Also changes the current working directory to a tempdir. 35 func SetConfig(t *testing.T, config string) { 36 temp := t.TempDir() 37 38 RestoreWd(t) 39 40 if err := os.Chdir(temp); err != nil { 41 t.Fatal("chdir:", err) 42 } 43 44 if err := os.WriteFile("exodus-rsync.conf", []byte(config), 0644); err != nil { 45 t.Fatal("writing config file:", err) 46 } 47 } 48 49 // Returns a reasonably configured Context which has a real logger present. 50 func testContext() context.Context { 51 ctx := context.Background() 52 ctx = log.NewContext(ctx, ext.log.NewLogger(args.Config{})) 53 return ctx 54 }