Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if my package imports another package of the same name, the fake imports both #8

Closed
vito opened this issue Sep 12, 2014 · 5 comments
Closed
Labels

Comments

@vito
Copy link
Collaborator

vito commented Sep 12, 2014

with:

package metrics

import (
    "os"
    "time"

    "github.com/cloudfoundry-incubator/executor/api"
    "github.com/cloudfoundry-incubator/executor/registry"
    "github.com/cloudfoundry/dropsonde/autowire/metrics"
)

type Source interface {
    CurrentCapacity() registry.Capacity
    TotalCapacity() registry.Capacity
    GetAllContainers() []api.Container
}

type Reporter struct {
    Source   Source
    Interval time.Duration
}

func (reporter *Reporter) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
    close(ready)

    for {
        metrics.SendValue("capacity.memory", 128, "Metric")
    }
}

running:

counterfeiter . Source

results in:

// This file was generated by counterfeiter
package fakes

import (
    "sync"

    "github.com/cloudfoundry-incubator/executor/api"
    "github.com/cloudfoundry-incubator/executor/metrics"
    "github.com/cloudfoundry-incubator/executor/registry"
    "github.com/cloudfoundry/dropsonde/autowire/metrics"
)

type FakeSource struct {
    CurrentCapacityStub        func() registry.Capacity
    currentCapacityMutex       sync.RWMutex
    currentCapacityArgsForCall []struct{}
    currentCapacityReturns struct {
        result1 registry.Capacity
    }
    TotalCapacityStub        func() registry.Capacity
    totalCapacityMutex       sync.RWMutex
    totalCapacityArgsForCall []struct{}
    totalCapacityReturns struct {
        result1 registry.Capacity
    }
    GetAllContainersStub        func() []api.Container
    getAllContainersMutex       sync.RWMutex
    getAllContainersArgsForCall []struct{}
    getAllContainersReturns struct {
        result1 []api.Container
    }
}

func (fake *FakeSource) CurrentCapacity() registry.Capacity {
    fake.currentCapacityMutex.Lock()
    defer fake.currentCapacityMutex.Unlock()
    fake.currentCapacityArgsForCall = append(fake.currentCapacityArgsForCall, struct{}{})
    if fake.CurrentCapacityStub != nil {
        return fake.CurrentCapacityStub()
    } else {
        return fake.currentCapacityReturns.result1
    }
}

func (fake *FakeSource) CurrentCapacityCallCount() int {
    fake.currentCapacityMutex.RLock()
    defer fake.currentCapacityMutex.RUnlock()
    return len(fake.currentCapacityArgsForCall)
}

func (fake *FakeSource) CurrentCapacityReturns(result1 registry.Capacity) {
    fake.currentCapacityReturns = struct {
        result1 registry.Capacity
    }{result1}
}

func (fake *FakeSource) TotalCapacity() registry.Capacity {
    fake.totalCapacityMutex.Lock()
    defer fake.totalCapacityMutex.Unlock()
    fake.totalCapacityArgsForCall = append(fake.totalCapacityArgsForCall, struct{}{})
    if fake.TotalCapacityStub != nil {
        return fake.TotalCapacityStub()
    } else {
        return fake.totalCapacityReturns.result1
    }
}

func (fake *FakeSource) TotalCapacityCallCount() int {
    fake.totalCapacityMutex.RLock()
    defer fake.totalCapacityMutex.RUnlock()
    return len(fake.totalCapacityArgsForCall)
}

func (fake *FakeSource) TotalCapacityReturns(result1 registry.Capacity) {
    fake.totalCapacityReturns = struct {
        result1 registry.Capacity
    }{result1}
}

func (fake *FakeSource) GetAllContainers() []api.Container {
    fake.getAllContainersMutex.Lock()
    defer fake.getAllContainersMutex.Unlock()
    fake.getAllContainersArgsForCall = append(fake.getAllContainersArgsForCall, struct{}{})
    if fake.GetAllContainersStub != nil {
        return fake.GetAllContainersStub()
    } else {
        return fake.getAllContainersReturns.result1
    }
}

func (fake *FakeSource) GetAllContainersCallCount() int {
    fake.getAllContainersMutex.RLock()
    defer fake.getAllContainersMutex.RUnlock()
    return len(fake.getAllContainersArgsForCall)
}

func (fake *FakeSource) GetAllContainersReturns(result1 []api.Container) {
    fake.getAllContainersReturns = struct {
        result1 []api.Container
    }{result1}
}

var _ metrics.Source = new(FakeSource)

it should probably not

@cryptix
Copy link

cryptix commented Feb 20, 2015

👍 also stumbled over this.

@ljfranklin
Copy link

We also ran into this. Any ideas on how we might fix this? We could change the package or the imports, but we'd rather not do either, as the package we're counterfeiting is purposely named the same as the package it imports as we are shadowing. e.g.

package sql

import (
    "database/sql"
)

type DB interface {
    Exec(query string, args ...interface{}) (sql.Result, error)
    Query(query string, args ...interface{}) (Rows, error)
}

@devoid
Copy link

devoid commented Sep 22, 2015

Running into this issue as well. A simple solution would be to allow counterfeiter to generate a source file within the same package as the counterfeited interface. Is there any particular reason that the "${pkgname}/fakes" package was chosen?

@tjarratt
Copy link
Collaborator

tjarratt commented Apr 8, 2016

@devoid mypackagename/fakes was chosen because it's separate from your implementation and it's fairly clear what is inside there. It's a good idea to keep your test doubles and test helpers separate from your actual code. Check out net/http and net/http/httptest in the standard library for some inspiration there.

That said, I think that this should be fixed. Any takers? Would love to help anyone that wants to work on a PR for this.

@joefitzgerald
Copy link
Collaborator

Is this still an issue in v6.0.0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants