fix(internal/fsx): remove pre Go 1.16 definitions (#270)
Occurred to me while working on https://github.com/ooni/probe/issues/1299.
This commit is contained in:
parent
87e5234586
commit
1da64f6d9f
|
@ -5,6 +5,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
|
@ -182,7 +183,7 @@ func (il inputLoader) loadLocal() ([]model.URLInfo, error) {
|
|||
}
|
||||
|
||||
// inputLoaderOpenFn is the type of the function to open a file.
|
||||
type inputLoaderOpenFn func(filepath string) (fsx.File, error)
|
||||
type inputLoaderOpenFn func(filepath string) (fs.File, error)
|
||||
|
||||
// readfile reads inputs from the specified file. The open argument should be
|
||||
// compatibile with stdlib's fs.Open and helps us with unit testing.
|
||||
|
|
|
@ -4,13 +4,13 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/kvstore"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
)
|
||||
|
@ -284,7 +284,7 @@ func TestInputLoaderInputOrQueryBackendWithEmptyFile(t *testing.T) {
|
|||
|
||||
type InputLoaderBrokenFS struct{}
|
||||
|
||||
func (InputLoaderBrokenFS) Open(filepath string) (fsx.File, error) {
|
||||
func (InputLoaderBrokenFS) Open(filepath string) (fs.File, error) {
|
||||
return InputLoaderBrokenFile{}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -3,31 +3,18 @@ package fsx
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// File is a generic file. This interface is taken from the draft
|
||||
// iofs golang design. We'll use fs.File when available.
|
||||
type File interface {
|
||||
Stat() (os.FileInfo, error)
|
||||
Read([]byte) (int, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// FS is a generic file system. Like File, it's adapted from
|
||||
// the draft iofs golang design document.
|
||||
type FS interface {
|
||||
Open(name string) (File, error)
|
||||
}
|
||||
|
||||
// Open is a wrapper for os.Open that ensures that we're opening a file.
|
||||
func Open(pathname string) (File, error) {
|
||||
func Open(pathname string) (fs.File, error) {
|
||||
return OpenWithFS(filesystem{}, pathname)
|
||||
}
|
||||
|
||||
// OpenWithFS is like Open but with explicit file system argument.
|
||||
func OpenWithFS(fs FS, pathname string) (File, error) {
|
||||
func OpenWithFS(fs fs.FS, pathname string) (fs.File, error) {
|
||||
file, err := fs.Open(pathname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -39,13 +26,16 @@ func OpenWithFS(fs FS, pathname string) (File, error) {
|
|||
}
|
||||
if info.IsDir() {
|
||||
file.Close()
|
||||
return nil, fmt.Errorf("input path points to a directory: %w", syscall.EISDIR)
|
||||
return nil, fmt.Errorf(
|
||||
"input path points to a directory: %w", syscall.EISDIR)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// filesystem is a private implementation of fs.FS.
|
||||
type filesystem struct{}
|
||||
|
||||
func (filesystem) Open(pathname string) (File, error) {
|
||||
// Open implements fs.FS.Open.
|
||||
func (filesystem) Open(pathname string) (fs.File, error) {
|
||||
return os.Open(pathname)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package fsx_test
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
|
@ -26,8 +27,8 @@ func (FailingStatFile) Stat() (os.FileInfo, error) {
|
|||
return nil, errStatFailed
|
||||
}
|
||||
|
||||
func (fs FailingStatFS) Open(pathname string) (fsx.File, error) {
|
||||
return FailingStatFile{CloseCount: fs.CloseCount}, nil
|
||||
func (f FailingStatFS) Open(pathname string) (fs.File, error) {
|
||||
return FailingStatFile(f), nil
|
||||
}
|
||||
|
||||
func (fs FailingStatFile) Close() error {
|
||||
|
|
Loading…
Reference in New Issue
Block a user