Skip to content

Commit

Permalink
Feat: create deletemock (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho committed Mar 12, 2021
1 parent a359138 commit 01390cf
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ playground-check:
../gomockhandler -config=gomockhandler.json check ;\
rm ../gomockhandler;

# test on playground
.PHONY:playground-delete
playground-delete:
go build . ;\
cd playground ;\
../gomockhandler -config=gomockhandler.json -destination=./mock/user.go deletemock ;\
rm ../gomockhandler;


# clean playground
.PHONY:clean
clean:
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ You can use all options of mockgen to add a new mock.

See [golang/mock#running-mockgen](https://github.com/golang/mock#running-mockgen) for more information.

Example(Source mode):
Source mode:
```
gomockhandler -project_root=/path/to/project -source=foo.go [other options]
```

Example(Reflect mode):
Reflect mode:
```
gomockhandler -project_root=/path/to/project [options] database/sql/driver Conn,Driver
```
Expand All @@ -112,13 +112,17 @@ replace from `mockgen` to `gomockhandler -project_root=/path/to/project_root`, a

After generating the config, your `go:generate` comments are no longer needed. You've been released from a slow-mockgen with `go generate`!

Let's delete `go:generate` comments.
Let's delete all `go:generate` comments for mockgen in your project.

### [WIP] Delete mocks
### Delete mocks

Currently, if you want to delete the mock, you have to modify the config manually...
You can remove the mocks to be generated from the config.

I'm working on developing it to be able to edit/delete it from the CLI.
You don't have to specify config option, when you are in project root.

```
gomockhandler -config=gomockhandler.json -destination=./mock/user.go deletemock
```

## generate mock

Expand Down
2 changes: 2 additions & 0 deletions gomockhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func main() {
realmain = rm.Mockgen
case "check":
realmain = rm.Check
case "deletemock":
realmain = rm.DeleteMock
default:
rm.MockgenRunner = prepareMockgenRunner()
realmain = rm.GenerateConfig
Expand Down
4 changes: 4 additions & 0 deletions model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (c *Config) PutMock(mock Mock) {
c.Mocks[mock.Destination] = &mock
}

func (c *Config) DeleteMock(dest string) {
delete(c.Mocks, dest)
}

var (
ErrNotFound = errors.New("config is not found in config")
)
Expand Down
22 changes: 22 additions & 0 deletions realmain/delete_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package realmain

import (
"log"
"path/filepath"
)

// DeleteMock delete a mock from config
func (r Runner) DeleteMock() {
configPath := r.Args.ConfigPath

chunk, err := r.ChunkRepo.Get(configPath)
if err != nil {
log.Fatalf("failed to get config: %v", err)
}

chunk.DeleteMock(filepath.Clean(r.Args.Destination))
if err := r.ChunkRepo.Put(chunk, configPath); err != nil {
log.Fatalf("failed to put config: %v", err)
}
return
}
4 changes: 2 additions & 2 deletions realmain/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (r Runner) GenerateConfig() {
if err != nil {
log.Fatalf("failed to get config: %v", err)
}
destinationPathInPro := util.PathInProject(r.Args.ProjectRoot, currentPath+"/"+r.Args.Destination)
destinationPathInPro := util.PathInProject(absRoot, currentPath+"/"+r.Args.Destination)
r.MockgenRunner.SetDestination(destinationPathInPro)

if r.Args.Source != "" {
sourcePathInPro := util.PathInProject(r.Args.ProjectRoot, currentPath+"/"+r.Args.Source)
sourcePathInPro := util.PathInProject(absRoot, currentPath+"/"+r.Args.Source)
r.MockgenRunner.SetSource(sourcePathInPro)
}
// store into config
Expand Down

0 comments on commit 01390cf

Please sign in to comment.