diff --git a/parser/jsonparser.go b/parser/jsonparser.go index d4edf9e8..333a19c8 100644 --- a/parser/jsonparser.go +++ b/parser/jsonparser.go @@ -25,8 +25,9 @@ func (p *Lama2Parser) PrimitiveType() (*gabs.Container, error) { // CustomPairMerge uses a gabs feature to deal with merge conflicts. // More here: https://github.com/HexmosTech/gabs/blob/master/gabs.go#L511 +// //nolint:all -func CustomPairMerge(destination, source interface{}) interface{} { +func CustomPairMerge(destination, source interface{}) interface{} { return source } diff --git a/preprocess/preprocess.go b/preprocess/preprocess.go index 952cfab2..d4ef948e 100644 --- a/preprocess/preprocess.go +++ b/preprocess/preprocess.go @@ -73,7 +73,7 @@ func debugOp(str string) { func escapeString(input string) string { output, err := json.Marshal(input) if err != nil { - log.Error().Str("Error marshaling JSON:","escapeString()") + log.Error().Str("Error marshaling JSON:", "escapeString()") } return string(output) } @@ -139,7 +139,6 @@ func readFile(filename string) (envMap map[string]string, err error) { return godotenv.Parse(file) } - func getEnvMap(envPath string, source string) (map[string]map[string]interface{}, error) { envs, err := readFile(envPath) if err != nil { @@ -174,7 +173,7 @@ func GetL2EnvVariables(dir string) ([]byte, error) { if err != nil { // If l2config file is not found, assign an empty map to l2EnvMap and continue l2ConfigEnvMap = make(map[string]map[string]interface{}) - }else{ + } else { l2ConfigEnvMap, err = getEnvMap(l2ConfigPath, "l2configenv") if err != nil { // If an error occurs, assign an empty map to l2EnvMap and continue diff --git a/tests/env_command_test.go b/tests/env_command_test.go index f48b6009..74913992 100644 --- a/tests/env_command_test.go +++ b/tests/env_command_test.go @@ -18,22 +18,47 @@ type EnvData struct { func TestL2EnvCommand(t *testing.T) { cmdArgs := []string{"-e", "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"} - runL2CommandAndParseJSON(t, cmdArgs...) + envMap := runL2CommandAndParseJSON(t, cmdArgs...) + // Check the "AHOST" key + checkAHost(t, envMap) + + // Check the "BHOST" key + checkBHost(t, envMap) } func TestL2EnvCommandVerbose(t *testing.T) { cmdArgs := []string{"-ev", "../elfparser/ElfTestSuite/root_variable_override/api/y_0020_root_override.l2"} - runL2CommandAndParseJSON(t, cmdArgs...) + envMap := runL2CommandAndParseJSON(t, cmdArgs...) + // Check the "AHOST" key + checkAHost(t, envMap) + + // Check the "BHOST" key + checkBHost(t, envMap) +} + +func TestL2EnvWithoutL2config(t *testing.T) { + cmdArgs := []string{"-ev", "../elfparser/ElfTestSuite/no_l2config/api/y_0021_no_l2config.l2"} + envMap := runL2CommandAndParseJSON(t, cmdArgs...) + // Check the "AHOST" key + checkAHost(t, envMap) +} + +func TestL2EnvWithoutL2env(t *testing.T) { + cmdArgs := []string{"-ev", "../elfparser/ElfTestSuite/no_l2env/api/y_0022_no_l2env.l2"} + envMap := runL2CommandAndParseJSON(t, cmdArgs...) + + // Check the "BHOST" key + checkBHost(t, envMap) } -func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) { +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 + return make(map[string]EnvData) } // Your existing code to run the l2 command and parse JSON @@ -47,7 +72,7 @@ func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) { if err != nil { // Handle the error if needed t.Errorf("Error running l2 command: %v\n", err) - return + return make(map[string]EnvData) } // Retrieve the captured stdout @@ -64,11 +89,7 @@ func runL2CommandAndParseJSON(t *testing.T, cmdArgs ...string) { t.Fatalf("Error unmarshaling JSON env: %v\nOutput:\n%s", err, stdoutOutput) } - // Check the "AHOST" key - checkAHost(t, envMap) - - // Check the "BHOST" key - checkBHost(t, envMap) + return envMap } // checkL2BinaryExists checks if the l2 binary file exists in the specified path