Skip to content

Commit

Permalink
change unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
EstyFridman1 committed Aug 5, 2024
1 parent 93854e7 commit 7877a68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
19 changes: 9 additions & 10 deletions src/terraform/structure/terraform_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ func (p *TerraformParser) WriteFile(readFilePath string, blocks []structure.IBlo
}

// can't delete files on windows if you dont close them
// if err = fd.Close(); err != nil {
// return err
// }
// err = os.Remove(tempFile.Name())
// if err != nil {
// return err
// }
if err = fd.Close(); err != nil {
return err
}
err = os.Remove(tempFile.Name())
if err != nil {
return err
}

// #nosec G304
f, err := os.OpenFile(writeFilePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0600)
Expand Down Expand Up @@ -828,19 +828,18 @@ func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) (map[string]
hclData := new(Resource)
hclBytes := tokens.Bytes()
hclBytes = []byte(strings.Replace(string(hclBytes), "{", " tags= {", 1))
// Unmarshalling the HCL data into a map

err := dethcl.Unmarshal([]byte(hclBytes), hclData)

Check failure on line 832 in src/terraform/structure/terraform_parser.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci-lint] reported by reviewdog 🐶 unnecessary conversion (unconvert) Raw Output: src/terraform/structure/terraform_parser.go:832:33: unnecessary conversion (unconvert) err := dethcl.Unmarshal([]byte(hclBytes), hclData) ^
if err != nil {
return nil, fmt.Errorf("failed to unmarshel data because %s", err)
}

jsonData, err := dethcl.Marshal(hclData)
tempHclData, err := dethcl.Marshal(hclData)
if err != nil {
return nil, fmt.Errorf("failed to marshel data because %s", err)

}
hclFile, diagnostics := hclwrite.ParseConfig(jsonData, "", hcl.InitialPos)
hclFile, diagnostics := hclwrite.ParseConfig(tempHclData, "", hcl.InitialPos)
if diagnostics != nil && diagnostics.HasErrors() {
return nil, fmt.Errorf("failed to convert to hclFile %s", diagnostics)
}
Expand Down
8 changes: 4 additions & 4 deletions src/terraform/structure/terraform_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestTerraformParser_SkipResourceByComment(t *testing.T) {
}

func TestParseTagAttribute(t *testing.T) {
filePath := "../../../tests/terraform/foorOption/one.tf"
filePath := "../../../tests/terraform/forLoop/main.tf"
expectedTags := map[string]string{
"c": "d",
}
Expand All @@ -69,9 +69,9 @@ func TestParseTagAttribute(t *testing.T) {
if tagsAttribute != nil {
tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{})
parsedTags, _ := parser.parseTagAttribute(tagsTokens)
if(block.GetResourceName()=="a"){
assert.Equal(t, parsedTags, expectedTags)
}
if block.GetResourceName() == "bucket_var_tags" {
assert.Equal(t, parsedTags, expectedTags)
}
}

}
Expand Down

0 comments on commit 7877a68

Please sign in to comment.