github.com/decred/politeia@v1.4.0/politeiad/backendv2/tstorebe/testing.go (about) 1 // Copyright (c) 2020-2021 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package tstorebe 6 7 import ( 8 "os" 9 "path/filepath" 10 "sync" 11 "testing" 12 13 "github.com/decred/politeia/politeiad/backendv2/tstorebe/tstore" 14 ) 15 16 // NewTestTstoreBackend returns a tstoreBackend that is setup for testing and a 17 // closure that cleans up all test data when invoked. 18 func NewTestTstoreBackend(t *testing.T) (*tstoreBackend, func()) { 19 t.Helper() 20 21 // Setup home dir and data dir 22 appDir, err := os.MkdirTemp("", "tstorebackend.test") 23 if err != nil { 24 t.Fatal(err) 25 } 26 dataDir := filepath.Join(appDir, "data") 27 28 tstoreBackend := tstoreBackend{ 29 appDir: appDir, 30 dataDir: dataDir, 31 tstore: tstore.NewTestTstore(t, dataDir), 32 recordMtxs: make(map[string]*sync.Mutex), 33 } 34 35 return &tstoreBackend, func() { 36 err = os.RemoveAll(appDir) 37 if err != nil { 38 t.Fatal(err) 39 } 40 } 41 }