Skip to content

Commit

Permalink
test init and utils, swap some interfaces to funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
miniscruff committed Dec 19, 2020
1 parent 52ce72a commit 3737ce6
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 144 deletions.
4 changes: 2 additions & 2 deletions cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func runBatch(cmd *cobra.Command, args []string) error {
fs := afero.NewOsFs()
afs := afero.Afero{Fs: fs}

config, err := LoadConfig(afs)
config, err := LoadConfig(afs.ReadFile)
if err != nil {
return err
}
Expand All @@ -59,7 +59,7 @@ func runBatch(cmd *cobra.Command, args []string) error {
}

path := filepath.Join(config.ChangesDir, config.UnreleasedDir, file.Name())
c, err := LoadChange(path, afs)
c, err := LoadChange(path, afs.ReadFile)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func (change Change) SaveUnreleased(wf WriteFiler, tn TimeNow, config Config) er
timeString := tn().Format(timeFormat)
filePath := fmt.Sprintf("%s/%s/%s-%s.yaml", config.ChangesDir, config.UnreleasedDir, change.Kind, timeString)

return wf.WriteFile(filePath, bs, os.ModePerm)
return wf(filePath, bs, os.ModePerm)
}

func LoadChange(path string, rf ReadFiler) (Change, error) {
var c Change
bs, err := rf.ReadFile(path)
bs, err := rf(path)
if err != nil {
return c, err
}
Expand Down
12 changes: 4 additions & 8 deletions cmd/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ var _ = Describe("Change", func() {

writeCalled := false

mockWf := newMockWriteFiler()
mockWf.mockWriteFile = func(filepath string, bytes []byte, perm os.FileMode) error {
mockWf := func(filepath string, bytes []byte, perm os.FileMode) error {
writeCalled = true
Expect(filepath).To(Equal("Changes/Unrel/kind-20150524-033010.yaml"))
Expect(bytes).To(Equal([]byte(changesYaml)))
Expand All @@ -41,8 +40,7 @@ var _ = Describe("Change", func() {
})

It("should load change from path", func() {
mockRf := newMockReadFiler()
mockRf.mockReadFile = func(filepath string) ([]byte, error) {
mockRf := func(filepath string) ([]byte, error) {
Expect(filepath).To(Equal("some_file.yaml"))
return []byte("kind: A\nbody: hey\n"), nil
}
Expand All @@ -56,8 +54,7 @@ var _ = Describe("Change", func() {
It("should return error from bad read", func() {
mockErr := errors.New("bad file")

mockRf := newMockReadFiler()
mockRf.mockReadFile = func(filepath string) ([]byte, error) {
mockRf := func(filepath string) ([]byte, error) {
Expect(filepath).To(Equal("some_file.yaml"))
return []byte(""), mockErr
}
Expand All @@ -67,8 +64,7 @@ var _ = Describe("Change", func() {
})

It("should return error from bad file", func() {
mockRf := newMockReadFiler()
mockRf.mockReadFile = func(filepath string) ([]byte, error) {
mockRf := func(filepath string) ([]byte, error) {
Expect(filepath).To(Equal("some_file.yaml"))
return []byte("not a yaml file---"), nil
}
Expand Down
58 changes: 23 additions & 35 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (_ *beADirMatcher) NegatedFailureMessage(actual interface{}) (message strin
type mockFs struct {
mockCreate func(string) (afero.File, error)
mockMkdirAll func(string, os.FileMode) error
mockOpen func(string) (afero.File, error)
memFs afero.Fs
}

Expand All @@ -123,25 +124,28 @@ func newMockFs() *mockFs {
}

func (m *mockFs) Create(name string) (afero.File, error) {
if m.mockCreate == nil {
return m.memFs.Create(name)
if m.mockCreate != nil {
return m.mockCreate(name)
}
return m.mockCreate(name)
return m.memFs.Create(name)
}

func (m *mockFs) Mkdir(name string, perm os.FileMode) error {
panic("not implemented") // TODO: Implement
}

func (m *mockFs) MkdirAll(path string, perm os.FileMode) error {
if m.mockMkdirAll == nil {
return m.memFs.MkdirAll(path, perm)
if m.mockMkdirAll != nil {
return m.mockMkdirAll(path, perm)
}
return m.mockMkdirAll(path, perm)
return m.memFs.MkdirAll(path, perm)
}

func (m *mockFs) Open(name string) (afero.File, error) {
panic("not implemented") // TODO: Implement
if m.mockOpen != nil {
return m.mockOpen(name)
}
return m.memFs.Open(name)
}

func (m *mockFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
Expand Down Expand Up @@ -177,13 +181,15 @@ func (m *mockFs) Chtimes(name string, atime, mtime time.Time) error {
}

type mockFile struct {
mockRead func([]byte) (int, error)
mockClose func() error
mockWrite func([]byte) (int, error)
mockWriteString func(string) (int, error)
memFile afero.File
}

func newMockFile(fs afero.Fs) *mockFile {
f, _ := fs.Create("temp file")
func newMockFile(fs afero.Fs, filename string) *mockFile {
f, _ := fs.Create(filename)
return &mockFile{
memFile: f,
}
Expand All @@ -200,7 +206,10 @@ func (m *mockFile) Close() error {
}

func (m *mockFile) Read(p []byte) (n int, err error) {
panic("not implemented") // TODO: Implement
if m.mockRead != nil {
return m.mockRead(p)
}
return m.memFile.Read(p)
}

func (m *mockFile) ReadAt(p []byte, off int64) (n int, err error) {
Expand All @@ -212,7 +221,10 @@ func (m *mockFile) Seek(offset int64, whence int) (int64, error) {
}

func (m *mockFile) Write(p []byte) (n int, err error) {
panic("not implemented") // TODO: Implement
if m.mockWrite != nil {
return m.mockWrite(p)
}
return m.memFile.Write(p)
}

func (m *mockFile) WriteAt(p []byte, off int64) (n int, err error) {
Expand Down Expand Up @@ -249,27 +261,3 @@ func (m *mockFile) WriteString(s string) (ret int, err error) {
}
return m.memFile.WriteString(s)
}

type mockWriteFiler struct {
mockWriteFile func(string, []byte, os.FileMode) error
}

func newMockWriteFiler() *mockWriteFiler {
return &mockWriteFiler{}
}

func (m *mockWriteFiler) WriteFile(filename string, data []byte, perm os.FileMode) error {
return m.mockWriteFile(filename, data, perm)
}

type mockReadFiler struct {
mockReadFile func(string) ([]byte, error)
}

func newMockReadFiler() *mockReadFiler {
return &mockReadFiler{}
}

func (m *mockReadFiler) ReadFile(filename string) ([]byte, error) {
return m.mockReadFile(filename)
}
4 changes: 2 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type Config struct {

func (config Config) Save(wf WriteFiler) error {
bs, _ := yaml.Marshal(&config)
return wf.WriteFile(configPath, bs, os.ModePerm)
return wf(configPath, bs, os.ModePerm)
}

func LoadConfig(rf ReadFiler) (Config, error) {
var c Config
bs, err := rf.ReadFile(configPath)
bs, err := rf(configPath)
if err != nil {
return c, err
}
Expand Down
12 changes: 4 additions & 8 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ kinds: []

writeCalled := false

mockWf := newMockWriteFiler()
mockWf.mockWriteFile = func(filepath string, bytes []byte, perm os.FileMode) error {
mockWf := func(filepath string, bytes []byte, perm os.FileMode) error {
writeCalled = true
Expect(filepath).To(Equal(configPath))
Expect(string(bytes)).To(Equal(configYaml))
Expand All @@ -43,8 +42,7 @@ kinds: []
})

It("should load change from path", func() {
mockRf := newMockReadFiler()
mockRf.mockReadFile = func(filepath string) ([]byte, error) {
mockRf := func(filepath string) ([]byte, error) {
Expect(filepath).To(Equal(configPath))
return []byte("changesDir: C\nheaderPath: header.rst\n"), nil
}
Expand All @@ -58,8 +56,7 @@ kinds: []
It("should return error from bad read", func() {
mockErr := errors.New("bad file")

mockRf := newMockReadFiler()
mockRf.mockReadFile = func(filepath string) ([]byte, error) {
mockRf := func(filepath string) ([]byte, error) {
Expect(filepath).To(Equal(configPath))
return []byte(""), mockErr
}
Expand All @@ -69,8 +66,7 @@ kinds: []
})

It("should return error from bad file", func() {
mockRf := newMockReadFiler()
mockRf.mockReadFile = func(filepath string) ([]byte, error) {
mockRf := func(filepath string) ([]byte, error) {
Expect(filepath).To(Equal(configPath))
return []byte("not a yaml file---"), nil
}
Expand Down
37 changes: 16 additions & 21 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"fmt"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

// initCmd represents the init command
Expand Down Expand Up @@ -36,9 +37,6 @@ func init() {
}

func runInit(cmd *cobra.Command, args []string) error {
var err error

fs := afero.NewOsFs()
config := Config{
ChangesDir: changesDir,
UnreleasedDir: "unreleased",
Expand All @@ -52,42 +50,39 @@ func runInit(cmd *cobra.Command, args []string) error {
"Added", "Changed", "Deprecated", "Removed", "Fixed", "Security",
},
}
afs := afero.Afero{Fs: fs}

err = config.Save(afs)
afs := afero.Afero{Fs: afero.NewOsFs()}
err := config.Save(afs.WriteFile)
if err != nil {
return err
}

err = fs.MkdirAll(fmt.Sprintf("%s/%s", config.ChangesDir, config.UnreleasedDir), 644)
if err != nil {
return err
}
return initPipeline(afs.MkdirAll, afs.WriteFile, config)
}

keepFile, err := fs.Create(fmt.Sprintf("%s/%s/.gitkeep", config.ChangesDir, config.UnreleasedDir))
if err != nil {
return err
}
defer keepFile.Close()
func initPipeline(mkdir MkdirAller, wf WriteFiler, config Config) error {
var err error

headerPath := filepath.Join(config.ChangesDir, config.HeaderPath)
unreleasedPath := filepath.Join(config.ChangesDir, config.UnreleasedDir)
keepPath := filepath.Join(unreleasedPath, ".gitkeep")

headerFile, err := fs.Create(fmt.Sprintf("%s/%s", config.ChangesDir, config.HeaderPath))
err = mkdir(unreleasedPath, 0644)
if err != nil {
return err
}
defer headerFile.Close()

_, err = headerFile.WriteString(defaultHeader)
err = wf(keepPath, []byte{}, os.ModePerm)
if err != nil {
return err
}

outputFile, err := fs.Create(config.ChangelogPath)
err = wf(headerPath, []byte(defaultHeader), os.ModePerm)
if err != nil {
return err
}
defer outputFile.Close()

_, err = outputFile.WriteString(defaultChangelog)
err = wf(config.ChangelogPath, []byte(defaultChangelog), os.ModePerm)
if err != nil {
return err
}
Expand Down

0 comments on commit 3737ce6

Please sign in to comment.