Skip to content

Commit

Permalink
Fix small bugs + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
myxo committed May 5, 2024
1 parent 09ff383 commit ced42fc
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 65 deletions.
26 changes: 21 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,34 @@ on:

jobs:

build:
runs-on: ubuntu-latest
build-and-test:
strategy:
matrix:
os: ["ubuntu-latest", "macos-latest"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v -rapid.checks=50000 ./...

golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: golangci-lint
uses: golangci/golangci-lint-action@v5
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.57
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.out
*.test
testdata/
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Yet another file system mock library in go

Features:
- performance in non mock path (no interfaces)
- profound testing (we use property based tsting to ensure simularity with real implementation
- profound testing (we use property based tsting to ensure simularity with real implementation)

Non features:
- make a varaity of different backends (like NetFS, google cloud, s3, etc.). I try to keep package as clean from dependencies as possible
- simulating of concurrent effect of filesystem (e.g. concurrent ReadDir with file removing)
- simulating of concurrent effect of filesystem (e.g. concurrent ReadDir with file removing in different goroutine)

TODO:
- [ ] Make count in test to see how much function envocation we have
Expand All @@ -17,3 +19,5 @@ TODO:
- [ ] Use more stdlib errors (how to test this?)
- [ ] Test relative paths
- [ ] Test wrapped error?
- [ ] move all tests to subpackage, so users don't have to depend on rapid and testify
- [ ] run with race, we should not give users false positives
7 changes: 5 additions & 2 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (f *FakeFile) pread(b []byte, off int64) (n int, err error) {
return 0, fmt.Errorf("%w file open without write permission", os.ErrPermission)
}
if off > int64(len(f.data.buff)) {
return 0, io.ErrUnexpectedEOF
return 0, io.EOF
}
n = copy(b, f.data.buff[off:])
if n == 0 {
Expand Down Expand Up @@ -227,10 +227,12 @@ func (f *FakeFile) ReadFrom(r io.Reader) (n int64, err error) {
// Hack copypasted from stdlib
// noReadFrom can be embedded alongside another type to
// hide the ReadFrom method of that other type.
//nolint:all
type noReadFrom struct{}

// ReadFrom hides another ReadFrom method.
// It should never be called.
//nolint:all
func (noReadFrom) ReadFrom(io.Reader) (int64, error) {
panic("can't happen")
}
Expand All @@ -239,6 +241,7 @@ func (noReadFrom) ReadFrom(io.Reader) (int64, error) {
// than ReadFrom. This is used to permit ReadFrom to call io.Copy
// without leading to a recursive call to ReadFrom.
type fileWithoutReadFrom struct {
//nolint:all
noReadFrom
*FakeFile
}
Expand Down Expand Up @@ -278,7 +281,7 @@ func (f *FakeFile) Sync() error {
if f.data == nil {
return os.ErrInvalid
}
clear(f.data.dyrtyPages)
f.data.dyrtyPages = f.data.dyrtyPages[:0]
return nil
}

Expand Down
13 changes: 12 additions & 1 deletion memory_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,18 @@ func (f *FakeFS) OpenFile(name string, flag int, perm os.FileMode) (*File, error
}, nil
}

func (f *FakeFS) Chdir(dir string) error { panic("TODO") }
func (f *FakeFS) Chdir(dir string) error {
inode, ok := f.inodes[dir]
if !ok {
return MakeWrappedError("Chdir", dir, os.ErrNotExist, "")
}
if !inode.isDirectory {
return MakeError("Chdir", dir, "not an directory")
}

f.workDir = dir
return nil
}

func (f *FakeFS) Chmod(name string, mode os.FileMode) error {
inode, ok := f.inodes[name]
Expand Down
Loading

0 comments on commit ced42fc

Please sign in to comment.