Skip to content

Commit

Permalink
Replace odo delete component integration with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jun 16, 2023
1 parent 803718a commit 63ef2f2
Show file tree
Hide file tree
Showing 13 changed files with 1,267 additions and 268 deletions.
10 changes: 5 additions & 5 deletions cmd/odo/alizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestOdoAlizer(t *testing.T) {
var output []api.DetectionResult
err := json.Unmarshal(b, &output)
if err != nil {
t.Error(err)
t.Fatal(err)
}
checkEqual(t, output[0].Devfile, "framework-name")
checkEqual(t, output[0].DevfileRegistry, "TheRegistryName")
Expand All @@ -86,26 +86,26 @@ func TestOdoAlizer(t *testing.T) {
if tt.clientset != nil {
clientset = tt.clientset()
}
runCommand(t, tt.args, clientset, func(err error, stdout, stderr string) {
runCommand(t, tt.args, runOptions{}, clientset, nil, func(err error, stdout, stderr string) {
if (err != nil) != (tt.wantErr != "") {
t.Fatalf("errWanted: %v\nGot: %v", tt.wantErr != "", err != nil)
}

if tt.wantErr != "" {
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("%q\nerror does not contain:\n%q", err.Error(), tt.wantErr)
t.Errorf("%q\nerror does not contain:\n%q", err.Error(), tt.wantErr)
}
}

if tt.wantStdout != "" {
if !strings.Contains(stdout, tt.wantStdout) {
t.Fatalf("%q\nstdout does not contain:\n%q", stdout, tt.wantStdout)
t.Errorf("%q\nstdout does not contain:\n%q", stdout, tt.wantStdout)
}
}

if tt.wantStderr != "" {
if !strings.Contains(stderr, tt.wantStderr) {
t.Fatalf("%q\nstderr does not contain:\n%q", stderr, tt.wantStderr)
t.Errorf("%q\nstderr does not contain:\n%q", stderr, tt.wantStderr)
}
}

Expand Down
24 changes: 21 additions & 3 deletions cmd/odo/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
"github.com/sethvargo/go-envconfig"
"github.com/spf13/pflag"
"k8s.io/klog"
)
Expand All @@ -21,34 +23,50 @@ func resetGlobalFlags() {
klog.InitFlags(nil)
}

type runOptions struct {
env map[string]string
config map[string]string
}

func runCommand(
t *testing.T,
args []string,
options runOptions,
clientset clientset.Clientset,
populateFS func(fs filesystem.Filesystem),
f func(err error, stdout, stderr string),
) {

// We are running the test on a new and empty directory (on real filesystem)
originWd, err := os.Getwd()
if err != nil {
t.Error(err)
t.Fatal(err)
}
defer func() {
_ = os.Chdir(originWd)
}()
cwd := t.TempDir()
err = os.Chdir(cwd)
if err != nil {
t.Error(err)
t.Fatal(err)
}

if populateFS != nil {
populateFS(clientset.FS)
}

ctx := context.Background()
envConfig, err := config.GetConfiguration()
envConfig, err := config.GetConfigurationWith(envconfig.MapLookuper(options.config))

if err != nil {
t.Fatal(err)
}
ctx = envcontext.WithEnvConfig(ctx, *envConfig)

for k, v := range options.env {
t.Setenv(k, v)
}

resetGlobalFlags()

var stdoutB, stderrB bytes.Buffer
Expand Down
Loading

0 comments on commit 63ef2f2

Please sign in to comment.