Skip to content

Commit

Permalink
Upgrade andrew, add new article and update the styles.
Browse files Browse the repository at this point in the history
Also use a real valid list instead of that crazy list element.
  • Loading branch information
gwynforthewyn committed Apr 20, 2024
1 parent f204da2 commit 973dee8
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected].6
RUN go install github.com/playtechnique/andrew/cmd/[email protected].7

FROM scratch

Expand Down
111 changes: 111 additions & 0 deletions website/articles/testing-panics-in-go.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<!DOCTYPE html>

<head>
<script type="text/javascript" src="/main.js"></script>
<title>testing panics in go</title>
<meta name="andrew-published-at" content="2024-04-20" </head>

<body>
<nav class="navigation">
<a href="/" class="link">front page</a>
<a href="/articles/index.html" class="link">articles</a>
<a href="/projects/index.html" class="link">projects</a>
</nav>

<link rel="stylesheet" href="/styles.css">
<div id="header"></div>
<main>
<article>
<section id="introduction">
<h1>You want to call a panic in a piece of code and have the test not fail?</h1>
<p>In a deferred function, call <code>recover()</code>. If it has a return value that is not nil, it <i>succeeded and caught the panic</i>
i.e. <code> if err == nil </code> then you can call <code>t.Fatal()</code> in your test.</p>
</section>

<section id="setup">
<h1>Here's the setup code</h1>
<p>Let's start a new package to demonstrate this:</p>
<pre><code>mkdir panicker && cd panicker && go mod init panicker && touch panicker_test.go
</code>
</pre>

<p>Here's the content of panicker_test.go:</p>
<pre><code>package panicker
import "testing"

func Panics() {
panic("I panic!")
}

func TestPanics(t *testing.T) {
Panics()
}
</code>
</pre>

<p>Here's how she runs:</p>
<pre><code>; 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
</code>
</pre>
</section>

<section id="code">
<h1>Here's the implementation</h1>
<p>Update TestPanics to contain a deferred call to recover().</p>
<pre><code>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()
}
</code>
</pre>
<p>How does she run now?</p>
<pre><code>; go test -v panicker
=== RUN TestPanics
--- PASS: TestPanics (0.00s)
PASS
ok panicker 0.123s
</code>
</pre>
<p>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.
</p>
</section>
</article>
</main>
</body>
34 changes: 19 additions & 15 deletions website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

<head>
<script type="text/javascript" src="/main.js"></script>

</head>

<body>
<div class="container">

<nav class="navigation">
<img src="images/logo.png">
<a href="/articles/index.html" class="link">articles</a>
<a href="/projects/index.html" class="link">projects</a>
</nav>

<nav class="navigation">
<img src="images/logo.png">
<a href="/articles/index.html" class="link">articles</a>
<a href="/projects/index.html" class="link">projects</a>
</nav>
<link rel="stylesheet" href="/styles.css">
<div id="header"></div>

<link rel="stylesheet" href="/styles.css">
<div id="header"></div>

<list style="list-style-type: none;">
<ul>Is your CI/CD system unmaintained or messy?</ul>
<ul>Do your deployments require manual steps to complete?</ul>
<ul>Do you know you have secrets littered everywhere, but not know when they're going to expire?</ul>
<ul>Is your QA environment a source of blame, developers saying they can't deploy because other people are getting tests?</ul>
<ul>Drop me a line, let's see what we can do together.</ul>
</list>
<ul style="list-style-type: none;">
<li>Is your CI/CD system unmaintained or messy?</li>
<li>Do your deployments require manual steps to complete?</li>
<li>Do you know you have secrets littered everywhere, but not know when they're going to expire?</li>
<li>Is your QA environment a source of blame, developers saying they can't deploy because other people are
getting tests?</li>
<li>Drop me a line, let's see what we can do together.</li>
</ul>
</div>
</body>
18 changes: 10 additions & 8 deletions website/styles.css
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -33,10 +39,6 @@ main {
margin-left: 10%;
}

body {
background: #f1e6ff;
}

article {
line-height: 1.5em;
width: inherit;
Expand Down

0 comments on commit 973dee8

Please sign in to comment.