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
pipx_test.go Normal file
View file

@ -0,0 +1,45 @@
// pipx_test.go
package main
import (
"os/exec"
"testing"
)
func TestPipxManager_Name(t *testing.T) {
pm := &PipxManager{}
if name := pm.Name(); name != "Pipx" {
t.Errorf("Expected name to be 'Pipx', got %s", name)
}
}
func TestPipxManager_InstallManager(t *testing.T) {
pm := &PipxManager{}
// Mock exec.LookPath
execLookPath = func(file string) (string, error) {
if file == "pipx" {
return "/usr/bin/pipx", nil
}
return "", exec.ErrNotFound
}
defer func() { execLookPath = exec.LookPath }()
if err := pm.InstallManager(); err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestPipxManager_InstallPackage(t *testing.T) {
pm := &PipxManager{}
// Mock exec.Command
execCommand = func(name string, arg ...string) *exec.Cmd {
return exec.Command("echo", "mocked pipx install")
}
defer func() { execCommand = exec.Command }()
if err := pm.InstallPackage("test-package"); err != nil {
t.Errorf("Expected no error, got %v", err)
}
}