Skip to content

Commit 203e5f2

Browse files
committed
add error checks
1 parent 5c0c264 commit 203e5f2

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

internal/hcl/hcl_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hcl_test
22

33
import (
4+
"encoding/json"
45
"path/filepath"
56
"strings"
67
"testing"
@@ -14,14 +15,20 @@ import (
1415

1516
func TestClusterToAdvancedCluster(t *testing.T) {
1617
const (
17-
root = "testdata/clu2adv"
18-
inSuffix = ".in.tf"
19-
outSuffix = ".out.tf"
18+
root = "testdata/clu2adv"
19+
inSuffix = ".in.tf"
20+
outSuffix = ".out.tf"
21+
errFilename = "errors.json"
2022
)
23+
fs := afero.NewOsFs()
24+
errMap := make(map[string]string)
25+
errContent, err := afero.ReadFile(fs, filepath.Join(root, errFilename))
26+
require.NoError(t, err)
27+
err = json.Unmarshal(errContent, &errMap)
28+
require.NoError(t, err)
2129
g := goldie.New(t,
2230
goldie.WithFixtureDir(root),
2331
goldie.WithNameSuffix(outSuffix))
24-
fs := afero.NewOsFs()
2532
pattern := filepath.Join(root, "*"+inSuffix)
2633
inputFiles, err := afero.Glob(fs, pattern)
2734
require.NoError(t, err)
@@ -31,7 +38,12 @@ func TestClusterToAdvancedCluster(t *testing.T) {
3138
inConfig, err := afero.ReadFile(fs, inputFile)
3239
require.NoError(t, err)
3340
outConfig, err := hcl.ClusterToAdvancedCluster(inConfig)
34-
require.NoError(t, err)
35-
g.Assert(t, testName, outConfig)
41+
if err == nil {
42+
g.Assert(t, testName, outConfig)
43+
} else {
44+
errMsg, found := errMap[testName]
45+
assert.True(t, found, "error not found for test %s", testName)
46+
assert.Contains(t, err.Error(), errMsg)
47+
}
3648
}
3749
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resource this is an invalid HCL configuration file
2+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"configuration_file_error": "failed to parse Terraform config file",
3+
"free_cluster_missing_attribute": "free cluster (because no replication_specs): attribute backing_provider_name not found"
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "resource1" "res1" {
2+
name = "name1"
3+
}
4+
5+
resource "mongodbatlas_cluster" "free_cluster" { # comment in the resource
6+
# comment in own line in the beginning
7+
count = local.use_free_cluster ? 1 : 0
8+
project_id = var.project_id # inline comment kept
9+
name = var.cluster_name
10+
# comment in own line in the middle is deleted
11+
provider_name = "TENANT" # inline comment for attribute moved is not kept
12+
provider_region_name = var.region
13+
provider_instance_size_name = "M0"
14+
# comment in own line at the end happens before replication_specs
15+
}
16+
17+
data "mongodbatlas_cluster" "cluster2" {
18+
name = "name4"
19+
}

0 commit comments

Comments
 (0)