Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions internal/gui/staging_integration_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,9 @@ func TestFileDrainPersist(t *testing.T) {
t.Parallel()

localTmpDir := t.TempDir()
localFilePath := filepath.Join(localTmpDir, "stage.json")

// Create file store
fileStore := file.NewStoreWithPath(localFilePath)
fileStore := file.NewStoreWithDir(localTmpDir)

// Create state with entries
state := staging.NewEmptyState()
Expand Down Expand Up @@ -560,10 +559,9 @@ func TestFileDrainPersist(t *testing.T) {
t.Parallel()

localTmpDir := t.TempDir()
localFilePath := filepath.Join(localTmpDir, "stage.json")

// Create file store with passphrase
fileStore := file.NewStoreWithPath(localFilePath)
fileStore := file.NewStoreWithDir(localTmpDir)
fileStore.SetPassphrase("test-passphrase")

// Create state
Expand All @@ -588,7 +586,7 @@ func TestFileDrainPersist(t *testing.T) {
assert.Equal(t, "secret-value", lo.FromPtr(drainedState.Entries[staging.ServiceSecret]["my-secret"].Value))

// Drain with wrong passphrase should fail
wrongStore := file.NewStoreWithPath(localFilePath)
wrongStore := file.NewStoreWithDir(localTmpDir)
wrongStore.SetPassphrase("wrong-passphrase")
_, err = wrongStore.Drain(context.Background(), "", true)
require.Error(t, err)
Expand All @@ -599,9 +597,8 @@ func TestFileDrainPersist(t *testing.T) {
t.Parallel()

localTmpDir := t.TempDir()
localFilePath := filepath.Join(localTmpDir, "stage.json")

fileStore := file.NewStoreWithPath(localFilePath)
fileStore := file.NewStoreWithDir(localTmpDir)

// Create and write state
state := staging.NewEmptyState()
Expand All @@ -628,9 +625,8 @@ func TestFileDrainPersist(t *testing.T) {
t.Parallel()

localTmpDir := t.TempDir()
localFilePath := filepath.Join(localTmpDir, "stage.json")

fileStore := file.NewStoreWithPath(localFilePath)
fileStore := file.NewStoreWithDir(localTmpDir)

// Create state with tags only (no entries)
state := staging.NewEmptyState()
Expand All @@ -653,9 +649,8 @@ func TestFileDrainPersist(t *testing.T) {
t.Parallel()

localTmpDir := t.TempDir()
localFilePath := filepath.Join(localTmpDir, "stage.json")

fileStore := file.NewStoreWithPath(localFilePath)
fileStore := file.NewStoreWithDir(localTmpDir)

// Create state with both services
state := staging.NewEmptyState()
Expand All @@ -682,9 +677,8 @@ func TestFileDrainPersist(t *testing.T) {
t.Parallel()

localTmpDir := t.TempDir()
localFilePath := filepath.Join(localTmpDir, "nonexistent.json")

fileStore := file.NewStoreWithPath(localFilePath)
fileStore := file.NewStoreWithDir(localTmpDir)

exists, err := fileStore.Exists()
require.NoError(t, err)
Expand All @@ -701,9 +695,8 @@ func TestFileDrainPersist(t *testing.T) {
t.Parallel()

localTmpDir := t.TempDir()
localFilePath := filepath.Join(localTmpDir, "stage.json")

fileStore := file.NewStoreWithPath(localFilePath)
fileStore := file.NewStoreWithDir(localTmpDir)

// Write first state
state1 := staging.NewEmptyState()
Expand Down
99 changes: 45 additions & 54 deletions internal/staging/cli/stash_drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cli_test

import (
"bytes"
"encoding/json"
"context"
"os"
"path/filepath"
"testing"
Expand All @@ -25,7 +25,6 @@ func TestGlobalDropRunner_Run(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "stage.json")

// Write test data
state := staging.NewEmptyState()
Expand All @@ -39,35 +38,36 @@ func TestGlobalDropRunner_Run(t *testing.T) {
Value: lo.ToPtr("secret-value"),
StagedAt: time.Now(),
}
data, err := json.MarshalIndent(state, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, data, 0o600))
fileStore := file.NewStoreWithDir(tmpDir)
require.NoError(t, fileStore.WriteState(context.Background(), "", state))

fileStore := file.NewStoreWithPath(path)
stdout := &bytes.Buffer{}

runner := &cli.GlobalDropRunner{
FileStore: fileStore,
Stdout: stdout,
}

err = runner.Run()
err := runner.Run()
require.NoError(t, err)
assert.Contains(t, stdout.String(), "All stashed changes dropped")

// File should be deleted
_, err = os.Stat(path)
// Files should be deleted
paramPath := filepath.Join(tmpDir, "param.json")
secretPath := filepath.Join(tmpDir, "secret.json")
_, err = os.Stat(paramPath)
assert.True(t, os.IsNotExist(err))
_, err = os.Stat(secretPath)
assert.True(t, os.IsNotExist(err))
})

t.Run("success - drop non-existent file (no error)", func(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "stage.json")
// Don't create the file
// Don't create any files

fileStore := file.NewStoreWithPath(path)
fileStore := file.NewStoreWithDir(tmpDir)
stdout := &bytes.Buffer{}

runner := &cli.GlobalDropRunner{
Expand All @@ -89,7 +89,6 @@ func TestServiceDropRunner_Run(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "stage.json")

// Write test data with both services
state := staging.NewEmptyState()
Expand All @@ -103,11 +102,9 @@ func TestServiceDropRunner_Run(t *testing.T) {
Value: lo.ToPtr("secret-value"),
StagedAt: time.Now(),
}
data, err := json.MarshalIndent(state, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, data, 0o600))
fileStore := file.NewStoreWithDir(tmpDir)
require.NoError(t, fileStore.WriteState(context.Background(), "", state))

fileStore := file.NewStoreWithPath(path)
stdout := &bytes.Buffer{}

runner := &cli.ServiceDropRunner{
Expand All @@ -117,12 +114,16 @@ func TestServiceDropRunner_Run(t *testing.T) {
}

// Drop only param service
err = runner.Run(t.Context())
err := runner.Run(t.Context())
require.NoError(t, err)
assert.Contains(t, stdout.String(), "Stashed param changes dropped")

// File should still exist with secret service
_, err = os.Stat(path)
// param.json should be deleted, secret.json should exist
paramPath := filepath.Join(tmpDir, "param.json")
secretPath := filepath.Join(tmpDir, "secret.json")
_, err = os.Stat(paramPath)
assert.True(t, os.IsNotExist(err))
_, err = os.Stat(secretPath)
require.NoError(t, err)

// Verify secret service is preserved
Expand All @@ -136,19 +137,16 @@ func TestServiceDropRunner_Run(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "stage.json")

// Write test data with tags
state := staging.NewEmptyState()
state.Tags[staging.ServiceParam]["/app/config"] = staging.TagEntry{
Add: map[string]string{"env": "prod"},
Remove: maputil.NewSet("old-tag"),
}
data, err := json.MarshalIndent(state, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, data, 0o600))
fileStore := file.NewStoreWithDir(tmpDir)
require.NoError(t, fileStore.WriteState(context.Background(), "", state))

fileStore := file.NewStoreWithPath(path)
stdout := &bytes.Buffer{}

runner := &cli.ServiceDropRunner{
Expand All @@ -157,20 +155,20 @@ func TestServiceDropRunner_Run(t *testing.T) {
Stdout: stdout,
}

err = runner.Run(t.Context())
err := runner.Run(t.Context())
require.NoError(t, err)
assert.Contains(t, stdout.String(), "Stashed param changes dropped")

// File should be deleted (empty state)
_, err = os.Stat(path)
// param.json should be deleted (empty state)
paramPath := filepath.Join(tmpDir, "param.json")
_, err = os.Stat(paramPath)
assert.True(t, os.IsNotExist(err))
})

t.Run("error - no stashed changes for specific service", func(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "stage.json")

// Write test data with only param service
state := staging.NewEmptyState()
Expand All @@ -179,11 +177,9 @@ func TestServiceDropRunner_Run(t *testing.T) {
Value: lo.ToPtr("test-value"),
StagedAt: time.Now(),
}
data, err := json.MarshalIndent(state, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, data, 0o600))
fileStore := file.NewStoreWithDir(tmpDir)
require.NoError(t, fileStore.WriteState(context.Background(), "", state))

fileStore := file.NewStoreWithPath(path)
stdout := &bytes.Buffer{}

runner := &cli.ServiceDropRunner{
Expand All @@ -193,7 +189,7 @@ func TestServiceDropRunner_Run(t *testing.T) {
}

// Try to drop secret service which has no entries
err = runner.Run(t.Context())
err := runner.Run(t.Context())
require.Error(t, err)
assert.Contains(t, err.Error(), "no stashed changes for secret")
})
Expand All @@ -202,7 +198,6 @@ func TestServiceDropRunner_Run(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "stage.json")

// Write test data with only one service
state := staging.NewEmptyState()
Expand All @@ -211,11 +206,9 @@ func TestServiceDropRunner_Run(t *testing.T) {
Value: lo.ToPtr("test-value"),
StagedAt: time.Now(),
}
data, err := json.MarshalIndent(state, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, data, 0o600))
fileStore := file.NewStoreWithDir(tmpDir)
require.NoError(t, fileStore.WriteState(context.Background(), "", state))

fileStore := file.NewStoreWithPath(path)
stdout := &bytes.Buffer{}

runner := &cli.ServiceDropRunner{
Expand All @@ -225,19 +218,19 @@ func TestServiceDropRunner_Run(t *testing.T) {
}

// Drop the only service
err = runner.Run(t.Context())
err := runner.Run(t.Context())
require.NoError(t, err)

// File should be deleted because state is now empty
_, err = os.Stat(path)
paramPath := filepath.Join(tmpDir, "param.json")
_, err = os.Stat(paramPath)
assert.True(t, os.IsNotExist(err))
})

t.Run("file preserved after drop specific service", func(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "stage.json")

// Write test data with both services
state := staging.NewEmptyState()
Expand All @@ -251,11 +244,9 @@ func TestServiceDropRunner_Run(t *testing.T) {
Value: lo.ToPtr("secret-value"),
StagedAt: time.Now(),
}
data, err := json.MarshalIndent(state, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, data, 0o600))
fileStore := file.NewStoreWithDir(tmpDir)
require.NoError(t, fileStore.WriteState(context.Background(), "", state))

fileStore := file.NewStoreWithPath(path)
stdout := &bytes.Buffer{}

runner := &cli.ServiceDropRunner{
Expand All @@ -264,20 +255,20 @@ func TestServiceDropRunner_Run(t *testing.T) {
Stdout: stdout,
}

err = runner.Run(t.Context())
err := runner.Run(t.Context())
require.NoError(t, err)

// File should still exist
_, err = os.Stat(path)
// secret.json should still exist, param.json should be deleted
paramPath := filepath.Join(tmpDir, "param.json")
secretPath := filepath.Join(tmpDir, "secret.json")
_, err = os.Stat(paramPath)
assert.True(t, os.IsNotExist(err))
_, err = os.Stat(secretPath)
require.NoError(t, err)

// Read and verify remaining data
//nolint:gosec // G304: path is from t.TempDir(), safe for test
remainingData, err := os.ReadFile(path)
remainingState, err := fileStore.Drain(t.Context(), staging.ServiceSecret, true)
require.NoError(t, err)

var remainingState staging.State
require.NoError(t, json.Unmarshal(remainingData, &remainingState))
assert.Empty(t, remainingState.Entries[staging.ServiceParam])
assert.Len(t, remainingState.Entries[staging.ServiceSecret], 1)
})
Expand Down
Loading
Loading