mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-30 04:55:13 +00:00
Created two new helper functions to create a `cli.Context` with and without a `cli.App`. Calling these functions simplifies a lot of test code. Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
54 lines
986 B
Go
54 lines
986 B
Go
// Copyright (c) 2017 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
func TestVersion(t *testing.T) {
|
|
const testAppName = "foo"
|
|
const testAppVersion = "0.1.0"
|
|
|
|
resetCLIGlobals()
|
|
|
|
savedRuntimeVersionFunc := runtimeVersion
|
|
|
|
defer func() {
|
|
runtimeVersion = savedRuntimeVersionFunc
|
|
}()
|
|
|
|
runtimeVersion := func() string {
|
|
return testAppVersion
|
|
}
|
|
|
|
ctx := createCLIContext(nil)
|
|
ctx.App.Name = testAppName
|
|
ctx.App.Version = runtimeVersion()
|
|
|
|
fn, ok := versionCLICommand.Action.(func(context *cli.Context) error)
|
|
assert.True(t, ok)
|
|
|
|
tmpfile, err := ioutil.TempFile("", "")
|
|
assert.NoError(t, err)
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
ctx.App.Writer = tmpfile
|
|
|
|
err = fn(ctx)
|
|
assert.NoError(t, err)
|
|
|
|
pattern := fmt.Sprintf("%s.*version.*%s", testAppName, testAppVersion)
|
|
err = grep(pattern, tmpfile.Name())
|
|
assert.NoError(t, err)
|
|
}
|