Skip to content

Commit

Permalink
tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Sep 20, 2023
1 parent 07b4e9b commit 55439cd
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
110 changes: 107 additions & 3 deletions internal/terraform/module/module_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ var randomSchemaJSON = `{
}
}`

func TestSchemaValidation_FullModule(t *testing.T) {
func TestSchemaModuleValidation_FullModule(t *testing.T) {
ctx := context.Background()
ss, err := state.NewStateStore()
if err != nil {
Expand Down Expand Up @@ -1015,7 +1015,7 @@ func TestSchemaValidation_FullModule(t *testing.T) {
}
}

func TestSchemaValidation_SingleFile(t *testing.T) {
func TestSchemaModuleValidation_SingleFile(t *testing.T) {
ctx := context.Background()
ss, err := state.NewStateStore()
if err != nil {
Expand Down Expand Up @@ -1060,4 +1060,108 @@ func TestSchemaValidation_SingleFile(t *testing.T) {
}
}

// TODO: Test for validation of tfvars
func TestSchemaVarsValidation_FullModule(t *testing.T) {
ctx := context.Background()
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}

testData, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
modPath := filepath.Join(testData, "invalid-tfvars")

err = ss.Modules.Add(modPath)
if err != nil {
t.Fatal(err)
}

fs := filesystem.NewFilesystem(ss.DocumentStore)
err = ParseModuleConfiguration(ctx, fs, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
err = LoadModuleMetadata(ctx, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
err = ParseVariables(ctx, fs, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
ctx = lsctx.WithRPCContext(ctx, lsctx.RPCContextData{
Method: "textDocument/didOpen",
URI: "file:///test/terraform.tfvars",
})
ctx = lsctx.WithLanguageId(ctx, ilsp.Tfvars.String())
err = SchemaVariablesValidation(ctx, ss.Modules, ss.ProviderSchemas, modPath)
if err != nil {
t.Fatal(err)
}

mod, err := ss.Modules.ModuleByPath(modPath)
if err != nil {
t.Fatal(err)
}

expectedCount := 1
diagsCount := mod.VarsDiagnostics[ast.SchemaValidationSource].Count()
if diagsCount != expectedCount {
t.Fatalf("expected %d diagnostics, %d given", expectedCount, diagsCount)
}
}

func TestSchemaVarsValidation_SingleFile(t *testing.T) {
ctx := context.Background()
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}

testData, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
modPath := filepath.Join(testData, "invalid-tfvars")

err = ss.Modules.Add(modPath)
if err != nil {
t.Fatal(err)
}

fs := filesystem.NewFilesystem(ss.DocumentStore)
err = ParseModuleConfiguration(ctx, fs, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
err = LoadModuleMetadata(ctx, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
err = ParseVariables(ctx, fs, ss.Modules, modPath)
if err != nil {
t.Fatal(err)
}
ctx = lsctx.WithRPCContext(ctx, lsctx.RPCContextData{
Method: "textDocument/didChange",
URI: "file:///test/terraform.tfvars",
})
ctx = lsctx.WithLanguageId(ctx, ilsp.Tfvars.String())
err = SchemaVariablesValidation(ctx, ss.Modules, ss.ProviderSchemas, modPath)
if err != nil {
t.Fatal(err)
}

mod, err := ss.Modules.ModuleByPath(modPath)
if err != nil {
t.Fatal(err)
}

expectedCount := 3
diagsCount := mod.VarsDiagnostics[ast.SchemaValidationSource].Count()
if diagsCount != expectedCount {
t.Fatalf("expected %d diagnostics, %d given", expectedCount, diagsCount)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo = "foo"
bar = "noot"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
variable "foo" {}

0 comments on commit 55439cd

Please sign in to comment.