2022-08-24 21:45:55 +00:00
|
|
|
// Copyright 2020-2022 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-06 22:19:09 +00:00
|
|
|
|
|
|
|
package testutil
|
|
|
|
|
2021-04-30 21:28:03 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
2020-08-06 22:19:09 +00:00
|
|
|
|
|
|
|
// ErrorWriter implements io.Writer by returning a fixed error.
|
|
|
|
type ErrorWriter struct {
|
|
|
|
ReturnError error
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.Writer = &ErrorWriter{}
|
|
|
|
|
|
|
|
func (e *ErrorWriter) Write([]byte) (int, error) { return 0, e.ReturnError }
|
2021-04-30 21:28:03 +00:00
|
|
|
|
|
|
|
func WriteStringToTempFile(t *testing.T, filename string, fileBody string) *os.File {
|
|
|
|
t.Helper()
|
2022-08-24 21:45:55 +00:00
|
|
|
f, err := os.CreateTemp("", filename)
|
2021-04-30 21:28:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
deferMe := func() {
|
|
|
|
err := os.Remove(f.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
t.Cleanup(deferMe)
|
|
|
|
_, err = f.WriteString(fileBody)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = f.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return f
|
|
|
|
}
|