Skip to content

Commit

Permalink
can't copy a key to the same key
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebob committed Jan 12, 2022
1 parent 120d1d0 commit 13d855c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd_generic.go
Expand Up @@ -604,6 +604,11 @@ func (m *Miniredis) cmdCopy(c *server.Peer, cmd string, args []string) {
toDB = fromDB
}

if fromDB == toDB && opts.from == opts.to {
c.WriteError("ERR source and destination objects are the same")
return
}

if !m.db(fromDB).exists(opts.from) {
c.WriteInt(0)
return
Expand Down
14 changes: 14 additions & 0 deletions integration/generic_test.go
Expand Up @@ -337,6 +337,20 @@ func TestCopy(t *testing.T) {
c.Do("TYPE", "replaceme")
c.Do("GET", "replaceme")
})
c.Do("SELECT", "0")

t.Run("copy to self", func(t *testing.T) {
// copy to self is never allowed
c.Do("SET", "double", "1")
c.Error("the same", "COPY", "double", "double")
c.Error("the same", "COPY", "double", "double", "REPLACE")
c.Do("COPY", "double", "double", "DB", "2") // different DB is fine
c.Do("SELECT", "2")
c.Do("TYPE", "double")

c.Error("the same", "COPY", "noexisting", "noexisting") // "copy to self?" check comes before key check
})
c.Do("SELECT", "0")

// deep copies?
t.Run("hash", func(t *testing.T) {
Expand Down

0 comments on commit 13d855c

Please sign in to comment.