Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions cmd/transform/listplugins/listplugins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package listplugins

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/konveyor/crane/internal/flags"
)

func TestNewListPluginsCommand_command_structure(t *testing.T) {
tests := []struct {
name string
globalFlags *flags.GlobalFlags
wantUse string
wantShortContain string
}{
{
name: "command has correct Use and Short",
globalFlags: &flags.GlobalFlags{},
wantUse: "list-plugins",
wantShortContain: "list",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewListPluginsCommand(tt.globalFlags)

if cmd.Use != tt.wantUse {
t.Errorf("cmd.Use = %q, want %q", cmd.Use, tt.wantUse)
}

if !strings.Contains(strings.ToLower(cmd.Short), tt.wantShortContain) {
t.Errorf("cmd.Short = %q, want it to contain %q", cmd.Short, tt.wantShortContain)
}

if cmd.RunE == nil {
t.Error("cmd.RunE is nil, want it to be set")
}

if cmd.PreRun == nil {
t.Error("cmd.PreRun is nil, want it to be set")
}
})
}
}

func TestListPluginsRun(t *testing.T) {
tests := []struct {
name string
setupDir func(t *testing.T) string
wantErr bool
errContains string
}{
{
name: "non-existent plugin directory returns no error",
setupDir: func(t *testing.T) string {
// Return a path that doesn't exist
// Note: GetFilteredPlugins handles non-existent dirs gracefully
return filepath.Join(os.TempDir(), "crane-test-nonexistent-"+t.Name())
},
wantErr: false,
},
{
name: "empty plugin directory returns no error",
setupDir: func(t *testing.T) string {
dir := t.TempDir()
return dir
},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pluginDir := tt.setupDir(t)

o := &Options{
globalFlags: &flags.GlobalFlags{},
Flags: Flags{
PluginDir: pluginDir,
SkipPlugins: []string{},
},
}

err := o.run()

if tt.wantErr {
if err == nil {
t.Errorf("run() error = nil, wantErr %v", tt.wantErr)
return
}
if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("run() error = %v, want error containing %q", err, tt.errContains)
}
} else {
if err != nil {
t.Errorf("run() unexpected error = %v", err)
}
}
})
}
}
119 changes: 119 additions & 0 deletions cmd/transform/optionals/optionals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package optionals

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/konveyor/crane/internal/flags"
)

func TestNewOptionalsCommand_command_structure(t *testing.T) {
tests := []struct {
name string
globalFlags *flags.GlobalFlags
wantUse string
wantShortContain string
}{
{
name: "command has correct Use and Short",
globalFlags: &flags.GlobalFlags{},
wantUse: "optionals",
wantShortContain: "optional",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := NewOptionalsCommand(tt.globalFlags)

if cmd.Use != tt.wantUse {
t.Errorf("cmd.Use = %q, want %q", cmd.Use, tt.wantUse)
}

if !strings.Contains(cmd.Short, tt.wantShortContain) {
t.Errorf("cmd.Short = %q, want it to contain %q", cmd.Short, tt.wantShortContain)
}

if cmd.RunE == nil {
t.Error("cmd.RunE is nil, want it to be set")
}

if cmd.PreRun == nil {
t.Error("cmd.PreRun is nil, want it to be set")
}
})
}
}

func TestOptionalsRun_empty_plugin_dir(t *testing.T) {
// Create an empty temp directory for plugins
emptyDir := t.TempDir()

o := &Options{
globalFlags: &flags.GlobalFlags{},
Flags: Flags{
PluginDir: emptyDir,
SkipPlugins: nil,
},
}

err := o.run()
if err != nil {
t.Errorf("run() with empty plugin directory returned error: %v, want nil", err)
}
}

func TestOptionalsRun_nonexistent_dir(t *testing.T) {
// Create a temp directory and then create a path that doesn't exist within it
tmpDir := t.TempDir()
nonexistentDir := filepath.Join(tmpDir, "nonexistent_plugin_dir")

o := &Options{
globalFlags: &flags.GlobalFlags{},
Flags: Flags{
PluginDir: nonexistentDir,
SkipPlugins: nil,
},
}

// According to the implementation, GetPlugins handles os.IsNotExist gracefully
// and returns the default pluginList (with kubernetes plugin) without error.
// So this test verifies that non-existent directories don't cause errors.
err := o.run()
if err != nil {
t.Errorf("run() with non-existent plugin directory returned error: %v, want nil", err)
}
}

func TestOptionalsRun_unreadable_dir(t *testing.T) {
// Skip this test on systems where we can't change permissions (e.g., running as root)
if os.Getuid() == 0 {
t.Skip("Skipping test when running as root")
}

// Create a directory that exists but cannot be read due to permissions
tmpDir := t.TempDir()
unreadableDir := filepath.Join(tmpDir, "unreadable")
if err := os.Mkdir(unreadableDir, 0000); err != nil {
t.Fatalf("Failed to create unreadable directory: %v", err)
}
// Ensure we restore permissions so cleanup works
t.Cleanup(func() {
os.Chmod(unreadableDir, 0755)

Check failure on line 104 in cmd/transform/optionals/optionals_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `os.Chmod` is not checked (errcheck)
})

o := &Options{
globalFlags: &flags.GlobalFlags{},
Flags: Flags{
PluginDir: unreadableDir,
SkipPlugins: nil,
},
}

err := o.run()
if err == nil {
t.Error("run() with unreadable plugin directory returned nil, want error")
}
}
Loading
Loading