|
15 | 15 | package containertest
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "log" |
| 18 | + "github.com/abcxyz/containertest" |
19 | 19 | )
|
20 | 20 |
|
21 | 21 | // This file implements the "functional options" pattern.
|
22 | 22 |
|
23 |
| -type config struct { |
24 |
| - killAfterSec int // This is in integer seconds because that's what Docker takes. |
25 |
| - service Service |
26 |
| - progressLogger TestLogger |
27 |
| -} |
28 |
| - |
29 |
| -// TestLogger allows the caller to optionally provide a custom logger for printing status updates about |
30 |
| -// service startup progress. The default is to use the go "log" package. |
31 |
| -// testing.TB satisfies TestLogger, and is usually what you want to put here. |
32 |
| -type TestLogger interface { |
33 |
| - Log(args ...any) |
34 |
| - Logf(format string, args ...any) |
35 |
| -} |
36 |
| - |
37 |
| -func makeDefaultConfig(service Service) *config { |
38 |
| - return &config{ |
39 |
| - killAfterSec: 10 * 60, |
40 |
| - service: service, |
41 |
| - progressLogger: &stdlibLogger{}, |
42 |
| - } |
43 |
| -} |
44 |
| - |
45 |
| -func buildConfig(service Service, opts ...Option) *config { |
46 |
| - config := makeDefaultConfig(service) |
47 |
| - for _, opt := range opts { |
48 |
| - config = opt(config) |
49 |
| - } |
50 |
| - return config |
51 |
| -} |
52 |
| - |
53 |
| -// Option sets a configuration option for this package. Users should not implement these functions, |
54 |
| -// they should use one of the With* functions. |
55 |
| -type Option func(*config) *config |
56 |
| - |
57 |
| -// WithKillAfterSeconds is an option that overrides the default time period after which the docker |
58 |
| -// container will kill itself. |
| 23 | +// TestLogger allows the caller to optionally provide a custom logger for |
| 24 | +// printing status updates about service startup progress. The default is to use |
| 25 | +// the go "log" package. testing.TB satisfies TestLogger, and is usually what |
| 26 | +// you want to put here. |
59 | 27 | //
|
60 |
| -// Containers might bypass the normal clean shutdown logic if the test terminates abnormally, such |
61 |
| -// as when ctrl-C is pressed during a test. Therefore we instruct the container to kill itself after |
62 |
| -// a while. The duration must be longer than longest test that uses the container. There's no harm in |
63 |
| -// leaving lots of extra time. |
64 |
| -func WithKillAfterSeconds(seconds int) Option { |
65 |
| - return func(c *config) *config { |
66 |
| - c.killAfterSec = seconds |
67 |
| - return c |
68 |
| - } |
69 |
| -} |
| 28 | +// Deprecated: This has moved to a new package. Use |
| 29 | +// [github.com/abcxyz/containertest.TestLogger] instead. |
| 30 | +type TestLogger = containertest.TestLogger |
70 | 31 |
|
71 |
| -// WithLogger overrides the default logger. This logger will receive messages about service startup |
72 |
| -// progress. The default is to use the go "log" package. |
73 |
| -func WithLogger(l TestLogger) Option { |
74 |
| - return func(c *config) *config { |
75 |
| - c.progressLogger = l |
76 |
| - return c |
77 |
| - } |
78 |
| -} |
79 |
| - |
80 |
| -// stdlibLogger is the default implementation of the TestLogger interface that calls log.Logf. |
81 |
| -type stdlibLogger struct{} |
| 32 | +// Option sets a configuration option for this package. Users should not |
| 33 | +// implement these functions, they should use one of the With* functions. |
| 34 | +// |
| 35 | +// Deprecated: This has moved to a new package. Use |
| 36 | +// [github.com/abcxyz/containertest.Option] instead. |
| 37 | +type Option = containertest.Option |
82 | 38 |
|
83 |
| -func (s *stdlibLogger) Logf(fmtStr string, args ...any) { |
84 |
| - log.Printf(fmtStr, args...) |
85 |
| -} |
| 39 | +// WithKillAfterSeconds is an option that overrides the default time period |
| 40 | +// after which the docker container will kill itself. |
| 41 | +// |
| 42 | +// Containers might bypass the normal clean shutdown logic if the test |
| 43 | +// terminates abnormally, such as when ctrl-C is pressed during a test. |
| 44 | +// Therefore we instruct the container to kill itself after a while. The |
| 45 | +// duration must be longer than longest test that uses the container. There's no |
| 46 | +// harm in leaving lots of extra time. |
| 47 | +// |
| 48 | +// Deprecated: This has moved to a new package. Use |
| 49 | +// [github.com/abcxyz/containertest.WithKillAfterSeconds] instead. |
| 50 | +var WithKillAfterSeconds = containertest.WithKillAfterSeconds |
86 | 51 |
|
87 |
| -func (s *stdlibLogger) Log(args ...any) { |
88 |
| - log.Print(args...) |
89 |
| -} |
| 52 | +// WithLogger overrides the default logger. This logger will receive messages |
| 53 | +// about service startup progress. The default is to use the go "log" package. |
| 54 | +// |
| 55 | +// Deprecated: This has moved to a new package. Use |
| 56 | +// [github.com/abcxyz/containertest.WithLogger] instead. |
| 57 | +var WithLogger = containertest.WithLogger |
0 commit comments