Skip to content

Commit

Permalink
config: introduce prime unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apprehensions committed Nov 3, 2023
1 parent ad30359 commit 5f564e7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
13 changes: 9 additions & 4 deletions config/cardpick.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package config

import (
"errors"
"fmt"
"strconv"

"github.com/vinegarhq/vinegar/sysinfo"
)

var (
ErrOpenGLBlind = errors.New("opengl is not capable of choosing the right gpu, it must be explicitly defined")
ErrNoCardFound = errors.New("gpu not found")
ErrBadGpuIndex = errors.New("gpu index cannot be negative")
)

func (b *Binary) pickCard() error {
if b.ForcedGpu == "" {
return nil
Expand Down Expand Up @@ -38,7 +43,7 @@ func (b *Binary) pickCard() error {
vk := b.Dxvk || b.Renderer == "Vulkan"

if n != 2 && (!vk && n != 1) {
return fmt.Errorf("opengl is not capable of choosing the right gpu, it must be explicitly defined")
return ErrOpenGLBlind
}

if n != 2 {
Expand All @@ -51,11 +56,11 @@ func (b *Binary) pickCard() error {
}

if idx < 0 {
return errors.New("gpu index cannot be negative")
return ErrBadGpuIndex
}

if n < idx+1 {
return errors.New("gpu not found")
return ErrNoCardFound
}

b.Env.Set("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE", "1")
Expand Down
83 changes: 83 additions & 0 deletions config/cardpick_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package config

import (
"errors"
"strconv"
"testing"
"github.com/vinegarhq/vinegar/sysinfo"
)

func TestCard(t *testing.T) {
sysinfo.Cards = []sysinfo.Card{}
b := Binary{
ForcedGpu: "meow",
Env: Environment{},
}

if err := b.pickCard(); !errors.Is(err, strconv.ErrSyntax) {
t.Fatal("expected to not handle string gpu")
}

b.ForcedGpu = "1"
if err := b.pickCard(); !errors.Is(err, ErrNoCardFound) {
t.Fatal("expected to handle no gpu found")
}

b.ForcedGpu = "-1"
if err := b.pickCard(); !errors.Is(err, ErrBadGpuIndex) {
t.Fatal("expected to not handle negative gpu index")
}
}

func TestPRIMECard(t *testing.T) {
b := Binary{
ForcedGpu: "integrated",
Env: Environment{},
}
sysinfo.Cards = []sysinfo.Card{
{
Driver: "i915",
Embedded: true,
},
}

if err := b.pickCard(); err != nil {
t.Fatal(err)
}

if _, ok := b.Env["DRI_PRIME"]; ok {
t.Fatal("expected no change")
}

sysinfo.Cards = append(sysinfo.Cards, sysinfo.Card{
Driver: "nvidia",
Embedded: false,
})

if err := b.pickCard(); err != nil {
t.Fatal(err)
}

if v := b.Env["DRI_PRIME"]; v != "0" {
t.Fatal("expected change in prime index")
}

if v := b.Env["__GLX_VENDOR_LIBRARY_NAME"]; v != "mesa" {
t.Fatal("expected glx vendor to be mesa")
}

b.ForcedGpu = "prime-discrete"
delete(b.Env, "DRI_PRIME")
delete(b.Env, "__GLX_VENDOR_LIBRARY_NAME")
if err := b.pickCard(); err != nil {
t.Fatal(err)
}

if v := b.Env["DRI_PRIME"]; v != "1" {
t.Fatal("expected change in descrete index")
}

if v := b.Env["__GLX_VENDOR_LIBRARY_NAME"]; v != "nvidia" {
t.Fatal("expected glx vendor to be nvidia")
}
}

0 comments on commit 5f564e7

Please sign in to comment.