From aeb920895dc2d624d9c68d1cde3e85f7456bf04c Mon Sep 17 00:00:00 2001 From: Jeff Halter <868228+jhalter@users.noreply.github.com> Date: Sun, 29 Jun 2025 16:25:30 -0700 Subject: Fix tests modifying test fixture files by using temporary directories Previously, running tests would modify test/config/Users/guest.yaml and create test/config/Users/test-user.yaml due to the automatic migration logic in YAMLAccountManager. This caused git diff to show changes after running tests. Solution: - Modified TestNewYAMLAccountManager to use t.TempDir() for test isolation - Added copyTestFiles helper to copy test fixtures to temporary directory - Tests now run against copies, leaving original fixtures untouched This ensures tests are properly isolated and don't have side effects on the repository's test fixture files. --- internal/mobius/account_manager_test.go | 131 ++++++++++++++------------------ 1 file changed, 55 insertions(+), 76 deletions(-) (limited to 'internal') diff --git a/internal/mobius/account_manager_test.go b/internal/mobius/account_manager_test.go index fe834a4..a6f39ca 100644 --- a/internal/mobius/account_manager_test.go +++ b/internal/mobius/account_manager_test.go @@ -1,89 +1,68 @@ package mobius import ( - "fmt" + "os" + "path" "github.com/jhalter/mobius/hotline" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "testing" ) -func TestNewYAMLAccountManager(t *testing.T) { - type args struct { - accountDir string - } - tests := []struct { - name string - args args - want *YAMLAccountManager - wantErr assert.ErrorAssertionFunc - }{ - { - name: "loads accounts from a directory", - args: args{ - accountDir: "test/config/Users", - }, - want: &YAMLAccountManager{ - accountDir: "test/config/Users", - accounts: map[string]hotline.Account{ - "admin": { - Name: "admin", - Login: "admin", - Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a", - Access: hotline.AccessBitmap{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00}, - }, - "Test User Name": { - Name: "test-user", - Login: "Test User Name", - Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS", - Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00}, - }, - "guest": { - Name: "guest", - Login: "guest", - Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y", - Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00}, - }, - }, - }, - wantErr: assert.NoError, - }, +// copyTestFiles copies test config files to a temporary directory +func copyTestFiles(t *testing.T, srcDir, dstDir string) { + files := []string{"admin.yaml", "guest.yaml", "user-with-old-access-format.yaml"} + + for _, file := range files { + srcPath := path.Join(srcDir, file) + dstPath := path.Join(dstDir, file) + + data, err := os.ReadFile(srcPath) + require.NoError(t, err, "Failed to read %s", srcPath) + + err = os.WriteFile(dstPath, data, 0644) + require.NoError(t, err, "Failed to write %s", dstPath) } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := NewYAMLAccountManager(tt.args.accountDir) - if !tt.wantErr(t, err, fmt.Sprintf("NewYAMLAccountManager(%v)", tt.args.accountDir)) { - return - } +} - assert.Equal(t, - &hotline.Account{ - Name: "admin", - Login: "admin", - Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a", - Access: hotline.AccessBitmap{0xff, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00}, - }, - got.Get("admin"), - ) +func TestNewYAMLAccountManager(t *testing.T) { + t.Run("loads accounts from a directory", func(t *testing.T) { + // Create temporary directory and copy test files + tempDir := t.TempDir() + copyTestFiles(t, "test/config/Users", tempDir) + + // Use temp directory instead of original test directory + got, err := NewYAMLAccountManager(tempDir) + require.NoError(t, err) - assert.Equal(t, - &hotline.Account{ - Login: "guest", - Name: "guest", - Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y", - Access: hotline.AccessBitmap{0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00}, - }, - got.Get("guest"), - ) + assert.Equal(t, + &hotline.Account{ + Name: "admin", + Login: "admin", + Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a", + Access: hotline.AccessBitmap{0xff, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00}, + }, + got.Get("admin"), + ) - assert.Equal(t, - &hotline.Account{ - Login: "test-user", - Name: "Test User Name", - Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS", - Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00}, - }, - got.Get("test-user"), - ) - }) - } + assert.Equal(t, + &hotline.Account{ + Login: "guest", + Name: "guest", + Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y", + Access: hotline.AccessBitmap{0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00}, + }, + got.Get("guest"), + ) + + assert.Equal(t, + &hotline.Account{ + Login: "test-user", + Name: "Test User Name", + Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS", + Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00}, + }, + got.Get("test-user"), + ) + }) } -- cgit