refactor: implement mocking functions and tests
This commit is contained in:
parent
339dba4e13
commit
f8ef2ef2d5
21 changed files with 398 additions and 90 deletions
78
utils_test.go
Normal file
78
utils_test.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
tests := []struct {
|
||||
a, b, want int
|
||||
}{
|
||||
{1, 2, 2},
|
||||
{5, 3, 5},
|
||||
{0, 0, 0},
|
||||
{-1, -5, -1},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got := max(tt.a, tt.b)
|
||||
if got != tt.want {
|
||||
t.Errorf("max(%d, %d) = %d; want %d", tt.a, tt.b, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteShellCommand(t *testing.T) {
|
||||
// Mock execCommand
|
||||
oldExecCommand := execCommand
|
||||
defer func() { execCommand = oldExecCommand }()
|
||||
|
||||
execCommand = func(command string, args ...string) *exec.Cmd {
|
||||
return exec.Command("echo", "mocked command")
|
||||
}
|
||||
|
||||
err := executeShellCommand("test command", "TEST_ENV=value")
|
||||
if err != nil {
|
||||
t.Errorf("executeShellCommand() error = %v; want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallWithProgress(t *testing.T) {
|
||||
mockManager := &MockPackageManager{
|
||||
packages: []string{"pkg1", "pkg2"},
|
||||
}
|
||||
|
||||
err := installWithProgress(mockManager, mockManager.packages)
|
||||
if err != nil {
|
||||
t.Errorf("installWithProgress() error = %v; want nil", err)
|
||||
}
|
||||
|
||||
if len(mockManager.installedPackages) != len(mockManager.packages) {
|
||||
t.Errorf("installWithProgress() installed %d packages; want %d", len(mockManager.installedPackages), len(mockManager.packages))
|
||||
}
|
||||
}
|
||||
|
||||
// MockPackageManager implementiert das PackageManager Interface für Tests
|
||||
type MockPackageManager struct {
|
||||
packages []string
|
||||
installedPackages []string
|
||||
}
|
||||
|
||||
func (m *MockPackageManager) Install(packages []string) error {
|
||||
m.installedPackages = append(m.installedPackages, packages...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockPackageManager) InstallPackage(pkg string) error {
|
||||
m.installedPackages = append(m.installedPackages, pkg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockPackageManager) InstallManager() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockPackageManager) Name() string {
|
||||
return "MockManager"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue