Skip to content

Commit

Permalink
Copy with trailing slash at destination fix and test.
Browse files Browse the repository at this point in the history
Signed-off-by: Vijay Kesanakurthi <[email protected]>
  • Loading branch information
vijay-kesanakurthi committed Oct 9, 2024
1 parent 970a349 commit 589195a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
18 changes: 13 additions & 5 deletions internal/action/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package action
import (
"context"
"fmt"
"github.com/gopasspw/gopass/internal/tree"
"path/filepath"
"strings"

Expand Down Expand Up @@ -43,17 +44,24 @@ func (s *Action) copy(ctx context.Context, from, to string, force bool) error {
}

func (s *Action) copyFlattenDir(ctx context.Context, from, to string, force bool) error {
entries, err := s.Store.List(ctx, 0)
entries, err := s.Store.List(ctx, tree.INF)

if err != nil {
return exit.Error(exit.List, err, "failed to list entries in %q", from)
}

fromPrefix := from
if !strings.HasSuffix(fromPrefix, "/") {
fromPrefix += "/"
}

for _, entry := range entries {
fromPath := filepath.Join(from, entry)
toPath := filepath.Join(to, filepath.Base(entry))
if strings.HasPrefix(entry, fromPrefix) {
toPath := filepath.Join(to, filepath.Base(entry))

if err := s.copyRegular(ctx, fromPath, toPath, force); err != nil {
return err
if err := s.copyRegular(ctx, entry, toPath, force); err != nil {
return err
}
}
}

Expand Down
92 changes: 92 additions & 0 deletions internal/action/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,95 @@ func TestCopyGpg(t *testing.T) {
assert.Equal(t, "barfoo", buf.String())
buf.Reset()
}

func TestCopyWithTrailingSlash(t *testing.T) {
u := gptest.NewUnitTester(t)

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

act, err := newMock(ctx, u.StoreDir(""))
require.NoError(t, err)
require.NotNil(t, act)
ctx = act.cfg.WithConfig(ctx)

require.NoError(t, act.cfg.Set("", "generate.autoclip", "false"))

buf := &bytes.Buffer{}
out.Stdout = buf
stdout = buf
defer func() {
stdout = os.Stdout
out.Stdout = os.Stdout
}()

color.NoColor = true

// generate foo
c := gptest.CliCtx(ctx, t, "foo")
require.NoError(t, act.Generate(c))
buf.Reset()

// copy foo bar
c = gptest.CliCtx(ctx, t, "foo", "bar")
require.NoError(t, act.Copy(c))
buf.Reset()

// copy foo bar (again, should fail)
{
ctx := ctxutil.WithAlwaysYes(ctx, false)
ctx = ctxutil.WithInteractive(ctx, false)
c.Context = ctx
require.Error(t, act.Copy(c))
buf.Reset()
}

// copy not-found still-not-there
c = gptest.CliCtx(ctx, t, "not-found", "still-not-there")
require.Error(t, act.Copy(c))
buf.Reset()

// copy
c = gptest.CliCtx(ctx, t)
require.Error(t, act.Copy(c))
buf.Reset()

// Create a directory structure
require.NoError(t, act.insertStdin(ctx, "secret/some/zab", []byte("secret"), false))
require.NoError(t, act.insertStdin(ctx, "secret/baz", []byte("another"), false))

// Test copying a directory with trailing slash
c = gptest.CliCtx(ctx, t, "secret/", "new/")
require.NoError(t, act.Copy(c))
buf.Reset()

// Verify the result
require.NoError(t, act.List(gptest.CliCtx(ctx, t)))
want := `gopass
├── bar
├── foo
├── new/
│ ├── baz
│ └── zab
└── secret/
├── baz
└── some/
└── zab
`

assert.Equal(t, want, buf.String())
buf.Reset()

// Verify content of copied files
ctx = ctxutil.WithTerminal(ctx, false)
require.NoError(t, act.show(ctx, c, "new/zab", false))
assert.Equal(t, "secret\n", buf.String())
buf.Reset()

require.NoError(t, act.show(ctx, c, "new/baz", false))
assert.Equal(t, "another\n", buf.String())
buf.Reset()

}

Check failure on line 274 in internal/action/copy_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

Check failure on line 274 in internal/action/copy_test.go

View workflow job for this annotation

GitHub Actions / linux

unnecessary trailing newline (whitespace)

0 comments on commit 589195a

Please sign in to comment.