Skip to content

Commit

Permalink
stdlib: add missing wrappers of io/fs for go1.16
Browse files Browse the repository at this point in the history
Fixes #1195
  • Loading branch information
mvertes authored Jul 27, 2021
1 parent c80c605 commit b41fa6e
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 1 deletion.
79 changes: 79 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -928,6 +929,84 @@ func TestMultiEvalNoName(t *testing.T) {
}
}

const goMinorVersionTest = 16

func TestHasIOFS(t *testing.T) {
code := `
// +build go1.16
package main
import (
"errors"
"io/fs"
)
func main() {
pe := fs.PathError{}
pe.Op = "nothing"
pe.Path = "/nowhere"
pe.Err = errors.New("an error")
println(pe.Error())
}
// Output:
// nothing /nowhere: an error
`

var buf bytes.Buffer
i := interp.New(interp.Options{Stdout: &buf})
if err := i.Use(interp.Symbols); err != nil {
t.Fatal(err)
}
if err := i.Use(stdlib.Symbols); err != nil {
t.Fatal(err)
}

if _, err := i.Eval(code); err != nil {
t.Fatal(err)
}

var expectedOutput string
var minor int
var err error
version := runtime.Version()
fields := strings.Fields(version)
// Go stable
if len(fields) == 1 {
v := strings.Split(version, ".")
if len(v) < 2 {
t.Fatalf("unexpected: %v", version)
}
minor, err = strconv.Atoi(v[1])
if err != nil {
t.Fatal(err)
}
} else {
// Go devel
if fields[0] != "devel" {
t.Fatalf("unexpected: %v", fields[0])
}
parts := strings.Split(fields[1], "-")
if len(parts) != 2 {
t.Fatalf("unexpected: %v", fields[1])
}
minor, err = strconv.Atoi(strings.TrimPrefix(parts[0], "go1."))
if err != nil {
t.Fatal(err)
}
}

if minor >= goMinorVersionTest {
expectedOutput = "nothing /nowhere: an error\n"
}

output := buf.String()
if buf.String() != expectedOutput {
t.Fatalf("got: %v, wanted: %v", output, expectedOutput)
}
}

func TestImportPathIsKey(t *testing.T) {
// No need to check the results of Eval, as TestFile already does it.
i := interp.New(interp.Options{GoPath: filepath.FromSlash("../_test/testdata/redeclaration-global7")})
Expand Down
188 changes: 188 additions & 0 deletions stdlib/go1_16_io_fs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stdlib/stdlib_go1.16.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package stdlib

//go:generate ../internal/cmd/extract/extract embed testing/fstest
//go:generate ../internal/cmd/extract/extract embed io/fs testing/fstest

0 comments on commit b41fa6e

Please sign in to comment.