github.com/knoebber/dotfile@v1.0.6/local/helpers_test.go (about)

     1  package local
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/knoebber/dotfile/dotfile"
     9  )
    10  
    11  const (
    12  	testAlias          = "testalias"
    13  	testMessage        = "test message"
    14  	testHash           = "9abdbcf4ea4e2c1c077c21b8c2f2470ff36c31ce"
    15  	testUpdatedHash    = "5d12fbbc6038e0b6a3e798dd790512ba03de7b6a"
    16  	testDir            = "testdata/"
    17  	testConfigPath     = testDir + "test_config.json"
    18  	testTrackedFile    = testDir + "testfile.txt"
    19  	testContent        = "Some stuff.\n"
    20  	testUpdatedContent = testContent + "Some new content.\nNew lines!\n"
    21  )
    22  
    23  func initTestData(t *testing.T) {
    24  	_ = os.Mkdir(testDir, 0755)
    25  	writeTestFile(t, []byte(testContent))
    26  }
    27  
    28  func updateTestFile(t *testing.T) {
    29  	writeTestFile(t, []byte(testUpdatedContent))
    30  }
    31  
    32  func clearTestStorage() {
    33  	_ = os.RemoveAll(testDir)
    34  }
    35  
    36  func resetTestStorage(t *testing.T) {
    37  	clearTestStorage()
    38  	initTestData(t)
    39  }
    40  
    41  func testStorage() *Storage {
    42  	return &Storage{
    43  		Dir:   testDir,
    44  		Alias: testAlias,
    45  	}
    46  }
    47  
    48  func setupTestFile(t *testing.T) *Storage {
    49  	clearTestStorage()
    50  	initTestData(t)
    51  
    52  	fullPath, err := filepath.Abs(testTrackedFile)
    53  	if err != nil {
    54  		t.Fatalf("getting full path for %q: %v", testTrackedFile, err)
    55  	}
    56  
    57  	s := testStorage()
    58  	s.FileData = &dotfile.TrackingData{
    59  		Path:    fullPath,
    60  		Commits: []dotfile.Commit{},
    61  	}
    62  
    63  	if err := dotfile.Init(s, fullPath, testAlias); err != nil {
    64  		t.Fatalf("initializing test file: %v", err)
    65  	}
    66  
    67  	// Read the newly initialized test file.
    68  	if err := s.SetTrackingData(); err != nil {
    69  		t.Fatalf("reading test file tracking data: %v", err)
    70  	}
    71  
    72  	return s
    73  }
    74  
    75  func writeTestFile(t *testing.T, contents []byte) {
    76  	if err := os.WriteFile(testTrackedFile, contents, 0644); err != nil {
    77  		t.Fatalf("setting up %s: %v", testTrackedFile, err)
    78  	}
    79  }