Skip to content

Commit

Permalink
Improving test case, test
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Jul 22, 2023
1 parent 87bbcc7 commit 9c28a5c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion parser/jsonparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
41 changes: 31 additions & 10 deletions tests/env_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9c28a5c

Please sign in to comment.