Skip to content

Commit

Permalink
Merge pull request #2147 from dgageot/override-helper
Browse files Browse the repository at this point in the history
Test helper to override value for tests
  • Loading branch information
tejal29 authored May 20, 2019
2 parents fd8ac69 + f704c64 commit 0f2cb02
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 13 deletions.
18 changes: 9 additions & 9 deletions cmd/skaffold/app/cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ func TestQuietFlag(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
defer func(flag bool) { quietFlag = flag }(quietFlag)
quietFlag = true
restore := testutil.Override(t, &quietFlag, true)
defer restore()

restoreFn := testutil.Override(t, &createRunnerAndBuildFunc, test.mock)
defer restoreFn()

defer func(tf *flags.TemplateFlag) { buildFormatFlag = tf }(buildFormatFlag)
if test.template != "" {
buildFormatFlag = flags.NewTemplateFlag(test.template, flags.BuildOutput{})
restoreTemplate := testutil.Override(t, &buildFormatFlag, flags.NewTemplateFlag(test.template, flags.BuildOutput{}))
defer restoreTemplate()
}

defer func(f func(context.Context, io.Writer) ([]build.Artifact, error)) { createRunnerAndBuildFunc = f }(createRunnerAndBuildFunc)
createRunnerAndBuildFunc = test.mock

var output bytes.Buffer
err := doBuild(&output)

Expand Down Expand Up @@ -116,8 +116,8 @@ func TestRunBuild(t *testing.T) {
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
defer func(f func(context.Context, io.Writer) ([]build.Artifact, error)) { createRunnerAndBuildFunc = f }(createRunnerAndBuildFunc)
createRunnerAndBuildFunc = test.mock
restore := testutil.Override(t, &createRunnerAndBuildFunc, test.mock)
defer restore()

err := doBuild(ioutil.Discard)

Expand Down
8 changes: 4 additions & 4 deletions cmd/skaffold/app/skaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestMainHelp(t *testing.T) {
errOutput bytes.Buffer
)

defer func(args []string) { os.Args = args }(os.Args)
os.Args = []string{"skaffold", "help"}
restore := testutil.Override(t, &os.Args, []string{"skaffold", "help"})
defer restore()

err := Run(&output, &errOutput)

Expand All @@ -42,8 +42,8 @@ func TestMainHelp(t *testing.T) {
}

func TestMainUnknownCommand(t *testing.T) {
defer func(args []string) { os.Args = args }(os.Args)
os.Args = []string{"skaffold", "unknown"}
restore := testutil.Override(t, &os.Args, []string{"skaffold", "unknown"})
defer restore()

err := Run(ioutil.Discard, ioutil.Discard)

Expand Down
Binary file added testutil/debug.test
Binary file not shown.
48 changes: 48 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package testutil
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -155,3 +156,50 @@ func ServeFile(t *testing.T, content []byte) (url string, tearDown func()) {

return ts.URL, ts.Close
}

// CreateTempFileWithContents creates a temporary file in the dir specified or
// os.TempDir by default with contents mentioned.
func CreateTempFileWithContents(t *testing.T, dir string, name string, content []byte) string {
t.Helper()
tmpfile, err := ioutil.TempFile(dir, name)
if err != nil {
t.Fatal(err)
}
if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
return tmpfile.Name()
}

// Override sets a dest variable to a given value.
// Returns the function to call to restore the variable
// to its original state.
func Override(t *testing.T, dest, tmp interface{}) func() {
t.Helper()

defer func() {
if r := recover(); r != nil {
t.Error("temporary value is of invalid type")
}
}()

dValue := reflect.ValueOf(dest).Elem()

// Save current value
curValue := reflect.New(dValue.Type()).Elem()
curValue.Set(dValue)

// Set to temporary value
var tmpV reflect.Value
if tmp == nil {
tmpV = reflect.Zero(dValue.Type())
} else {
tmpV = reflect.ValueOf(tmp)
}
dValue.Set(tmpV)

return func() { dValue.Set(curValue) }
}
48 changes: 48 additions & 0 deletions testutil/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutil

import "testing"

var (
strVariable = "original"
fnVariable = func() string { return "original" }
)

func TestOverride(t *testing.T) {
restore := Override(t, &strVariable, "temporary")
CheckDeepEqual(t, "temporary", strVariable)

restore()
CheckDeepEqual(t, "original", strVariable)
}

func TestOverrideFunction(t *testing.T) {
restore := Override(t, &fnVariable, func() string { return "temporary" })
CheckDeepEqual(t, "temporary", fnVariable())

restore()
CheckDeepEqual(t, "original", fnVariable())
}

func TestOverrideToNil(t *testing.T) {
restore := Override(t, &fnVariable, nil)
CheckDeepEqual(t, true, fnVariable == nil)

restore()
CheckDeepEqual(t, "original", fnVariable())
}

0 comments on commit 0f2cb02

Please sign in to comment.