JSON configs describe network and node MACs; scenarios run command sequences with expect checks. Share UART client API across CLI and tests. Co-authored-by: Cursor <cursoragent@cursor.com>
80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
package autotest
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
ConfigDir = "testdata/configs"
|
|
ScenarioDir = "testdata/scenarios"
|
|
)
|
|
|
|
func ResolveConfigPath(name string) (string, error) {
|
|
return resolveJSON(name, ConfigDir)
|
|
}
|
|
|
|
func ResolveScenarioPath(name string) (string, error) {
|
|
return resolveJSON(name, ScenarioDir)
|
|
}
|
|
|
|
func resolveJSON(name, dir string) (string, error) {
|
|
if name == "" {
|
|
return "", fmt.Errorf("empty name")
|
|
}
|
|
if filepath.Ext(name) == ".json" {
|
|
if fileExists(name) {
|
|
return name, nil
|
|
}
|
|
}
|
|
base := name
|
|
if filepath.Ext(base) != ".json" {
|
|
base += ".json"
|
|
}
|
|
for _, root := range configRoots() {
|
|
p := filepath.Join(root, dir, base)
|
|
if fileExists(p) {
|
|
return p, nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("not found: %s (searched %s under gotool)", base, dir)
|
|
}
|
|
|
|
func configRoots() []string {
|
|
var roots []string
|
|
if wd, err := os.Getwd(); err == nil {
|
|
roots = append(roots, wd)
|
|
}
|
|
// When run from repo root via make.
|
|
roots = append(roots, "goTool", filepath.Join("..", "goTool"))
|
|
return roots
|
|
}
|
|
|
|
func fileExists(path string) bool {
|
|
st, err := os.Stat(path)
|
|
return err == nil && !st.IsDir()
|
|
}
|
|
|
|
func ListJSONFiles(dir string) ([]string, error) {
|
|
for _, root := range configRoots() {
|
|
p := filepath.Join(root, dir)
|
|
entries, err := os.ReadDir(p)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var names []string
|
|
for _, e := range entries {
|
|
if e.IsDir() || filepath.Ext(e.Name()) != ".json" {
|
|
continue
|
|
}
|
|
names = append(names, strings.TrimSuffix(e.Name(), ".json"))
|
|
}
|
|
if len(names) > 0 {
|
|
return names, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("directory %s not found", dir)
|
|
}
|