Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
hanagantig committed May 6, 2023
2 parents 7bc0a84 + 45ef346 commit 687caf6
Show file tree
Hide file tree
Showing 46 changed files with 694 additions and 290 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
on:
push:
branches: [ main ]
pull_request:
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.18.x,1.19.x,1.20.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Install staticcheck
if: matrix.go-version == '1.20.x'
run: go install honnef.co/go/tools/cmd/staticcheck@latest
shell: bash
- name: Update PATH
run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
shell: bash
- name: Checkout code
uses: actions/checkout@v1
- name: Fmt
if: matrix.platform != 'windows-latest' # :(
run: "diff <(gofmt -d .) <(printf '')"
shell: bash
- name: Vet
run: go vet ./...
- name: Staticcheck
if: matrix.go-version == '1.20.x'
run: staticcheck ./...
- name: Test
run: go test -race ./...
26 changes: 0 additions & 26 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A FileSystem Abstraction System for Go

[![Build Status](https://travis-ci.org/spf13/afero.svg)](https://travis-ci.org/spf13/afero) [![Build status](https://ci.appveyor.com/api/projects/status/github/spf13/afero?branch=master&svg=true)](https://ci.appveyor.com/project/spf13/afero) [![GoDoc](https://godoc.org/github.com/spf13/afero?status.svg)](https://godoc.org/github.com/spf13/afero) [![Join the chat at https://gitter.im/spf13/afero](https://badges.gitter.im/Dev%20Chat.svg)](https://gitter.im/spf13/afero?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Test](https://github.com/spf13/afero/actions/workflows/test.yml/badge.svg)](https://github.com/spf13/afero/actions/workflows/test.yml) [![GoDoc](https://godoc.org/github.com/spf13/afero?status.svg)](https://godoc.org/github.com/spf13/afero) [![Join the chat at https://gitter.im/spf13/afero](https://badges.gitter.im/Dev%20Chat.svg)](https://gitter.im/spf13/afero?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

# Overview

Expand Down
6 changes: 3 additions & 3 deletions afero.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ type Fs interface {
// Chown changes the uid and gid of the named file.
Chown(name string, uid, gid int) error

//Chtimes changes the access and modification times of the named file
// Chtimes changes the access and modification times of the named file
Chtimes(name string, atime time.Time, mtime time.Time) error
}

var (
ErrFileClosed = errors.New("File is closed")
ErrOutOfRange = errors.New("Out of range")
ErrTooLarge = errors.New("Too large")
ErrOutOfRange = errors.New("out of range")
ErrTooLarge = errors.New("too large")
ErrFileNotFound = os.ErrNotExist
ErrFileExists = os.ErrExist
ErrDestinationExists = os.ErrExist
Expand Down
59 changes: 41 additions & 18 deletions afero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"io"
iofs "io/fs"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -27,8 +28,10 @@ import (
"testing"
)

var testName = "test.txt"
var Fss = []Fs{&MemMapFs{}, &OsFs{}}
var (
testName = "test.txt"
Fss = []Fs{&MemMapFs{}, &OsFs{}}
)

var testRegistry map[Fs][]string = make(map[Fs][]string)

Expand All @@ -44,7 +47,6 @@ func testDir(fs Fs) string {

func tmpFile(fs Fs) File {
x, err := TempFile(fs, "", "afero")

if err != nil {
panic(fmt.Sprint("unable to work with temp file", err))
}
Expand All @@ -54,7 +56,7 @@ func tmpFile(fs Fs) File {
return x
}

//Read with length 0 should not return EOF.
// Read with length 0 should not return EOF.
func TestRead0(t *testing.T) {
for _, fs := range Fss {
f := tmpFile(fs)
Expand Down Expand Up @@ -82,31 +84,31 @@ func TestOpenFile(t *testing.T) {
tmp := testDir(fs)
path := filepath.Join(tmp, testName)

f, err := fs.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
f, err := fs.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
t.Error(fs.Name(), "OpenFile (O_CREATE) failed:", err)
continue
}
io.WriteString(f, "initial")
f.Close()

f, err = fs.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0600)
f, err = fs.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
t.Error(fs.Name(), "OpenFile (O_APPEND) failed:", err)
continue
}
io.WriteString(f, "|append")
f.Close()

f, err = fs.OpenFile(path, os.O_RDONLY, 0600)
f, _ = fs.OpenFile(path, os.O_RDONLY, 0o600)
contents, _ := ioutil.ReadAll(f)
expectedContents := "initial|append"
if string(contents) != expectedContents {
t.Errorf("%v: appending, expected '%v', got: '%v'", fs.Name(), expectedContents, string(contents))
}
f.Close()

f, err = fs.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0600)
f, err = fs.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o600)
if err != nil {
t.Error(fs.Name(), "OpenFile (O_TRUNC) failed:", err)
continue
Expand Down Expand Up @@ -332,7 +334,7 @@ func TestSeek(t *testing.T) {
whence int
out int64
}
var tests = []test{
tests := []test{
{0, 1, int64(len(data))},
{0, 0, 0},
{5, 0, 5},
Expand Down Expand Up @@ -423,7 +425,7 @@ func setupTestDirReusePath(t *testing.T, fs Fs, path string) string {

func setupTestFiles(t *testing.T, fs Fs, path string) string {
testSubDir := filepath.Join(path, "more", "subdirectories", "for", "testing", "we")
err := fs.MkdirAll(testSubDir, 0700)
err := fs.MkdirAll(testSubDir, 0o700)
if err != nil && !os.IsExist(err) {
t.Fatal(err)
}
Expand Down Expand Up @@ -530,22 +532,43 @@ func TestReaddirSimple(t *testing.T) {

func TestReaddir(t *testing.T) {
defer removeAllTestFiles(t)
for num := 0; num < 6; num++ {
const nums = 6
for num := 0; num < nums; num++ {
outputs := make([]string, len(Fss))
infos := make([]string, len(Fss))
for i, fs := range Fss {
testSubDir := setupTestDir(t, fs)
//tDir := filepath.Dir(testSubDir)
root, err := fs.Open(testSubDir)
if err != nil {
t.Fatal(err)
}
defer root.Close()

for j := 0; j < 6; j++ {
infosn := make([]string, nums)

for j := 0; j < nums; j++ {
info, err := root.Readdir(num)
outputs[i] += fmt.Sprintf("%v Error: %v\n", myFileInfo(info), err)
infos[i] += fmt.Sprintln(len(info), err)
s := fmt.Sprintln(len(info), err)
infosn[j] = s
infos[i] += s
}
root.Close()

// Also check fs.ReadDirFile interface if implemented
if _, ok := root.(iofs.ReadDirFile); ok {
root, err = fs.Open(testSubDir)
if err != nil {
t.Fatal(err)
}
defer root.Close()

for j := 0; j < nums; j++ {
dirEntries, err := root.(iofs.ReadDirFile).ReadDir(num)
s := fmt.Sprintln(len(dirEntries), err)
if s != infosn[j] {
t.Fatalf("%s: %s != %s", fs.Name(), s, infosn[j])
}
}
}
}

Expand Down Expand Up @@ -615,7 +638,7 @@ func TestReaddirAll(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var namesRoot = []string{}
namesRoot := []string{}
for _, e := range rootInfo {
namesRoot = append(namesRoot, e.Name())
}
Expand All @@ -630,7 +653,7 @@ func TestReaddirAll(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var namesSub = []string{}
namesSub := []string{}
for _, e := range subInfo {
namesSub = append(namesSub, e.Name())
}
Expand Down Expand Up @@ -700,7 +723,7 @@ func removeAllTestFiles(t *testing.T) {
func equal(name1, name2 string) (r bool) {
switch runtime.GOOS {
case "windows":
r = strings.ToLower(name1) == strings.ToLower(name2)
r = strings.EqualFold(name1, name2)
default:
r = name1 == name2
}
Expand Down
9 changes: 2 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This currently does nothing. We have moved to GitHub action, but this is kept
# until spf13 has disabled this project in AppVeyor.
version: '{build}'
clone_folder: C:\gopath\src\github.com\spf13\afero
environment:
Expand All @@ -6,10 +8,3 @@ build_script:
- cmd: >-
go version
go env
go get -v github.com/spf13/afero/...
go build -v github.com/spf13/afero/...
test_script:
- cmd: go test -count=1 -cover -race -v github.com/spf13/afero/...
13 changes: 12 additions & 1 deletion basepath.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package afero

import (
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)

var _ Lstater = (*BasePathFs)(nil)
var (
_ Lstater = (*BasePathFs)(nil)
_ fs.ReadDirFile = (*BasePathFile)(nil)
)

// The BasePathFs restricts all operations to a given path within an Fs.
// The given file name to the operations on this Fs will be prepended with
Expand All @@ -33,6 +37,13 @@ func (f *BasePathFile) Name() string {
return strings.TrimPrefix(sourcename, filepath.Clean(f.path))
}

func (f *BasePathFile) ReadDir(n int) ([]fs.DirEntry, error) {
if rdf, ok := f.File.(fs.ReadDirFile); ok {
return rdf.ReadDir(n)
}
return readDirFile{f.File}.ReadDir(n)
}

func NewBasePathFs(source Fs, path string) Fs {
return &BasePathFs{source: source, path: path}
}
Expand Down
19 changes: 8 additions & 11 deletions basepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestBasePath(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
baseFs.MkdirAll("/base/path/tmp", 0o777)
bp := NewBasePathFs(baseFs, "/base/path")

if _, err := bp.Create("/tmp/foo"); err != nil {
Expand All @@ -23,8 +23,8 @@ func TestBasePath(t *testing.T) {

func TestBasePathRoot(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/foo/baz", 0777)
baseFs.MkdirAll("/base/path/boo/", 0777)
baseFs.MkdirAll("/base/path/foo/baz", 0o777)
baseFs.MkdirAll("/base/path/boo/", 0o777)
bp := NewBasePathFs(baseFs, "/base/path")

rd, err := ReadDir(bp, string(os.PathSeparator))
Expand Down Expand Up @@ -56,7 +56,6 @@ func TestRealPath(t *testing.T) {
subDir := filepath.Join(baseDir, "s1")

realPath, err := bp.RealPath("/s1")

if err != nil {
t.Errorf("Got error %s", err)
}
Expand All @@ -77,7 +76,6 @@ func TestRealPath(t *testing.T) {
// is not inside the base file system.
// The user will receive an os.ErrNotExist later.
surrealPath, err := bp.RealPath(anotherDir)

if err != nil {
t.Errorf("Got error %s", err)
}
Expand All @@ -88,7 +86,6 @@ func TestRealPath(t *testing.T) {
t.Errorf("Expected \n%s got \n%s", excpected, surrealPath)
}
}

}

func TestNestedBasePaths(t *testing.T) {
Expand Down Expand Up @@ -119,7 +116,7 @@ func TestNestedBasePaths(t *testing.T) {
}

for _, s := range specs {
if err := s.BaseFs.MkdirAll(s.FileName, 0755); err != nil {
if err := s.BaseFs.MkdirAll(s.FileName, 0o755); err != nil {
t.Errorf("Got error %s", err.Error())
}
if _, err := s.BaseFs.Stat(s.FileName); err != nil {
Expand All @@ -143,9 +140,9 @@ func TestNestedBasePaths(t *testing.T) {

func TestBasePathOpenFile(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
baseFs.MkdirAll("/base/path/tmp", 0o777)
bp := NewBasePathFs(baseFs, "/base/path")
f, err := bp.OpenFile("/tmp/file.txt", os.O_CREATE, 0600)
f, err := bp.OpenFile("/tmp/file.txt", os.O_CREATE, 0o600)
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
Expand All @@ -156,7 +153,7 @@ func TestBasePathOpenFile(t *testing.T) {

func TestBasePathCreate(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
baseFs.MkdirAll("/base/path/tmp", 0o777)
bp := NewBasePathFs(baseFs, "/base/path")
f, err := bp.Create("/tmp/file.txt")
if err != nil {
Expand All @@ -169,7 +166,7 @@ func TestBasePathCreate(t *testing.T) {

func TestBasePathTempFile(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
baseFs.MkdirAll("/base/path/tmp", 0o777)
bp := NewBasePathFs(baseFs, "/base/path")

tDir, err := TempDir(bp, "/tmp", "")
Expand Down

0 comments on commit 687caf6

Please sign in to comment.