refactor: implement mocking functions and tests

This commit is contained in:
Patryk Hegenberg 2025-01-15 21:39:17 +01:00
parent 339dba4e13
commit f8ef2ef2d5
21 changed files with 398 additions and 90 deletions

45
golang_test.go Normal file
View file

@ -0,0 +1,45 @@
// golang_test.go
package main
import (
"os/exec"
"testing"
)
func TestGolangManager_Name(t *testing.T) {
gm := &GolangManager{}
if name := gm.Name(); name != "Golang" {
t.Errorf("Expected name to be 'Golang', got %s", name)
}
}
func TestGolangManager_InstallManager(t *testing.T) {
gm := &GolangManager{}
// Mock exec.LookPath
execLookPath = func(file string) (string, error) {
if file == "go" {
return "/usr/local/go/bin/go", nil
}
return "", exec.ErrNotFound
}
defer func() { execLookPath = exec.LookPath }()
if err := gm.InstallManager(); err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestGolangManager_InstallPackage(t *testing.T) {
gm := &GolangManager{}
// Mock exec.Command
execCommand = func(name string, arg ...string) *exec.Cmd {
return exec.Command("echo", "mocked go install")
}
defer func() { execCommand = exec.Command }()
if err := gm.InstallPackage("github.com/test/package"); err != nil {
t.Errorf("Expected no error, got %v", err)
}
}