Skip to content

Commit

Permalink
daemon, strutil: move daemon.quotedNames to strutil.Quoted
Browse files Browse the repository at this point in the history
  • Loading branch information
chipaca committed Nov 30, 2016
1 parent 85855be commit 7d48347
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
19 changes: 5 additions & 14 deletions daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/strutil"
)

var api = []*Command{
Expand Down Expand Up @@ -810,16 +811,6 @@ func modeFlags(devMode, jailMode bool) (snapstate.Flags, error) {

}

// quotedNames formats a slice of snap names (or other strings) to a
// quoted list of comma separated names, e.g. `"snap1", "snap2"`
func quotedNames(names []string) string {
quoted := make([]string, len(names))
for i, name := range names {
quoted[i] = strconv.Quote(name)
}
return strings.Join(quoted, ", ")
}

func snapUpdateMany(inst *snapInstruction, st *state.State) (msg string, updated []string, tasksets []*state.TaskSet, err error) {
// we need refreshed snap-declarations to enforce refresh-control as best as we can, this also ensures that snap-declarations and their prerequisite assertions are updated regularly
if err := assertstateRefreshSnapDeclarations(st, inst.userID); err != nil {
Expand All @@ -835,14 +826,14 @@ func snapUpdateMany(inst *snapInstruction, st *state.State) (msg string, updated
case 0:
// not really needed but be paranoid
if len(inst.Snaps) != 0 {
return "", nil, nil, fmt.Errorf("internal error: when asking for a refresh of %s no update was found but no error was generated", quotedNames(inst.Snaps))
return "", nil, nil, fmt.Errorf("internal error: when asking for a refresh of %s no update was found but no error was generated", strutil.Quoted(inst.Snaps))
}
// FIXME: instead don't generated a change(?) at all
msg = fmt.Sprintf(i18n.G("Refresh all snaps: no updates"))
case 1:
msg = fmt.Sprintf(i18n.G("Refresh snap %q"), inst.Snaps[0])
default:
quoted := quotedNames(updated)
quoted := strutil.Quoted(updated)
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Refresh snaps %s"), quoted)
}
Expand All @@ -862,7 +853,7 @@ func snapInstallMany(inst *snapInstruction, st *state.State) (msg string, instal
case 1:
msg = fmt.Sprintf(i18n.G("Install snap %q"), inst.Snaps[0])
default:
quoted := quotedNames(inst.Snaps)
quoted := strutil.Quoted(inst.Snaps)
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Install snaps %s"), quoted)
}
Expand Down Expand Up @@ -934,7 +925,7 @@ func snapRemoveMany(inst *snapInstruction, st *state.State) (msg string, removed
case 1:
msg = fmt.Sprintf(i18n.G("Remove snap %q"), inst.Snaps[0])
default:
quoted := quotedNames(inst.Snaps)
quoted := strutil.Quoted(inst.Snaps)
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Remove snaps %s"), quoted)
}
Expand Down
13 changes: 0 additions & 13 deletions daemon/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,6 @@ func (s *apiSuite) TestSnapInfoOneIntegration(c *check.C) {
c.Check(rsp.Result, check.DeepEquals, expected.Result)
}

func (s *apiSuite) TestQuotedNames(c *check.C) {
for _, t := range []struct {
in []string
out string
}{
{[]string{}, ""},
{[]string{"snap1"}, `"snap1"`},
{[]string{"snap1", "snap2"}, `"snap1", "snap2"`},
} {
c.Check(quotedNames(t.in), check.Equals, t.out)
}
}

func (s *apiSuite) TestSnapInfoWithAuth(c *check.C) {
state := snapCmd.d.overlord.State()
state.Lock()
Expand Down
13 changes: 13 additions & 0 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package strutil

import (
"math/rand"
"strconv"
"strings"
"time"
)

Expand All @@ -44,3 +46,14 @@ func MakeRandomString(length int) string {

return out
}

// Quoted formats a slice of strings to a quoted list of
// comma-separated strings, e.g. `"snap1", "snap2"`
func Quoted(names []string) string {
quoted := make([]string, len(names))
for i, name := range names {
quoted[i] = strconv.Quote(name)
}

return strings.Join(quoted, ", ")
}
37 changes: 27 additions & 10 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,45 @@
*
*/

package strutil
package strutil_test

import (
"math/rand"
"testing"

. "gopkg.in/check.v1"
"gopkg.in/check.v1"

"github.com/snapcore/snapd/strutil"
)

func Test(t *testing.T) { TestingT(t) }
func Test(t *testing.T) { check.TestingT(t) }

type MakeRandomStringTestSuite struct{}
type strutilSuite struct{}

var _ = Suite(&MakeRandomStringTestSuite{})
var _ = check.Suite(&strutilSuite{})

func (ts *MakeRandomStringTestSuite) TestMakeRandomString(c *C) {
func (ts *strutilSuite) TestMakeRandomString(c *check.C) {
// for our tests
rand.Seed(1)

s1 := MakeRandomString(10)
c.Assert(s1, Equals, "pw7MpXh0JB")
s1 := strutil.MakeRandomString(10)
c.Assert(s1, check.Equals, "pw7MpXh0JB")

s2 := strutil.MakeRandomString(5)
c.Assert(s2, check.Equals, "4PQyl")
}

s2 := MakeRandomString(5)
c.Assert(s2, Equals, "4PQyl")
func (*strutilSuite) TestQuoted(c *check.C) {
for _, t := range []struct {
in []string
out string
}{
{nil, ""},
{[]string{}, ""},
{[]string{"one"}, `"one"`},
{[]string{"one", "two"}, `"one", "two"`},
{[]string{"one", `tw"`}, `"one", "tw\""`},
} {
c.Check(strutil.Quoted(t.in), check.Equals, t.out, check.Commentf("expected %#v -> %s", t.in, t.out))
}
}

0 comments on commit 7d48347

Please sign in to comment.