github.com/iotexproject/iotex-core@v1.14.1-rc1/testutil/file.go (about)

     1  // Copyright (c) 2018 IoTeX
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package testutil
     7  
     8  import (
     9  	"os"
    10  
    11  	"github.com/iotexproject/iotex-core/pkg/util/fileutil"
    12  )
    13  
    14  // PathOfTempFile returns path of a new temporary file
    15  func PathOfTempFile(dirName string) (string, error) {
    16  	tempFile, err := os.CreateTemp(os.TempDir(), dirName)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  	return tempFile.Name(), tempFile.Close()
    21  }
    22  
    23  // CleanupPath detects the existence of test DB file and removes it if found
    24  func CleanupPath(path string) {
    25  	if fileutil.FileExists(path) && os.RemoveAll(path) != nil {
    26  		panic("Fail to remove testDB file")
    27  	}
    28  }