go-hep.org/x/hep@v0.38.1/groot/riofs/plugin/http/http_test.go (about)

     1  // Copyright ©2019 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  func TestTmpFile(t *testing.T) {
    13  	f, err := os.CreateTemp("", "riofs-remote-")
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	tmp := tmpFile{f}
    18  	defer tmp.Close()
    19  
    20  	const want = "foo\n"
    21  	_, err = tmp.WriteString(want)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	err = tmp.Sync()
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	raw, err := os.ReadFile(tmp.Name())
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	str := string(raw)
    36  	if str != want {
    37  		t.Fatalf("got=%q. want=%q", str, want)
    38  	}
    39  
    40  	err = tmp.Close()
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	_, err = os.Stat(tmp.Name())
    46  	if err == nil {
    47  		t.Fatalf("file %q should have been removed", tmp.Name())
    48  	}
    49  }