Files
fenrir/internal/scripts/python_parity_test.go

85 lines
3.1 KiB
Go

// Fenrir console screen reader.
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
package scripts
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)
// This is an optional differential test against a contributor's real Python
// checkout. Normal Go builds/tests never require Python Fenrir to be installed.
func TestPythonLoaderParity(t *testing.T) {
source := os.Getenv("FENRIR_PYTHON_SOURCE")
if source == "" {
t.Skip("set FENRIR_PYTHON_SOURCE to compare the real Python loader")
}
root := t.TempDir()
local, system := filepath.Join(root, "local"), filepath.Join(root, "system")
for _, directory := range []string{local, system} {
if err := os.Mkdir(directory, 0700); err != nil {
t.Fatal(err)
}
}
names := []string{"Music__-__KEY_SHIFT__+__KEY_U.sh", "Music__-__KEY_SHIFT__+__KEY_X.sh", "same__-__KEY_B.py", "KEY_A", "explicit__-__KEY_SCRIPT__+__KEY_KPPLUS", "space name__-__key_ctrl__+__key_f12.sh", "multi__-__KEY_A__+__KEY_B", "bad__-__KEY_UNKNOWN", "bad__-__KEY_A__+__", "__skip__-__KEY_Z.sh", ".hidden__-__KEY_V.sh", "bad__-__KEY_A__-__KEY_B"}
for _, name := range names {
if err := os.WriteFile(filepath.Join(local, name), nil, 0600); err != nil {
t.Fatal(err)
}
}
for _, name := range []string{"same__-__KEY_N.sh", "other__-__KEY_Q", "another__-__KEY_A", "Music__-__KEY_Y.sh"} {
if err := os.WriteFile(filepath.Join(system, name), nil, 0600); err != nil {
t.Fatal(err)
}
}
command := exec.Command("python3", "../../tests/python_script_contract.py", source, local, system)
command.Env = append(os.Environ(), "PYTHONDONTWRITEBYTECODE=1")
output, err := command.CombinedOutput()
if err != nil {
t.Fatalf("Python contract fixture: %v\n%s", err, output)
}
type record struct {
Name string `json:"name"`
Path string `json:"path"`
Keys []string `json:"keys"`
}
var python []record
if err := json.Unmarshal(output, &python); err != nil {
t.Fatal(err, string(output))
}
installed, problems := Discover([]string{local, system}, nil)
if len(problems) != 0 {
t.Fatal(problems)
}
var goRecords []record
for _, script := range installed {
keys := append([]string(nil), script.Keys...)
sort.Strings(keys)
goRecords = append(goRecords, record{Name: strings.TrimPrefix(string(script.ID), "script:"), Path: script.Path, Keys: keys})
}
if !reflect.DeepEqual(goRecords, python) {
t.Fatalf("Go: %+v\nPython: %+v", goRecords, python)
}
}