Skip to content

Commit a31b8c3

Browse files
committed
Refactor test configuration setup to use temporary HOME directory for isolation and improve environment variable handling
1 parent 8be2b0f commit a31b8c3

2 files changed

Lines changed: 14 additions & 34 deletions

File tree

sourcecontrol/pkg/commitmanager/manager_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,22 @@ func setupTestRepo(t *testing.T) (*sourcerepo.SourceRepository, string) {
3737
func setupTestConfig(t *testing.T, repo *sourcerepo.SourceRepository) {
3838
t.Helper()
3939

40-
// Use environment variables for test configuration
41-
// This is what the manager falls back to anyway
42-
os.Setenv("GIT_AUTHOR_NAME", "Test User")
43-
os.Setenv("GIT_AUTHOR_EMAIL", "test@example.com")
44-
45-
// Cleanup function to restore environment
40+
// Create a temporary HOME directory to isolate config files
41+
// This prevents tests from reading/writing to the real user config
42+
tempHome, err := os.MkdirTemp("", "test-home-*")
43+
if err != nil {
44+
t.Fatalf("Failed to create temp home dir: %v", err)
45+
}
4646
t.Cleanup(func() {
47-
os.Unsetenv("GIT_AUTHOR_NAME")
48-
os.Unsetenv("GIT_AUTHOR_EMAIL")
47+
os.RemoveAll(tempHome)
4948
})
49+
50+
// Use t.Setenv for test-scoped environment variables
51+
// This is safe for parallel test execution and handles cleanup automatically
52+
t.Setenv("HOME", tempHome) // Unix/Linux
53+
t.Setenv("USERPROFILE", tempHome) // Windows
54+
t.Setenv("GIT_AUTHOR_NAME", "Test User")
55+
t.Setenv("GIT_AUTHOR_EMAIL", "test@example.com")
5056
}
5157

5258
// addFileToIndex adds a file to the index

sourcecontrol/pkg/index/index.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ func (idx *Index) Clear() {
147147
}
148148

149149
// Paths returns a slice of all staged file paths.
150-
// The returned slice is a copy and can be safely modified.
151-
//
152-
// Returns:
153-
// - Slice of relative paths for all staged files
154150
func (idx *Index) Paths() []scpath.RelativePath {
155151
paths := make([]scpath.RelativePath, len(idx.Entries))
156152
for i, e := range idx.Entries {
@@ -160,8 +156,6 @@ func (idx *Index) Paths() []scpath.RelativePath {
160156
}
161157

162158
// Count returns the total number of staged files in the index.
163-
//
164-
// Returns the number of entries.
165159
func (idx *Index) Count() int {
166160
return len(idx.Entries)
167161
}
@@ -210,11 +204,6 @@ func (idx *Index) Serialize(w io.Writer) error {
210204
// - Signature: "DIRC" (4 bytes)
211205
// - Version: uint32 big-endian (4 bytes)
212206
// - Entry count: uint32 big-endian (4 bytes)
213-
//
214-
// Parameters:
215-
// - w: Writer to output the header
216-
//
217-
// Returns an error if any write operation fails.
218207
func (idx *Index) writeHeader(w io.Writer) error {
219208
if _, err := w.Write([]byte(IndexSignature)); err != nil {
220209
return fmt.Errorf("failed to write signature: %w", err)
@@ -276,13 +265,6 @@ func (idx *Index) Deserialize(r io.Reader) error {
276265

277266
// validateChecksum verifies the SHA-1 checksum of the index data.
278267
// This ensures the index file hasn't been corrupted or tampered with.
279-
//
280-
// Parameters:
281-
// - data: Complete index file data including checksum
282-
//
283-
// Returns an error if:
284-
// - Data is too small to contain a checksum
285-
// - Calculated checksum doesn't match stored checksum
286268
func validateChecksum(data []byte) error {
287269
if len(data) < IndexHeaderSize+IndexChecksumSize {
288270
return fmt.Errorf("invalid index file: too small")
@@ -301,14 +283,6 @@ func validateChecksum(data []byte) error {
301283

302284
// readHeader reads and validates the 12-byte index header.
303285
// Initializes the Entries slice based on the entry count from the header.
304-
//
305-
// Parameters:
306-
// - r: Reader positioned at the start of the index data
307-
//
308-
// Returns an error if:
309-
// - Signature is not "DIRC"
310-
// - Version is not supported (currently only version 2)
311-
// - Any read operation fails
312286
func (idx *Index) readHeader(r io.Reader) error {
313287
sig := make([]byte, 4)
314288
if _, err := io.ReadFull(r, sig); err != nil {

0 commit comments

Comments
 (0)