From c187956f1975096e7297a26a2cf8b557a028600e Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Sun, 21 Mar 2021 19:05:59 +0000 Subject: [PATCH] fup/minicheddar: add basic spawn test --- web/fup/default.nix | 6 ++- web/fup/minicheddar/minicheddar.go | 4 +- web/fup/minicheddar/minicheddar_test.go | 51 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 web/fup/minicheddar/minicheddar_test.go diff --git a/web/fup/default.nix b/web/fup/default.nix index 6ed930908e..79dd7c77d1 100644 --- a/web/fup/default.nix +++ b/web/fup/default.nix @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 -{ pkgs, ... }: +{ depot, pkgs, ... }: let inherit (pkgs) lib; in @@ -12,6 +12,10 @@ pkgs.buildGoModule { src = ./.; + nativeBuildInputs = [ + depot.third_party.cheddar + ]; + vendorSha256 = "sha256:0myd1p61q777ybbwdz8k4nbchh2hv1yr8008061m3gc44s3gsphx"; meta = with pkgs.lib; { diff --git a/web/fup/minicheddar/minicheddar.go b/web/fup/minicheddar/minicheddar.go index f4345cda1b..0c252090d5 100644 --- a/web/fup/minicheddar/minicheddar.go +++ b/web/fup/minicheddar/minicheddar.go @@ -50,7 +50,7 @@ func (c *Cheddar) Markdown(ctx context.Context, text string) (safehtml.HTML, err defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return safehtml.HTML{}, fmt.Errorf("markdown rendering request returned status %s; not OK", resp.StatusCode) + return safehtml.HTML{}, fmt.Errorf("markdown rendering request returned status %d; not OK", resp.StatusCode) } var respData markdownReqResp @@ -96,7 +96,7 @@ func (c *Cheddar) Code(ctx context.Context, filename, theme, text string) (safeh defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return safehtml.HTML{}, fmt.Errorf("code rendering request returned status %s; not OK", resp.StatusCode) + return safehtml.HTML{}, fmt.Errorf("code rendering request returned status %d; not OK", resp.StatusCode) } var respData codeResp diff --git a/web/fup/minicheddar/minicheddar_test.go b/web/fup/minicheddar/minicheddar_test.go new file mode 100644 index 0000000000..00c0e3337e --- /dev/null +++ b/web/fup/minicheddar/minicheddar_test.go @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2021 Luke Granger-Brown +// +// SPDX-License-Identifier: Apache-2.0 + +package minicheddar_test + +import ( + "context" + "os/exec" + "testing" + + "hg.lukegb.com/lukegb/depot/web/fup/minicheddar" +) + +func TestSpawn(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + cpath, err := exec.LookPath("cheddar") + if err != nil { + t.Skip("couldn't find cheddar") + } + + cheddar, err := minicheddar.Spawn(ctx, cpath) + if err != nil { + t.Fatalf("minicheddar.Spawn: %v", err) + } + defer cheddar.Close() + + t.Run("Code", func(t *testing.T) { + html, err := cheddar.Code(ctx, "index.html", "Some Theme", "\n\n") + if err != nil { + t.Fatalf("minicheddar.Code: %v", err) + } + + if html.String() == "" { + t.Fatalf("minicheddar.Code = %q; want non-empty string", html.String()) + } + }) + + t.Run("Markdown", func(t *testing.T) { + html, err := cheddar.Markdown(ctx, "* Bulleted\n* Lists\n") + if err != nil { + t.Fatalf("minicheddar.Markdown: %v", err) + } + + if html.String() == "" { + t.Fatalf("minicheddar.Markdown = %q; want non-empty string", html.String()) + } + }) +}