-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gob mode to support custom package loading techniques in place of…
… `--exec_only` (#214) Change #207 replaced reflect mode with package mode. In doing so, we no longer had to create separate reflect programs that would encode the resulting package into a gob for the rest of mockgen to read from. Because of that, we removed the `--exec_only` flag. However, this flag was actually used to pass in programs that created gob encodings in other ways, allowing customization of the package loading and encoding portion of mockgen. As an alternative, this adds a new flag to mockgen `--model_gob`, which allows mockgen users to pass a path to a gob encoding of a `model.Package`, so that custom programs for encoding package information can still be used in conjunction with mockgen.
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/gob" | ||
"os" | ||
|
||
"go.uber.org/mock/mockgen/model" | ||
) | ||
|
||
func gobMode(path string) (*model.Package, error) { | ||
in, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer in.Close() | ||
var pkg model.Package | ||
if err := gob.NewDecoder(in).Decode(&pkg); err != nil { | ||
return nil, err | ||
} | ||
return &pkg, nil | ||
} |
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/gob" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGobMode(t *testing.T) { | ||
|
||
// Encode a package to a temporary gob. | ||
parser := packageModeParser{} | ||
want, err := parser.parsePackage( | ||
"go.uber.org/mock/mockgen/internal/tests/package_mode" /* package name */, | ||
[]string{ "Human", "Earth" } /* ifaces */, | ||
) | ||
path := filepath.Join(t.TempDir(), "model.gob") | ||
outfile, err := os.Create(path) | ||
require.NoError(t, err) | ||
require.NoError(t, gob.NewEncoder(outfile).Encode(want)) | ||
outfile.Close() | ||
|
||
// Ensure gobMode loads it correctly. | ||
got, err := gobMode(path) | ||
require.NoError(t, err) | ||
assert.Equal(t, want, got) | ||
} |
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