-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,797 additions
and
1,396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,24 +34,19 @@ SHELL := env \ | |
# Tools | ||
# | ||
|
||
TOOLS += $(TOOLDIR)/gobin | ||
$(TOOLDIR)/gobin: | ||
GO111MODULE=off go get -u github.com/myitcv/gobin | ||
|
||
# external tool | ||
define tool # 1: binary-name, 2: go-import-path | ||
TOOLS += $(TOOLDIR)/$(1) | ||
|
||
$(TOOLDIR)/$(1): $(TOOLDIR)/gobin Makefile | ||
gobin $(V) "$(2)" | ||
$(TOOLDIR)/$(1): Makefile | ||
GOBIN="$(CURDIR)/$(TOOLDIR)" go install "$(2)" | ||
endef | ||
|
||
$(eval $(call tool,godoc,golang.org/x/tools/cmd/godoc)) | ||
$(eval $(call tool,gofumpt,mvdan.cc/gofumpt)) | ||
$(eval $(call tool,goimports,golang.org/x/tools/cmd/goimports)) | ||
$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/[email protected])) | ||
$(eval $(call tool,gomod,github.com/Helcaraxan/gomod)) | ||
$(eval $(call tool,mockgen,mockgen,github.com/golang/mock/[email protected])) | ||
$(eval $(call tool,godoc,golang.org/x/tools/cmd/godoc@latest)) | ||
$(eval $(call tool,gofumpt,mvdan.cc/gofumpt@latest)) | ||
$(eval $(call tool,goimports,golang.org/x/tools/cmd/goimports@latest)) | ||
$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/[email protected])) | ||
$(eval $(call tool,gomod,github.com/Helcaraxan/gomod@latest)) | ||
|
||
.PHONY: tools | ||
tools: $(TOOLS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package golden | ||
|
||
import "os" | ||
|
||
type FS interface { | ||
// MkdirAll creates a directory named path, along with any necessary | ||
// parents, and returns nil, or else returns an error. The permission bits | ||
// perm (before umask) are used for all directories that MkdirAll creates. | ||
MkdirAll(path string, perm os.FileMode) error | ||
|
||
// ReadFile reads the named file and returns the contents. A successful call | ||
// returns err == nil, not err == EOF. Because ReadFile reads the whole | ||
// file, it does not treat an EOF from Read as an error to be reported. | ||
ReadFile(filename string) ([]byte, error) | ||
|
||
// WriteFile writes data to a file named by filename. If the file does not | ||
// exist, WriteFile creates it with permissions perm; otherwise WriteFile | ||
// truncates it before writing, without changing permissions. | ||
WriteFile(name string, data []byte, perm os.FileMode) error | ||
} | ||
|
||
type fsImpl struct{} | ||
|
||
var _ FS = fsImpl{} | ||
|
||
// NewFS returns a new FS instance which operates against the host file system | ||
// via calls to functions in the os package. | ||
func NewFS() FS { | ||
return fsImpl{} | ||
} | ||
|
||
// DefaultFS is the default FS instance used by all top-level package functions, | ||
// including the Default Golden instance, and also the New function. | ||
var DefaultFS = NewFS() | ||
|
||
func (fsImpl) MkdirAll(path string, perm os.FileMode) error { | ||
return os.MkdirAll(path, perm) | ||
} | ||
|
||
func (fsImpl) ReadFile(filename string) ([]byte, error) { | ||
return os.ReadFile(filename) | ||
} | ||
|
||
func (fsImpl) WriteFile(filename string, data []byte, perm os.FileMode) error { | ||
return os.WriteFile(filename, data, perm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package golden | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMkdirAll(t *testing.T) { | ||
tempDir := t.TempDir() | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
perm os.FileMode | ||
wantErr bool | ||
}{ | ||
{"create new dir", "newdir", 0o755, false}, | ||
{"create nested dirs", "nested/dir/structure", 0o755, false}, | ||
{"invalid path", string([]byte{0, 0}), 0o755, true}, | ||
} | ||
|
||
fs := NewFS() | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
path := filepath.Join(tempDir, tt.path) | ||
err := fs.MkdirAll(path, tt.perm) | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
_, err := os.Stat(path) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestReadFile(t *testing.T) { | ||
tempDir := t.TempDir() | ||
|
||
sampleFilePath := filepath.Join(tempDir, "sample.txt") | ||
sampleContent := []byte("Hello, world!") | ||
err := os.WriteFile(sampleFilePath, sampleContent, 0o600) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
filename string | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{"read existing file", sampleFilePath, sampleContent, false}, | ||
{"file does not exist", "nonexistent.txt", nil, true}, | ||
} | ||
|
||
fs := NewFS() | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := fs.ReadFile(tt.filename) | ||
|
||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(tt.want), string(got)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWriteFile(t *testing.T) { | ||
tempDir := t.TempDir() | ||
|
||
tests := []struct { | ||
name string | ||
filename string | ||
data []byte | ||
perm os.FileMode | ||
wantErr bool | ||
}{ | ||
{ | ||
"write to new file", | ||
"newfile.txt", | ||
[]byte("new content"), | ||
0o644, | ||
false, | ||
}, | ||
{ | ||
"overwrite existing file", | ||
"existing.txt", | ||
[]byte("overwritten content"), | ||
0o644, | ||
false, | ||
}, | ||
{ | ||
"invalid filename", | ||
string([]byte{0, 0}), | ||
[]byte("invalid filename"), | ||
0o644, | ||
true, | ||
}, | ||
{ | ||
"non-existent directory", | ||
"nonexistentdir/newfile.txt", | ||
[]byte("this will fail"), | ||
0o644, | ||
true, | ||
}, | ||
} | ||
|
||
fs := NewFS() | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
filePath := filepath.Join(tempDir, tt.filename) | ||
err := fs.WriteFile(filePath, tt.data, tt.perm) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
content, err := os.ReadFile(filePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.data, content) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
module github.com/jimeh/go-golden | ||
|
||
go 1.15 | ||
go 1.18 | ||
|
||
require ( | ||
github.com/golang/mock v1.6.0 | ||
github.com/jimeh/envctl v0.1.0 | ||
github.com/jimeh/go-mocktesting v0.1.0 | ||
github.com/spf13/afero v1.6.0 | ||
github.com/stretchr/testify v1.7.0 | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
github.com/stretchr/testify v1.9.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/objx v0.5.2 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.