| 2228 | } |
| 2229 | |
| 2230 | func TestInject(t *testing.T) { |
| 2231 | is := is.New(t) |
| 2232 | ctx := context.Background() |
| 2233 | dir := t.TempDir() |
| 2234 | td := testdir.New(dir) |
| 2235 | td.Files["log/log.go"] = ` |
| 2236 | package log |
| 2237 | func New() *Logger { return &Logger{} } |
| 2238 | type Logger struct {} |
| 2239 | func (l *Logger) Info(msg string) {} |
| 2240 | ` |
| 2241 | td.Files["session/session.go"] = ` |
| 2242 | package session |
| 2243 | import "errors" |
| 2244 | import "net/http" |
| 2245 | import "app.com/log" |
| 2246 | func New(log *log.Logger, w http.ResponseWriter, r *http.Request) *Session { |
| 2247 | return &Session{log, w, r} |
| 2248 | } |
| 2249 | type Session struct { |
| 2250 | log *log.Logger |
| 2251 | w http.ResponseWriter |
| 2252 | r *http.Request |
| 2253 | } |
| 2254 | func (s *Session) Set(key, value string) { |
| 2255 | s.log.Info("setting session") |
| 2256 | http.SetCookie(s.w, &http.Cookie{Name: key, Value: value }) |
| 2257 | } |
| 2258 | func (s *Session) Clear() error { |
| 2259 | return errors.New("session: unable to clear") |
| 2260 | } |
| 2261 | ` |
| 2262 | td.Files["db/db.go"] = ` |
| 2263 | package db |
| 2264 | import "context" |
| 2265 | var loaded = 0 |
| 2266 | func Load(ctx context.Context) (*Client, error) { |
| 2267 | loaded++ |
| 2268 | return &Client{}, nil |
| 2269 | } |
| 2270 | type Client struct{} |
| 2271 | func (c *Client) Loaded() int { |
| 2272 | return loaded |
| 2273 | } |
| 2274 | ` |
| 2275 | td.Files["controller/controller.go"] = ` |
| 2276 | package controller |
| 2277 | import "app.com/session" |
| 2278 | import "app.com/db" |
| 2279 | type Controller struct { |
| 2280 | Session *session.Session |
| 2281 | DB *db.Client |
| 2282 | } |
| 2283 | func (c *Controller) Create() error { |
| 2284 | c.Session.Set("sessionid", "some-key") |
| 2285 | return nil |
| 2286 | } |
| 2287 | func (c *Controller) Delete() error { |