Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored and jpogran committed Sep 19, 2023
1 parent 3956181 commit d4170e1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
9 changes: 7 additions & 2 deletions internal/terraform/module/module_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"io/fs"
"log"
"path/filepath"
"time"

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -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
}
Expand Down
37 changes: 8 additions & 29 deletions internal/terraform/module/module_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync"
"testing"
Expand Down Expand Up @@ -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)

Expand All @@ -1007,30 +991,25 @@ 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"] {
t.Fatal("file should mismatch")
}

// 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")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ variable "gogo" {

variable "awesome" {

}

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
variable "wakka" {

}

6 changes: 3 additions & 3 deletions internal/terraform/parser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d4170e1

Please sign in to comment.