github.com/mithrandie/csvq@v1.18.1/lib/action/main_test.go (about)

     1  package action
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  var TestDir = filepath.Join(os.TempDir(), "csvq_action_test")
    10  var TestDataDir string
    11  
    12  func GetTestFilePath(filename string) string {
    13  	return filepath.Join(TestDir, filename)
    14  }
    15  
    16  func TestMain(m *testing.M) {
    17  	os.Exit(run(m))
    18  }
    19  
    20  func run(m *testing.M) int {
    21  	defer teardown()
    22  
    23  	setup()
    24  	return m.Run()
    25  }
    26  
    27  func setup() {
    28  	if _, err := os.Stat(TestDir); err == nil {
    29  		_ = os.RemoveAll(TestDir)
    30  	}
    31  
    32  	wdir, _ := os.Getwd()
    33  	TestDataDir = filepath.Join(wdir, "..", "..", "testdata", "csv")
    34  
    35  	if _, err := os.Stat(TestDir); os.IsNotExist(err) {
    36  		_ = os.Mkdir(TestDir, 0755)
    37  	}
    38  
    39  	r, _ := os.Open(filepath.Join(TestDataDir, "empty.txt"))
    40  	os.Stdin = r
    41  }
    42  
    43  func teardown() {
    44  	if _, err := os.Stat(TestDir); err == nil {
    45  		_ = os.RemoveAll(TestDir)
    46  	}
    47  }