add minimal test for 'pkg/loader' + update loader to let 'lsb_release -is' command for its caller

This commit is contained in:
xdrm-brackets 2018-11-07 12:52:59 +01:00
parent 82d694b7cb
commit ac3732d318
2 changed files with 54 additions and 16 deletions

View File

@ -6,24 +6,15 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
) )
var ErrUnknownDistribution = errors.New("unknown linux distribution") var ErrUnknownDistribution = errors.New("unknown linux distribution")
var ErrNoCandidateInstalled = errors.New("no package-manager candidate installed") var ErrNoCandidateInstalled = errors.New("no package-manager candidate installed")
var ErrNoDriverFound = errors.New("no driver found for the package manager") var ErrNoDriverFound = errors.New("no driver found for the package manager")
func Load() (PackageManager, error) { func Load(_distro string) (PackageManager, error) {
// 1. get distro name // 1. load config file
out, err := exec.Command("lsb_release", "-is").Output()
if err != nil {
return nil, err
}
distro := strings.ToLower(strings.Trim(string(out), " \n\t"))
// 2. load config file
driverTable := filepath.Join(os.Getenv("GOPATH"), "src/git.xdrm.io/xdrm-brackets/nix-amer/meta/pkg-drivers.json") driverTable := filepath.Join(os.Getenv("GOPATH"), "src/git.xdrm.io/xdrm-brackets/nix-amer/meta/pkg-drivers.json")
file, err := os.Open(driverTable) file, err := os.Open(driverTable)
if err != nil { if err != nil {
@ -31,7 +22,7 @@ func Load() (PackageManager, error) {
} }
defer file.Close() defer file.Close()
// 3. Parse json // 2. Parse json
table := make(map[string][]string) table := make(map[string][]string)
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
if err := decoder.Decode(&table); err != nil { if err := decoder.Decode(&table); err != nil {
@ -39,13 +30,13 @@ func Load() (PackageManager, error) {
} }
decoder = nil decoder = nil
// 4. Get available package-manager list for distro // 3. Get available package-manager list for distro
available, exists := table[distro] available, exists := table[_distro]
if !exists || len(available) < 1 { if !exists || len(available) < 1 {
return nil, ErrUnknownDistribution return nil, ErrUnknownDistribution
} }
// 5. Check each available package-manager in order // 4. Check each available package-manager in order
selected := "" selected := ""
for _, current := range available { for _, current := range available {
if exec.Command("which", current).Run() == nil { if exec.Command("which", current).Run() == nil {
@ -59,7 +50,7 @@ func Load() (PackageManager, error) {
return nil, ErrNoCandidateInstalled return nil, ErrNoCandidateInstalled
} }
// 6. Instanciate // 5. Instanciate
switch selected { switch selected {
case "apt-get": case "apt-get":
return new(Apt), nil return new(Apt), nil

View File

@ -0,0 +1,47 @@
package pkg
import (
"os/exec"
"strings"
"testing"
)
func TestUnknownDistribution(t *testing.T) {
_, err := Load("invalid-distro-xxx")
if err != ErrUnknownDistribution {
t.Errorf("expected <%s>, got <%s>", ErrUnknownDistribution, err)
t.Fail()
}
}
func TestNoCandidateInstalled(t *testing.T) {
// get host distro
out, err := exec.Command("lsb_release", "-is").Output()
if err != nil {
t.Errorf("cannot get host linux distribution")
t.Fail()
}
hostDistro := strings.ToLower(strings.Trim(string(out), " \n\t"))
// valid candidate
_, err = Load(hostDistro)
if err != nil {
t.Errorf("expected <nil>, got <%s>", err)
t.Fail()
}
// invalid candidate
distro := "solus"
if hostDistro == distro {
distro = "ubuntu"
}
_, err = Load(distro)
if err != ErrNoCandidateInstalled {
t.Errorf("expected <%s>, got <%s>", ErrNoCandidateInstalled, err)
t.Fail()
}
}