From 2b6e35e4543be10e042d7d9fe9ac809894d9a992 Mon Sep 17 00:00:00 2001 From: lovestaco Date: Wed, 16 Aug 2023 10:06:29 +0530 Subject: [PATCH] Extracting test common functions, coderefactor --- tests/basic_commands_test.go | 40 +------------- tests/cli_test.go | 2 +- tests/env_command_test.go | 82 ++++------------------------ tests/utils/test_utils.go | 100 +++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 110 deletions(-) create mode 100644 tests/utils/test_utils.go diff --git a/tests/basic_commands_test.go b/tests/basic_commands_test.go index 2f44d7f8..0364241b 100644 --- a/tests/basic_commands_test.go +++ b/tests/basic_commands_test.go @@ -2,52 +2,16 @@ package tests import ( - "bytes" - "os/exec" "strings" "testing" - "github.com/rs/zerolog/log" + testutils "github.com/HexmosTech/lama2/tests/utils" ) -func runL2CommandAndGetOutput(t *testing.T, cmdArgs ...string) string { - - // 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 "" - } - - // Code to run the l2 command - cmd := exec.Command(l2BinPath, cmdArgs...) - - var stdout 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 "" - } - - // Retrieve the captured stdout - stdoutOutput := stdout.String() - log.Info().Str("Test env_command", stdoutOutput).Msg("output from command") - return stdoutOutput -} - func TestNormalExecution(t *testing.T) { fpath := "../elfparser/ElfTestSuite/y_0000_basic_get.l2" cmdArgs := []string{fpath} - output := runL2CommandAndGetOutput(t, cmdArgs...) + output := testutils.RunL2CommandAndGetOutput(t, cmdArgs...) expectedOutputPart := "\"url\": \"http://httpbin.org/get\"" if !strings.Contains(output, expectedOutputPart) { diff --git a/tests/cli_test.go b/tests/cli_test.go index e62ba480..179b6d36 100644 --- a/tests/cli_test.go +++ b/tests/cli_test.go @@ -18,7 +18,7 @@ func TestCmdBasic(t *testing.T) { expected := lama2cmd.Opts{ Verbose: []bool{true}, Nocolor: false, - Env: "UNSET", + Env: "UNSET_VU5TRVQ", Positional: struct { LamaAPIFile string }{LamaAPIFile: "../elfparser/ElfTestSuite/y_0000_basic_get.l2"}, diff --git a/tests/env_command_test.go b/tests/env_command_test.go index bdfa93bc..65dddf33 100644 --- a/tests/env_command_test.go +++ b/tests/env_command_test.go @@ -2,25 +2,15 @@ package tests import ( - "bytes" - "encoding/json" - "fmt" - "os" - "os/exec" "testing" - "github.com/rs/zerolog/log" + testutils "github.com/HexmosTech/lama2/tests/utils" ) -type EnvData struct { - Src string `json:"src"` - Val string `json:"val"` -} - func TestL2EnvCommand(t *testing.T) { fpath := "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2" cmdArgs := []string{"-e=", fpath} - envMap := runL2CommandAndParseJSON(t, cmdArgs...) + envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...) // Check the "AHOST" key is present checkAHost(t, envMap) @@ -32,7 +22,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 := runL2CommandAndParseJSON(t, cmdArgs...) + envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...) // Check the "AHOST" key is present checkAHost(t, envMap) @@ -44,7 +34,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 := runL2CommandAndParseJSON(t, cmdArgs...) + envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...) // Check the "BHOST" key is present checkBHost(t, envMap) @@ -56,7 +46,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 := runL2CommandAndParseJSON(t, cmdArgs...) + envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...) // Check the "AHOST" key is present checkAHost(t, envMap) @@ -68,7 +58,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 := runL2CommandAndParseJSON(t, cmdArgs...) + envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...) // Check the "AHOST" key is present checkAHost(t, envMap) @@ -77,64 +67,14 @@ 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 := runL2CommandAndParseJSON(t, cmdArgs...) + envMap := testutils.RunL2CommandAndParseJSON(t, cmdArgs...) // Check the "BHOST" key is present checkBHost(t, envMap) } -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) - } - - // 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 - t.Errorf("Error running l2 command: %v\n", err) - return make(map[string]EnvData) - } - - // 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) - - envMap := make(map[string]EnvData) - err = json.Unmarshal(outputBytes, &envMap) - if err != nil { - t.Fatalf("Error unmarshaling JSON env: %v\nOutput:\n%s", err, stdoutOutput) - } - - return envMap -} - -// checkL2BinaryExists checks if the l2 binary file exists in the specified path -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 -} - // checkAHost checks the "AHOST" key in the JSON map -func checkAHost(t *testing.T, envMap map[string]EnvData) { +func checkAHost(t *testing.T, envMap map[string]testutils.EnvData) { if ahost, ok := envMap["AHOST"]; !ok { t.Error("Expected 'AHOST' key in the JSON, but it was not found") } else { @@ -149,7 +89,7 @@ func checkAHost(t *testing.T, envMap map[string]EnvData) { } // checkBHost checks the "BHOST" key in the JSON map -func checkBHost(t *testing.T, envMap map[string]EnvData) { +func checkBHost(t *testing.T, envMap map[string]testutils.EnvData) { if bhost, ok := envMap["BHOST"]; !ok { t.Error("Expected 'BHOST' key in the JSON, but it was not found") } else { @@ -163,13 +103,13 @@ func checkBHost(t *testing.T, envMap map[string]EnvData) { } } -func checkBHostDoesNotExist(t *testing.T, envMap map[string]EnvData) { +func checkBHostDoesNotExist(t *testing.T, envMap map[string]testutils.EnvData) { if _, ok := envMap["BHOST"]; ok { t.Error("Expected 'BHOST' key not to be present in the JSON, but it was found") } } -func checkAHostDoesNotExist(t *testing.T, envMap map[string]EnvData) { +func checkAHostDoesNotExist(t *testing.T, envMap map[string]testutils.EnvData) { if _, ok := envMap["AHOST"]; ok { t.Error("Expected 'AHOST' key not to be present in the JSON, but it was found") } diff --git a/tests/utils/test_utils.go b/tests/utils/test_utils.go new file mode 100644 index 00000000..94db4f7d --- /dev/null +++ b/tests/utils/test_utils.go @@ -0,0 +1,100 @@ +package testutils + +import ( + "bytes" + "encoding/json" + "fmt" + "os" + "os/exec" + "testing" + + "github.com/rs/zerolog/log" +) + +type EnvData struct { + Src string `json:"src"` + Val string `json:"val"` +} + +func RunL2CommandAndGetOutput(t *testing.T, cmdArgs ...string) string { + + // 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 "" + } + + // Code to run the l2 command + cmd := exec.Command(l2BinPath, cmdArgs...) + + var stdout 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 "" + } + + // Retrieve the captured stdout + stdoutOutput := stdout.String() + log.Info().Str("Test env_command", stdoutOutput).Msg("output from command") + return stdoutOutput +} + +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) + } + + // 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 + t.Errorf("Error running l2 command: %v\n", err) + return make(map[string]EnvData) + } + + // 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) + + envMap := make(map[string]EnvData) + err = json.Unmarshal(outputBytes, &envMap) + if err != nil { + t.Fatalf("Error unmarshaling JSON env: %v\nOutput:\n%s", err, stdoutOutput) + } + + 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 +}