Fix staticcheck lint errors

This commit is contained in:
Bjørn Erik Pedersen 2022-07-14 13:06:04 +02:00
parent 939bf3d6b2
commit 52b64170ec
17 changed files with 47 additions and 38 deletions

View File

@ -30,8 +30,8 @@ jobs:
shell: bash shell: bash
- name: Vet - name: Vet
run: go vet ./... run: go vet ./...
#- name: Staticcheck - name: Staticcheck
# if: matrix.go-version != '1.16.x' if: matrix.go-version != '1.16.x'
# run: staticcheck ./... run: staticcheck ./...
- name: Test - name: Test
run: go test -race ./... run: go test -race ./...

View File

@ -103,8 +103,8 @@ type Fs interface {
var ( var (
ErrFileClosed = errors.New("File is closed") ErrFileClosed = errors.New("File is closed")
ErrOutOfRange = errors.New("Out of range") ErrOutOfRange = errors.New("out of range")
ErrTooLarge = errors.New("Too large") ErrTooLarge = errors.New("too large")
ErrFileNotFound = os.ErrNotExist ErrFileNotFound = os.ErrNotExist
ErrFileExists = os.ErrExist ErrFileExists = os.ErrExist
ErrDestinationExists = os.ErrExist ErrDestinationExists = os.ErrExist

View File

@ -98,7 +98,7 @@ func TestOpenFile(t *testing.T) {
io.WriteString(f, "|append") io.WriteString(f, "|append")
f.Close() f.Close()
f, err = fs.OpenFile(path, os.O_RDONLY, 0600) f, _ = fs.OpenFile(path, os.O_RDONLY, 0600)
contents, _ := ioutil.ReadAll(f) contents, _ := ioutil.ReadAll(f)
expectedContents := "initial|append" expectedContents := "initial|append"
if string(contents) != expectedContents { if string(contents) != expectedContents {
@ -700,7 +700,7 @@ func removeAllTestFiles(t *testing.T) {
func equal(name1, name2 string) (r bool) { func equal(name1, name2 string) (r bool) {
switch runtime.GOOS { switch runtime.GOOS {
case "windows": case "windows":
r = strings.ToLower(name1) == strings.ToLower(name2) r = strings.EqualFold(name1, name2)
default: default:
r = name1 == name2 r = name1 == name2
} }

View File

@ -79,7 +79,7 @@ func TestUnionCreateExisting(t *testing.T) {
fh.Close() fh.Close()
fh, _ = base.Open("/home/test/file.txt") fh, _ = base.Open("/home/test/file.txt")
data, err = ioutil.ReadAll(fh) data, _ = ioutil.ReadAll(fh)
if string(data) != "This is a test" { if string(data) != "This is a test" {
t.Errorf("Got wrong data in base file") t.Errorf("Got wrong data in base file")
} }
@ -326,9 +326,9 @@ func TestUnionCacheWrite(t *testing.T) {
t.Errorf("Failed to write file") t.Errorf("Failed to write file")
} }
fh.Seek(0, os.SEEK_SET) fh.Seek(0, io.SeekStart)
buf := make([]byte, 4) buf := make([]byte, 4)
_, err = fh.Read(buf) _, _ = fh.Read(buf)
fh.Write([]byte(" IS A")) fh.Write([]byte(" IS A"))
fh.Close() fh.Close()

View File

@ -302,6 +302,9 @@ func (fs *Fs) Remove(name string) error {
} }
var infos []os.FileInfo var infos []os.FileInfo
infos, err = dir.Readdir(0) infos, err = dir.Readdir(0)
if err != nil {
return err
}
if len(infos) > 0 { if len(infos) > 0 {
return syscall.ENOTEMPTY return syscall.ENOTEMPTY
} }
@ -345,6 +348,9 @@ func (fs *Fs) RemoveAll(path string) error {
var infos []os.FileInfo var infos []os.FileInfo
infos, err = dir.Readdir(0) infos, err = dir.Readdir(0)
if err != nil {
return err
}
for _, info := range infos { for _, info := range infos {
nameToRemove := fs.normSeparators(info.Name()) nameToRemove := fs.normSeparators(info.Name())
err = fs.RemoveAll(path + fs.separator + nameToRemove) err = fs.RemoveAll(path + fs.separator + nameToRemove)

View File

@ -496,7 +496,7 @@ func TestGcsOpenFile(t *testing.T) {
t.Fatalf("failed to close a file \"%s\": %s", name, err) t.Fatalf("failed to close a file \"%s\": %s", name, err)
} }
file, err = gcsAfs.OpenFile(name, os.O_CREATE, 0600) _, err = gcsAfs.OpenFile(name, os.O_CREATE, 0600)
if !errors.Is(err, syscall.EPERM) { if !errors.Is(err, syscall.EPERM) {
t.Errorf("%v: open for write: got %v, expected %v", name, err, syscall.EPERM) t.Errorf("%v: open for write: got %v, expected %v", name, err, syscall.EPERM)
} }

View File

@ -29,7 +29,7 @@ type httpDir struct {
} }
func (d httpDir) Open(name string) (http.File, error) { func (d httpDir) Open(name string) (http.File, error) {
if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 || if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||
strings.Contains(name, "\x00") { strings.Contains(name, "\x00") {
return nil, errors.New("http: invalid character in file path") return nil, errors.New("http: invalid character in file path")
} }

View File

@ -37,13 +37,13 @@ func TestReadFile(t *testing.T) {
testFS.Create("this_exists.go") testFS.Create("this_exists.go")
filename := "rumpelstilzchen" filename := "rumpelstilzchen"
contents, err := fsutil.ReadFile(filename) _, err := fsutil.ReadFile(filename)
if err == nil { if err == nil {
t.Fatalf("ReadFile %s: error expected, none found", filename) t.Fatalf("ReadFile %s: error expected, none found", filename)
} }
filename = "this_exists.go" filename = "this_exists.go"
contents, err = fsutil.ReadFile(filename) contents, err := fsutil.ReadFile(filename)
if err != nil { if err != nil {
t.Fatalf("ReadFile %s: %v", filename, err) t.Fatalf("ReadFile %s: %v", filename, err)
} }

View File

@ -186,7 +186,7 @@ func (f *File) Readdirnames(n int) (names []string, err error) {
func (f *File) Read(b []byte) (n int, err error) { func (f *File) Read(b []byte) (n int, err error) {
f.fileData.Lock() f.fileData.Lock()
defer f.fileData.Unlock() defer f.fileData.Unlock()
if f.closed == true { if f.closed {
return 0, ErrFileClosed return 0, ErrFileClosed
} }
if len(b) > 0 && int(f.at) == len(f.fileData.data) { if len(b) > 0 && int(f.at) == len(f.fileData.data) {
@ -214,7 +214,7 @@ func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
} }
func (f *File) Truncate(size int64) error { func (f *File) Truncate(size int64) error {
if f.closed == true { if f.closed {
return ErrFileClosed return ErrFileClosed
} }
if f.readOnly { if f.readOnly {
@ -236,7 +236,7 @@ func (f *File) Truncate(size int64) error {
} }
func (f *File) Seek(offset int64, whence int) (int64, error) { func (f *File) Seek(offset int64, whence int) (int64, error) {
if f.closed == true { if f.closed {
return 0, ErrFileClosed return 0, ErrFileClosed
} }
switch whence { switch whence {
@ -251,7 +251,7 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
} }
func (f *File) Write(b []byte) (n int, err error) { func (f *File) Write(b []byte) (n int, err error) {
if f.closed == true { if f.closed {
return 0, ErrFileClosed return 0, ErrFileClosed
} }
if f.readOnly { if f.readOnly {
@ -330,8 +330,8 @@ func (s *FileInfo) Size() int64 {
var ( var (
ErrFileClosed = errors.New("File is closed") ErrFileClosed = errors.New("File is closed")
ErrOutOfRange = errors.New("Out of range") ErrOutOfRange = errors.New("out of range")
ErrTooLarge = errors.New("Too large") ErrTooLarge = errors.New("too large")
ErrFileNotFound = os.ErrNotExist ErrFileNotFound = os.ErrNotExist
ErrFileExists = os.ErrExist ErrFileExists = os.ErrExist
ErrDestinationExists = os.ErrExist ErrDestinationExists = os.ErrExist

View File

@ -214,7 +214,7 @@ func TestMultipleOpenFiles(t *testing.T) {
if err != nil { if err != nil {
t.Error("fh.Write failed: " + err.Error()) t.Error("fh.Write failed: " + err.Error())
} }
_, err = fh1.Seek(0, os.SEEK_SET) _, err = fh1.Seek(0, io.SeekStart)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -16,12 +16,12 @@ func TestFilterReadOnly(t *testing.T) {
func TestFilterReadonlyRemoveAndRead(t *testing.T) { func TestFilterReadonlyRemoveAndRead(t *testing.T) {
mfs := &MemMapFs{} mfs := &MemMapFs{}
fh, err := mfs.Create("/file.txt") fh, _ := mfs.Create("/file.txt")
fh.Write([]byte("content here")) fh.Write([]byte("content here"))
fh.Close() fh.Close()
fs := NewReadOnlyFs(mfs) fs := NewReadOnlyFs(mfs)
err = fs.Remove("/file.txt") err := fs.Remove("/file.txt")
if err == nil { if err == nil {
t.Errorf("Did not fail to remove file") t.Errorf("Did not fail to remove file")
} }

View File

@ -215,6 +215,9 @@ func MakeSSHKeyPair(bits int, pubKeyPath, privateKeyPath string) error {
// generate and write private key as PEM // generate and write private key as PEM
privateKeyFile, err := os.Create(privateKeyPath) privateKeyFile, err := os.Create(privateKeyPath)
if err != nil {
return err
}
defer privateKeyFile.Close() defer privateKeyFile.Close()
if err != nil { if err != nil {
return err return err

View File

@ -266,7 +266,7 @@ func TestOpenFile(t *testing.T) {
} }
file.Close() file.Close()
file, err = afs.OpenFile(f.name, os.O_CREATE, 0600) _, err = afs.OpenFile(f.name, os.O_CREATE, 0600)
if !errors.Is(err, syscall.EPERM) { if !errors.Is(err, syscall.EPERM) {
t.Errorf("%v: open for write: got %v, expected %v", f.name, err, syscall.EPERM) t.Errorf("%v: open for write: got %v, expected %v", f.name, err, syscall.EPERM)
} }

View File

@ -65,7 +65,7 @@ func (f *UnionFile) ReadAt(s []byte, o int64) (int, error) {
if f.Layer != nil { if f.Layer != nil {
n, err := f.Layer.ReadAt(s, o) n, err := f.Layer.ReadAt(s, o)
if (err == nil || err == io.EOF) && f.Base != nil { if (err == nil || err == io.EOF) && f.Base != nil {
_, err = f.Base.Seek(o+int64(n), os.SEEK_SET) _, err = f.Base.Seek(o+int64(n), io.SeekStart)
} }
return n, err return n, err
} }

10
util.go
View File

@ -25,6 +25,7 @@ import (
"strings" "strings"
"unicode" "unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform" "golang.org/x/text/transform"
"golang.org/x/text/unicode/norm" "golang.org/x/text/unicode/norm"
) )
@ -158,16 +159,12 @@ func UnicodeSanitize(s string) string {
// Transform characters with accents into plain forms. // Transform characters with accents into plain forms.
func NeuterAccents(s string) string { func NeuterAccents(s string) string {
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC) t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.String(t, string(s)) result, _, _ := transform.String(t, string(s))
return result return result
} }
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func (a Afero) FileContainsBytes(filename string, subslice []byte) (bool, error) { func (a Afero) FileContainsBytes(filename string, subslice []byte) (bool, error) {
return FileContainsBytes(a.Fs, filename, subslice) return FileContainsBytes(a.Fs, filename, subslice)
} }
@ -299,6 +296,9 @@ func IsEmpty(fs Fs, path string) (bool, error) {
} }
defer f.Close() defer f.Close()
list, err := f.Readdir(-1) list, err := f.Readdir(-1)
if err != nil {
return false, err
}
return len(list) == 0, nil return len(list) == 0, nil
} }
return fi.Size() == 0, nil return fi.Size() == 0, nil

View File

@ -185,7 +185,7 @@ func createZeroSizedFileInTempDir() (File, error) {
func createNonZeroSizedFileInTempDir() (File, error) { func createNonZeroSizedFileInTempDir() (File, error) {
f, err := createZeroSizedFileInTempDir() f, err := createZeroSizedFileInTempDir()
if err != nil { if err != nil {
// no file ?? return nil, err
} }
byteString := []byte("byteString") byteString := []byte("byteString")
err = WriteFile(testFS, f.Name(), byteString, 0644) err = WriteFile(testFS, f.Name(), byteString, 0644)
@ -200,7 +200,7 @@ func createNonZeroSizedFileInTempDir() (File, error) {
func deleteFileInTempDir(f File) { func deleteFileInTempDir(f File) {
err := testFS.Remove(f.Name()) err := testFS.Remove(f.Name())
if err != nil { if err != nil {
// now what? panic(err)
} }
} }
@ -217,7 +217,7 @@ func createEmptyTempDir() (string, error) {
func createTempDirWithZeroLengthFiles() (string, error) { func createTempDirWithZeroLengthFiles() (string, error) {
d, dirErr := createEmptyTempDir() d, dirErr := createEmptyTempDir()
if dirErr != nil { if dirErr != nil {
//now what? return "", dirErr
} }
filePrefix := "_path_test_" filePrefix := "_path_test_"
_, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir() _, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir()
@ -235,7 +235,7 @@ func createTempDirWithZeroLengthFiles() (string, error) {
func createTempDirWithNonZeroLengthFiles() (string, error) { func createTempDirWithNonZeroLengthFiles() (string, error) {
d, dirErr := createEmptyTempDir() d, dirErr := createEmptyTempDir()
if dirErr != nil { if dirErr != nil {
//now what? return "", dirErr
} }
filePrefix := "_path_test_" filePrefix := "_path_test_"
f, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir() f, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir()
@ -406,7 +406,7 @@ func TestGetTempDir(t *testing.T) {
func deleteTempDir(d string) { func deleteTempDir(d string) {
err := os.RemoveAll(d) err := os.RemoveAll(d)
if err != nil { if err != nil {
// now what? panic(err)
} }
} }

View File

@ -85,10 +85,10 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
return 0, afero.ErrFileClosed return 0, afero.ErrFileClosed
} }
switch whence { switch whence {
case os.SEEK_SET: case io.SeekStart:
case os.SEEK_CUR: case io.SeekCurrent:
offset += f.offset offset += f.offset
case os.SEEK_END: case io.SeekEnd:
offset += int64(f.zipfile.UncompressedSize64) offset += int64(f.zipfile.UncompressedSize64)
default: default:
return 0, syscall.EINVAL return 0, syscall.EINVAL