github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/state_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"path/filepath"
     8  	"regexp"
     9  	"sort"
    10  	"testing"
    11  
    12  	"github.com/terramate-io/tf/states/statemgr"
    13  )
    14  
    15  // testStateBackups returns the list of backups in order of creation
    16  // (oldest first) in the given directory.
    17  func testStateBackups(t *testing.T, dir string) []string {
    18  	// Find all the backups
    19  	list, err := filepath.Glob(filepath.Join(dir, "*"+DefaultBackupExtension))
    20  	if err != nil {
    21  		t.Fatalf("err: %s", err)
    22  	}
    23  
    24  	// Sort them which will put them naturally in the right order
    25  	sort.Strings(list)
    26  
    27  	return list
    28  }
    29  
    30  func TestStateDefaultBackupExtension(t *testing.T) {
    31  	testCwd(t)
    32  
    33  	s, err := (&StateMeta{}).State()
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	backupPath := s.(*statemgr.Filesystem).BackupPath()
    39  	match := regexp.MustCompile(`terraform\.tfstate\.\d+\.backup$`).MatchString
    40  	if !match(backupPath) {
    41  		t.Fatal("Bad backup path:", backupPath)
    42  	}
    43  }