package main import ( "path/filepath" "testing" intdevice "netdome.biz/paul/qmk-rgb/internal/device" "netdome.biz/paul/qmk-rgb/internal/via" ) // stubOpenTarget points the commands at one protocol, the display names a board // would supply and the channels that board has. It returns the restore function. // The zone the command read is the one it resolves, so a test that passes a // multi-channel zone gets the same channels the real seam would resolve. func stubOpenTarget(t *testing.T, proto rgbProtocol, display map[uint16]string, channels []via.Channel, vendorID, productID uint16) func() { t.Helper() original := openTarget openTarget = func(zone string) (rgbProtocol, targetDeviceData, []via.Channel, error) { target := targetDeviceData{ Device: intdevice.Device{VendorID: vendorID, ProductID: productID}, Display: display, } requested, err := resolveZoneName(zone, display, nil) if err != nil { return nil, target, nil, err } target.Requested = requested resolved, err := resolveChannels(proto, target) if err != nil { return nil, target, nil, err } return proto, target, resolved, nil } return func() { openTarget = original } } // impact80Target is the board most command tests describe. func impact80Target(t *testing.T, proto rgbProtocol) func() { t.Helper() return stubOpenTarget(t, proto, impact80Display(), impact80Channels(), 0x36B0, 0x309F) } // withJSON turns on the machine shape for a test that asserts the JSON // commands, which print text unless it is asked for. func withJSON(t *testing.T) { t.Helper() original := jsonOutput jsonOutput = true t.Cleanup(func() { jsonOutput = original }) } // vendoredDefinitions points the data directory at the repository's definitions // directory, so a test that needs the Impact 80's effect names uses the file that // ships with the tool rather than the copy built into the binary. The two are the // same file, so a test that overrides the directory and one that does not both // resolve the same names, which is the point of a test that has to tell them // apart. func vendoredDefinitions(t *testing.T) func() { t.Helper() return forceDefinitionsDir(t, filepath.Join("..", "..", "definitions")) }