"time"
)
-// tmpDir creates a temporary directory and returns its name.
-func tmpDir(t *testing.T) string {
- name, err := os.MkdirTemp("", "pack")
- if err != nil {
- t.Fatal(err)
- }
- return name
-}
-
// testCreate creates an archive in the specified directory.
func testCreate(t *testing.T, dir string) {
name := filepath.Join(dir, "pack.a")
// Test that we can create an archive, write to it, and get the same contents back.
// Tests the rv and then the pv command on a new archive.
func TestCreate(t *testing.T) {
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
testCreate(t, dir)
}
// Test that we can create an archive twice with the same name (Issue 8369).
func TestCreateTwice(t *testing.T) {
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
testCreate(t, dir)
testCreate(t, dir)
}
// Test that we can create an archive, put some files in it, and get back a correct listing.
// Tests the tv command.
func TestTableOfContents(t *testing.T) {
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
name := filepath.Join(dir, "pack.a")
ar := openArchive(name, os.O_RDWR|os.O_CREATE, nil)
// Test that we can create an archive, put some files in it, and get back a file.
// Tests the x command.
func TestExtract(t *testing.T) {
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
name := filepath.Join(dir, "pack.a")
ar := openArchive(name, os.O_RDWR|os.O_CREATE, nil)
// Add some entries by hand.
func TestHello(t *testing.T) {
testenv.MustHaveGoBuild(t)
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
hello := filepath.Join(dir, "hello.go")
prog := `
package main
}
testenv.MustHaveGoBuild(t)
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
large := filepath.Join(dir, "large.go")
f, err := os.Create(large)
if err != nil {
func TestIssue21703(t *testing.T) {
testenv.MustHaveGoBuild(t)
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
const aSrc = `package a; const X = "\n!\n"`
err := os.WriteFile(filepath.Join(dir, "a.go"), []byte(aSrc), 0666)
func TestCreateWithCompilerObj(t *testing.T) {
testenv.MustHaveGoBuild(t)
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
src := filepath.Join(dir, "p.go")
prog := "package p; var X = 42\n"
err := os.WriteFile(src, []byte(prog), 0666)
func TestRWithNonexistentFile(t *testing.T) {
testenv.MustHaveGoBuild(t)
- dir := tmpDir(t)
- defer os.RemoveAll(dir)
+ dir := t.TempDir()
src := filepath.Join(dir, "p.go")
prog := "package p; var X = 42\n"
err := os.WriteFile(src, []byte(prog), 0666)