Skip to content

Commit

Permalink
Merge pull request #94 from maxbrunsfeld/support-go-modules
Browse files Browse the repository at this point in the history
support go modules
  • Loading branch information
joefitzgerald authored Oct 12, 2018
2 parents 097f6b6 + 605aa2a commit d68f4b0
Show file tree
Hide file tree
Showing 84 changed files with 2,471 additions and 3,930 deletions.
15 changes: 10 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ fixtures/symlinked_fixturesfakes/
locator/locatorfakes/
terminal/terminalfakes/
fixtures/dup_packages/a/afakes/
fixtures/dup_packages/a/v1/v1fakes/
fixtures/dup_packages/b/v1/v1fakes/
fixtures/dup_packages/a/foo/foofakes/
fixtures/dup_packages/b/foo/foofakes/
fixtures/dup_packages/dup_packagesfakes/
fixtures/dup_packages/v1/v1fakes/
fixtures/dup_packages/v1/v1fakes/
fixtures/dup_packages/foo/foofakes/
fixtures/dup_packages/foo/foofakes/
fixtures/hyphenated_package_same_name/some_package/some_packagefakes/
fixtures/alias_import_name/test/testfakes/
fixtures/vendored
fixtures/vendored/vendoredfakes
fixtures/vendored/bar/barfakes
fixtures/vendored/baz/bazfakes
fixtures/packagegen/packagegenfakes
.idea
integration/testdata/output
/counterfeiter
.envrc
22 changes: 15 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
language: go
go:
- "1.10"
- 1.11
- tip

matrix:
include:
- go: "1.10.x"
script: scripts/deps.sh && scripts/ci.sh
- go: "1.11.x"
script: scripts/ci.sh
- go: "tip"
script: scripts/ci.sh

env:
- GO111MODULE=on

install: true

sudo : false # uses new containerized infrastructure
go_import_path: github.com/maxbrunsfeld/counterfeiter
install: scripts/deps.sh
script: scripts/test.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Wrote `FakeMySpecialInterface` to `path/to/foo/foofakes/fake_my_special_interfac

### Running The Tests For `counterfeiter`

If you want to run the tests for `counterfeiter` (perhaps, because you want to contribute a PR), all you have to do is run `scripts/test.sh`.
If you want to run the tests for `counterfeiter` (perhaps, because you want to contribute a PR), all you have to do is run `scripts/ci.sh`.

### Contributions

Expand Down
22 changes: 11 additions & 11 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
platform: x64

clone_folder: c:\gopath\src\github.com\maxbrunsfeld\counterfeiter
shallow_clone: true
clone_depth: 10
clone_folder: c:\projects\counterfeiter

environment:
GOPATH: c:\gopath

install:
test_script:
- cd c:\projects\counterfeiter
- set PATH=c:\gopath\bin;c:\go\bin;C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin;%PATH%
- echo %PATH%
- echo %GOPATH%
- set PATH=%GOPATH%\bin;c:\go\bin;C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin;%PATH%
- go version
- go env

test_script:
- echo %PATH%
- echo %GOPATH%
- set PATH=%GOPATH%\bin;c:\go\bin;C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin;%PATH%
- ps: scripts/ci_build_windows.ps1
- go install .
- copy scripts\counterfeiter.bat c:\gopath\bin
- go generate ./...
- go build -v ./...
- go test -v -race ./...

build: off
deploy: off
61 changes: 23 additions & 38 deletions arguments/parser.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package arguments

import (
"go/build"
"log"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"unicode"

"github.com/maxbrunsfeld/counterfeiter/terminal"
)

//go:generate counterfeiter . ArgumentParser
Expand All @@ -21,10 +20,8 @@ func NewArgumentParser(
currentWorkingDir CurrentWorkingDir,
symlinkEvaler SymlinkEvaler,
fileStatReader FileStatReader,
ui terminal.UI,
) ArgumentParser {
return &argumentParser{
ui: ui,
failHandler: failHandler,
currentWorkingDir: currentWorkingDir,
symlinkEvaler: symlinkEvaler,
Expand All @@ -45,7 +42,7 @@ func (argParser *argumentParser) parseInterfaceArgs(args ...string) ParsedArgume
var outputPathFlagValue string
var rootDestinationDir string
var sourcePackageDir string
var importPath string
var packagePath string

if outputPathFlag != nil {
outputPathFlagValue = *outputPathFlag
Expand All @@ -59,7 +56,7 @@ func (argParser *argumentParser) parseInterfaceArgs(args ...string) ParsedArgume
fullyQualifiedInterface := strings.Split(args[0], ".")
interfaceName = fullyQualifiedInterface[len(fullyQualifiedInterface)-1]
rootDestinationDir = argParser.currentWorkingDir()
importPath = strings.Join(fullyQualifiedInterface[:len(fullyQualifiedInterface)-1], ".")
packagePath = strings.Join(fullyQualifiedInterface[:len(fullyQualifiedInterface)-1], ".")
}

fakeImplName := getFakeName(interfaceName, *fakeNameFlag)
Expand All @@ -71,12 +68,19 @@ func (argParser *argumentParser) parseInterfaceArgs(args ...string) ParsedArgume
)

packageName := restrictToValidPackageName(filepath.Base(filepath.Dir(outputPath)))
if packagePath == "" {
packagePath = sourcePackageDir
}
if strings.HasPrefix(packagePath, build.Default.GOPATH) {
packagePath = strings.Replace(packagePath, build.Default.GOPATH+"/src/", "", -1)
}

log.Printf("Parsed Arguments:\nInterface Name: %s\nPackage Path: %s\nDestination Package Name: %s", interfaceName, packagePath, packageName)
return ParsedArguments{
GenerateInterfaceAndShimFromPackageDirectory: false,
SourcePackageDir: sourcePackageDir,
OutputPath: outputPath,
ImportPath: importPath,
SourcePackageDir: sourcePackageDir,
OutputPath: outputPath,
PackagePath: packagePath,

InterfaceName: interfaceName,
DestinationPackageName: packageName,
Expand All @@ -87,9 +91,8 @@ func (argParser *argumentParser) parseInterfaceArgs(args ...string) ParsedArgume
}

func (argParser *argumentParser) parsePackageArgs(args ...string) ParsedArguments {
dir := argParser.getPackageDir(args[0])

packageName := path.Base(dir) + "shim"
packagePath := args[0]
packageName := path.Base(packagePath) + "shim"

var outputPath string
if *outputPathFlag != "" {
Expand All @@ -99,19 +102,19 @@ func (argParser *argumentParser) parsePackageArgs(args ...string) ParsedArgument
outputPath = path.Join(argParser.currentWorkingDir(), packageName)
}

log.Printf("Parsed Arguments:\nPackage Name: %s\nDestination Package Name: %s", packagePath, packageName)
return ParsedArguments{
GenerateInterfaceAndShimFromPackageDirectory: true,
SourcePackageDir: dir,
OutputPath: outputPath,

SourcePackageDir: packagePath,
OutputPath: outputPath,
PackagePath: packagePath,
DestinationPackageName: packageName,

PrintToStdOut: any(args, "-"),
FakeImplName: strings.ToUpper(path.Base(packagePath))[:1] + path.Base(packagePath)[1:],
PrintToStdOut: any(args, "-"),
}
}

type argumentParser struct {
ui terminal.UI
failHandler FailHandler
currentWorkingDir CurrentWorkingDir
symlinkEvaler SymlinkEvaler
Expand All @@ -122,7 +125,7 @@ type ParsedArguments struct {
GenerateInterfaceAndShimFromPackageDirectory bool

SourcePackageDir string // abs path to the dir containing the interface to fake
ImportPath string // import path to the package 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

DestinationPackageName string // often the base-dir for OutputPath but must be a valid package name
Expand Down Expand Up @@ -170,24 +173,6 @@ func packageNameForPath(pathToPackage string) string {
return packageName + "fakes"
}

func (argParser *argumentParser) getPackageDir(arg string) string {
if filepath.IsAbs(arg) {
return arg
}

pathToCheck := path.Join(runtime.GOROOT(), "src", arg)

stat, err := argParser.fileStatReader(pathToCheck)
if err != nil {
argParser.failHandler("No such file or directory '%s'", arg)
}
if !stat.IsDir() {
argParser.failHandler("No such file or directory '%s'", arg)
}

return pathToCheck
}

func (argParser *argumentParser) getSourceDir(path string) string {
if !filepath.IsAbs(path) {
path = filepath.Join(argParser.currentWorkingDir(), path)
Expand Down
16 changes: 4 additions & 12 deletions arguments/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import (
"os"
"path"
"path/filepath"
"runtime"
"time"

"testing"

"github.com/maxbrunsfeld/counterfeiter/terminal/terminalfakes"

. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
Expand All @@ -33,8 +30,6 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
var symlinkEvaler SymlinkEvaler
var fileStatReader FileStatReader

var ui *terminalfakes.FakeUI

var failWasCalled bool
var failWasCalledWithMessage string
var failWasCalledWithArgs []interface{}
Expand All @@ -45,7 +40,6 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
cwd,
symlinkEvaler,
fileStatReader,
ui,
)
parsedArgs = subject.ParseArguments(args...)
}
Expand All @@ -65,8 +59,6 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
return "/home/test-user/workspace"
}

ui = new(terminalfakes.FakeUI)

symlinkEvaler = func(input string) (string, error) {
return input, nil
}
Expand All @@ -84,12 +76,12 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {

it("doesn't parse extraneous arguments", func() {
Expect(parsedArgs.InterfaceName).To(Equal(""))
Expect(parsedArgs.FakeImplName).To(Equal(""))
Expect(parsedArgs.FakeImplName).To(Equal("Os"))
})

when("given a stdlib package", func() {
it("sets arguments as expected", func() {
Expect(parsedArgs.SourcePackageDir).To(Equal(path.Join(runtime.GOROOT(), "src/os")))
Expect(parsedArgs.SourcePackageDir).To(Equal("os"))
Expect(parsedArgs.OutputPath).To(Equal(path.Join(cwd(), "osshim")))
Expect(parsedArgs.DestinationPackageName).To(Equal("osshim"))
})
Expand All @@ -113,7 +105,7 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
})

it("provides a path for the interface source", func() {
Expect(parsedArgs.ImportPath).To(Equal("someonesinterfaces"))
Expect(parsedArgs.PackagePath).To(Equal("someonesinterfaces"))
})

it("treats the last segment as the interface to counterfeit", func() {
Expand Down Expand Up @@ -147,7 +139,7 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
})

it("provides a path for the interface source", func() {
Expect(parsedArgs.ImportPath).To(Equal("io"))
Expect(parsedArgs.PackagePath).To(Equal("io"))
})

it("treats the last segment as the interface to counterfeit", func() {
Expand Down
7 changes: 0 additions & 7 deletions arguments/parser_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"testing"

"github.com/maxbrunsfeld/counterfeiter/terminal/terminalfakes"

. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
Expand All @@ -30,8 +28,6 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
var symlinkEvaler SymlinkEvaler
var fileStatReader FileStatReader

var ui *terminalfakes.FakeUI

var failWasCalled bool
var failWasCalledWithMessage string
var failWasCalledWithArgs []interface{}
Expand All @@ -42,7 +38,6 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
cwd,
symlinkEvaler,
fileStatReader,
ui,
)
parsedArgs = subject.ParseArguments(args...)
}
Expand All @@ -63,8 +58,6 @@ func testParsingArguments(t *testing.T, when spec.G, it spec.S) {
return "C:\\Users\\test-user\\workspace"
}

ui = new(terminalfakes.FakeUI)

symlinkEvaler = func(input string) (string, error) {
return input, nil
}
Expand Down
Loading

0 comments on commit d68f4b0

Please sign in to comment.