From 13d855cec0d6ad393438b6e06cf3d24d41f7c0de Mon Sep 17 00:00:00 2001 From: Harmen Date: Wed, 12 Jan 2022 10:54:56 +0100 Subject: [PATCH] can't copy a key to the same key --- cmd_generic.go | 5 +++++ integration/generic_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cmd_generic.go b/cmd_generic.go index dabd2f49..d9bac197 100644 --- a/cmd_generic.go +++ b/cmd_generic.go @@ -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 diff --git a/integration/generic_test.go b/integration/generic_test.go index 3bb3b378..91f1ddac 100644 --- a/integration/generic_test.go +++ b/integration/generic_test.go @@ -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) {