Skip to content

Commit

Permalink
[bugfix] Fix writes to global config from tests (#2727)
Browse files Browse the repository at this point in the history
* [bugfix] Fix writes to global config from tests

Fixes #2725

Signed-off-by: Dominik Schulz <[email protected]>

* Shorten readonly config creation.

Signed-off-by: Dominik Schulz <[email protected]>

* Address review comments

Signed-off-by: Dominik Schulz <[email protected]>

---------

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Dec 1, 2023
1 parent bf426e6 commit 263b781
Show file tree
Hide file tree
Showing 108 changed files with 340 additions and 319 deletions.
6 changes: 3 additions & 3 deletions internal/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func newMock(ctx context.Context, path string) (*Action, error) {
cfg := config.NewNoWrites()
cfg := config.NewInMemory()
if err := cfg.SetPath(path); err != nil {
return nil, err
}
Expand Down Expand Up @@ -48,7 +48,7 @@ func newMock(ctx context.Context, path string) (*Action, error) {
func TestAction(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithInteractive(ctx, false)

act, err := newMock(ctx, u.StoreDir(""))
Expand All @@ -73,7 +73,7 @@ func TestNew(t *testing.T) {
assert.NotNil(t, u)

td := t.TempDir()
cfg := config.NewNoWrites()
cfg := config.NewInMemory()
sv := semver.Version{}

t.Run("init a new store", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/action/aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package action

import (
"bytes"
"context"
"os"
"testing"

"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
Expand All @@ -15,7 +15,7 @@ import (
func TestAliases(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)
act, err := newMock(ctx, u.StoreDir(""))
Expand Down
4 changes: 2 additions & 2 deletions internal/action/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package action

import (
"bytes"
"context"
"os"
"testing"

"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/gopass/secrets"
Expand All @@ -17,7 +17,7 @@ import (
func TestAudit(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)
act, err := newMock(ctx, u.StoreDir(""))
Expand Down
14 changes: 7 additions & 7 deletions internal/action/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package action
import (
"bufio"
"bytes"
"context"
"math/rand"
"os"
"path/filepath"
"testing"

"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
Expand All @@ -19,7 +19,7 @@ import (
func TestBinary(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)

Expand All @@ -45,7 +45,7 @@ func TestBinaryCat(t *testing.T) {

u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestBinaryCat(t *testing.T) {
func TestBinaryCatSizes(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)

Expand Down Expand Up @@ -154,7 +154,7 @@ func TestBinaryCatSizes(t *testing.T) {
func TestBinaryCopy(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)

Expand Down Expand Up @@ -216,7 +216,7 @@ func TestBinaryCopy(t *testing.T) {
func TestBinarySum(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)

Expand Down Expand Up @@ -247,7 +247,7 @@ func TestBinarySum(t *testing.T) {
func TestBinaryGet(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)

Expand Down
12 changes: 6 additions & 6 deletions internal/action/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func aGitRepo(ctx context.Context, t *testing.T, u *gptest.Unit, name string) st
func TestClone(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)
ctx = backend.WithStorageBackend(ctx, backend.GitFS)
Expand Down Expand Up @@ -94,11 +94,11 @@ func TestCloneBackendIsStoredForMount(t *testing.T) {
stdout = os.Stdout
}()

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)

cfg := config.NewNoWrites()
cfg := config.NewInMemory()
require.NoError(t, cfg.SetPath(u.StoreDir("")))

act, err := newAction(cfg, semver.Version{}, false)
Expand All @@ -125,7 +125,7 @@ func TestCloneGetGitConfig(t *testing.T) {
r2 := gptest.UnsetVars(termio.EmailVars...)
defer r2()

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)

Expand Down Expand Up @@ -153,11 +153,11 @@ func TestCloneCheckDecryptionKeys(t *testing.T) {
stdout = os.Stdout
}()

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithInteractive(ctx, false)

cfg := config.NewNoWrites()
cfg := config.NewInMemory()
require.NoError(t, cfg.SetPath(u.StoreDir("")))

act, err := newAction(cfg, semver.Version{}, false)
Expand Down
4 changes: 2 additions & 2 deletions internal/action/commands_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package action

import (
"context"
"testing"

"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -40,7 +40,7 @@ func testCommand(t *testing.T, cmd *cli.Command) {
func TestCommands(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithInteractive(ctx, false)
act, err := newMock(ctx, u.StoreDir(""))
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions internal/action/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package action

import (
"bytes"
"context"
"os"
"testing"

"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestComplete(t *testing.T) {
stdout = os.Stdout
}()

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithInteractive(ctx, false)
act, err := newMock(ctx, u.StoreDir(""))
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions internal/action/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package action

import (
"bytes"
"context"
"os"
"strings"
"testing"

"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
Expand All @@ -17,7 +17,7 @@ import (
func TestConfig(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithInteractive(ctx, false)
act, err := newMock(ctx, u.StoreDir(""))
require.NoError(t, err)
Expand Down
16 changes: 8 additions & 8 deletions internal/action/context_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package action

import (
"context"
"testing"

"github.com/gopasspw/gopass/internal/config"
"github.com/stretchr/testify/assert"
)

func TestWithClip(t *testing.T) {
ctx := context.Background()
ctx := config.NewContextInMemory()

if IsClip(ctx) {
t.Errorf("Should be false")
Expand All @@ -20,7 +20,7 @@ func TestWithClip(t *testing.T) {
}

func TestWithPasswordOnly(t *testing.T) {
ctx := context.Background()
ctx := config.NewContextInMemory()

if IsPasswordOnly(ctx) {
t.Errorf("Should be false")
Expand All @@ -32,14 +32,14 @@ func TestWithPasswordOnly(t *testing.T) {
}

func TestWithPrintQR(t *testing.T) {
ctx := context.Background()
ctx := config.NewContextInMemory()

assert.False(t, IsPrintQR(ctx))
assert.True(t, IsPrintQR(WithPrintQR(ctx, true)))
}

func TestWithRevision(t *testing.T) {
ctx := context.Background()
ctx := config.NewContextInMemory()

assert.Equal(t, "", GetRevision(ctx))
assert.Equal(t, "foo", GetRevision(WithRevision(ctx, "foo")))
Expand All @@ -48,21 +48,21 @@ func TestWithRevision(t *testing.T) {
}

func TestWithKey(t *testing.T) {
ctx := context.Background()
ctx := config.NewContextInMemory()

assert.Equal(t, "", GetKey(ctx))
assert.Equal(t, "foo", GetKey(WithKey(ctx, "foo")))
}

func TestWithOnlyClip(t *testing.T) {
ctx := context.Background()
ctx := config.NewContextInMemory()

assert.False(t, IsOnlyClip(ctx))
assert.True(t, IsOnlyClip(WithOnlyClip(ctx, true)))
}

func TestWithAlsoClip(t *testing.T) {
ctx := context.Background()
ctx := config.NewContextInMemory()

assert.False(t, IsAlsoClip(ctx))
assert.True(t, IsAlsoClip(WithAlsoClip(ctx, true)))
Expand Down
4 changes: 2 additions & 2 deletions internal/action/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package action

import (
"bytes"
"context"
"os"
"testing"

"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/gopass/secrets"
Expand All @@ -17,7 +17,7 @@ import (
func TestConvert(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithTerminal(ctx, false)
ctx = ctxutil.WithInteractive(ctx, false)
Expand Down
6 changes: 3 additions & 3 deletions internal/action/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package action

import (
"bytes"
"context"
"os"
"testing"

"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
Expand All @@ -18,7 +18,7 @@ import (
func TestCopy(t *testing.T) {
u := gptest.NewUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithInteractive(ctx, false)
ctx = ctxutil.WithAlwaysYes(ctx, true)

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestCopyGpg(t *testing.T) {

u := gptest.NewGUnitTester(t)

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithInteractive(ctx, false)
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = backend.WithCryptoBackend(ctx, backend.GPGCLI)
Expand Down
4 changes: 2 additions & 2 deletions internal/action/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package action

import (
"bytes"
"context"
"os"
"testing"

aclip "github.com/atotto/clipboard"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/tests/gptest"
Expand All @@ -18,7 +18,7 @@ func TestCreate(t *testing.T) {

aclip.Unsupported = true

ctx := context.Background()
ctx := config.NewContextInMemory()
ctx = ctxutil.WithAlwaysYes(ctx, true)

act, err := newMock(ctx, u.StoreDir(""))
Expand Down
Loading

0 comments on commit 263b781

Please sign in to comment.