Skip to content

Commit

Permalink
Fix generating into same package
Browse files Browse the repository at this point in the history
  • Loading branch information
nkovacs committed Jul 19, 2019
1 parent f85ca4b commit 3f49db4
Show file tree
Hide file tree
Showing 20 changed files with 1,015 additions and 37 deletions.
25 changes: 19 additions & 6 deletions arguments/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func New(args []string, workingDir string, evaler Evaler, stater Stater) (*Parse
"Display this help",
)

testFlag := fs.Bool(
"test",
false,
"Append \"_test\" to pacakge name",
)

err := fs.Parse(args[1:])
if err != nil {
return nil, err
Expand All @@ -59,9 +65,9 @@ func New(args []string, workingDir string, evaler Evaler, stater Stater) (*Parse

packageMode := *packageFlag
result := &ParsedArguments{
PrintToStdOut: any(args, "-"),
PrintToStdOut: any(args, "-"),
GenerateInterfaceAndShimFromPackageDirectory: packageMode,
GenerateMode: *generateFlag,
GenerateMode: *generateFlag,
}
if *generateFlag {
return result, nil
Expand All @@ -75,6 +81,9 @@ func New(args []string, workingDir string, evaler Evaler, stater Stater) (*Parse
result.parseOutputPath(packageMode, workingDir, *outputPathFlag, fs.Args())
result.parseDestinationPackageName(packageMode, fs.Args())
result.parsePackagePath(packageMode, fs.Args())
if *testFlag {
result.TestPackage = true
}
return result, nil
}

Expand Down Expand Up @@ -154,11 +163,13 @@ func (a *ParsedArguments) parseOutputPath(packageMode bool, workingDir string, o

func (a *ParsedArguments) parseDestinationPackageName(packageMode bool, args []string) {
if packageMode {
// TODO: a.DestinationPackagePath
a.parsePackagePath(packageMode, args)
a.DestinationPackageName = path.Base(a.PackagePath) + "shim"
return
}

a.DestinationPackagePath = filepath.Dir(a.OutputPath)
a.DestinationPackageName = restrictToValidPackageName(filepath.Base(filepath.Dir(a.OutputPath)))
}

Expand All @@ -182,11 +193,13 @@ func (a *ParsedArguments) parsePackagePath(packageMode bool, args []string) {
type ParsedArguments struct {
GenerateInterfaceAndShimFromPackageDirectory bool

SourcePackageDir string // abs path to the dir containing the interface to fake
PackagePath string // package path to the package containing the interface to fake
OutputPath string // path to write the fake file to
SourcePackageDir string // abs path to the dir containing the interface to fake
PackagePath string // package path to the package containing the interface to fake
OutputPath string // path to write the fake file to
DestinationPackagePath string // path to destination package
TestPackage bool // append "_test" to package name

DestinationPackageName string // often the base-dir for OutputPath but must be a valid package name
DestinationPackageName string // often the base-dir for OutputPath but must be a valid package name DEPRECATED

InterfaceName string // the interface to counterfeit
FakeImplName string // the name of the struct implementing the given interface
Expand Down
6 changes: 5 additions & 1 deletion arguments/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package arguments
const usage = `
USAGE
counterfeiter
[-generate>] [-o <output-path>] [-p] [--fake-name <fake-name>]
[-generate>] [-o <output-path>] [-p] [--fake-name <fake-name>] [--test]
[<source-path>] <interface> [-]
ARGUMENTS
Expand Down Expand Up @@ -83,4 +83,8 @@ OPTIONS
example:
# writes "CoolThing" to ./mypackagefakes/cool_thing.go
counterfeiter --fake-name CoolThing ./mypackage MyInterface
--test
When generating into the same directory as the source interface,
append a "_test" suffix to the package name of the generated file.
`
9 changes: 9 additions & 0 deletions fixtures/same/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package same // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/same"

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o same_fake.go . SomeInterface
type SomeInterface interface {
DoThings(string, uint64) (int, error)
DoNothing()
DoASlice([]byte)
DoAnArray([4]byte)
}
9 changes: 9 additions & 0 deletions fixtures/same/notexported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package same // import "github.com/maxbrunsfeld/counterfeiter/v6/fixtures/same"

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o notexported_fake.go . someNotExportedInterface
type someNotExportedInterface interface {
DoThings(string, uint64) (int, error)
DoNothing()
DoASlice([]byte)
DoAnArray([4]byte)
}
225 changes: 225 additions & 0 deletions fixtures/same/notexported_fake.go

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

Loading

0 comments on commit 3f49db4

Please sign in to comment.