File: tempfile_test.go

package info (click to toggle)
incus 6.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,864 kB
  • sloc: sh: 16,015; ansic: 3,121; python: 456; makefile: 321; ruby: 51; sql: 50; lisp: 6
file content (24 lines) | stat: -rw-r--r-- 547 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package osarch

import (
	"os"

	"github.com/stretchr/testify/suite"
)

// WriteTempFile writes content to a temporary file.
func WriteTempFile(s *suite.Suite, dir string, prefix string, content string) (string, func()) {
	f, err := os.CreateTemp(dir, prefix)
	if err != nil {
		s.T().Errorf("Failed to create temporary file: %v", err)
	}

	defer func() { _ = f.Close() }()

	_, err = f.WriteString(content)
	if err != nil {
		s.T().Errorf("Failed to write string to temp file: %v", err)
	}

	return f.Name(), func() { _ = os.Remove(f.Name()) }
}