Skip to content

Commit

Permalink
Merge pull request #190 from luoliwoshang/llcppgtest/demo
Browse files Browse the repository at this point in the history
test:assign conf origin
  • Loading branch information
tsingbx authored Feb 27, 2025
2 parents 50efe01 + 0c3f016 commit fafca53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 14 additions & 8 deletions cmd/llcppgtest/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
//
// Parameters:
// - demoRoot: Path to the root directory of a single demo case
func RunGenPkgDemo(demoRoot string) {
// - confDir: Path to the configuration directory relative to demoRoot, defaults to "." if empty
func RunGenPkgDemo(demoRoot string, confDir string) {
fmt.Printf("Testing demo: %s\n", demoRoot)

absPath, err := filepath.Abs(demoRoot)
Expand All @@ -37,7 +38,12 @@ func RunGenPkgDemo(demoRoot string) {
}
demoPkgName := filepath.Base(absPath)

configFile := filepath.Join(absPath, "llcppg.cfg")
if confDir == "" {
confDir = "."
}

configPath := filepath.Join(absPath, confDir)
configFile := filepath.Join(configPath, "llcppg.cfg")
fmt.Printf("Looking for config file at: %s\n", configFile)

if _, err = os.Stat(configFile); os.IsNotExist(err) {
Expand All @@ -55,7 +61,7 @@ func RunGenPkgDemo(demoRoot string) {
// copy configs to out dir
cfgFiles := []string{"llcppg.cfg", "llcppg.pub", "llcppg.symb.json"}
for _, cfg := range cfgFiles {
src := filepath.Join(absPath, cfg)
src := filepath.Join(configPath, cfg)
dst := filepath.Join(outDir, cfg)
var content []byte
content, err = os.ReadFile(src)
Expand Down Expand Up @@ -119,7 +125,7 @@ func RunGenPkgDemo(demoRoot string) {
}

// Get all first-level directories containing llcppg.cfg
func getFirstLevelDemos(baseDir string) []string {
func getFirstLevelDemos(baseDir string, confDir string) []string {
entries, err := os.ReadDir(baseDir)
if err != nil {
panic(fmt.Sprintf("failed to read directory: %v", err))
Expand All @@ -129,7 +135,7 @@ func getFirstLevelDemos(baseDir string) []string {
for _, entry := range entries {
if entry.IsDir() {
demoRoot := filepath.Join(baseDir, entry.Name())
configPath := filepath.Join(demoRoot, "llcppg.cfg")
configPath := filepath.Join(demoRoot, confDir, "llcppg.cfg")
if _, err := os.Stat(configPath); err == nil {
demos = append(demos, demoRoot)
}
Expand All @@ -138,22 +144,22 @@ func getFirstLevelDemos(baseDir string) []string {
return demos
}

func RunAllGenPkgDemos(baseDir string) {
func RunAllGenPkgDemos(baseDir string, confDir string) {
fmt.Printf("Starting generated package tests in directory: %s\n", baseDir)

stat, err := os.Stat(baseDir)
if err != nil || !stat.IsDir() {
panic(fmt.Sprintf("specified path is not a directory or does not exist: %s", baseDir))
}

demos := getFirstLevelDemos(baseDir)
demos := getFirstLevelDemos(baseDir, confDir)
if len(demos) == 0 {
panic(fmt.Sprintf("no directories containing llcppg.cfg found in %s", baseDir))
}

// Test each demo
for _, demo := range demos {
RunGenPkgDemo(demo)
RunGenPkgDemo(demo, confDir)
}
fmt.Println("All generated package demos passed:", strings.Join(demos, ","))
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/llcppgtest/llcppgtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
HelpFlagName FlagName = "help"
DemosFlagName FlagName = "demos"
DemoFlagName FlagName = "demo"
DemoConfName FlagName = "conf"
)

func RunCommandWithOut(out *io.PipeWriter, dir, cmdName string, args ...string) {
Expand Down Expand Up @@ -218,6 +219,7 @@ func main() {
flag.BoolVar(&help, string(HelpFlagName), false, "print help message")
demosPath := flag.String(string(DemosFlagName), "", "test all first-level demo directories in the specified path")
demoPath := flag.String(string(DemoFlagName), "", "test the specified demo directory")
confPath := flag.String(string(DemoConfName), "", "path to the config directory relative to demo directory, defaults to \".\" if empty")
flag.Parse()

if help || len(os.Args) == 1 {
Expand Down Expand Up @@ -264,9 +266,9 @@ func main() {
pkgs := getPkgs()
runPkgs(pkgs, cfg)
case appMode == runDemos:
demo.RunAllGenPkgDemos(*demosPath)
demo.RunAllGenPkgDemos(*demosPath, *confPath)
case appMode == runDemo:
demo.RunGenPkgDemo(*demoPath)
demo.RunGenPkgDemo(*demoPath, *confPath)
default:
if len(flag.Args()) > 0 {
arg := flag.Arg(0)
Expand Down

0 comments on commit fafca53

Please sign in to comment.