Skip to content

Commit

Permalink
fixed to use gonch
Browse files Browse the repository at this point in the history
  • Loading branch information
deadcheat committed May 29, 2018
1 parent edbcf16 commit 6fe63e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
34 changes: 14 additions & 20 deletions generator/usecase/file/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@ package file

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/deadcheat/gonch"

"github.com/deadcheat/goblet/generator/mock"
"github.com/golang/mock/gomock"
)

func TestAddFile_ForSingleFiles(t *testing.T) {
// Prepare dir and file
contentStr := "hello world!"
dir, path, _ := createTempDirAndFile("temp.txt", contentStr)
defer os.RemoveAll(dir) // clean up
content := []byte(contentStr)
d := gonch.New("", "tmpdir")
defer d.Close() // clean up

// Prepare mock
c := gomock.NewController(t)
defer c.Finish()

m := mock.NewMockRegexpRepository(c)
// successfull pattern
filenameSuccess := "success.txt"
path := filepath.Join(d.Dir(), filenameSuccess)
d.AddFile("success", filenameSuccess, content, 0666)
m.EXPECT().MatchAny(path).Return(true)
// should not be contained
wrongPath := filepath.Join(dir, "doesnotmatch.txt")
ioutil.WriteFile(wrongPath, []byte("test"), 0666)
fileWrongPath := "doesnotmatch.txt"
wrongPath := filepath.Join(d.Dir(), fileWrongPath)
d.AddFile("wrong path file", fileWrongPath, content, 0666)
m.EXPECT().MatchAny(wrongPath).Return(false)
// file can not be opened
closedPath := filepath.Join(dir, "closed.txt")
d.AddFile("wrong path file", wrongPath, content, 0666)
closedPath := filepath.Join(d.Dir(), "closed.txt")
ioutil.WriteFile(closedPath, []byte(""), 0000)
m.EXPECT().MatchAny(closedPath).Return(true)

Expand All @@ -46,7 +53,7 @@ func TestAddFile_ForSingleFiles(t *testing.T) {
t.Error("addFile should return ErrFileIsNotMatchExpression but returned ", err)
}
// filename does not exist
brokenPath := filepath.Join(dir, "/this/is/match/but/broken.txt")
brokenPath := filepath.Join(d.Dir(), "/this/is/match/but/broken.txt")
err = u.addFile(brokenPath)
if err == nil {
t.Error("addFile should return some error")
Expand All @@ -56,16 +63,3 @@ func TestAddFile_ForSingleFiles(t *testing.T) {
t.Error("addFile should return some error")
}
}

func createTempDirAndFile(fileName, content string) (dir, path string, err error) {
dir, err = ioutil.TempDir("", "tmpdir")
if err != nil {
return
}

path = filepath.Join(dir, fileName)
if err = ioutil.WriteFile(path, []byte(content), 0666); err != nil {
return
}
return
}
22 changes: 12 additions & 10 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ import:
version: ^1.1.1
subpackages:
- gomock
- package: github.com/deadcheat/gonch
version: ^0.1.0

0 comments on commit 6fe63e5

Please sign in to comment.