From 973dee8918f527bbfb40316e357d8858a82fe117 Mon Sep 17 00:00:00 2001 From: Gwyn Date: Sat, 20 Apr 2024 09:44:44 -0600 Subject: [PATCH] Upgrade andrew, add new article and update the styles. Also use a real valid list instead of that crazy list element. --- Dockerfile | 2 +- website/articles/testing-panics-in-go.html | 111 +++++++++++++++++++++ website/index.html | 34 ++++--- website/styles.css | 18 ++-- 4 files changed, 141 insertions(+), 24 deletions(-) create mode 100644 website/articles/testing-panics-in-go.html diff --git a/Dockerfile b/Dockerfile index 7e4dcbe..03910ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM golang:1.22 AS base WORKDIR /usr/src/app ENV CGO_ENABLED=0 -RUN go install github.com/playtechnique/andrew/cmd/andrew@v0.0.6 +RUN go install github.com/playtechnique/andrew/cmd/andrew@v0.0.7 FROM scratch diff --git a/website/articles/testing-panics-in-go.html b/website/articles/testing-panics-in-go.html new file mode 100644 index 0000000..b04201b --- /dev/null +++ b/website/articles/testing-panics-in-go.html @@ -0,0 +1,111 @@ + + + + + testing panics in go + + + + + + + +
+
+
+

You want to call a panic in a piece of code and have the test not fail?

+

In a deferred function, call recover(). If it has a return value that is not nil, it succeeded and caught the panic + i.e. if err == nil then you can call t.Fatal() in your test.

+
+ +
+

Here's the setup code

+

Let's start a new package to demonstrate this:

+
mkdir panicker && cd panicker && go mod init panicker && touch panicker_test.go
+
+
+ +

Here's the content of panicker_test.go:

+
package panicker
+import "testing"
+
+func Panics() {
+    panic("I panic!")
+}
+
+func TestPanics(t *testing.T) {
+    Panics()
+}       
+
+
+ +

Here's how she runs:

+
; go test -v panicker
+=== RUN   TestPanics
+--- FAIL: TestPanics (0.00s)
+panic: I panic! [recovered]
+        panic: I panic!
+
+goroutine 34 [running]:
+testing.tRunner.func1.2({0x10441d540, 0x1044455e0})
+        /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1631 +0x1c4
+testing.tRunner.func1()
+        /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1634 +0x33c
+panic({0x10441d540?, 0x1044455e0?})
+        /opt/homebrew/Cellar/go/1.22.2/libexec/src/runtime/panic.go:770 +0x124
+panicker.Panics(...)
+        /Users/gwyn/Source/panicker/panicker_test.go:6
+panicker.TestPanics(0x14000124680?)
+        /Users/gwyn/Source/panicker/panicker_test.go:17 +0x30
+testing.tRunner(0x14000124680, 0x104444dc0)
+        /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1689 +0xec
+created by testing.(*T).Run in goroutine 1
+        /opt/homebrew/Cellar/go/1.22.2/libexec/src/testing/testing.go:1742 +0x318
+FAIL    panicker        0.082s
+FAIL
+
+
+
+ +
+

Here's the implementation

+

Update TestPanics to contain a deferred call to recover().

+
package panicker
+
+import "testing"
+
+func Panics() {
+    panic("I panic!")
+}
+
+func TestPanics(t *testing.T) {
+    defer func() {
+        err := recover()
+        if err == nil {
+            t.Fatalf("Expected panic with invalid address, received %v", err)
+        }
+    }()
+
+    Panics()
+}
+        
+    
+

How does she run now?

+
; go test -v panicker
+=== RUN   TestPanics
+--- PASS: TestPanics (0.00s)
+PASS
+ok      panicker        0.123s
+
+
+

Notice the error check condition: recover() is non-nil if it's actually called; if recover()'s return value is nil, it was never triggered, + so we didn't actually trigger our expected error. +

+
+
+
+ diff --git a/website/index.html b/website/index.html index 136a45d..bcad8c0 100644 --- a/website/index.html +++ b/website/index.html @@ -2,24 +2,28 @@ + +
+ + - + + - - - - -
    Is your CI/CD system unmaintained or messy?
-
    Do your deployments require manual steps to complete?
-
    Do you know you have secrets littered everywhere, but not know when they're going to expire?
-
    Is your QA environment a source of blame, developers saying they can't deploy because other people are getting tests?
-
    Drop me a line, let's see what we can do together.
-
+ +
\ No newline at end of file diff --git a/website/styles.css b/website/styles.css index 7f7464c..5e7aa8f 100644 --- a/website/styles.css +++ b/website/styles.css @@ -1,16 +1,22 @@ /* background color pale pink - #ffe6ef */ +head { + background: #ffe6ef; +} nav.navigation { position: relative; text-align: right; - background: linear-gradient(#ffe6ef, #f1e6ff); - padding-bottom: 5vh; + border-radius: 40vh 40vh 0vh 0vh; + background: linear-gradient(#ffe6ef, #ffffff); + padding: 5vh; + overflow: hidden; } nav.navigation .link { - border: solid black; + /* border: solid black; + border-radius: 40vh; */ display: inline-block; width: fit-content; - padding: 1em; + padding: 1vh; color: black; } @@ -33,10 +39,6 @@ main { margin-left: 10%; } -body { - background: #f1e6ff; -} - article { line-height: 1.5em; width: inherit;