generated from MacroPower/go_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor cmd and pkg/plugin for easier testing
- Loading branch information
1 parent
1cdc0f1
commit 64cc3ab
Showing
12 changed files
with
231 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
kclcmd "kcl-lang.io/cli/cmd/kcl/commands" | ||
) | ||
|
||
func NewRootCmd(name, shortDesc, longDesc string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: name, | ||
Short: shortDesc, | ||
Long: longDesc, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
Version: GetVersionString(), | ||
} | ||
cmd.AddCommand(kclcmd.NewRunCmd()) | ||
cmd.AddCommand(kclcmd.NewLintCmd()) | ||
cmd.AddCommand(kclcmd.NewDocCmd()) | ||
cmd.AddCommand(kclcmd.NewFmtCmd()) | ||
cmd.AddCommand(kclcmd.NewTestCmd()) | ||
cmd.AddCommand(kclcmd.NewVetCmd()) | ||
cmd.AddCommand(kclcmd.NewCleanCmd()) | ||
cmd.AddCommand(kclcmd.NewImportCmd()) | ||
cmd.AddCommand(kclcmd.NewModCmd()) | ||
cmd.AddCommand(kclcmd.NewRegistryCmd()) | ||
cmd.AddCommand(kclcmd.NewServerCmd()) | ||
cmd.AddCommand(NewVersionCmd()) | ||
cmd.AddCommand(NewChartCmd()) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/MacroPower/kclx/internal/cli" | ||
) | ||
|
||
var testDataDir string | ||
|
||
func init() { | ||
//nolint:dogsled | ||
_, filename, _, _ := runtime.Caller(0) | ||
dir := filepath.Dir(filename) | ||
testDataDir = filepath.Join(dir, "testdata") | ||
|
||
os.Setenv("KCLX_HELM_PLUGIN_DISABLED", "true") | ||
os.Setenv("KCLX_OS_PLUGIN_DISABLED", "true") | ||
os.Setenv("KCLX_HTTP_PLUGIN_DISABLED", "true") | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
t.Parallel() | ||
|
||
tc := cli.NewRootCmd("test", "", "") | ||
out := bytes.NewBufferString("") | ||
outFile := filepath.Join(testDataDir, "got/simple.json") | ||
err := os.MkdirAll(filepath.Dir(outFile), 0o755) | ||
require.NoError(t, err) | ||
|
||
tc.SetArgs([]string{"run", filepath.Join(testDataDir, "simple.k"), "--format=json", "--output", outFile}) | ||
tc.SetOut(out) | ||
|
||
err = tc.Execute() | ||
require.NoError(t, err) | ||
require.Empty(t, out.String()) | ||
|
||
outData, err := os.ReadFile(outFile) | ||
require.NoError(t, err) | ||
|
||
require.JSONEq(t, `{"a":1}`, string(outData)) | ||
} | ||
|
||
func BenchmarkRun(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
tc := cli.NewRootCmd("test", "", "") | ||
|
||
//plugin.RegisterPlugin(helm.HelmPlugin) | ||
|
||
out := bytes.NewBufferString("") | ||
tc.SetArgs([]string{"run", filepath.Join(testDataDir, "simple.k"), "--output=/dev/null"}) | ||
tc.SetOut(out) | ||
err := tc.Execute() | ||
require.NoError(b, err) | ||
require.Empty(b, out.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.