From 15a29d4ee7e00cc3c260be9e425de55b52265165 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 19 Aug 2017 20:20:59 -0300 Subject: [PATCH] removed duplicated code in snapcraft tests --- pipeline/snapcraft/snapcraft_test.go | 37 +++++++++++----------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/pipeline/snapcraft/snapcraft_test.go b/pipeline/snapcraft/snapcraft_test.go index c1ccba742..d5365337a 100644 --- a/pipeline/snapcraft/snapcraft_test.go +++ b/pipeline/snapcraft/snapcraft_test.go @@ -57,14 +57,7 @@ func TestRunPipe(t *testing.T) { }, }, } - for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64", "linuxarm64", "linuxarmhf"} { - var folder = "mybin_" + plat - assert.NoError(os.Mkdir(filepath.Join(dist, folder), 0755)) - var binPath = filepath.Join(dist, folder, "mybin") - _, err = os.Create(binPath) - assert.NoError(err) - ctx.AddBinary(plat, folder, "mybin", binPath) - } + addBinaries(t, ctx, "mybin", dist) assert.NoError(Pipe{}.Run(ctx)) } @@ -87,13 +80,7 @@ func TestRunPipeWithName(t *testing.T) { }, }, } - for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64", "linuxarm64", "linuxarmhf"} { - var folder = "testprojectname_" + plat - assert.NoError(os.Mkdir(filepath.Join(dist, folder), 0755)) - var binPath = filepath.Join(dist, folder, "testprojectname") - _, err = os.Create(binPath) - ctx.AddBinary(plat, folder, "testprojectname", binPath) - } + addBinaries(t, ctx, "testprojectname", dist) assert.NoError(Pipe{}.Run(ctx)) yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "testprojectname_linuxamd64", "prime", "meta", "snap.yaml")) assert.NoError(err) @@ -127,13 +114,7 @@ func TestRunPipeWithPlugsAndDaemon(t *testing.T) { }, }, } - for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64", "linuxarm64", "linuxarmhf"} { - var folder = "mybin_" + plat - assert.NoError(os.Mkdir(filepath.Join(dist, folder), 0755)) - var binPath = filepath.Join(dist, folder, "mybin") - _, err = os.Create(binPath) - ctx.AddBinary(plat, folder, "mybin", binPath) - } + addBinaries(t, ctx, "mybin", dist) assert.NoError(Pipe{}.Run(ctx)) yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "mybin_linuxamd64", "prime", "meta", "snap.yaml")) assert.NoError(err) @@ -161,3 +142,15 @@ func TestNoSnapcraftInPath(t *testing.T) { } assert.EqualError(Pipe{}.Run(ctx), ErrNoSnapcraft.Error()) } + +func addBinaries(t *testing.T, ctx *context.Context, name, dist string) { + var assert = assert.New(t) + for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64", "linuxarm64", "linuxarmhf"} { + var folder = name + "_" + plat + assert.NoError(os.Mkdir(filepath.Join(dist, folder), 0755)) + var binPath = filepath.Join(dist, folder, name) + _, err := os.Create(binPath) + assert.NoError(err) + ctx.AddBinary(plat, folder, name, binPath) + } +}