This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new 6823f542 [dubboctl] Refactoring dubboctl partially complete
6823f542 is described below

commit 6823f54254858f744f6108cd6c9782b2101965ea
Author: mfordjody <[email protected]>
AuthorDate: Wed Oct 30 10:16:08 2024 +0800

    [dubboctl] Refactoring dubboctl partially complete
---
 dubboctl/cmd/repository_test.go                    | 44 --------------
 dubboctl/{cmd => pkg/deploy}/create_test.go        | 71 +++++++++++++++-------
 .../root_test.go => pkg/deploy/repository_test.go} | 55 +++++++++++------
 dubboctl/{cmd => pkg/manifest}/manifest_test.go    | 11 ++--
 dubboctl/{cmd => pkg/profile}/profile_test.go      | 24 +++++++-
 5 files changed, 115 insertions(+), 90 deletions(-)

diff --git a/dubboctl/cmd/repository_test.go b/dubboctl/cmd/repository_test.go
deleted file mode 100644
index 751991e0..00000000
--- a/dubboctl/cmd/repository_test.go
+++ /dev/null
@@ -1,44 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements.  See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You 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 cmd
-
-import (
-       "github.com/apache/dubbo-kubernetes/dubboctl/pkg/deploy"
-       "testing"
-)
-
-// TestRepository_List ensures that the 'list' subcommand shows the client's
-// set of repositories by name for builtin repositories, by explicitly
-// setting the repositories' path to a new path which includes no others.
-func TestRepository_List(t *testing.T) {
-       _ = fromTempDirectory(t)
-
-       cmd := deploy.NewRepositoryListCmd(deploy.NewClient)
-       cmd.SetArgs([]string{}) // Do not use test command args
-
-       // Execute the command, capturing the output sent to stdout
-       stdout := piped(t)
-       if err := cmd.Execute(); err != nil {
-               t.Fatal(err)
-       }
-
-       // Assert the output matches expect (whitespace trimmed)
-       expect := "default"
-       output := stdout()
-       if output != expect {
-               t.Fatalf("expected:\n'%v'\ngot:\n'%v'\n", expect, output)
-       }
-}
diff --git a/dubboctl/cmd/create_test.go b/dubboctl/pkg/deploy/create_test.go
similarity index 63%
rename from dubboctl/cmd/create_test.go
rename to dubboctl/pkg/deploy/create_test.go
index 9ed795b1..abae84b7 100644
--- a/dubboctl/cmd/create_test.go
+++ b/dubboctl/pkg/deploy/create_test.go
@@ -13,14 +13,22 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package cmd
+package deploy
 
 import (
        "errors"
-       "github.com/apache/dubbo-kubernetes/dubboctl/pkg/deploy"
+       "github.com/apache/dubbo-kubernetes/dubboctl/cmd"
        "testing"
 )
 
+import (
+       "github.com/ory/viper"
+)
+
+import (
+       . "github.com/apache/dubbo-kubernetes/dubboctl/internal/testing"
+)
+
 import (
        "github.com/apache/dubbo-kubernetes/dubboctl/internal/util"
 )
@@ -28,11 +36,11 @@ import (
 // TestCreate_Execute ensures that an invocation of create with minimal 
settings
 // and valid input completes without error; degenerate case.
 func TestCreate_Execute(t *testing.T) {
-       _ = fromTempDirectory(t)
+       _ = FromTempDirectory(t)
 
-       cmd := GetRootCmd([]string{"create", "--language", "go", "myfunc"})
+       cmds := cmd.GetRootCmd([]string{"create", "--language", "go", "myfunc"})
 
-       if err := cmd.Execute(); err != nil {
+       if err := cmds.Execute(); err != nil {
                t.Fatal(err)
        }
 }
@@ -40,12 +48,12 @@ func TestCreate_Execute(t *testing.T) {
 // TestCreate_NoRuntime ensures that an invocation of create must be
 // done with a runtime.
 func TestCreate_NoRuntime(t *testing.T) {
-       _ = fromTempDirectory(t)
+       _ = FromTempDirectory(t)
 
-       cmd := GetRootCmd([]string{"create", "myfunc"})
+       cmds := cmd.GetRootCmd([]string{"create", "myfunc"})
 
-       err := cmd.Execute()
-       var e deploy.ErrNoRuntime
+       err := cmds.Execute()
+       var e ErrNoRuntime
        if !errors.As(err, &e) {
                t.Fatalf("Did not receive ErrNoRuntime. Got %v", err)
        }
@@ -54,12 +62,12 @@ func TestCreate_NoRuntime(t *testing.T) {
 // TestCreate_WithNoRuntime ensures that an invocation of create must be
 // done with one of the valid runtimes only.
 func TestCreate_WithInvalidRuntime(t *testing.T) {
-       _ = fromTempDirectory(t)
+       _ = FromTempDirectory(t)
 
-       cmd := GetRootCmd([]string{"create", "--language", "invalid", "myfunc"})
+       cmds := cmd.GetRootCmd([]string{"create", "--language", "invalid", 
"myfunc"})
 
-       err := cmd.Execute()
-       var e deploy.ErrInvalidRuntime
+       err := cmds.Execute()
+       var e ErrInvalidRuntime
        if !errors.As(err, &e) {
                t.Fatalf("Did not receive ErrInvalidRuntime. Got %v", err)
        }
@@ -68,12 +76,12 @@ func TestCreate_WithInvalidRuntime(t *testing.T) {
 // TestCreate_InvalidTemplate ensures that an invocation of create must be
 // done with one of the valid templates only.
 func TestCreate_InvalidTemplate(t *testing.T) {
-       _ = fromTempDirectory(t)
+       _ = FromTempDirectory(t)
 
-       cmd := GetRootCmd([]string{"create", "--language", "go", "--template", 
"invalid", "myfunc"})
+       cmds := cmd.GetRootCmd([]string{"create", "--language", "go", 
"--template", "invalid", "myfunc"})
 
-       err := cmd.Execute()
-       var e deploy.ErrInvalidTemplate
+       err := cmds.Execute()
+       var e ErrInvalidTemplate
        if !errors.As(err, &e) {
                t.Fatalf("Did not receive ErrInvalidTemplate. Got %v", err)
        }
@@ -82,12 +90,12 @@ func TestCreate_InvalidTemplate(t *testing.T) {
 // TestCreate_ValidatesName ensures that the create command only accepts
 // DNS-1123 labels for function name.
 func TestCreate_ValidatesName(t *testing.T) {
-       _ = fromTempDirectory(t)
+       _ = FromTempDirectory(t)
 
        // Execute the command with a function name containing invalid 
characters and
        // confirm the expected error is returned
-       cmd := GetRootCmd([]string{"create", "invalid!"})
-       err := cmd.Execute()
+       cmds := cmd.GetRootCmd([]string{"create", "invalid!"})
+       err := cmds.Execute()
        var e util.ErrInvalidApplicationName
        if !errors.As(err, &e) {
                t.Fatalf("Did not receive ErrInvalidApplicationName. Got %v", 
err)
@@ -97,15 +105,32 @@ func TestCreate_ValidatesName(t *testing.T) {
 // TestCreate_ConfigOptional ensures that the system can be used without
 // any additional configuration being required.
 func TestCreate_ConfigOptional(t *testing.T) {
-       _ = fromTempDirectory(t)
+       _ = FromTempDirectory(t)
 
        t.Setenv("XDG_CONFIG_HOME", t.TempDir())
 
-       cmd := GetRootCmd([]string{"create", "--language=go", "myfunc"})
-       if err := cmd.Execute(); err != nil {
+       cmds := cmd.GetRootCmd([]string{"create", "--language=go", "myfunc"})
+       if err := cmds.Execute(); err != nil {
                t.Fatal(err)
        }
 
        // Not failing is success.  Config files or settings beyond what are
        // automatically written to to the given config home are currently 
optional.
 }
+
+// FromTempDirectory is a test helper which endeavors to create
+// an environment clean of developer's settings for use during CLI testing.
+func FromTempDirectory(t *testing.T) string {
+       t.Helper()
+       ClearEnvs(t)
+
+       // By default unit tests presume no config exists unless provided in 
testdata.
+       t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+
+       // creates and CDs to a temp directory
+       d, done := Mktemp(t)
+
+       // Return to original directory and resets viper.
+       t.Cleanup(func() { done(); viper.Reset() })
+       return d
+}
\ No newline at end of file
diff --git a/dubboctl/cmd/root_test.go b/dubboctl/pkg/deploy/repository_test.go
similarity index 68%
rename from dubboctl/cmd/root_test.go
rename to dubboctl/pkg/deploy/repository_test.go
index 294b955f..e0deb9e9 100644
--- a/dubboctl/cmd/root_test.go
+++ b/dubboctl/pkg/deploy/repository_test.go
@@ -13,38 +13,42 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package cmd
+package deploy_test
 
 import (
+       "github.com/apache/dubbo-kubernetes/dubboctl/pkg/deploy"
+       "github.com/ory/viper"
        "io"
        "os"
        "strings"
        "testing"
 )
 
-import (
-       "github.com/ory/viper"
-)
-
 import (
        . "github.com/apache/dubbo-kubernetes/dubboctl/internal/testing"
 )
 
-// fromTempDirectory is a test helper which endeavors to create
-// an environment clean of developer's settings for use during CLI testing.
-func fromTempDirectory(t *testing.T) string {
-       t.Helper()
-       ClearEnvs(t)
+// TestRepository_List ensures that the 'list' subcommand shows the client's
+// set of repositories by name for builtin repositories, by explicitly
+// setting the repositories' path to a new path which includes no others.
+func TestRepository_List(t *testing.T) {
+       _ = FromTempDirectory(t)
 
-       // By default unit tests presume no config exists unless provided in 
testdata.
-       t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+       cmd := deploy.NewRepositoryListCmd(deploy.NewClient)
+       cmd.SetArgs([]string{}) // Do not use test command args
 
-       // creates and CDs to a temp directory
-       d, done := Mktemp(t)
+       // Execute the command, capturing the output sent to stdout
+       stdout := piped(t)
+       if err := cmd.Execute(); err != nil {
+               t.Fatal(err)
+       }
 
-       // Return to original directory and resets viper.
-       t.Cleanup(func() { done(); viper.Reset() })
-       return d
+       // Assert the output matches expect (whitespace trimmed)
+       expect := "default"
+       output := stdout()
+       if output != expect {
+               t.Fatalf("expected:\n'%v'\ngot:\n'%v'\n", expect, output)
+       }
 }
 
 // pipe the output of stdout to a buffer whose value is returned
@@ -82,3 +86,20 @@ func piped(t *testing.T) func() string {
                return strings.TrimSpace(b.String())
        }
 }
+
+// FromTempDirectory is a test helper which endeavors to create
+// an environment clean of developer's settings for use during CLI testing.
+func FromTempDirectory(t *testing.T) string {
+       t.Helper()
+       ClearEnvs(t)
+
+       // By default unit tests presume no config exists unless provided in 
testdata.
+       t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+
+       // creates and CDs to a temp directory
+       d, done := Mktemp(t)
+
+       // Return to original directory and resets viper.
+       t.Cleanup(func() { done(); viper.Reset() })
+       return d
+}
diff --git a/dubboctl/cmd/manifest_test.go 
b/dubboctl/pkg/manifest/manifest_test.go
similarity index 94%
rename from dubboctl/cmd/manifest_test.go
rename to dubboctl/pkg/manifest/manifest_test.go
index 4361abbd..5c92829c 100644
--- a/dubboctl/cmd/manifest_test.go
+++ b/dubboctl/pkg/manifest/manifest_test.go
@@ -13,10 +13,11 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package cmd
+package manifest
 
 import (
        "bytes"
+       "github.com/apache/dubbo-kubernetes/dubboctl/cmd"
        "os"
        "strings"
        "testing"
@@ -170,16 +171,16 @@ func TestManifestDiff(t *testing.T) {
        }
 }
 
-func testExecute(t *testing.T, cmd string, wantErr bool) string {
+func testExecute(t *testing.T, cmds string, wantErr bool) string {
        var out bytes.Buffer
-       args := strings.Split(cmd, " ")
-       rootCmd := GetRootCmd(args)
+       args := strings.Split(cmds, " ")
+       rootCmd := cmd.GetRootCmd(args)
        rootCmd.SetOut(&out)
        if err := rootCmd.Execute(); err != nil {
                if wantErr {
                        return ""
                }
-               t.Errorf("execute %s failed, err: %s", cmd, err)
+               t.Errorf("execute %s failed, err: %s", cmds, err)
                return ""
        }
        if wantErr {
diff --git a/dubboctl/cmd/profile_test.go b/dubboctl/pkg/profile/profile_test.go
similarity index 87%
rename from dubboctl/cmd/profile_test.go
rename to dubboctl/pkg/profile/profile_test.go
index 3c0dd4d8..193bf255 100644
--- a/dubboctl/cmd/profile_test.go
+++ b/dubboctl/pkg/profile/profile_test.go
@@ -13,9 +13,12 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package cmd
+package profile
 
 import (
+       "bytes"
+       "github.com/apache/dubbo-kubernetes/dubboctl/cmd"
+       "strings"
        "testing"
 )
 
@@ -128,3 +131,22 @@ func TestProfileDiff(t *testing.T) {
                })
        }
 }
+
+func testExecute(t *testing.T, cmds string, wantErr bool) string {
+       var out bytes.Buffer
+       args := strings.Split(cmds, " ")
+       rootCmd := cmd.GetRootCmd(args)
+       rootCmd.SetOut(&out)
+       if err := rootCmd.Execute(); err != nil {
+               if wantErr {
+                       return ""
+               }
+               t.Errorf("execute %s failed, err: %s", cmds, err)
+               return ""
+       }
+       if wantErr {
+               t.Errorf("want err but got no err")
+               return ""
+       }
+       return out.String()
+}

Reply via email to