|
| 1 | +# Go-Specific Review Guidelines |
| 2 | + |
| 3 | +These guidelines supplement the general [REVIEW.md](REVIEW.md) with |
| 4 | +Go-specific expectations. |
| 5 | + |
| 6 | +## Separating Parsing from I/O |
| 7 | + |
| 8 | +Have the parser accept a string or `io.Reader`, then have a separate function |
| 9 | +that opens the file and calls the parser: |
| 10 | + |
| 11 | +```go |
| 12 | +// ✅ Good: parser is a pure function, easy to unit test |
| 13 | +func parseConfig(data string) (*Config, error) { ... } |
| 14 | + |
| 15 | +func loadConfig(path string) (*Config, error) { |
| 16 | + data, err := os.ReadFile(path) |
| 17 | + if err != nil { |
| 18 | + return nil, err |
| 19 | + } |
| 20 | + return parseConfig(string(data)) |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## Don't Ignore (Swallow) Errors |
| 25 | + |
| 26 | +Avoid discarding errors with the blank identifier. Most errors should be |
| 27 | +returned to the caller. If not, at least log the error: |
| 28 | + |
| 29 | +```go |
| 30 | +// ❌ Avoid: error is silently swallowed |
| 31 | +_ = doSomething() |
| 32 | + |
| 33 | +// ✅ Good: propagate |
| 34 | +if err := doSomething(); err != nil { |
| 35 | + return err |
| 36 | +} |
| 37 | + |
| 38 | +// ✅ OK if the error is truly ignorable: log it |
| 39 | +if err := doSomething(); err != nil { |
| 40 | + log.Debug("ignoring error", "err", err) |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Gomega and Test Assertions |
| 45 | + |
| 46 | +We use [gomega](https://github.com/onsi/gomega) for test assertions. Follow |
| 47 | +these conventions: |
| 48 | + |
| 49 | +### Use `g.Eventually` for Polling |
| 50 | + |
| 51 | +Gomega's `Eventually` handles polling, timeouts, and failure reporting. |
| 52 | + |
| 53 | +### Return `(T, error)` from `Eventually` Callbacks |
| 54 | + |
| 55 | +Return the specific field you care about and let gomega matchers describe the |
| 56 | +expectation declaratively. This produces better failure messages because |
| 57 | +gomega can show what the value actually was vs. what was expected. |
| 58 | + |
| 59 | +```go |
| 60 | +// ✅ Good: return the field, match with gomega |
| 61 | +g.Eventually(func() ([]metav1.Condition, error) { |
| 62 | + var p bootcv1alpha1.BootcNodePool |
| 63 | + err := k8sClient.Get(ctx, client.ObjectKey{Name: name}, &p) |
| 64 | + return p.Status.Conditions, err |
| 65 | +}).Should(ContainElement(And( |
| 66 | + HaveField("Type", bootcv1alpha1.PoolDegraded), |
| 67 | + HaveField("Status", metav1.ConditionTrue), |
| 68 | + HaveField("Reason", bootcv1alpha1.PoolNodeDegraded), |
| 69 | +))) |
| 70 | + |
| 71 | +// ❌ Avoid: assertions inside the callback with Succeed() |
| 72 | +g.Eventually(func(g Gomega) { |
| 73 | + var p bootcv1alpha1.BootcNodePool |
| 74 | + g.Expect(k8sClient.Get(ctx, ...)).To(Succeed()) |
| 75 | + cond := apimeta.FindStatusCondition(p.Status.Conditions, ...) |
| 76 | + g.Expect(cond).NotTo(BeNil()) |
| 77 | + g.Expect(cond.Status).To(Equal(...)) |
| 78 | +}).Should(Succeed()) |
| 79 | +``` |
| 80 | + |
| 81 | +### Return the Narrowest Type |
| 82 | + |
| 83 | +Extract exactly the field you want to assert on — labels, conditions, |
| 84 | +ownerReference — rather than returning the whole object or a `bool`: |
| 85 | + |
| 86 | +```go |
| 87 | +// Labels |
| 88 | +g.Eventually(func() (map[string]string, error) { |
| 89 | + var n corev1.Node |
| 90 | + err := k8sClient.Get(ctx, client.ObjectKey{Name: name}, &n) |
| 91 | + return n.Labels, err |
| 92 | +}).Should(HaveKey(bootcv1alpha1.LabelManaged)) |
| 93 | + |
| 94 | +// OwnerReference |
| 95 | +g.Eventually(func() (*metav1.OwnerReference, error) { |
| 96 | + var bn bootcv1alpha1.BootcNode |
| 97 | + err := k8sClient.Get(ctx, client.ObjectKey{Name: name}, &bn) |
| 98 | + return metav1.GetControllerOf(&bn), err |
| 99 | +}).Should(And(Not(BeNil()), HaveField("Name", pool.Name))) |
| 100 | +``` |
| 101 | + |
| 102 | +### Use Composed Matchers for Struct Assertions |
| 103 | + |
| 104 | +Prefer `HaveField` and `ContainElement(And(...))` to match on struct fields |
| 105 | +declaratively rather than manually extracting fields and asserting one by one: |
| 106 | + |
| 107 | +```go |
| 108 | +// ✅ Good: declarative, one expression |
| 109 | +g.Expect(conditions).To(ContainElement(And( |
| 110 | + HaveField("Type", bootcv1alpha1.PoolDegraded), |
| 111 | + HaveField("Status", metav1.ConditionTrue), |
| 112 | + HaveField("Reason", bootcv1alpha1.PoolInvalidSpec), |
| 113 | +))) |
| 114 | + |
| 115 | +// ❌ Avoid: manual lookup + sequential field assertions |
| 116 | +cond := apimeta.FindStatusCondition(conditions, bootcv1alpha1.PoolDegraded) |
| 117 | +g.Expect(cond).NotTo(BeNil()) |
| 118 | +g.Expect(cond.Status).To(Equal(metav1.ConditionTrue)) |
| 119 | +g.Expect(cond.Reason).To(Equal(bootcv1alpha1.PoolInvalidSpec)) |
| 120 | +``` |
| 121 | + |
| 122 | +### Assert Specific Errors When Expected |
| 123 | + |
| 124 | +When a test expects a particular error, match on the concrete error type or |
| 125 | +value: |
| 126 | + |
| 127 | +```go |
| 128 | +// ✅ Good: we know the API server should reject this |
| 129 | +g.Expect(err).To(MatchError(apierrors.IsInvalid, "IsInvalid")) |
| 130 | +``` |
0 commit comments