Skip to content

Commit

Permalink
Add tests for errors on Construct
Browse files Browse the repository at this point in the history
  • Loading branch information
izumin5210 committed Jul 26, 2017
1 parent af73e78 commit f5dbfa8
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 1 deletion.
5 changes: 4 additions & 1 deletion domain/scaffold/repo/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func (r *repo) Construct(
}

if err = r.fs.CreateFile(outputPath, content); err != nil {
return errors.Cause(err)
if conflicted {
return errors.Wrapf(err, "Failed to overwrite %q", outputPath)
}
return errors.Wrapf(err, "Failed to create %q", outputPath)
}
cb(outputPath, false, conflicted, scaffold.ConstructSuccess)
}
Expand Down
158 changes: 158 additions & 0 deletions domain/scaffold/repo/construct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/izumin5210/scaffold/domain/scaffold"
"github.com/izumin5210/scaffold/infra/fs"
"github.com/pkg/errors"
)

type constructTestContext struct {
Expand Down Expand Up @@ -123,6 +124,31 @@ func setupConstructTest(
}
}

func constructErrorTest(t *testing.T, fn func(ctx *constructTestContext)) {
ctx := getConstructTestContext(t)
defer ctx.ctrl.Finish()

fn(ctx)

err := ctx.repo.Construct(
ctx.scaffold,
ctx.name,
func(path string, dir, conflicted bool, status scaffold.ConstructStatus) {
t.Errorf("Unexpected callback call (%s, %t, %t, %v)", path, dir, conflicted, status)
},
func(path, oldContent, newContent string) bool {
if oldContent == newContent {
t.Errorf("Unexpected callback call (%s, %s, %s)", path, oldContent, newContent)
}
return true
},
)

if err == nil {
t.Error("Shoulr return error")
}
}

func Test_Construct(t *testing.T) {
ctx := getConstructTestContext(t)
defer ctx.ctrl.Finish()
Expand Down Expand Up @@ -280,3 +306,135 @@ func Test_Construct(t *testing.T) {
}
}
}

func Test_Construct_WhenFailedToGetEntries(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return(nil, errors.New("error"))
})
}

func Test_Construct_WhenGetEntriesReturnBrokenPath(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
f, err := fs.NewFile(filepath.Join(ctx.scffPath, "{{name}"))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{f}, nil)
})
}

func Test_Construct_WhenGetEntriesReturnBrokenTemplate(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
f, err := fs.NewFile(filepath.Join(ctx.scffPath, "foo.go"))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{f}, nil)
ctx.fs.EXPECT().ReadFile(f.Path()).
Return([]byte("package {{name}"), nil)
})
}

func Test_Construct_WhenFailToReadFile(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
f, err := fs.NewFile(filepath.Join(ctx.scffPath, "foo.go"))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{f}, nil)
ctx.fs.EXPECT().ReadFile(f.Path()).
Return(nil, errors.New("error"))
})
}

func Test_Construct_WhenFailToCreateDir(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
d, err := fs.NewDir(filepath.Join(ctx.scffPath, "foo"))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{d}, nil)
ctx.fs.EXPECT().CreateDir(filepath.Join(ctx.rootPath, d.BaseName())).
Return(false, errors.New("error"))
})
}

func Test_Construct_WhenFailToCheckExistence(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
p := "foo.go"
f, err := fs.NewFile(filepath.Join(ctx.scffPath, p))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{f}, nil)
ctx.fs.EXPECT().ReadFile(f.Path()).
Return([]byte("package {{name}}"), nil)
ctx.fs.EXPECT().Exists(filepath.Join(ctx.rootPath, p)).
Return(false, errors.New("error"))
})
}

func Test_Construct_WhenFailToReadExistingFile(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
p := "foo.go"
f, err := fs.NewFile(filepath.Join(ctx.scffPath, p))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{f}, nil)
ctx.fs.EXPECT().ReadFile(f.Path()).
Return([]byte("package {{name}}"), nil)
outPath := filepath.Join(ctx.rootPath, p)
ctx.fs.EXPECT().Exists(outPath).
Return(true, nil)
ctx.fs.EXPECT().ReadFile(outPath).
Return(nil, errors.New("error"))
})
}

func Test_Construct_WhenFailToCreateFile(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
p := "foo.go"
f, err := fs.NewFile(filepath.Join(ctx.scffPath, p))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{f}, nil)
ctx.fs.EXPECT().ReadFile(f.Path()).
Return([]byte("package {{name}}"), nil)
outPath := filepath.Join(ctx.rootPath, p)
ctx.fs.EXPECT().Exists(outPath).
Return(false, nil)
ctx.fs.EXPECT().CreateFile(outPath, gomock.Any()).
Return(errors.New("error"))
})
}

func Test_Construct_WhenFailToOverwriteFile(t *testing.T) {
constructErrorTest(t, func(ctx *constructTestContext) {
p := "foo.go"
f, err := fs.NewFile(filepath.Join(ctx.scffPath, p))
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
ctx.fs.EXPECT().GetEntries(ctx.scffPath, true).
Return([]fs.Entry{f}, nil)
ctx.fs.EXPECT().ReadFile(f.Path()).
Return([]byte("package {{name}}"), nil)
outPath := filepath.Join(ctx.rootPath, p)
ctx.fs.EXPECT().Exists(outPath).
Return(true, nil)
ctx.fs.EXPECT().ReadFile(outPath).
Return([]byte("package foo"), nil)
ctx.fs.EXPECT().CreateFile(outPath, gomock.Any()).
Return(errors.New("error"))
})
}

0 comments on commit f5dbfa8

Please sign in to comment.