Skip to content

Commit

Permalink
Making compose functions and extracting repeated func, coderefactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Aug 16, 2023
1 parent 2b6e35e commit c9a2bc1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 66 deletions.
8 changes: 6 additions & 2 deletions tests/basic_commands_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// basic_commands_test.go
package tests

import (
Expand All @@ -11,7 +10,12 @@ import (
func TestNormalExecution(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/y_0000_basic_get.l2"
cmdArgs := []string{fpath}
output := testutils.RunL2CommandAndGetOutput(t, cmdArgs...)

output, err := testutils.RunL2CommandAndGetOutput(cmdArgs...)
if err != nil {
t.Errorf("Error running L2 command: %v", err)
return
}

expectedOutputPart := "\"url\": \"http://httpbin.org/get\""
if !strings.Contains(output, expectedOutputPart) {
Expand Down
21 changes: 14 additions & 7 deletions tests/env_command_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// env_command_test.go
package tests

import (
Expand All @@ -7,10 +6,18 @@ import (
testutils "github.com/HexmosTech/lama2/tests/utils"
)

func runL2Command(t *testing.T, cmdArgs ...string) map[string]testutils.EnvData {
envMap, err := testutils.RunL2CommandAndParseJSON(cmdArgs...)
if err != nil {
t.Fatalf("Error running L2 command: %v", err)
}
return envMap
}

func TestL2EnvCommand(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=", fpath}
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)
envMap := runL2Command(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -22,7 +29,7 @@ func TestL2EnvCommand(t *testing.T) {
func TestL2RelevantEnvForAString(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=A", fpath}
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)
envMap := runL2Command(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -34,7 +41,7 @@ func TestL2RelevantEnvForAString(t *testing.T) {
func TestL2RelevantEnvForBString(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=B", fpath}
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)
envMap := runL2Command(t, cmdArgs...)

// Check the "BHOST" key is present
checkBHost(t, envMap)
Expand All @@ -46,7 +53,7 @@ func TestL2RelevantEnvForBString(t *testing.T) {
func TestL2EnvCommandVerbose(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"
cmdArgs := []string{"-e=", "-v", fpath}
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)
envMap := runL2Command(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -58,7 +65,7 @@ func TestL2EnvCommandVerbose(t *testing.T) {
func TestL2EnvWithoutL2config(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/no_l2config/api/y_0021_no_l2config.l2"
cmdArgs := []string{"-e=", fpath}
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)
envMap := runL2Command(t, cmdArgs...)

// Check the "AHOST" key is present
checkAHost(t, envMap)
Expand All @@ -67,7 +74,7 @@ func TestL2EnvWithoutL2config(t *testing.T) {
func TestL2EnvWithoutL2env(t *testing.T) {
fpath := "../elfparser/ElfTestSuite/no_l2env/api/y_0022_no_l2env.l2"
cmdArgs := []string{"-e=", fpath}
envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...)
envMap := runL2Command(t, cmdArgs...)

// Check the "BHOST" key is present
checkBHost(t, envMap)
Expand Down
93 changes: 36 additions & 57 deletions tests/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/rs/zerolog/log"
)
Expand All @@ -16,85 +15,65 @@ type EnvData struct {
Val string `json:"val"`
}

func RunL2CommandAndGetOutput(t *testing.T, cmdArgs ...string) string {
func checkL2BinaryExists(l2BinPath string) error {
if _, err := os.Stat(l2BinPath); os.IsNotExist(err) {
return fmt.Errorf("l2 binary not found in the build folder %s, please change the path", l2BinPath)
}
return nil
}

// Get the full path to the l2 binary
func getL2BinaryPath() (string, error) {
l2BinPath := "../build/l2"

// Check if the l2 binary file exists
if err := checkL2BinaryExists(l2BinPath); err != nil {
t.Error(err)
return ""
err := checkL2BinaryExists(l2BinPath)
if err != nil {
return "", err
}
return l2BinPath, nil
}

// Code to run the l2 command
cmd := exec.Command(l2BinPath, cmdArgs...)
func runCommand(binPath string, cmdArgs ...string) (string, error) {
cmd := exec.Command(binPath, cmdArgs...)

var stdout bytes.Buffer
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout

var stderr bytes.Buffer
cmd.Stderr = &stderr

// Execute the command
err := cmd.Run()
if err != nil {
log.Error().Str("Error", stderr.String()).Msg("Error running l2 command")
t.Errorf("Error running l2 command: %v\n", err)
return ""
log.Error().Str("Error", stderr.String()).Msg("Error running command")
return "", fmt.Errorf("error running command: %v", err)
}

// Retrieve the captured stdout
stdoutOutput := stdout.String()
log.Info().Str("Test env_command", stdoutOutput).Msg("output from command")
return stdoutOutput
output := stdout.String()
log.Debug().Str("Test env_command", output).Msg("Output from command")
return output, nil
}

func RunL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) map[string]EnvData {
// Get the full path to the l2 binary
l2BinPath := "../build/l2"

// Check if the l2 binary file exists
if err := checkL2BinaryExists(l2BinPath); err != nil {
t.Error(err)
return make(map[string]EnvData)
func RunL2CommandAndGetOutput(cmdArgs ...string) (string, error) {
l2BinPath, err := getL2BinaryPath()
if err != nil {
return "", err
}

// Your existing code to run the l2 command and parse JSON
cmd := exec.Command(l2BinPath, cmdArgs...)

var stdout bytes.Buffer
cmd.Stdout = &stdout
return runCommand(l2BinPath, cmdArgs...)
}

// Execute the command
err := cmd.Run()
func RunL2CommandAndParseJSON(cmdArgs ...string) (map[string]EnvData, error) {
l2BinPath, err := getL2BinaryPath()
if err != nil {
// Handle the error if needed
t.Errorf("Error running l2 command: %v\n", err)
return make(map[string]EnvData)
return nil, err
}

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

log.Debug().Str("Test env_command", stdoutOutput).Msg("output from command")

// Convert the stdoutOutput string to []byte slice
outputBytes := []byte(stdoutOutput)
output, err := runCommand(l2BinPath, cmdArgs...)
if err != nil {
return nil, err
}

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

return envMap
}

func checkL2BinaryExists(l2BinPath string) error {
// Check if the l2 binary file exists
if _, err := os.Stat(l2BinPath); os.IsNotExist(err) {
return fmt.Errorf("l2 binary not found in the build folder %s, please change the path", l2BinPath)
}
return nil
return envMap, nil
}

0 comments on commit c9a2bc1

Please sign in to comment.