Skip to content

Commit

Permalink
Replace internal decoder with hcl-lang
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Oct 30, 2020
1 parent c90be01 commit 476f67f
Show file tree
Hide file tree
Showing 59 changed files with 543 additions and 7,467 deletions.
16 changes: 4 additions & 12 deletions commands/completion_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

"github.com/hashicorp/terraform-ls/internal/filesystem"
ihcl "github.com/hashicorp/terraform-ls/internal/hcl"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
"github.com/hashicorp/terraform-ls/logging"
Expand Down Expand Up @@ -92,13 +91,6 @@ func (c *CompletionCommand) Run(args []string) int {
return 1
}

text, err := file.Text()
if err != nil {
c.Ui.Error(err.Error())
return 1
}

hclFile := ihcl.NewFile(file, text)
fPos, err := ilsp.FilePositionFromDocumentPosition(lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{
URI: fh.DocumentURI(),
Expand All @@ -110,27 +102,27 @@ func (c *CompletionCommand) Run(args []string) int {
return 1
}

w, err := rootmodule.NewRootModule(context.Background(), fs, fh.Dir())
rm, err := rootmodule.NewRootModule(context.Background(), fs, fh.Dir())
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to load root module: %s", err.Error()))
return 1
}
p, err := w.Parser()
d, err := rm.Decoder()
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to find parser: %s", err.Error()))
return 1
}

pos := fPos.Position()

candidates, err := p.CompletionCandidatesAtPos(hclFile, pos)
candidates, err := d.CandidatesAtPos(file.Filename(), pos)
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to find candidates: %s", err.Error()))
return 1
}

cc := &lsp.ClientCapabilities{}
items := ilsp.CompletionList(candidates, pos, cc.TextDocument)
items := ilsp.CompletionList(candidates, cc.TextDocument)

c.Ui.Output(fmt.Sprintf("%#v", items))
return 0
Expand Down
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ require (
github.com/google/go-cmp v0.5.1
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/hcl/v2 v2.5.2-0.20200528183353-fa7c453538de
github.com/hashicorp/terraform-config-inspect v0.0.0-20200806211835-c481b8bfa41e
github.com/hashicorp/hcl-lang v0.0.0-20201030165913-78d715cf8480
github.com/hashicorp/hcl/v2 v2.6.0
github.com/hashicorp/terraform-exec v0.11.1-0.20201007122305-ea2094d52cb5
github.com/hashicorp/terraform-json v0.5.0
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136
github.com/hashicorp/terraform-json v0.6.0
github.com/hashicorp/terraform-schema v0.0.0-20201030170010-a0fe8ef0d9bd
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.1
github.com/mitchellh/go-homedir v1.1.0
Expand All @@ -24,9 +24,6 @@ require (
github.com/spf13/afero v1.3.2
github.com/stretchr/testify v1.4.0
github.com/vektra/mockery/v2 v2.3.0
github.com/zclconf/go-cty v1.2.1
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
)

replace github.com/sourcegraph/go-lsp => github.com/radeksimko/go-lsp v0.1.0
32 changes: 26 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -179,21 +181,27 @@ github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+Db
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20201030165913-78d715cf8480 h1:cT1ikuCDvhBXhnWehL/Abl6JVGvQPdob4D0GoQrVZ9o=
github.com/hashicorp/hcl-lang v0.0.0-20201030165913-78d715cf8480/go.mod h1:A0mcesqbT4EQkWw/CMBBp8pM5kl8wEekshClBJRGeUw=
github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90=
github.com/hashicorp/hcl/v2 v2.5.2-0.20200528183353-fa7c453538de h1:bCeWhTigOmP9am0cJA+5kaTtA2RFDmnWIRtBIxo+Ydg=
github.com/hashicorp/hcl/v2 v2.5.2-0.20200528183353-fa7c453538de/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/hcl/v2 v2.6.0 h1:3krZOfGY6SziUXa6H9PJU6TyohHn7I+ARYnhbeNBz+o=
github.com/hashicorp/hcl/v2 v2.6.0/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hashicorp/terraform-config-inspect v0.0.0-20200806211835-c481b8bfa41e h1:wIsEsIITggCC4FTO9PisDjy561UU7OPL6uTu7tnkHH8=
github.com/hashicorp/terraform-config-inspect v0.0.0-20200806211835-c481b8bfa41e/go.mod h1:Z0Nnk4+3Cy89smEbrq+sl1bxc9198gIP4I7wcQF6Kqs=
github.com/hashicorp/terraform-config-inspect v0.0.0-20201021083235-edf96fe612be h1:ysCslG9aaVmOty0UYh/86POxLQsMQuvwrAQvO5HSjgg=
github.com/hashicorp/terraform-config-inspect v0.0.0-20201021083235-edf96fe612be/go.mod h1:Z0Nnk4+3Cy89smEbrq+sl1bxc9198gIP4I7wcQF6Kqs=
github.com/hashicorp/terraform-exec v0.11.1-0.20201007122305-ea2094d52cb5 h1:P+lBGicJEG3ijvOrDdQf/Oo8UrG4QAJbdY3g9OGBnr0=
github.com/hashicorp/terraform-exec v0.11.1-0.20201007122305-ea2094d52cb5/go.mod h1:eQdBvA0Xr/ZJNilY8TzrtePLSqLyexk9PSwVwzzHTjY=
github.com/hashicorp/terraform-json v0.5.0 h1:7TV3/F3y7QVSuN4r9BEXqnWqrAyeOtON8f0wvREtyzs=
github.com/hashicorp/terraform-json v0.5.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136 h1:81Dg7SK6Q5vzqFItO8e1iIF2Nj8bLXV23NXjEgbev/s=
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/terraform-json v0.6.0 h1:nMTj4t9ysC7xJ72rvVsDqhUccvbUINrjhPqafeUeREk=
github.com/hashicorp/terraform-json v0.6.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-schema v0.0.0-20201030170010-a0fe8ef0d9bd h1:t9HoYFfDiJnHubRXDYS2n2W69AndgDd9OLK/PoZQ1Ng=
github.com/hashicorp/terraform-schema v0.0.0-20201030170010-a0fe8ef0d9bd/go.mod h1:9botPSAy/JJXyFTDR8H9I/5AWV9WgK/bn6juNDQIDIc=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down Expand Up @@ -246,6 +254,8 @@ github.com/mitchellh/cli v1.0.0 h1:iGBIsUe3+HZ/AD/Vd7DErOt5sU9fa8Uj7A2s1aggv1Y=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/cli v1.1.1 h1:J64v/xD7Clql+JVKSvkYojLOXu1ibnY9ZjGLwSt/89w=
github.com/mitchellh/cli v1.1.1/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand All @@ -261,6 +271,8 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down Expand Up @@ -342,6 +354,8 @@ github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4A
github.com/vektra/mockery/v2 v2.3.0 h1:AHHAbvKortfmW7QOqB+Y8gm49TLZu+fJ0s+yJVd6KBc=
github.com/vektra/mockery/v2 v2.3.0/go.mod h1:rBZUbbhMbiSX1WlCGsOgAi6xjuJGxB7KKbnoL0XNYW8=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand All @@ -351,6 +365,10 @@ github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLE
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.2.1 h1:vGMsygfmeCl4Xb6OA5U5XVAaQZ69FvoG7X2jUtQujb8=
github.com/zclconf/go-cty v1.2.1/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.6.1 h1:wHtZ+LSSQVwUSb+XIJ5E9hgAQxyWATZsAWT+ESJ9dQ0=
github.com/zclconf/go-cty v1.6.1/go.mod h1:VDR4+I79ubFBGm1uJac1226K5yANQFHeauxPBoP54+o=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down Expand Up @@ -488,6 +506,8 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.1 h1:QzqyMA1tlu6CgqCDUtU9V+ZKhLFT2dkJuANu5QaxI3I=
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Expand Down
12 changes: 6 additions & 6 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
ctxTfExecTimeout = &contextKey{"terraform execution timeout"}
ctxWatcher = &contextKey{"watcher"}
ctxRootModuleMngr = &contextKey{"root module manager"}
ctxParserFinder = &contextKey{"parser finder"}
ctxDecoderFinder = &contextKey{"decoder finder"}
ctxTfFormatterFinder = &contextKey{"terraform formatter finder"}
ctxRootModuleCaFi = &contextKey{"root module candidate finder"}
ctxRootModuleWalker = &contextKey{"root module walker"}
Expand Down Expand Up @@ -124,14 +124,14 @@ func RootModuleManager(ctx context.Context) (rootmodule.RootModuleManager, error
return wm, nil
}

func WithParserFinder(ctx context.Context, pf rootmodule.ParserFinder) context.Context {
return context.WithValue(ctx, ctxParserFinder, pf)
func WithDecoderFinder(ctx context.Context, pf rootmodule.DecoderFinder) context.Context {
return context.WithValue(ctx, ctxDecoderFinder, pf)
}

func ParserFinder(ctx context.Context) (rootmodule.ParserFinder, error) {
pf, ok := ctx.Value(ctxParserFinder).(rootmodule.ParserFinder)
func DecoderFinder(ctx context.Context) (rootmodule.DecoderFinder, error) {
pf, ok := ctx.Value(ctxDecoderFinder).(rootmodule.DecoderFinder)
if !ok {
return nil, missingContextErr(ctxParserFinder)
return nil, missingContextErr(ctxDecoderFinder)
}
return pf, nil
}
Expand Down
5 changes: 2 additions & 3 deletions internal/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"sync"

"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -191,7 +190,7 @@ func (fs *fsystem) ReadFile(name string) ([]byte, error) {

func (fs *fsystem) ReadDir(name string) ([]os.FileInfo, error) {
memList, err := afero.ReadDir(fs.memFs, name)
if err != nil {
if err != nil && !os.IsNotExist(err) {
return nil, err
}
osList, err := afero.ReadDir(fs.osFs, name)
Expand Down Expand Up @@ -219,7 +218,7 @@ func fileIsInList(list []os.FileInfo, file os.FileInfo) bool {
return false
}

func (fs *fsystem) Open(name string) (tfconfig.File, error) {
func (fs *fsystem) Open(name string) (File, error) {
f, err := fs.memFs.Open(name)
if err != nil && os.IsNotExist(err) {
return fs.osFs.Open(name)
Expand Down
29 changes: 28 additions & 1 deletion internal/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ func TestFilesystem_Open_osOnly(t *testing.T) {

func TestFilesystem_Open_memOnly(t *testing.T) {
fs := NewFilesystem()
fh := &testHandler{uri: "file:///tmp/test.tf"}
tmpDir := TempDir(t)
testPath := filepath.Join(tmpDir, "test.tf")
fh := testHandlerFromPath(testPath)

content := "test content"
err := fs.CreateDocument(fh, []byte(content))
if err != nil {
Expand Down Expand Up @@ -405,6 +408,30 @@ func TestFilesystem_Open_memAndOs(t *testing.T) {
}
}

func TestFilesystem_Create_memOnly(t *testing.T) {
fs := NewFilesystem()
tmpDir := TempDir(t)
testPath := filepath.Join(tmpDir, "test.tf")
fh := testHandlerFromPath(testPath)

content := "test content"
err := fs.CreateDocument(fh, []byte(content))
if err != nil {
t.Fatal(err)
}

infos, err := fs.ReadDir(tmpDir)
if err != nil {
t.Fatal(err)
}

expectedFis := []string{"test.tf"}
names := namesFromFileInfos(infos)
if diff := cmp.Diff(expectedFis, names); diff != "" {
t.Fatalf("file list mismatch: %s", diff)
}
}

func TempDir(t *testing.T) string {
tmpDir := filepath.Join(os.TempDir(), "terraform-ls", t.Name())

Expand Down
11 changes: 9 additions & 2 deletions internal/filesystem/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/hashicorp/terraform-ls/internal/source"
)

Expand Down Expand Up @@ -49,5 +48,13 @@ type Filesystem interface {
// direct FS methods
ReadFile(name string) ([]byte, error)
ReadDir(name string) ([]os.FileInfo, error)
Open(name string) (tfconfig.File, error)
Open(name string) (File, error)
}

// File represents an open file in FS
// See io/fs.File in http://golang.org/s/draft-iofs-design
type File interface {
Stat() (os.FileInfo, error)
Read([]byte) (int, error)
Close() error
}
Loading

0 comments on commit 476f67f

Please sign in to comment.