Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WrapFS: for compatibility with the stdlib io/fs package #86

Merged
merged 2 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
executors:
go:
docker:
- image: docker.mirror.hashicorp.services/cimg/go:1.17
- image: docker.mirror.hashicorp.services/cimg/go:1.18

jobs:
test:
Expand Down
16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
module github.com/hashicorp/terraform-config-inspect

require (
github.com/agext/levenshtein v1.2.2 // indirect
github.com/go-test/deep v1.0.3
github.com/google/go-cmp v0.3.0 // indirect
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f
github.com/hashicorp/hcl/v2 v2.0.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.3.0 // indirect
github.com/zclconf/go-cty v1.1.0
)

go 1.13
require (
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg v1.0.0 // indirect
github.com/google/go-cmp v0.3.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
golang.org/x/text v0.3.2 // indirect
)

go 1.18
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand All @@ -24,27 +21,19 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/zclconf/go-cty v1.1.0 h1:uJwc9HiBOCpoKIObTQaLR+tsEXx1HBHnOsOOpcdhZgw=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand All @@ -56,7 +45,6 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
81 changes: 77 additions & 4 deletions tfconfig/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,99 @@
package tfconfig

import (
"io/fs"
"io/ioutil"
"os"
"time"
)

// FS represents a minimal filesystem implementation
// See io/fs.FS in http://golang.org/s/draft-iofs-design
// FS is an interface used by [LoadModuleFromFilesystem].
//
// Unfortunately this package implemented a draft version of the io/fs.FS
// API before it was finalized and so this interface is not compatible with
// the final design. To use this package with the final filesystem API design,
// use [WrapFS] to wrap a standard filesystem implementation so that it
// implements this interface.
type FS interface {
Open(name string) (File, error)
ReadFile(name string) ([]byte, error)
ReadDir(dirname string) ([]os.FileInfo, error)
}

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

// wrapFS is a rather regrettable adapter from the standard library filesystem
// interfaces to the one we have designed in this package, since we adopted
// a draft of that API before it was finalized and the final is incompatible.
type wrapFS struct {
wrapped fs.FS
}

// WrapFS wraps a standard library filesystem implementation so that it
// implements this package's own (slightly-incompatible) interface [FS].
func WrapFS(wrapped fs.FS) FS {
return wrapFS{wrapped}
}

func (wfs wrapFS) Open(name string) (File, error) {
return wfs.wrapped.Open(name)
}

func (wfs wrapFS) ReadFile(name string) ([]byte, error) {
return fs.ReadFile(wfs.wrapped, name)
}

func (wfs wrapFS) ReadDir(dirname string) ([]os.FileInfo, error) {
entries, err := fs.ReadDir(wfs.wrapped, dirname)
var ret []os.FileInfo
if len(entries) != 0 {
ret = make([]os.FileInfo, len(entries))
for i, entry := range entries {
ret[i] = wrapFileInfoDirEntry{entry}
}
}
return ret, err
}

type wrapFileInfoDirEntry struct {
wrapped fs.DirEntry
}

func (d wrapFileInfoDirEntry) IsDir() bool {
return d.wrapped.IsDir()
}

func (d wrapFileInfoDirEntry) ModTime() time.Time {
// this package doesn't actually care about modification times,
// so we don't need to implement this.
panic("unimplemented")
}

func (d wrapFileInfoDirEntry) Mode() fs.FileMode {
// this package doesn't actually care about file modes,
// so we don't need to implement this.
panic("unimplemented")
}

func (d wrapFileInfoDirEntry) Name() string {
return d.wrapped.Name()
}

func (d wrapFileInfoDirEntry) Size() int64 {
// this package doesn't actually care about file sizes,
// so we don't need to implement this.
panic("unimplemented")
}

func (d wrapFileInfoDirEntry) Sys() any {
return nil
}

type osFs struct{}

func (fs *osFs) Open(name string) (File, error) {
Expand Down
57 changes: 52 additions & 5 deletions tfconfig/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package tfconfig
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/go-test/deep"
"github.com/google/go-cmp/cmp"
)

func TestLoadModule(t *testing.T) {
Expand Down Expand Up @@ -49,10 +50,56 @@ func TestLoadModule(t *testing.T) {
t.Fatalf("failed to parse the actual result (!?): %s", err)
}

if diff := deep.Equal(got, want); diff != nil {
for _, problem := range diff {
t.Errorf("%s", problem)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong result\n%s", diff)
}
})
}
}

func TestLoadModuleFromFilesystem(t *testing.T) {
fixturesDir := "testdata"
testDirs, err := ioutil.ReadDir(fixturesDir)
if err != nil {
t.Fatal(err)
}

for _, info := range testDirs {
if !info.IsDir() {
continue
}
t.Run(info.Name(), func(t *testing.T) {
name := info.Name()
path := filepath.Join(fixturesDir, name)
fs := os.DirFS(".")

wantSrc, err := ioutil.ReadFile(filepath.Join(path, name+".out.json"))
if err != nil {
t.Fatalf("failed to read result file: %s", err)
}
var want map[string]interface{}
err = json.Unmarshal(wantSrc, &want)
if err != nil {
t.Fatalf("failed to parse result file: %s", err)
}

gotObj, _ := LoadModuleFromFilesystem(WrapFS(fs), path)
if gotObj == nil {
t.Fatalf("result object is nil; want a real object")
}

gotSrc, err := json.Marshal(gotObj)
if err != nil {
t.Fatalf("result is not JSON-able: %s", err)
}
var got map[string]interface{}
err = json.Unmarshal(gotSrc, &got)
if err != nil {
t.Fatalf("failed to parse the actual result (!?): %s", err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong result\n%s", diff)
}
})
}
Expand Down