From d4170e1c5abdac39c46e68d35e63a36e2a85df8f Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 18 Sep 2023 15:20:21 +0100 Subject: [PATCH] fix tests --- internal/terraform/module/module_ops.go | 9 ++++- internal/terraform/module/module_ops_test.go | 37 ++++--------------- .../testdata/single-file-change-module/foo.tf | 2 +- .../single-file-change-module/main.tf | 2 +- internal/terraform/parser/module.go | 6 +-- 5 files changed, 20 insertions(+), 36 deletions(-) diff --git a/internal/terraform/module/module_ops.go b/internal/terraform/module/module_ops.go index 4a063ab27..1f855b03b 100644 --- a/internal/terraform/module/module_ops.go +++ b/internal/terraform/module/module_ops.go @@ -11,6 +11,7 @@ import ( "io" "io/fs" "log" + "path/filepath" "time" "github.com/hashicorp/go-multierror" @@ -373,9 +374,13 @@ func ParseModuleConfiguration(ctx context.Context, fs ReadOnlyFS, modStore *stat // Only parse the file that's being changed/opened, unless this is 1st-time parsing if mod.ModuleParsingState == op.OpStateLoaded && rpcContext.IsDidChangeRequest() { // the file has already been parsed, so only examine this file and not the whole module - fileName, _ := uri.PathFromURI(rpcContext.URI) + filePath, err := uri.PathFromURI(rpcContext.URI) + if err != nil { + return err + } + fileName := filepath.Base(filePath) - f, fDiags, err := parser.ParseModuleFile(fs, fileName) + f, fDiags, err := parser.ParseModuleFile(fs, filePath) if err != nil { return err } diff --git a/internal/terraform/module/module_ops_test.go b/internal/terraform/module/module_ops_test.go index 2ad219c5d..4d500492c 100644 --- a/internal/terraform/module/module_ops_test.go +++ b/internal/terraform/module/module_ops_test.go @@ -13,7 +13,6 @@ import ( "log" "net/http" "net/http/httptest" - "os" "path/filepath" "sync" "testing" @@ -973,21 +972,6 @@ func TestParseModuleConfiguration(t *testing.T) { t.Fatal(err) } - fmt.Println("--------before---------") - fmt.Println(string(before.ParsedModuleFiles["foo.tf"].Bytes)) - fmt.Println("--------before---------") - - - f, err := os.OpenFile(filepath.Join(singleFileModulePath, "foo.tf"), - os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - t.Fatal(err) - } - if _, err := f.WriteString("\n\nvariable \"awesome\" {\n\n}\n"); err != nil { - t.Fatal(err) - } - f.Close() - // ignore job state ctx = job.WithIgnoreState(ctx, true) @@ -1007,10 +991,6 @@ func TestParseModuleConfiguration(t *testing.T) { if err != nil { t.Fatal(err) } - fmt.Println("--------after---------") - fmt.Println(string(after.ParsedModuleFiles["foo.tf"].Bytes)) - fmt.Println("--------after---------") - // test if foo.tf is not the same as first seen if before.ParsedModuleFiles["foo.tf"] == after.ParsedModuleFiles["foo.tf"] { @@ -1018,19 +998,18 @@ func TestParseModuleConfiguration(t *testing.T) { } // test if main.tf is the same as first seen - if before.ParsedModuleFiles["main.tf"] == after.ParsedModuleFiles["main.tf"] { + if before.ParsedModuleFiles["main.tf"] != after.ParsedModuleFiles["main.tf"] { t.Fatal("file mismatch") } - // examine diags should change for foo.tf - diagsBefore := before.ModuleDiagnostics["foo.tf"] - diagsAfter := after.ModuleDiagnostics["foo.tf"] - // if before.ModuleDiagnostics["foo.tf"] == after.ModuleDiagnostics["foo.tf"] { - // t.Fatal("diags should mismatch") - // } + // examine diags should change for foo.tf + if before.ModuleDiagnostics["foo.tf"][0] == after.ModuleDiagnostics["foo.tf"][0] { + t.Fatal("diags should mismatch") + } - if diff := cmp.Diff(diagsBefore, diagsAfter, ctydebug.CmpOptions); diff != "" { - t.Fatalf("diags should mismatch: %s", diff) + // examine diags should change for main.tf + if before.ModuleDiagnostics["main.tf"][0] != after.ModuleDiagnostics["main.tf"][0] { + t.Fatal("diags should match") } } diff --git a/internal/terraform/module/testdata/single-file-change-module/foo.tf b/internal/terraform/module/testdata/single-file-change-module/foo.tf index ded33220b..c6e93b84d 100644 --- a/internal/terraform/module/testdata/single-file-change-module/foo.tf +++ b/internal/terraform/module/testdata/single-file-change-module/foo.tf @@ -5,4 +5,4 @@ variable "gogo" { variable "awesome" { -} + diff --git a/internal/terraform/module/testdata/single-file-change-module/main.tf b/internal/terraform/module/testdata/single-file-change-module/main.tf index 2a412ee20..2d431a4b9 100644 --- a/internal/terraform/module/testdata/single-file-change-module/main.tf +++ b/internal/terraform/module/testdata/single-file-change-module/main.tf @@ -1,3 +1,3 @@ variable "wakka" { -} + diff --git a/internal/terraform/parser/module.go b/internal/terraform/parser/module.go index fa51963bb..5daf5b1a7 100644 --- a/internal/terraform/parser/module.go +++ b/internal/terraform/parser/module.go @@ -54,14 +54,14 @@ func ParseModuleFiles(fs FS, modPath string) (ast.ModFiles, ast.ModDiags, error) return files, diags, nil } -func ParseModuleFile(fs FS, fileName string) (*hcl.File, hcl.Diagnostics, error) { - src, err := fs.ReadFile(fileName) +func ParseModuleFile(fs FS, filePath string) (*hcl.File, hcl.Diagnostics, error) { + src, err := fs.ReadFile(filePath) if err != nil { // If a file isn't accessible, return return nil, nil, err } - name := filepath.Base((fileName)) + name := filepath.Base(filePath) filename := ast.ModFilename(name) f, pDiags := parseFile(src, filename)