Skip to content

Commit

Permalink
directly exec
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Jul 20, 2023
1 parent 39d0b7d commit ee55886
Showing 1 changed file with 27 additions and 70 deletions.
97 changes: 27 additions & 70 deletions tests/env_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,37 @@ type EnvData struct {
}

func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) {
// Store the original working directory
originalPath, err := os.Getwd()
if err != nil {
fmt.Println("Error getting the current working directory:", err)
return
}
// Get the full path to the l2 binary
l2BinPath := "/home/runner/work/Lama2/Lama2/build/l2"

// Change the current working directory to lama2Path
l2BinPath := "/home/runner/work/Lama2/Lama2/build"
err = os.Chdir(l2BinPath)
if err != nil {
fmt.Println("Error changing the current working directory:", err)
return
}

// Print the current working directory to verify
wd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting the current working directory:", err)
return
}
fmt.Println("Current working directory after changing:", wd)
// Your existing code to run the l2 command and parse JSON
cmd := exec.Command(l2BinPath, cmdArgs...)

var stdout bytes.Buffer
cmd.Stdout = &stdout

// Execute the command
err := cmd.Run()
if err != nil {
// Handle the error if needed
fmt.Printf("Error running l2 command: %v\n", err)
return
}

// Now let's check if the l2 binary is present in the current working directory
fileInfo, err := os.Stat("./l2")
if err != nil {
fmt.Println("Error checking l2 binary:", err)
return
}
fmt.Println("Is l2 binary present:", fileInfo.Mode().IsRegular())
// Retrieve the captured stdout
stdoutOutput := stdout.String()

// Change back to the original working directory after debugging
defer func() {
err := os.Chdir(originalPath)
if err != nil {
fmt.Println("Error changing back to the original working directory:", err)
}
}()
fmt.Println(stdoutOutput)

// Your existing code to run the l2 command and parse JSON
cmd := exec.Command("./l2", cmdArgs...) // Use "./l2" as the executable name
// Convert the stdoutOutput string to []byte slice
outputBytes := []byte(stdoutOutput)

var stdout bytes.Buffer
cmd.Stdout = &stdout
envMap := make(map[string]EnvData)
err = json.Unmarshal(outputBytes, &envMap)
if err != nil {
t.Fatalf("Error unmarshaling JSON: %v\nOutput:\n%s", err, stdoutOutput)
}

// Execute the command
err = cmd.Run() // Uncomment this line to execute the "l2" command
if err != nil {
// Handle the error if needed
fmt.Printf("Error running l2 command: %v\n", err)
return
}

// Retrieve the captured stdout
stdoutOutput := stdout.String()

fmt.Println(stdoutOutput)

// Convert the stdoutOutput string to []byte slice
outputBytes := []byte(stdoutOutput)

envMap := make(map[string]EnvData)
err = json.Unmarshal(outputBytes, &envMap)
if err != nil {
t.Fatalf("Error unmarshaling JSON: %v\nOutput:\n%s", err, stdoutOutput)
}

// Example assertion: Check the "AHOST" key
if ahost, ok := envMap["AHOST"]; !ok {
t.Error("Expected 'AHOST' key in the JSON, but it was not found")
Expand All @@ -96,10 +60,7 @@ if err != nil {
}

}
err = os.Chdir(originalPath)
if err != nil {
fmt.Println("Error changing back to the original working directory:", err)
}

}

func TestL2EnvCommand(t *testing.T) {
Expand All @@ -112,15 +73,11 @@ func TestL2EnvCommand(t *testing.T) {

// Print the current working directory
fmt.Println("Current working directory:", wd)



// Your existing code to run the l2 command and parse JSON
cmdArgs := []string{"-e", "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"}
runL2CommandAndParseJSON(t, cmdArgs...)
}



func listFilesInDir(dirPath string) {
fmt.Println("Contents of", dirPath+":")
Expand All @@ -133,4 +90,4 @@ func listFilesInDir(dirPath string) {
for _, entry := range entries {
fmt.Println(entry.Name())
}
}
}

0 comments on commit ee55886

Please sign in to comment.